KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > ddl > impl > CreateTrigger


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.ddl.impl;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import org.openide.util.NbBundle;
28 import org.netbeans.lib.ddl.CreateTriggerCommand;
29 import org.netbeans.lib.ddl.DDLException;
30
31 /**
32 * Interface of database action command. Instances should remember connection
33 * information of DatabaseSpecification and use it in execute() method. This is a base interface
34 * used heavily for sub-interfacing (it is not subclassing :)
35 */

36
37 public class CreateTrigger extends AbstractCommand implements CreateTriggerCommand {
38     public static final int BEFORE = 1;
39     public static final int AFTER = 2;
40
41     /** Arguments */
42     private Vector JavaDoc events;
43
44     /** for each row */
45     boolean eachrow;
46
47     /** Condition */
48     private String JavaDoc cond;
49
50     /** Table */
51     private String JavaDoc table;
52
53     /** Timing */
54     int timing;
55
56     /** Body of the procedure */
57     private String JavaDoc body;
58
59     public static String JavaDoc getTimingName(int code)
60     {
61         switch (code) {
62         case BEFORE: return "BEFORE"; // NOI18N
63
case AFTER: return "AFTER"; // NOI18N
64
}
65
66         return null;
67     }
68
69     static final long serialVersionUID =-2217362040968396712L;
70     public CreateTrigger()
71     {
72         events = new Vector JavaDoc();
73     }
74
75     public String JavaDoc getTableName()
76     {
77         return table;
78     }
79
80     public void setTableName(String JavaDoc tab)
81     {
82         table = tab;
83     }
84
85     public boolean getForEachRow()
86     {
87         return eachrow;
88     }
89
90     public void setForEachRow(boolean flag)
91     {
92         eachrow = flag;
93     }
94
95
96     /** Returns text of procedure */
97     public String JavaDoc getText()
98     {
99         return body;
100     }
101
102     /** Sets name of table */
103     public void setText(String JavaDoc text)
104     {
105         body = text;
106     }
107
108     public String JavaDoc getCondition()
109     {
110         return cond;
111     }
112
113     public void setCondition(String JavaDoc con)
114     {
115         cond = con;
116     }
117
118     public int getTiming()
119     {
120         return timing;
121     }
122
123     public void setTiming(int time)
124     {
125         timing = time;
126     }
127
128     /** Returns arguments */
129     public Vector JavaDoc getEvents()
130     {
131         return events;
132     }
133
134     public TriggerEvent getEvent(int index)
135     {
136         return (TriggerEvent)events.get(index);
137     }
138
139     /** Sets argument array */
140     public void setEvents(Vector JavaDoc argarr)
141     {
142         events = argarr;
143     }
144
145     public void setEvent(int index, TriggerEvent arg)
146     {
147         events.set(index, arg);
148     }
149
150     public TriggerEvent createTriggerEvent(int when, String JavaDoc columnname)
151     throws DDLException
152     {
153         try {
154             Map JavaDoc gprops = (Map JavaDoc)getSpecification().getProperties();
155             Map JavaDoc props = (Map JavaDoc)getSpecification().getCommandProperties(Specification.CREATE_TRIGGER);
156             Map JavaDoc bindmap = (Map JavaDoc)props.get("Binding"); // NOI18N
157
String JavaDoc tname = (String JavaDoc)bindmap.get("EVENT"); // NOI18N
158
if (tname != null) {
159                 Map JavaDoc typemap = (Map JavaDoc)gprops.get(tname);
160                 if (typemap == null) throw new InstantiationException JavaDoc(
161                     MessageFormat.format(
162                         NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableLocateObject"), // NOI18N
163
new String JavaDoc[] {tname}));
164                 Class JavaDoc typeclass = Class.forName((String JavaDoc)typemap.get("Class")); // NOI18N
165
String JavaDoc format = (String JavaDoc)typemap.get("Format"); // NOI18N
166
TriggerEvent evt = (TriggerEvent)typeclass.newInstance();
167                 Map JavaDoc temap = (Map JavaDoc)props.get("TriggerEventMap"); // NOI18N
168
evt.setName(TriggerEvent.getName(when));
169                 evt.setColumn(columnname);
170                 evt.setFormat(format);
171                 return (TriggerEvent)evt;
172             } else throw new InstantiationException JavaDoc(
173                     MessageFormat.format(
174                         NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableLocateType"), // NOI18N
175
new String JavaDoc[] {"EVENT", bindmap.toString() })); // NOI18N
176
} catch (Exception JavaDoc e) {
177             throw new DDLException(e.getMessage());
178         }
179     }
180
181     public void addTriggerEvent(int when)
182     throws DDLException
183     {
184         addTriggerEvent(when, null);
185     }
186
187     public void addTriggerEvent(int when, String JavaDoc columnname)
188     throws DDLException
189     {
190         TriggerEvent te = createTriggerEvent(when, columnname);
191         if (te != null) events.add(te);
192     }
193
194     public Map JavaDoc getCommandProperties() throws DDLException {
195         Map JavaDoc props = (Map JavaDoc)getSpecification().getProperties();
196         String JavaDoc evs = "", argdelim = (String JavaDoc)props.get("TriggerEventListDelimiter"); // NOI18N
197
Map JavaDoc cmdprops = super.getCommandProperties();
198
199         Enumeration JavaDoc col_e = events.elements();
200         while (col_e.hasMoreElements()) {
201             TriggerEvent evt = (TriggerEvent)col_e.nextElement();
202             boolean inscomma = col_e.hasMoreElements();
203             evs = evs + evt.getCommand(this)+(inscomma ? argdelim : "");
204         }
205
206         cmdprops.put("trigger.events", evs); // NOI18N
207
cmdprops.put("trigger.condition", cond); // NOI18N
208
cmdprops.put("trigger.timing", getTimingName(timing)); // NOI18N
209
cmdprops.put("table.name", quote(table)); // NOI18N
210
cmdprops.put("trigger.body", body); // NOI18N
211
if (eachrow)
212             cmdprops.put("each.row", ""); // NOI18N
213

214         return cmdprops;
215     }
216 }
217
Popular Tags