KickJava   Java API By Example, From Geeks To Geeks.

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


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.request.FindQualifiers;
26 import org.apache.juddi.util.jdbc.DynamicQuery;
27
28 /**
29  * @author Steve Viens (sviens@apache.org)
30  */

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

55   public static Vector JavaDoc select(String JavaDoc businessKey,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,businessKey);
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));//("SERVICE_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 FindBusinessService 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 FindBusinessService Statement: "+e.getMessage(),e);
105       }
106     }
107   }
108
109   /**
110    *
111    */

112   private static void appendWhere(DynamicQuery sql,String JavaDoc businessKey)
113   {
114     sql.append("WHERE S.BUSINESS_KEY = ? ");
115     sql.addValue(businessKey);
116   }
117
118   /**
119    * Utility method used to construct SQL "IN" statements such as
120    * the following SQL example:
121    *
122    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
123    *
124    * @param sql StringBuffer to append the final results to
125    * @param keysIn Vector of Strings used to construct the "IN" clause
126    */

127   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
128   {
129     if (keysIn == null)
130       return;
131
132     sql.append("AND S.SERVICE_KEY IN (");
133
134     int keyCount = keysIn.size();
135     for (int i=0; i<keyCount; i++)
136     {
137       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
138       sql.append("?");
139       sql.addValue(key);
140
141       if ((i+1) < keyCount)
142         sql.append(",");
143     }
144
145     sql.append(") ");
146   }
147
148   /**
149    *
150    */

151   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
152   {
153     sql.append("ORDER BY ");
154
155     if (qualifiers == null)
156       sql.append("S.LAST_UPDATE DESC");
157     else if (qualifiers.sortByDateAsc)
158       sql.append("S.LAST_UPDATE ASC");
159     else
160       sql.append("S.LAST_UPDATE DESC");
161   }
162 }
163
Popular Tags