KickJava   Java API By Example, From Geeks To Geeks.

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


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 FindBindingByCategoryQuery
35 {
36   // private reference to the jUDDI logger
37
private static Log log = LogFactory.getLog(FindBindingByCategoryQuery.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 B.BINDING_KEY,B.LAST_UPDATE ");
45     sql.append("FROM BINDING_TEMPLATE B,BINDING_CATEGORY C ");
46     selectSQL = sql.toString();
47   }
48
49   /**
50    * Select ...
51    *
52    * @param serviceKey
53    * @param categoryBag
54    * @param keysIn
55    * @param qualifiers
56    * @param connection JDBC connection
57    * @throws java.sql.SQLException
58    */

59   public static Vector JavaDoc select(String JavaDoc serviceKey,CategoryBag categoryBag,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
60     throws java.sql.SQLException JavaDoc
61   {
62     // If there is a keysIn vector but it doesn't contain
63
// any keys then the previous query has exhausted
64
// all possibilities of a match so skip this call.
65
//
66
if ((keysIn != null) && (keysIn.size() == 0))
67       return keysIn;
68
69     Vector JavaDoc keysOut = new Vector JavaDoc();
70     PreparedStatement JavaDoc statement = null;
71     ResultSet JavaDoc resultSet = null;
72     DynamicQuery dynStmt = new DynamicQuery();
73     
74     DynamicQuery sql = new DynamicQuery(selectSQL);
75     appendWhere(sql,serviceKey,categoryBag,qualifiers);
76     appendIn(sql,keysIn);
77     appendOrderBy(sql,qualifiers);
78     
79     try
80     {
81       log.debug(sql.toString());
82       
83       statement = sql.buildPreparedStatement(connection);
84       resultSet = statement.executeQuery();
85       while (resultSet.next())
86       {
87         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
88
}
89
90       return keysOut;
91     }
92     finally
93     {
94       try {
95         resultSet.close();
96       }
97       catch (Exception JavaDoc e)
98       {
99         log.warn("An Exception was encountered while attempting to close " +
100           "the Find BindingTemplate ResultSet: "+e.getMessage(),e);
101       }
102
103       try {
104         statement.close();
105       }
106       catch (Exception JavaDoc e)
107       {
108         log.warn("An Exception was encountered while attempting to close " +
109           "the Find BindingTemplate Statement: "+e.getMessage(),e);
110       }
111     }
112   }
113
114   /**
115    *
116    */

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

192   public static Vector JavaDoc select(String JavaDoc serviceKey,KeyedReference keyedRef,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
193     throws java.sql.SQLException JavaDoc
194   {
195     // If there is a keysIn vector but it doesn't contain
196
// any keys then the previous query has exhausted
197
// all possibilities of a match so skip this call.
198
//
199
if ((keysIn != null) && (keysIn.size() == 0))
200       return keysIn;
201
202     Vector JavaDoc keysOut = new Vector JavaDoc();
203     PreparedStatement JavaDoc statement = null;
204     ResultSet JavaDoc resultSet = null;
205     DynamicQuery dynStmt = new DynamicQuery();
206     
207     DynamicQuery sql = new DynamicQuery(selectSQL);
208     appendWhere(sql,serviceKey,keyedRef,qualifiers);
209     appendIn(sql,keysIn);
210     appendOrderBy(sql,qualifiers);
211     
212     try
213     {
214       log.debug(sql.toString());
215       
216       statement = sql.buildPreparedStatement(connection);
217       resultSet = statement.executeQuery();
218       while (resultSet.next())
219       {
220         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
221
}
222
223       return keysOut;
224     }
225     finally
226     {
227       try {
228         resultSet.close();
229       }
230       catch (Exception JavaDoc e)
231       {
232         log.warn("An Exception was encountered while attempting to close " +
233           "the Find BindingTemplate ResultSet: "+e.getMessage(),e);
234       }
235
236       try {
237         statement.close();
238       }
239       catch (Exception JavaDoc e)
240       {
241         log.warn("An Exception was encountered while attempting to close " +
242           "the Find BindingTemplate Statement: "+e.getMessage(),e);
243       }
244     }
245   }
246
247   /**
248    *
249    */

250   private static void appendWhere(DynamicQuery sql,String JavaDoc serviceKey,KeyedReference keyedRef,FindQualifiers qualifiers)
251   {
252     sql.append("WHERE C.BINDING_KEY = B.BINDING_KEY ");
253     if (serviceKey != null)
254     {
255       sql.append("AND B.SERVICE_KEY = ? ");
256       sql.addValue(serviceKey);
257     }
258     
259     if (keyedRef != null)
260     {
261       sql.append("AND (");
262  
263       String JavaDoc key = keyedRef.getTModelKey();
264       String JavaDoc name = keyedRef.getKeyName();
265       String JavaDoc value = keyedRef.getKeyValue();
266        
267       if (name == null)
268         name = "";
269         
270       if (value == null)
271         value = "";
272         
273       // If the tModelKey involved is that of uddi-org:general_keywords,
274
// the keyNames are identical (DO NOT IGNORE keyName). Otherwise
275
// keyNames are not significant. Omitted keyNames are treated as
276
// identical to empty (zero length) keyNames.
277
//
278
if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY))
279       {
280         sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)");
281         sql.addValue(key);
282         sql.addValue(name);
283         sql.addValue(value);
284       }
285       else
286       {
287         sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)");
288         sql.addValue(key);
289         sql.addValue(value);
290       }
291   
292       sql.append(") ");
293     }
294   }
295
296   /**
297    * Utility method used to construct SQL "IN" statements such as
298    * the following SQL example:
299    *
300    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
301    *
302    * @param sql StringBuffer to append the final results to
303    * @param keysIn Vector of Strings used to construct the "IN" clause
304    */

305   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
306   {
307     if (keysIn == null)
308       return;
309
310     sql.append("AND B.BINDING_KEY IN (");
311
312     int keyCount = keysIn.size();
313     for (int i=0; i<keyCount; i++)
314     {
315       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
316       
317       sql.append("?");
318       sql.addValue(key);
319
320       if ((i+1) < keyCount)
321         sql.append(",");
322     }
323
324     sql.append(") ");
325   }
326
327   /**
328    *
329    */

330   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
331   {
332     sql.append("ORDER BY ");
333
334     if (qualifiers == null)
335       sql.append("B.LAST_UPDATE DESC");
336     else if (qualifiers.sortByDateAsc)
337       sql.append("B.LAST_UPDATE ASC");
338     else
339       sql.append("B.LAST_UPDATE DESC");
340   }
341 }
342
Popular Tags