KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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