001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.ddl.impl;
043:
044: import java.text.MessageFormat;
045: import java.util.Enumeration;
046: import java.util.Map;
047: import java.util.Vector;
048:
049: import org.openide.util.NbBundle;
050: import org.netbeans.lib.ddl.CreateTriggerCommand;
051: import org.netbeans.lib.ddl.DDLException;
052:
053: /**
054: * Interface of database action command. Instances should remember connection
055: * information of DatabaseSpecification and use it in execute() method. This is a base interface
056: * used heavily for sub-interfacing (it is not subclassing :)
057: */
058:
059: public class CreateTrigger extends AbstractCommand implements
060: CreateTriggerCommand {
061: public static final int BEFORE = 1;
062: public static final int AFTER = 2;
063:
064: /** Arguments */
065: private Vector events;
066:
067: /** for each row */
068: boolean eachrow;
069:
070: /** Condition */
071: private String cond;
072:
073: /** Table */
074: private String table;
075:
076: /** Timing */
077: int timing;
078:
079: /** Body of the procedure */
080: private String body;
081:
082: public static String getTimingName(int code) {
083: switch (code) {
084: case BEFORE:
085: return "BEFORE"; // NOI18N
086: case AFTER:
087: return "AFTER"; // NOI18N
088: }
089:
090: return null;
091: }
092:
093: static final long serialVersionUID = -2217362040968396712L;
094:
095: public CreateTrigger() {
096: events = new Vector();
097: }
098:
099: public String getTableName() {
100: return table;
101: }
102:
103: public void setTableName(String tab) {
104: table = tab;
105: }
106:
107: public boolean getForEachRow() {
108: return eachrow;
109: }
110:
111: public void setForEachRow(boolean flag) {
112: eachrow = flag;
113: }
114:
115: /** Returns text of procedure */
116: public String getText() {
117: return body;
118: }
119:
120: /** Sets name of table */
121: public void setText(String text) {
122: body = text;
123: }
124:
125: public String getCondition() {
126: return cond;
127: }
128:
129: public void setCondition(String con) {
130: cond = con;
131: }
132:
133: public int getTiming() {
134: return timing;
135: }
136:
137: public void setTiming(int time) {
138: timing = time;
139: }
140:
141: /** Returns arguments */
142: public Vector getEvents() {
143: return events;
144: }
145:
146: public TriggerEvent getEvent(int index) {
147: return (TriggerEvent) events.get(index);
148: }
149:
150: /** Sets argument array */
151: public void setEvents(Vector argarr) {
152: events = argarr;
153: }
154:
155: public void setEvent(int index, TriggerEvent arg) {
156: events.set(index, arg);
157: }
158:
159: public TriggerEvent createTriggerEvent(int when, String columnname)
160: throws DDLException {
161: try {
162: Map gprops = (Map) getSpecification().getProperties();
163: Map props = (Map) getSpecification().getCommandProperties(
164: Specification.CREATE_TRIGGER);
165: Map bindmap = (Map) props.get("Binding"); // NOI18N
166: String tname = (String) bindmap.get("EVENT"); // NOI18N
167: if (tname != null) {
168: Map typemap = (Map) gprops.get(tname);
169: if (typemap == null)
170: throw new InstantiationException(
171: MessageFormat
172: .format(
173: NbBundle
174: .getBundle(
175: "org.netbeans.lib.ddl.resources.Bundle")
176: .getString(
177: "EXC_UnableLocateObject"), // NOI18N
178: new String[] { tname }));
179: Class typeclass = Class.forName((String) typemap
180: .get("Class")); // NOI18N
181: String format = (String) typemap.get("Format"); // NOI18N
182: TriggerEvent evt = (TriggerEvent) typeclass
183: .newInstance();
184: Map temap = (Map) props.get("TriggerEventMap"); // NOI18N
185: evt.setName(TriggerEvent.getName(when));
186: evt.setColumn(columnname);
187: evt.setFormat(format);
188: return (TriggerEvent) evt;
189: } else
190: throw new InstantiationException(
191: MessageFormat
192: .format(
193: NbBundle
194: .getBundle(
195: "org.netbeans.lib.ddl.resources.Bundle")
196: .getString(
197: "EXC_UnableLocateType"), // NOI18N
198: new String[] { "EVENT",
199: bindmap.toString() })); // NOI18N
200: } catch (Exception e) {
201: throw new DDLException(e.getMessage());
202: }
203: }
204:
205: public void addTriggerEvent(int when) throws DDLException {
206: addTriggerEvent(when, null);
207: }
208:
209: public void addTriggerEvent(int when, String columnname)
210: throws DDLException {
211: TriggerEvent te = createTriggerEvent(when, columnname);
212: if (te != null)
213: events.add(te);
214: }
215:
216: public Map getCommandProperties() throws DDLException {
217: Map props = (Map) getSpecification().getProperties();
218: String evs = "", argdelim = (String) props
219: .get("TriggerEventListDelimiter"); // NOI18N
220: Map cmdprops = super .getCommandProperties();
221:
222: Enumeration col_e = events.elements();
223: while (col_e.hasMoreElements()) {
224: TriggerEvent evt = (TriggerEvent) col_e.nextElement();
225: boolean inscomma = col_e.hasMoreElements();
226: evs = evs + evt.getCommand(this )
227: + (inscomma ? argdelim : "");
228: }
229:
230: cmdprops.put("trigger.events", evs); // NOI18N
231: cmdprops.put("trigger.condition", cond); // NOI18N
232: cmdprops.put("trigger.timing", getTimingName(timing)); // NOI18N
233: cmdprops.put("table.name", quote(table)); // NOI18N
234: cmdprops.put("trigger.body", body); // NOI18N
235: if (eachrow)
236: cmdprops.put("each.row", ""); // NOI18N
237:
238: return cmdprops;
239: }
240: }
|