KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > jdbc > TModelTable


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.datastore.jdbc;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.juddi.datatype.OverviewDoc;
27 import org.apache.juddi.datatype.tmodel.TModel;
28
29 /**
30  * @author Steve Viens (sviens@apache.org)
31  */

32 class TModelTable
33 {
34   // private reference to the jUDDI logger
35
private static Log log = LogFactory.getLog(TModelTable.class);
36
37   static String JavaDoc insertSQL = null;
38   static String JavaDoc deleteSQL = null;
39   static String JavaDoc updateSQL = null;
40   static String JavaDoc selectSQL = null;
41   static String JavaDoc selectByPublisherSQL = null;
42   static String JavaDoc verifyOwnershipSQL = null;
43
44   static
45   {
46     // buffer used to build SQL statements
47
StringBuffer JavaDoc sql = null;
48
49     // build insertSQL
50
sql = new StringBuffer JavaDoc(150);
51     sql.append("INSERT INTO TMODEL (");
52     sql.append("TMODEL_KEY,");
53     sql.append("AUTHORIZED_NAME,");
54     sql.append("PUBLISHER_ID,");
55     sql.append("OPERATOR,");
56     sql.append("NAME,");
57     sql.append("OVERVIEW_URL,");
58     sql.append("LAST_UPDATE) ");
59     sql.append("VALUES (?,?,?,?,?,?,?)");
60     insertSQL = sql.toString();
61
62     // build deleteSQL
63
sql = new StringBuffer JavaDoc(100);
64     sql.append("DELETE FROM TMODEL ");
65     sql.append("WHERE TMODEL_KEY=?");
66     deleteSQL = sql.toString();
67
68     // build updateSQL
69
sql = new StringBuffer JavaDoc(100);
70     sql.append("UPDATE TMODEL ");
71     sql.append("SET DELETED='true' ");
72     sql.append("WHERE TMODEL_KEY=?");
73     updateSQL = sql.toString();
74
75     // build selectSQL
76
sql = new StringBuffer JavaDoc(200);
77     sql.append("SELECT ");
78     sql.append("AUTHORIZED_NAME,");
79     sql.append("OPERATOR,");
80     sql.append("NAME,");
81     sql.append("OVERVIEW_URL,");
82     sql.append("DELETED ");
83     sql.append("FROM TMODEL ");
84     sql.append("WHERE TMODEL_KEY=? ");
85     sql.append("AND DELETED IS NULL");
86     selectSQL = sql.toString();
87
88     // build selectByPublisherSQL
89
sql = new StringBuffer JavaDoc(200);
90     sql.append("SELECT ");
91     sql.append("TMODEL_KEY,");
92     sql.append("DELETED ");
93     sql.append("FROM TMODEL ");
94     sql.append("WHERE PUBLISHER_ID=? ");
95     sql.append("AND DELETED IS NULL");
96     selectByPublisherSQL = sql.toString();
97
98     // build verifyOwnershipSQL
99
sql = new StringBuffer JavaDoc(200);
100     sql.append("SELECT * ");
101     sql.append("FROM TMODEL ");
102     sql.append("WHERE TMODEL_KEY=? ");
103     sql.append("AND PUBLISHER_ID=? " );
104     sql.append("AND DELETED IS NULL");
105     verifyOwnershipSQL = sql.toString();
106   }
107
108   /**
109    * Insert new row into the TMODEL table.
110    *
111    * @param tModel TModel object holding values to be inserted
112    * @param publisherID
113    * @param connection JDBC connection
114    * @throws java.sql.SQLException
115    */

116   public static void insert(TModel tModel,String JavaDoc publisherID,Connection JavaDoc connection)
117     throws java.sql.SQLException JavaDoc
118   {
119     PreparedStatement JavaDoc statement = null;
120     Timestamp JavaDoc timeStamp = new Timestamp JavaDoc(System.currentTimeMillis());
121
122     String JavaDoc overviewURL = null;
123     if ((tModel.getOverviewDoc() != null) && (tModel.getOverviewDoc().getOverviewURL() != null))
124       overviewURL = tModel.getOverviewDoc().getOverviewURL().getValue();
125
126     try
127     {
128       statement = connection.prepareStatement(insertSQL);
129       statement.setString(1,tModel.getTModelKey().toString());
130       statement.setString(2,tModel.getAuthorizedName());
131       statement.setString(3,publisherID);
132       statement.setString(4,tModel.getOperator());
133       statement.setString(5,tModel.getName());
134       statement.setString(6,overviewURL);
135       statement.setTimestamp(7,timeStamp);
136
137       log.debug(insertSQL +
138         "\n\t TMODEL_KEY=" + tModel.getTModelKey().toString() +
139         "\n\t AUTHORIZED_NAME=" + tModel.getAuthorizedName() +
140         "\n\t PUBLISHER_ID=" + publisherID +
141         "\n\t OPERATOR=" + tModel.getOperator() +
142         "\n\t NAME=" + tModel.getName() +
143         "\n\t OVERVIEW_URL=" + overviewURL +
144         "\n\t LAST_UPDATE=" + timeStamp.getTime() + "\n");
145
146       // insert
147
statement.executeUpdate();
148     }
149     finally
150     {
151       try {
152         statement.close();
153       }
154       catch (Exception JavaDoc e) { /* ignored */ }
155     }
156   }
157
158   /**
159    * Delete row from the TMODEL table.
160    *
161    * @param tModelKey
162    * @param connection JDBC connection
163    * @throws java.sql.SQLException
164    */

165   public static void delete(String JavaDoc tModelKey,Connection JavaDoc connection)
166     throws java.sql.SQLException JavaDoc
167   {
168     PreparedStatement JavaDoc statement = null;
169
170     try
171     {
172       // prepare the delete
173
statement = connection.prepareStatement(deleteSQL);
174       statement.setString(1,tModelKey.toString());
175
176       log.debug(deleteSQL +
177         "\n\t TMODEL_KEY=" + tModelKey.toString() + "\n");
178
179       // execute
180
statement.executeUpdate();
181     }
182     finally
183     {
184       try {
185         statement.close();
186       }
187       catch (Exception JavaDoc e) { /* ignored */ }
188     }
189   }
190
191   /**
192    * Update the TMODEL table setting the value of the DELETED column to 'true'.
193    *
194    * @param tModelKey
195    * @param connection JDBC connection
196    * @throws java.sql.SQLException
197    */

198   public static void markAsDeleted(String JavaDoc tModelKey,Connection JavaDoc connection)
199     throws java.sql.SQLException JavaDoc
200   {
201     PreparedStatement JavaDoc statement = null;
202
203     try
204     {
205       // prepare the delete
206
statement = connection.prepareStatement(updateSQL);
207       statement.setString(1,tModelKey.toString());
208
209       log.debug(updateSQL +
210         "\n\t TMODEL_KEY=" + tModelKey.toString() + "\n");
211
212       // execute
213
statement.executeUpdate();
214     }
215     finally
216     {
217       try {
218         statement.close();
219       }
220       catch (Exception JavaDoc e) { /* ignored */ }
221     }
222   }
223
224   /**
225    * Select one row from the TMODEL table.
226    *
227    * @param tModelKey
228    * @param connection
229    * @throws java.sql.SQLException
230    */

231   public static TModel select(String JavaDoc tModelKey,Connection JavaDoc connection)
232     throws java.sql.SQLException JavaDoc
233   {
234     TModel tModel = null;
235     PreparedStatement JavaDoc statement = null;
236     ResultSet JavaDoc resultSet = null;
237
238     try
239     {
240       statement = connection.prepareStatement(selectSQL);
241       statement.setString(1,tModelKey.toString());
242
243       log.debug(selectSQL +
244         "\n\t TMODEL_KEY=" + tModelKey.toString() + "\n");
245
246       resultSet = statement.executeQuery();
247       if (resultSet.next())
248       {
249         tModel = new TModel();
250         tModel.setTModelKey(tModelKey);
251         tModel.setAuthorizedName(resultSet.getString(1));//("AUTHORIZED_NAME"));
252
tModel.setOperator(resultSet.getString(2));//("OPERATOR"));
253
tModel.setName(resultSet.getString(3));//("NAME"));
254

255         OverviewDoc overviewDoc = new OverviewDoc();
256         overviewDoc.setOverviewURL(resultSet.getString(4));//("OVERVIEW_URL"));
257
tModel.setOverviewDoc(overviewDoc);
258       }
259
260       return tModel;
261     }
262     finally
263     {
264       try {
265         resultSet.close();
266         statement.close();
267       }
268       catch (Exception JavaDoc e) { /* ignored */ }
269     }
270   }
271
272   /**
273    * Select all TModelKeys from the business_entities table for a given
274    * 'publisherID' value.
275    *
276    * @param publisherID The User ID of the TModel owner.
277    * @param connection JDBC The JDBC connection
278    * @throws java.sql.SQLException
279    */

280   public static Vector JavaDoc selectByPublisherID(String JavaDoc publisherID,Connection JavaDoc connection)
281     throws java.sql.SQLException JavaDoc
282   {
283     Vector JavaDoc keyList = new Vector JavaDoc();
284     PreparedStatement JavaDoc statement = null;
285     ResultSet JavaDoc resultSet = null;
286
287     try
288     {
289       // create a statement to query with
290
statement = connection.prepareStatement(selectByPublisherSQL);
291       statement.setString(1,publisherID.toString());
292
293       log.debug(selectByPublisherSQL +
294         "\n\t PUBLISHER_ID=" + publisherID + "\n");
295
296       // execute the statement
297
resultSet = statement.executeQuery();
298       while (resultSet.next())
299         keyList.add(resultSet.getString(1));//("TMODEL_KEY"));
300

301       return keyList;
302     }
303     finally
304     {
305       try {
306         resultSet.close();
307         statement.close();
308       }
309       catch (Exception JavaDoc e) { /* ignored */ }
310     }
311   }
312
313   /**
314    * Verify that 'publisherID' has the authority to update or delete
315    * TModel identified by the tModelKey parameter
316    *
317    * @param tModelKey
318    * @param publisherID
319    * @param connection
320    * @throws java.sql.SQLException
321    */

322   public static boolean verifyOwnership(String JavaDoc tModelKey,String JavaDoc publisherID,Connection JavaDoc connection)
323     throws java.sql.SQLException JavaDoc
324   {
325     if ((tModelKey == null) || (publisherID == null))
326       return false;
327
328     boolean authorized = false;
329     PreparedStatement JavaDoc statement = null;
330     ResultSet JavaDoc resultSet = null;
331
332     try
333     {
334       statement = connection.prepareStatement(verifyOwnershipSQL);
335       statement.setString(1,tModelKey);
336       statement.setString(2,publisherID);
337
338       log.debug(verifyOwnershipSQL +
339         "\n\t TMODEL_KEY=" + tModelKey +
340         "\n\t PUBLISHER_ID=" + publisherID + "\n");
341
342       resultSet = statement.executeQuery();
343       if (resultSet.next())
344         authorized = true;
345
346       return authorized;
347     }
348     finally
349     {
350       try {
351         resultSet.close();
352         statement.close();
353       }
354       catch (Exception JavaDoc e) { /* ignored */ }
355     }
356   }
357 }
358
Popular Tags