KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Vector JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.juddi.datatype.CategoryBag;
26 import org.apache.juddi.datatype.KeyedReference;
27 import org.apache.juddi.datatype.request.FindQualifiers;
28 import org.apache.juddi.datatype.tmodel.TModel;
29 import org.apache.juddi.util.jdbc.DynamicQuery;
30
31 /**
32  * @author Steve Viens (sviens@apache.org)
33  */

34 class FindTModelByCategoryQuery
35 {
36   // private reference to the jUDDI logger
37
private static Log log = LogFactory.getLog(FindTModelByCategoryQuery.class);
38
39   static String JavaDoc selectSQL;
40   static
41   {
42     // build selectSQL
43
StringBuffer JavaDoc sql = new StringBuffer JavaDoc(200);
44     sql.append("SELECT M.TMODEL_KEY,M.LAST_UPDATE ");
45     sql.append("FROM TMODEL M,TMODEL_CATEGORY C ");
46     selectSQL = sql.toString();
47   }
48
49   /**
50    * Select ...
51    *
52    * @param connection JDBC connection
53    * @throws java.sql.SQLException
54    */

55   public static Vector JavaDoc select(CategoryBag categoryBag,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
56     throws java.sql.SQLException JavaDoc
57   {
58     // If there is a keysIn vector but it doesn't contain
59
// any keys then the previous query has exhausted
60
// all possibilities of a match so skip this call.
61
//
62
if ((keysIn != null) && (keysIn.size() == 0))
63       return keysIn;
64
65     Vector JavaDoc keysOut = new Vector JavaDoc();
66     PreparedStatement JavaDoc statement = null;
67     ResultSet JavaDoc resultSet = null;
68
69     // construct the SQL statement
70
DynamicQuery sql = new DynamicQuery(selectSQL);
71     appendWhere(sql,categoryBag,qualifiers);
72     appendIn(sql,keysIn);
73     appendOrderBy(sql,qualifiers);
74
75     try
76     {
77       log.debug(sql.toString());
78
79       statement = sql.buildPreparedStatement(connection);
80       resultSet = statement.executeQuery();
81
82       while (resultSet.next())
83         keysOut.addElement(resultSet.getString(1));//("TMODEL_KEY"));
84

85       return keysOut;
86     }
87     finally
88     {
89       try {
90         resultSet.close();
91       }
92       catch (Exception JavaDoc e)
93       {
94         log.warn("An Exception was encountered while attempting to close " +
95           "the Find TModel ResultSet: "+e.getMessage(),e);
96       }
97
98       try {
99         statement.close();
100       }
101       catch (Exception JavaDoc e)
102       {
103         log.warn("An Exception was encountered while attempting to close " +
104           "the Find TModel Statement: "+e.getMessage(),e);
105       }
106     }
107   }
108
109   /**
110    *
111    */

112   private static void appendWhere(DynamicQuery sql,CategoryBag categoryBag,FindQualifiers qualifiers)
113   {
114     sql.append("WHERE M.TMODEL_KEY = C.TMODEL_KEY ");
115
116     if (categoryBag != null)
117     {
118       Vector JavaDoc keyedRefVector = categoryBag.getKeyedReferenceVector();
119
120       if (keyedRefVector != null)
121       {
122         int vectorSize = keyedRefVector.size();
123         if (vectorSize > 0)
124         {
125           sql.append("AND (");
126
127           for (int i=0; i<vectorSize; i++)
128           {
129             KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i);
130             String JavaDoc key = keyedRef.getTModelKey();
131             String JavaDoc name = keyedRef.getKeyName();
132             String JavaDoc value = keyedRef.getKeyValue();
133             
134             if (name == null)
135               name = "";
136             
137             if (value == null)
138               value = "";
139             
140             // If the tModelKey involved is that of uddi-org:general_keywords,
141
// the keyNames are identical (DO NOT IGNORE keyName). Otherwise
142
// keyNames are not significant. Omitted keyNames are treated as
143
// identical to empty (zero length) keyNames.
144
//
145
if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY))
146             {
147               sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)");
148               sql.addValue(key);
149               sql.addValue(name);
150               sql.addValue(value);
151
152               if (i+1 < vectorSize)
153                 sql.append(" OR ");
154             }
155             else
156             {
157               sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)");
158               sql.addValue(key);
159               sql.addValue(value);
160
161               if (i+1 < vectorSize)
162                 sql.append(" OR ");
163             }
164           }
165
166           sql.append(") ");
167         }
168       }
169     }
170   }
171
172   /**
173    * Select ...
174    *
175    * @param connection JDBC connection
176    * @throws java.sql.SQLException
177    */

