KickJava   Java API By Example, From Geeks To Geeks.

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


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.TModelBag;
26 import org.apache.juddi.datatype.request.FindQualifiers;
27 import org.apache.juddi.util.jdbc.DynamicQuery;
28
29 /**
30  * tModelBag: This is a list of tModel uuid_key values that represent the
31  * technical fingerprint of a bindingTemplate structure to find. All
32  * bindingTemplate structures within any businessService associated with the
33  * businessEntity specified by the businessKey argument will be searched.
34  *
35  * If more than one tModel key is specified in this structure, only
36  * businessService structures that contain bindingTemplate structures with
37  * fingerprint information that matches all of the tModel keys specified will
38  * be returned (logical AND only).
39  *
40  * @author Steve Viens (sviens@apache.org)
41  */

42 class FindServiceByTModelKeyQuery
43 {
44   // private reference to the jUDDI logger
45
private static Log log = LogFactory.getLog(FindServiceByTModelKeyQuery.class);
46
47   static String JavaDoc selectSQL;
48   static
49   {
50     // build selectSQL
51
StringBuffer JavaDoc sql = new StringBuffer JavaDoc(200);
52     sql.append("SELECT S.SERVICE_KEY,S.LAST_UPDATE ");
53     sql.append("FROM BUSINESS_SERVICE S,BINDING_TEMPLATE T,TMODEL_INSTANCE_INFO I ");
54     selectSQL = sql.toString();
55   }
56
57   /**
58    * Select ...
59    *
60    * @param connection JDBC connection
61    * @throws java.sql.SQLException
62    */

63   public static Vector JavaDoc select(String JavaDoc businessKey,TModelBag tModelBag,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
64     throws java.sql.SQLException JavaDoc
65   {
66     // if there is a keysIn vector but it doesn't contain
67
// any keys then the previous query has exhausted
68
// all possibilities of a match so skip this call.
69
if ((keysIn != null) && (keysIn.size() == 0))
70       return keysIn;
71
72     Vector JavaDoc keysOut = new Vector JavaDoc();
73     PreparedStatement JavaDoc statement = null;
74     ResultSet JavaDoc resultSet = null;
75
76     // construct the SQL statement
77
DynamicQuery sql = new DynamicQuery(selectSQL);
78     appendWhere(sql,businessKey,tModelBag,qualifiers);
79     appendIn(sql,keysIn);
80     appendOrderBy(sql,qualifiers);
81
82     try
83     {
84       log.debug(sql.toString());
85
86       statement = sql.buildPreparedStatement(connection);
87       resultSet = statement.executeQuery();
88
89       while (resultSet.next())
90         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
91

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

119   private static void appendWhere(DynamicQuery sql,String JavaDoc businessKey,TModelBag tModelBag,FindQualifiers qualifiers)
120   {
121     sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY ");
122     sql.append("AND T.SERVICE_KEY = S.SERVICE_KEY ");
123
124     if (businessKey != null)
125     {
126       sql.append("AND S.BUSINESS_KEY = ? ");
127       sql.addValue(businessKey);
128     }
129     
130     Vector JavaDoc keyVector = tModelBag.getTModelKeyVector();
131
132     int vectorSize = keyVector.size();
133     if (vectorSize > 0)
134     {
135       sql.append("AND (");
136
137       for (int i=0; i<vectorSize; i++)
138       {
139         String JavaDoc key = (String JavaDoc)keyVector.elementAt(i);
140
141         sql.append("I.TMODEL_KEY = ? ");
142         sql.addValue(key);
143         
144         if (i+1 < vectorSize)
145           sql.append(" OR ");
146       }
147
148       sql.append(") ");
149     }
150   }
151
152   /**
153    * Select ...
154    *
155    * @param connection JDBC connection
156    * @throws java.sql.SQLException
157    */

158   public static Vector JavaDoc select(String JavaDoc businessKey,String JavaDoc tModelKey,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
159     throws java.sql.SQLException JavaDoc
160   {
161     // if there is a keysIn vector but it doesn't contain
162
// any keys then the previous query has exhausted
163
// all possibilities of a match so skip this call.
164
if ((keysIn != null) && (keysIn.size() == 0))
165       return keysIn;
166
167     Vector JavaDoc keysOut = new Vector JavaDoc();
168     PreparedStatement JavaDoc statement = null;
169     ResultSet JavaDoc resultSet = null;
170
171     // construct the SQL statement
172
DynamicQuery sql = new DynamicQuery(selectSQL);
173     appendWhere(sql,businessKey,tModelKey,qualifiers);
174     appendIn(sql,keysIn);
175     appendOrderBy(sql,qualifiers);
176
177     try
178     {
179       log.debug(sql.toString());
180
181       statement = sql.buildPreparedStatement(connection);
182       resultSet = statement.executeQuery();
183
184       while (resultSet.next())
185         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
186

187       return keysOut;
188     }
189     finally
190     {
191       try {
192         resultSet.close();
193       }
194       catch (Exception JavaDoc e)
195       {
196         log.warn("An Exception was encountered while attempting to close " +
197           "the Find BusinessService ResultSet: "+e.getMessage(),e);
198       }
199
200       try {
201         statement.close();
202       }
203       catch (Exception JavaDoc e)
204       {
205         log.warn("An Exception was encountered while attempting to close " +
206           "the Find BusinessService Statement: "+e.getMessage(),e);
207       }
208     }
209   }
210
211   /**
212    *
213    */

214   private static void appendWhere(DynamicQuery sql,String JavaDoc businessKey,String JavaDoc tModelKey,FindQualifiers qualifiers)
215   {
216     sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY ");
217     sql.append("AND T.SERVICE_KEY = S.SERVICE_KEY ");
218
219     if ((businessKey != null) && (businessKey.trim().length() > 0))
220     {
221       sql.append("AND S.BUSINESS_KEY = ? ");
222       sql.addValue(businessKey);
223     }
224     
225     if ((tModelKey != null) && (tModelKey.trim().length() > 0))
226     {
227       sql.append("AND I.TMODEL_KEY = ? ");
228       sql.addValue(tModelKey);
229     }
230   }
231
232   /**
233    * Utility method used to construct SQL "IN" statements such as
234    * the following SQL example:
235    *
236    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
237    *
238    * @param sql StringBuffer to append the final results to
239    * @param keysIn Vector of Strings used to construct the "IN" clause
240    */

241   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
242   {
243     if (keysIn == null)
244       return;
245
246     sql.append("AND S.SERVICE_KEY IN (");
247
248     int keyCount = keysIn.size();
249     for (int i=0; i<keyCount; i++)
250     {
251       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
252       sql.append("?");
253       sql.addValue(key);
254
255       if ((i+1) < keyCount)
256         sql.append(",");
257     }
258
259     sql.append(") ");
260   }
261
262   /**
263    *
264    */

265   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
266   {
267     sql.append("ORDER BY ");
268
269     if (qualifiers == null)
270       sql.append("S.LAST_UPDATE DESC");
271     else if (qualifiers.sortByDateAsc)
272       sql.append("S.LAST_UPDATE ASC");
273     else
274       sql.append("S.LAST_UPDATE DESC");
275   }
276 }
277
Popular Tags