View Javadoc

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;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.Timestamp;
22  import java.util.Vector;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.juddi.datatype.Name;
27  import org.apache.juddi.datatype.OverviewDoc;
28  import org.apache.juddi.datatype.tmodel.TModel;
29  import org.apache.juddi.registry.RegistryEngine;
30  import org.apache.juddi.util.Config;
31  
32  /***
33   * @author Steve Viens (sviens@apache.org)
34   */
35  class TModelTable
36  {
37    // private reference to the jUDDI logger
38    private static Log log = LogFactory.getLog(TModelTable.class);
39  
40    static String insertSQL = null;
41    static String deleteSQL = null;
42    static String updateSQL = null;
43    static String selectSQL = null;
44    static String selectByPublisherSQL = null;
45    static String verifyOwnershipSQL = null;
46    static String tablePrefix = "";
47    
48    static
49    {
50     tablePrefix = Config.getStringProperty(
51         RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
52      // buffer used to build SQL statements
53      StringBuffer sql = null;
54  
55      // build insertSQL
56      sql = new StringBuffer(150);
57      sql.append("INSERT INTO ").append(tablePrefix).append("TMODEL (");
58      sql.append("TMODEL_KEY,");
59      sql.append("AUTHORIZED_NAME,");
60      sql.append("PUBLISHER_ID,");
61      sql.append("OPERATOR,");
62      sql.append("NAME,");
63      sql.append("LANG_CODE,");
64      sql.append("OVERVIEW_URL,");
65      sql.append("LAST_UPDATE) ");
66      sql.append("VALUES (?,?,?,?,?,?,?,?)");
67      insertSQL = sql.toString();
68  
69      // build deleteSQL
70      sql = new StringBuffer(100);
71      sql.append("DELETE FROM ").append(tablePrefix).append("TMODEL ");
72      sql.append("WHERE TMODEL_KEY=?");
73      deleteSQL = sql.toString();
74  
75      // build updateSQL
76      sql = new StringBuffer(100);
77      sql.append("UPDATE ").append(tablePrefix).append("TMODEL ");
78      sql.append("SET DELETED='true' ");
79      sql.append("WHERE TMODEL_KEY=?");
80      updateSQL = sql.toString();
81  
82      // build selectSQL
83      sql = new StringBuffer(200);
84      sql.append("SELECT ");
85      sql.append("AUTHORIZED_NAME,");
86      sql.append("OPERATOR,");
87      sql.append("NAME,");
88      sql.append("LANG_CODE,");
89      sql.append("OVERVIEW_URL,");
90      sql.append("DELETED ");
91      sql.append("FROM ").append(tablePrefix).append("TMODEL ");
92      sql.append("WHERE TMODEL_KEY=? ");
93      sql.append("AND DELETED IS NULL");
94      selectSQL = sql.toString();
95  
96      // build selectByPublisherSQL
97      sql = new StringBuffer(200);
98      sql.append("SELECT ");
99      sql.append("TMODEL_KEY,");
100     sql.append("DELETED ");
101     sql.append("FROM ").append(tablePrefix).append("TMODEL ");
102     sql.append("WHERE PUBLISHER_ID=? ");
103     sql.append("AND DELETED IS NULL");
104     selectByPublisherSQL = sql.toString();
105 
106     // build verifyOwnershipSQL
107     sql = new StringBuffer(200);
108     sql.append("SELECT * ");
109     sql.append("FROM ").append(tablePrefix).append("TMODEL ");
110     sql.append("WHERE TMODEL_KEY=? ");
111     sql.append("AND PUBLISHER_ID=? " );
112     sql.append("AND DELETED IS NULL");
113     verifyOwnershipSQL = sql.toString();
114   }
115 
116   /***
117    * Insert new row into the TMODEL table.
118    *
119    * @param tModel TModel object holding values to be inserted
120    * @param publisherID
121    * @param connection JDBC connection
122    * @throws java.sql.SQLException
123    */
124   public static void insert(TModel tModel,String publisherID,Connection connection)
125     throws java.sql.SQLException
126   {
127     PreparedStatement statement = null;
128     Timestamp timeStamp = new Timestamp(System.currentTimeMillis());
129 
130     String overviewURL = null;
131     if ((tModel.getOverviewDoc() != null) && (tModel.getOverviewDoc().getOverviewURL() != null))
132       overviewURL = tModel.getOverviewDoc().getOverviewURL().getValue();
133 
134     try
135     {
136       statement = connection.prepareStatement(insertSQL);
137       statement.setString(1,tModel.getTModelKey().toString());
138       statement.setString(2,tModel.getAuthorizedName());
139       statement.setString(3,publisherID);
140       statement.setString(4,tModel.getOperator());
141       statement.setString(5,tModel.getName().getValue());
142       statement.setString(6,tModel.getName().getLanguageCode());
143       statement.setString(7,overviewURL);
144       statement.setTimestamp(8,timeStamp);
145 
146       if (log.isDebugEnabled()) {
147           log.debug(insertSQL +
148             "\n\t TMODEL_KEY=" + tModel.getTModelKey().toString() +
149             "\n\t AUTHORIZED_NAME=" + tModel.getAuthorizedName() +
150             "\n\t PUBLISHER_ID=" + publisherID +
151             "\n\t OPERATOR=" + tModel.getOperator() +
152             "\n\t NAME=" + tModel.getName().getValue() +
153             "\n\t LANG_CODE=" + tModel.getName().getLanguageCode() +
154             "\n\t OVERVIEW_URL=" + overviewURL +
155             "\n\t LAST_UPDATE=" + timeStamp.getTime() + "\n");
156       }
157 
158       // insert
159       statement.executeUpdate();
160     }
161     finally
162     {
163       try {
164         statement.close();
165       }
166       catch (Exception e) { /* ignored */ }
167     }
168   }
169 
170   /***
171    * Delete row from the TMODEL table.
172    *
173    * @param  tModelKey
174    * @param  connection JDBC connection
175    * @throws java.sql.SQLException
176    */
177   public static void delete(String tModelKey,Connection connection)
178     throws java.sql.SQLException
179   {
180     PreparedStatement statement = null;
181 
182     try
183     {
184       // prepare the delete
185       statement = connection.prepareStatement(deleteSQL);
186       statement.setString(1,tModelKey.toString());
187 
188       if (log.isDebugEnabled()) {
189           log.debug(deleteSQL +
190             "\n\t TMODEL_KEY=" + tModelKey.toString() + "\n");
191       }
192 
193       // execute
194       statement.executeUpdate();
195     }
196     finally
197     {
198       try {
199         statement.close();
200       }
201       catch (Exception e) { /* ignored */ }
202     }
203   }
204 
205   /***
206    * Update the TMODEL table setting the value of the DELETED column to 'true'.
207    *
208    * @param  tModelKey
209    * @param  connection JDBC connection
210    * @throws java.sql.SQLException
211    */
212   public static void markAsDeleted(String tModelKey,Connection connection)
213     throws java.sql.SQLException
214   {
215     PreparedStatement statement = null;
216 
217     try
218     {
219       // prepare the delete
220       statement = connection.prepareStatement(updateSQL);
221       statement.setString(1,tModelKey.toString());
222 
223       if (log.isDebugEnabled()) {
224           log.debug(updateSQL +
225             "\n\t TMODEL_KEY=" + tModelKey.toString() + "\n");
226       }
227 
228       // execute
229       statement.executeUpdate();
230     }
231     finally
232     {
233       try {
234         statement.close();
235       }
236       catch (Exception e) { /* ignored */ }
237     }
238   }
239 
240   /***
241    * Select one row from the TMODEL table.
242    *
243    * @param  tModelKey
244    * @param  connection
245    * @throws java.sql.SQLException
246    */
247   public static TModel select(String tModelKey,Connection connection)
248     throws java.sql.SQLException
249   {
250     TModel tModel = null;
251     PreparedStatement statement = null;
252     ResultSet resultSet = null;
253 
254     try
255     {
256       statement = connection.prepareStatement(selectSQL);
257       statement.setString(1,tModelKey.toString());
258 
259       if (log.isDebugEnabled()) {
260           log.debug(selectSQL +
261             "\n\t TMODEL_KEY=" + tModelKey.toString() + "\n");
262       }
263 
264       resultSet = statement.executeQuery();
265       if (resultSet.next())
266       {
267         tModel = new TModel();
268         tModel.setTModelKey(tModelKey);
269         tModel.setAuthorizedName(resultSet.getString(1));//("AUTHORIZED_NAME"));
270         tModel.setOperator(resultSet.getString(2));//("OPERATOR"));
271 
272         Name name = new Name();
273         name.setValue(resultSet.getString(3));//("NAME"));
274         name.setLanguageCode(resultSet.getString(4));//("LANG_CODE"));
275 
276         tModel.setName(name);
277 
278         OverviewDoc overviewDoc = new OverviewDoc();
279         overviewDoc.setOverviewURL(resultSet.getString(5));//("OVERVIEW_URL"));
280         tModel.setOverviewDoc(overviewDoc);
281       }
282 
283       return tModel;
284     }
285     finally
286     {
287       try {
288         resultSet.close();
289         statement.close();
290       }
291       catch (Exception e) { /* ignored */ }
292     }
293   }
294 
295   /***
296    * Select all TModelKeys from the business_entities table for a given
297    * 'publisherID' value.
298    *
299    * @param  publisherID The User ID of the TModel owner.
300    * @param connection JDBC The JDBC connection
301    * @throws java.sql.SQLException
302    */
303   public static Vector selectByPublisherID(String publisherID,Connection connection)
304     throws java.sql.SQLException
305   {
306     Vector keyList = new Vector();
307     PreparedStatement statement = null;
308     ResultSet resultSet = null;
309 
310     try
311     {
312       // create a statement to query with
313       statement = connection.prepareStatement(selectByPublisherSQL);
314       statement.setString(1,publisherID.toString());
315 
316       if (log.isDebugEnabled()) {
317           log.debug(selectByPublisherSQL +
318             "\n\t PUBLISHER_ID=" + publisherID + "\n");
319       }
320 
321       // execute the statement
322       resultSet = statement.executeQuery();
323       while (resultSet.next())
324         keyList.add(resultSet.getString(1));//("TMODEL_KEY"));
325 
326       return keyList;
327     }
328     finally
329     {
330       try {
331         resultSet.close();
332         statement.close();
333       }
334       catch (Exception e) { /* ignored */ }
335     }
336   }
337 
338   /***
339    * Verify that 'publisherID' has the authority to update or delete
340    * TModel identified by the tModelKey parameter
341    *
342    * @param  tModelKey
343    * @param  publisherID
344    * @param  connection
345    * @throws java.sql.SQLException
346    */
347   public static boolean verifyOwnership(String tModelKey,String publisherID,Connection connection)
348     throws java.sql.SQLException
349   {
350     if ((tModelKey == null) || (publisherID == null))
351       return false;
352 
353     boolean authorized = false;
354     PreparedStatement statement = null;
355     ResultSet resultSet = null;
356 
357     try
358     {
359       statement = connection.prepareStatement(verifyOwnershipSQL);
360       statement.setString(1,tModelKey);
361       statement.setString(2,publisherID);
362 
363       if (log.isDebugEnabled()) {
364           log.debug(verifyOwnershipSQL +
365             "\n\t TMODEL_KEY=" + tModelKey +
366             "\n\t PUBLISHER_ID=" + publisherID + "\n");
367       }
368 
369       resultSet = statement.executeQuery();
370       if (resultSet.next())
371         authorized = true;
372 
373       return authorized;
374     }
375     finally
376     {
377       try {
378         resultSet.close();
379         statement.close();
380       }
381       catch (Exception e) { /* ignored */ }
382     }
383   }
384 }