178   public static Vector JavaDoc select(KeyedReference keyedRef,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
179     throws java.sql.SQLException JavaDoc
180   {
181     // If there is a keysIn vector but it doesn't contain
182
// any keys then the previous query has exhausted
183
// all possibilities of a match so skip this call.
184
//
185
if ((keysIn != null) && (keysIn.size() == 0))
186       return keysIn;
187
188     Vector JavaDoc keysOut = new Vector JavaDoc();
189     PreparedStatement JavaDoc statement = null;
190     ResultSet JavaDoc resultSet = null;
191
192     // construct the SQL statement
193
DynamicQuery sql = new DynamicQuery(selectSQL);
194     appendWhere(sql,keyedRef,qualifiers);
195     appendIn(sql,keysIn);
196     appendOrderBy(sql,qualifiers);
197
198     try
199     {
200       log.debug(sql.toString());
201
202       statement = sql.buildPreparedStatement(connection);
203       resultSet = statement.executeQuery();
204
205       while (resultSet.next())
206         keysOut.addElement(resultSet.getString(1));//("TMODEL_KEY"));
207

208       return keysOut;
209     }
210     finally
211     {
212       try {
213         resultSet.close();
214       }
215       catch (Exception JavaDoc e)
216       {
217         log.warn("An Exception was encountered while attempting to close " +
218           "the Find TModel ResultSet: "+e.getMessage(),e);
219       }
220
221       try {
222         statement.close();
223       }
224       catch (Exception JavaDoc e)
225       {
226         log.warn("An Exception was encountered while attempting to close " +
227           "the Find TModel Statement: "+e.getMessage(),e);
228       }
229     }
230   }
231
232   /**
233    *
234    */

235   private static void appendWhere(DynamicQuery sql,KeyedReference keyedRef,FindQualifiers qualifiers)
236   {
237     sql.append("WHERE M.TMODEL_KEY = C.TMODEL_KEY ");
238
239     if (keyedRef != null)
240     {
241       sql.append("AND (");
242
243       String JavaDoc key = keyedRef.getTModelKey();
244       String JavaDoc name = keyedRef.getKeyName();
245       String JavaDoc value = keyedRef.getKeyValue();
246         
247       if (name == null)
248         name = "";
249         
250       if (value == null)
251         value = "";
252         
253       // If the tModelKey involved is that of uddi-org:general_keywords,
254
// the keyNames are identical (DO NOT IGNORE keyName). Otherwise
255
// keyNames are not significant. Omitted keyNames are treated as
256
// identical to empty (zero length) keyNames.
257
//
258
if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY))
259       {
260         sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)");
261         sql.addValue(key);
262         sql.addValue(name);
263         sql.addValue(value);
264       }
265       else
266       {
267         sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)");
268         sql.addValue(key);
269         sql.addValue(value);
270       }
271
272       sql.append(") ");
273     }
274   }
275
276   /**
277    * Utility method used to construct SQL "IN" statements such as
278    * the following SQL example:
279    *
280    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
281    *
282    * @param sql StringBuffer to append the final results to
283    * @param keysIn Vector of Strings used to construct the "IN" clause
284    */

285   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
286   {
287     if (keysIn == null)
288       return;
289
290     sql.append("AND M.TMODEL_KEY IN (");
291
292     int keyCount = keysIn.size();
293     for (int i=0; i<keyCount; i++)
294     {
295       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
296       sql.append("?");
297       sql.addValue(key);
298
299       if ((i+1) < keyCount)
300         sql.append(",");
301     }
302
303     sql.append(") ");
304   }
305
306   /**
307    *
308    */

309   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
310   {
311     sql.append("ORDER BY ");
312
313     if (qualifiers == null)
314       sql.append("M.LAST_UPDATE DESC");
315     else if (qualifiers.sortByDateAsc)
316       sql.append("M.LAST_UPDATE ASC");
317     else
318       sql.append("M.LAST_UPDATE DESC");
319   }
320 }
321
Popular Tags