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