1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.TModelBag;
26 import org.apache.juddi.datatype.request.FindQualifiers;
27 import org.apache.juddi.registry.RegistryEngine;
28 import org.apache.juddi.util.Config;
29 import org.apache.juddi.util.jdbc.DynamicQuery;
30
31 /***
32 * tModelBag: This is a list of tModel uuid_key values that represent the
33 * technical fingerprint of a bindingTemplate structure to find. All
34 * bindingTemplate structures within any businessService associated with the
35 * businessEntity specified by the businessKey argument will be searched.
36 *
37 * If more than one tModel key is specified in this structure, only
38 * businessService structures that contain bindingTemplate structures with
39 * fingerprint information that matches all of the tModel keys specified will
40 * be returned (logical AND only).
41 *
42 * @author Steve Viens (sviens@apache.org)
43 */
44 class FindServiceByTModelKeyQuery
45 {
46
47 private static Log log = LogFactory.getLog(FindServiceByTModelKeyQuery.class);
48
49 static String selectSQL;
50 static String tablePrefix;
51 static
52 {
53 tablePrefix = Config.getStringProperty(
54 RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
55
56 StringBuffer sql = new StringBuffer(200);
57 sql.append("SELECT S.SERVICE_KEY,S.LAST_UPDATE ");
58 sql.append("FROM ").append(tablePrefix).append("BUSINESS_SERVICE S,")
59 .append(tablePrefix).append("BINDING_TEMPLATE T,")
60 .append(tablePrefix).append("TMODEL_INSTANCE_INFO I ");
61 selectSQL = sql.toString();
62 }
63
64 /***
65 * Select ...
66 *
67 * @param connection JDBC connection
68 * @throws java.sql.SQLException
69 */
70 public static Vector select(String businessKey,TModelBag tModelBag,Vector keysIn,FindQualifiers qualifiers,Connection connection)
71 throws java.sql.SQLException
72 {
73
74
75
76 if ((keysIn != null) && (keysIn.size() == 0))
77 return keysIn;
78
79 Vector keysOut = new Vector();
80 PreparedStatement statement = null;
81 ResultSet resultSet = null;
82
83
84 DynamicQuery sql = new DynamicQuery(selectSQL);
85 appendWhere(sql,businessKey,tModelBag,qualifiers);
86 appendIn(sql,keysIn);
87 appendOrderBy(sql,qualifiers);
88
89 try
90 {
91 log.debug(sql.toString());
92
93 statement = sql.buildPreparedStatement(connection);
94 resultSet = statement.executeQuery();
95
96 while (resultSet.next())
97 keysOut.addElement(resultSet.getString(1));
98
99 return keysOut;
100 }
101 finally
102 {
103 try {
104 resultSet.close();
105 }
106 catch (Exception e)
107 {
108 log.warn("An Exception was encountered while attempting to close " +
109 "the Find BusinessService ResultSet: "+e.getMessage(),e);
110 }
111
112 try {
113 statement.close();
114 }
115 catch (Exception e)
116 {
117 log.warn("An Exception was encountered while attempting to close " +
118 "the Find BusinessService Statement: "+e.getMessage(),e);
119 }
120 }
121 }
122
123 /***
124 *
125 */
126 private static void appendWhere(DynamicQuery sql,String businessKey,TModelBag tModelBag,FindQualifiers qualifiers)
127 {
128 sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY ");
129 sql.append("AND T.SERVICE_KEY = S.SERVICE_KEY ");
130
131 if (businessKey != null)
132 {
133 sql.append("AND S.BUSINESS_KEY = ? ");
134 sql.addValue(businessKey);
135 }
136
137 Vector keyVector = tModelBag.getTModelKeyVector();
138
139 int vectorSize = keyVector.size();
140 if (vectorSize > 0)
141 {
142 sql.append("AND (");
143
144 for (int i=0; i<vectorSize; i++)
145 {
146 String key = (String)keyVector.elementAt(i);
147
148 sql.append("I.TMODEL_KEY = ? ");
149 sql.addValue(key);
150
151 if (i+1 < vectorSize)
152 sql.append(" OR ");
153 }
154
155 sql.append(") ");
156 }
157 }
158
159 /***
160 * Select ...
161 *
162 * @param connection JDBC connection
163 * @throws java.sql.SQLException
164 */
165 public static Vector select(String businessKey,String tModelKey,Vector keysIn,FindQualifiers qualifiers,Connection connection)
166 throws java.sql.SQLException
167 {
168
169
170
171 if ((keysIn != null) && (keysIn.size() == 0))
172 return keysIn;
173
174 Vector keysOut = new Vector();
175 PreparedStatement statement = null;
176 ResultSet resultSet = null;
177
178
179 DynamicQuery sql = new DynamicQuery(selectSQL);
180 appendWhere(sql,businessKey,tModelKey,qualifiers);
181 appendIn(sql,keysIn);
182 appendOrderBy(sql,qualifiers);
183
184 try
185 {
186 log.debug(sql.toString());
187
188 statement = sql.buildPreparedStatement(connection);
189 resultSet = statement.executeQuery();
190
191 while (resultSet.next())
192 keysOut.addElement(resultSet.getString(1));
193
194 return keysOut;
195 }
196 finally
197 {
198 try {
199 resultSet.close();
200 }
201 catch (Exception e)
202 {
203 log.warn("An Exception was encountered while attempting to close " +
204 "the Find BusinessService ResultSet: "+e.getMessage(),e);
205 }
206
207 try {
208 statement.close();
209 }
210 catch (Exception e)
211 {
212 log.warn("An Exception was encountered while attempting to close " +
213 "the Find BusinessService Statement: "+e.getMessage(),e);
214 }
215 }
216 }
217
218 /***
219 *
220 */
221 private static void appendWhere(DynamicQuery sql,String businessKey,String tModelKey,FindQualifiers qualifiers)
222 {
223 sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY ");
224 sql.append("AND T.SERVICE_KEY = S.SERVICE_KEY ");
225
226 if ((businessKey != null) && (businessKey.trim().length() > 0))
227 {
228 sql.append("AND S.BUSINESS_KEY = ? ");
229 sql.addValue(businessKey);
230 }
231
232 if ((tModelKey != null) && (tModelKey.trim().length() > 0))
233 {
234 sql.append("AND I.TMODEL_KEY = ? ");
235 sql.addValue(tModelKey);
236 }
237 }
238
239 /***
240 * Utility method used to construct SQL "IN" statements such as
241 * the following SQL example:
242 *
243 * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
244 *
245 * @param sql StringBuffer to append the final results to
246 * @param keysIn Vector of Strings used to construct the "IN" clause
247 */
248 private static void appendIn(DynamicQuery sql,Vector keysIn)
249 {
250 if (keysIn == null)
251 return;
252
253 sql.append("AND S.SERVICE_KEY IN (");
254
255 int keyCount = keysIn.size();
256 for (int i=0; i<keyCount; i++)
257 {
258 String key = (String)keysIn.elementAt(i);
259 sql.append("?");
260 sql.addValue(key);
261
262 if ((i+1) < keyCount)
263 sql.append(",");
264 }
265
266 sql.append(") ");
267 }
268
269 /***
270 *
271 */
272 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
273 {
274 sql.append("ORDER BY ");
275
276 if (qualifiers == null)
277 sql.append("S.LAST_UPDATE DESC");
278 else if (qualifiers.sortByDateAsc)
279 sql.append("S.LAST_UPDATE ASC");
280 else
281 sql.append("S.LAST_UPDATE DESC");
282 }
283 }