KickJava   Java API By Example, From Geeks To Geeks.

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


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 FindServiceByCategoryQuery
35 {
36   // private reference to the jUDDI logger
37
private static Log log = LogFactory.getLog(FindServiceByCategoryQuery.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 S.SERVICE_KEY,S.LAST_UPDATE ");
45     sql.append("FROM BUSINESS_SERVICE S,SERVICE_CATEGORY C ");
46     selectSQL = sql.toString();
47   }
48
49   /**
50    * Select ...
51    *
52    * @param businessKey
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 businessKey,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
73     // construct the SQL statement
74
DynamicQuery sql = new DynamicQuery(selectSQL);
75     appendWhere(sql,businessKey,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
86       while (resultSet.next())
87         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
88

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

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

190   public static Vector JavaDoc select(String JavaDoc businessKey,KeyedReference keyedRef,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
191     throws java.sql.SQLException JavaDoc
192   {
193     // If there is a keysIn vector but it doesn't contain
194
// any keys then the previous query has exhausted
195
// all possibilities of a match so skip this call.
196
//
197
if ((keysIn != null) && (keysIn.size() == 0))
198       return keysIn;
199
200     Vector JavaDoc keysOut = new Vector JavaDoc();
201     PreparedStatement JavaDoc statement = null;
202     ResultSet JavaDoc resultSet = null;
203
204     // construct the SQL statement
205
DynamicQuery sql = new DynamicQuery(selectSQL);
206     appendWhere(sql,businessKey,keyedRef,qualifiers);
207     appendIn(sql,keysIn);
208     appendOrderBy(sql,qualifiers);
209
210     try
211     {
212       log.debug(sql.toString());
213
214       statement = sql.buildPreparedStatement(connection);
215       resultSet = statement.executeQuery();
216
217       while (resultSet.next())
218         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
219

220       return keysOut;
221     }
222     finally
223     {
224       try {
225         resultSet.close();
226       }
227       catch (Exception JavaDoc e)
228       {
229         log.warn("An Exception was encountered while attempting to close " +
230           "the Find BusinessService ResultSet: "+e.getMessage(),e);
231       }
232
233       try {
234         statement.close();
235       }
236       catch (Exception JavaDoc e)
237       {
238         log.warn("An Exception was encountered while attempting to close " +
239           "the Find BusinessService Statement: "+e.getMessage(),e);
240       }
241     }
242   }
243
244   /**
245    *
246    */

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

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

326   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
327   {
328     sql.append("ORDER BY ");
329
330     if (qualifiers == null)
331       sql.append("S.LAST_UPDATE DESC");
332     else if (qualifiers.sortByDateAsc)
333       sql.append("S.LAST_UPDATE ASC");
334     else
335       sql.append("S.LAST_UPDATE DESC");
336   }
337 }
338
Popular Tags