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.util.Vector;
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.registry.RegistryEngine;
30  import org.apache.juddi.util.Config;
31  import org.apache.juddi.util.jdbc.DynamicQuery;
32  
33  /***
34   * @author Steve Viens (sviens@apache.org)
35   */
36  class FindServiceByCategoryQuery
37  {
38    // private reference to the jUDDI logger
39    private static Log log = LogFactory.getLog(FindServiceByCategoryQuery.class);
40  
41    static String selectSQL;
42    static String tablePrefix;
43    static
44    {
45     tablePrefix = Config.getStringProperty(
46         RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
47      // build selectSQL
48      StringBuffer sql = new StringBuffer(200);
49      sql.append("SELECT S.SERVICE_KEY,S.LAST_UPDATE ");
50      sql.append("FROM ").append(tablePrefix).append("BUSINESS_SERVICE S,").append(tablePrefix).append("SERVICE_CATEGORY C ");
51      selectSQL = sql.toString();
52    }
53  
54    /***
55     * Select ...
56     *
57     * @param businessKey
58     * @param categoryBag
59     * @param keysIn
60     * @param qualifiers
61     * @param connection JDBC connection
62     * @throws java.sql.SQLException
63     */
64    public static Vector select(String businessKey,CategoryBag categoryBag,Vector keysIn,FindQualifiers qualifiers,Connection connection)
65      throws java.sql.SQLException
66    {
67      // If there is a keysIn vector but it doesn't contain
68      // any keys then the previous query has exhausted
69      // all possibilities of a match so skip this call.
70      //
71      if ((keysIn != null) && (keysIn.size() == 0))
72        return keysIn;
73  
74      Vector keysOut = new Vector();
75      PreparedStatement statement = null;
76      ResultSet resultSet = null;
77  
78      // construct the SQL statement
79      DynamicQuery sql = new DynamicQuery(selectSQL);
80      appendWhere(sql,businessKey,categoryBag,qualifiers);
81      appendIn(sql,keysIn);
82      appendOrderBy(sql,qualifiers);
83  
84      try
85      {
86        log.debug(sql.toString());
87  
88        statement = sql.buildPreparedStatement(connection);
89        resultSet = statement.executeQuery();
90  
91        while (resultSet.next())
92          keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
93  
94        return keysOut;
95      }
96      finally
97      {
98        try {
99          resultSet.close();
100       }
101       catch (Exception e)
102       {
103         log.warn("An Exception was encountered while attempting to close " +
104           "the Find BusinessService ResultSet: "+e.getMessage(),e);
105       }
106 
107       try {
108         statement.close();
109       }
110       catch (Exception e)
111       {
112         log.warn("An Exception was encountered while attempting to close " +
113           "the Find BusinessService Statement: "+e.getMessage(),e);
114       }
115     }
116   }
117   /***
118    *
119    */
120   private static void appendWhere(DynamicQuery sql,String businessKey,CategoryBag categoryBag,FindQualifiers qualifiers)
121   {
122     sql.append("WHERE C.SERVICE_KEY = S.SERVICE_KEY ");
123     if (businessKey != null && businessKey.length() > 0)
124     {
125       sql.append("AND S.BUSINESS_KEY = ? ");
126       sql.addValue(businessKey);
127     }
128     
129     if (categoryBag != null)
130     {
131       Vector keyedRefVector = categoryBag.getKeyedReferenceVector();
132   
133       if (keyedRefVector != null)
134       {
135         int vectorSize = keyedRefVector.size();
136         if (vectorSize > 0)
137         {
138           sql.append("AND (");
139   
140           for (int i=0; i<vectorSize; i++)
141           {
142             KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i);
143             String key = keyedRef.getTModelKey();
144             String name = keyedRef.getKeyName();
145             String value = keyedRef.getKeyValue();
146             
147             if (name == null)
148               name = "";
149             
150             if (value == null)
151               value = "";
152             
153             // If the tModelKey involved is that of uddi-org:general_keywords, 
154             // the keyNames are identical (DO NOT IGNORE keyName). Otherwise 
155             // keyNames are not significant. Omitted keyNames are treated as 
156             // identical to empty (zero length) keyNames.
157             //
158             if (key == null || key.length() == 0 || key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY)) 
159             {
160               sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)");
161               sql.addValue(TModel.GENERAL_KEYWORDS_TMODEL_KEY);
162               sql.addValue(name);
163               sql.addValue(value);
164 
165               if (i+1 < vectorSize)
166                 sql.append(" OR ");
167             }
168             else 
169             {
170               sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)");
171               sql.addValue(key);
172               sql.addValue(value);
173 
174               if (i+1 < vectorSize)
175                 sql.append(" OR ");
176             }
177           }
178   
179           sql.append(") ");
180         }
181       }
182     }
183   }
184 
185   /***
186    * Select ...
187    *
188    * @param businessKey
189    * @param keyedRef
190    * @param keysIn
191    * @param qualifiers
192    * @param connection JDBC connection
193    * @throws java.sql.SQLException
194    */
195   public static Vector select(String businessKey,KeyedReference keyedRef,Vector keysIn,FindQualifiers qualifiers,Connection connection)
196     throws java.sql.SQLException
197   {
198     // If there is a keysIn vector but it doesn't contain
199     // any keys then the previous query has exhausted
200     // all possibilities of a match so skip this call.
201     //
202     if ((keysIn != null) && (keysIn.size() == 0))
203       return keysIn;
204 
205     Vector keysOut = new Vector();
206     PreparedStatement statement = null;
207     ResultSet resultSet = null;
208 
209     // construct the SQL statement
210     DynamicQuery sql = new DynamicQuery(selectSQL);
211     appendWhere(sql,businessKey,keyedRef,qualifiers);
212     appendIn(sql,keysIn);
213     appendOrderBy(sql,qualifiers);
214 
215     try
216     {
217       log.debug(sql.toString());
218 
219       statement = sql.buildPreparedStatement(connection);
220       resultSet = statement.executeQuery();
221 
222       while (resultSet.next())
223         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
224 
225       return keysOut;
226     }
227     finally
228     {
229       try {
230         resultSet.close();
231       }
232       catch (Exception e)
233       {
234         log.warn("An Exception was encountered while attempting to close " +
235           "the Find BusinessService ResultSet: "+e.getMessage(),e);
236       }
237 
238       try {
239         statement.close();
240       }
241       catch (Exception e)
242       {
243         log.warn("An Exception was encountered while attempting to close " +
244           "the Find BusinessService Statement: "+e.getMessage(),e);
245       }
246     }
247   }
248 
249   /***
250    *
251    */
252   private static void appendWhere(DynamicQuery sql,String businessKey,KeyedReference keyedRef,FindQualifiers qualifiers)
253   {
254     sql.append("WHERE C.SERVICE_KEY = S.SERVICE_KEY ");
255     if (businessKey != null)
256     {
257       sql.append("AND S.BUSINESS_KEY = ? ");
258       sql.addValue(businessKey);
259     }
260   
261     if (keyedRef != null)
262     {
263       sql.append("AND (");
264 
265       String key = keyedRef.getTModelKey();
266       String name = keyedRef.getKeyName();
267       String value = keyedRef.getKeyValue();
268           
269       if (name == null)
270         name = "";
271           
272       if (value == null)
273         value = "";
274           
275       // If the tModelKey involved is that of uddi-org:general_keywords, 
276       // the keyNames are identical (DO NOT IGNORE keyName). Otherwise 
277       // keyNames are not significant. Omitted keyNames are treated as 
278       // identical to empty (zero length) keyNames.
279       //
280       if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY)) 
281       {
282         sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)");
283         sql.addValue(key);
284         sql.addValue(name);
285         sql.addValue(value); 
286       }
287       else 
288       {
289         sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)");
290         sql.addValue(key);
291         sql.addValue(value); 
292       }
293 
294       sql.append(") ");
295     }
296   }
297 
298   /***
299    * Utility method used to construct SQL "IN" statements such as
300    * the following SQL example:
301    *
302    *   SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
303    *
304    * @param sql StringBuffer to append the final results to
305    * @param keysIn Vector of Strings used to construct the "IN" clause
306    */
307   private static void appendIn(DynamicQuery sql,Vector keysIn)
308   {
309     if (keysIn == null)
310       return;
311 
312     sql.append("AND S.SERVICE_KEY IN (");
313 
314     int keyCount = keysIn.size();
315     for (int i=0; i<keyCount; i++)
316     {
317       String key = (String)keysIn.elementAt(i);
318       sql.append("?");
319       sql.addValue(key);
320 
321       if ((i+1) < keyCount)
322         sql.append(",");
323     }
324 
325     sql.append(") ");
326   }
327 
328   /***
329    *
330    */
331   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
332   {
333     sql.append("ORDER BY ");
334 
335     if (qualifiers == null)
336       sql.append("S.LAST_UPDATE DESC");
337     else if (qualifiers.sortByDateAsc)
338       sql.append("S.LAST_UPDATE ASC");
339     else
340       sql.append("S.LAST_UPDATE DESC");
341   }
342 }