1 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.util.jdbc.DynamicQuery; 30 31 34 class FindServiceByCategoryQuery 35 { 36 private static Log log = LogFactory.getLog(FindServiceByCategoryQuery.class); 38 39 static String selectSQL; 40 static 41 { 42 StringBuffer sql = new StringBuffer (200); 44 sql.append("SELECT S.SERVICE_KEY,S.LAST_UPDATE "); 45 sql.append("FROM BUSINESS_SERVICE S,SERVICE_CATEGORY C "); 46 selectSQL = sql.toString(); 47 } 48 49 59 public static Vector select(String businessKey,CategoryBag categoryBag,Vector keysIn,FindQualifiers qualifiers,Connection connection) 60 throws java.sql.SQLException 61 { 62 if ((keysIn != null) && (keysIn.size() == 0)) 67 return keysIn; 68 69 Vector keysOut = new Vector (); 70 PreparedStatement statement = null; 71 ResultSet resultSet = null; 72 73 DynamicQuery sql = new DynamicQuery(selectSQL); 75 appendWhere(sql,businessKey,categoryBag,qualifiers); 76 appendIn(sql,keysIn); 77 appendOrderBy(sql,qualifiers); 78 79 try 80 { 81 log.debug(sql.toString()); 82 83 statement = sql.buildPreparedStatement(connection); 84 resultSet = statement.executeQuery(); 85 86 while (resultSet.next()) 87 keysOut.addElement(resultSet.getString(1)); 89 return keysOut; 90 } 91 finally 92 { 93 try { 94 resultSet.close(); 95 } 96 catch (Exception e) 97 { 98 log.warn("An Exception was encountered while attempting to close " + 99 "the Find BusinessService ResultSet: "+e.getMessage(),e); 100 } 101 102 try { 103 statement.close(); 104 } 105 catch (Exception e) 106 { 107 log.warn("An Exception was encountered while attempting to close " + 108 "the Find BusinessService Statement: "+e.getMessage(),e); 109 } 110 } 111 } 112 115 private static void appendWhere(DynamicQuery sql,String businessKey,CategoryBag categoryBag,FindQualifiers qualifiers) 116 { 117 sql.append("WHERE C.SERVICE_KEY = S.SERVICE_KEY "); 118 if (businessKey != null) 119 { 120 sql.append("AND S.BUSINESS_KEY = ? "); 121 sql.addValue(businessKey); 122 } 123 124 if (categoryBag != null) 125 { 126 Vector keyedRefVector = categoryBag.getKeyedReferenceVector(); 127 128 if (keyedRefVector != null) 129 { 130 int vectorSize = keyedRefVector.size(); 131 if (vectorSize > 0) 132 { 133 sql.append("AND ("); 134 135 for (int i=0; i<vectorSize; i++) 136 { 137 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i); 138 String key = keyedRef.getTModelKey(); 139 String name = keyedRef.getKeyName(); 140 String value = keyedRef.getKeyValue(); 141 142 if (name == null) 143 name = ""; 144 145 if (value == null) 146 value = ""; 147 148 if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY)) 154 { 155 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)"); 156 sql.addValue(key); 157 sql.addValue(name); 158 sql.addValue(value); 159 160 if (i+1 < vectorSize) 161 sql.append(" OR "); 162 } 163 else 164 { 165 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)"); 166 sql.addValue(key); 167 sql.addValue(value); 168 169 if (i+1 < vectorSize) 170 sql.append(" OR "); 171 } 172 } 173 174 sql.append(") "); 175 } 176 } 177 } 178 } 179 180 190 public static Vector select(String businessKey,KeyedReference keyedRef,Vector keysIn,FindQualifiers qualifiers,Connection connection) 191 throws java.sql.SQLException 192 { 193 if ((keysIn != null) && (keysIn.size() == 0)) 198 return keysIn; 199 200 Vector keysOut = new Vector (); 201 PreparedStatement statement = null; 202 ResultSet resultSet = null; 203 204 DynamicQuery sql = new DynamicQuery(selectSQL); 206 appendWhere(sql,businessKey,keyedRef,qualifiers); 207 appendIn(sql,keysIn); 208 appendOrderBy(sql,qualifiers); 209 210 try 211 { 212 log.debug(sql.toString()); 213 214 statement = sql.buildPreparedStatement(connection); 215 resultSet = statement.executeQuery(); 216 217 while (resultSet.next()) 218 keysOut.addElement(resultSet.getString(1)); 220 return keysOut; 221 } 222 finally 223 { 224 try { 225 resultSet.close(); 226 } 227 catch (Exception e) 228 { 229 log.warn("An Exception was encountered while attempting to close " + 230 "the Find BusinessService ResultSet: "+e.getMessage(),e); 231 } 232 233 try { 234 statement.close(); 235 } 236 catch (Exception e) 237 { 238 log.warn("An Exception was encountered while attempting to close " + 239 "the Find BusinessService Statement: "+e.getMessage(),e); 240 } 241 } 242 } 243 244 247 private static void appendWhere(DynamicQuery sql,String businessKey,KeyedReference keyedRef,FindQualifiers qualifiers) 248 { 249 sql.append("WHERE C.SERVICE_KEY = S.SERVICE_KEY "); 250 if (businessKey != null) 251 { 252 sql.append("AND S.BUSINESS_KEY = ? "); 253 sql.addValue(businessKey); 254 } 255 256 if (keyedRef != null) 257 { 258 sql.append("AND ("); 259 260 String key = keyedRef.getTModelKey(); 261 String name = keyedRef.getKeyName(); 262 String value = keyedRef.getKeyValue(); 263 264 if (name == null) 265 name = ""; 266 267 if (value == null) 268 value = ""; 269 270 if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY)) 276 { 277 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)"); 278 sql.addValue(key); 279 sql.addValue(name); 280 sql.addValue(value); 281 } 282 else 283 { 284 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)"); 285 sql.addValue(key); 286 sql.addValue(value); 287 } 288 289 sql.append(") "); 290 } 291 } 292 293 302 private static void appendIn(DynamicQuery sql,Vector keysIn) 303 { 304 if (keysIn == null) 305 return; 306 307 sql.append("AND S.SERVICE_KEY IN ("); 308 309 int keyCount = keysIn.size(); 310 for (int i=0; i<keyCount; i++) 311 { 312 String key = (String )keysIn.elementAt(i); 313 sql.append("?"); 314 sql.addValue(key); 315 316 if ((i+1) < keyCount) 317 sql.append(","); 318 } 319 320 sql.append(") "); 321 } 322 323 326 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 327 { 328 sql.append("ORDER BY "); 329 330 if (qualifiers == null) 331 sql.append("S.LAST_UPDATE DESC"); 332 else if (qualifiers.sortByDateAsc) 333 sql.append("S.LAST_UPDATE ASC"); 334 else 335 sql.append("S.LAST_UPDATE DESC"); 336 } 337 } 338 | Popular Tags |