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 FindTModelByCategoryQuery 35 { 36 private static Log log = LogFactory.getLog(FindTModelByCategoryQuery.class); 38 39 static String selectSQL; 40 static 41 { 42 StringBuffer sql = new StringBuffer (200); 44 sql.append("SELECT M.TMODEL_KEY,M.LAST_UPDATE "); 45 sql.append("FROM TMODEL M,TMODEL_CATEGORY C "); 46 selectSQL = sql.toString(); 47 } 48 49 55 public static Vector select(CategoryBag categoryBag,Vector keysIn,FindQualifiers qualifiers,Connection connection) 56 throws java.sql.SQLException 57 { 58 if ((keysIn != null) && (keysIn.size() == 0)) 63 return keysIn; 64 65 Vector keysOut = new Vector (); 66 PreparedStatement statement = null; 67 ResultSet resultSet = null; 68 69 DynamicQuery sql = new DynamicQuery(selectSQL); 71 appendWhere(sql,categoryBag,qualifiers); 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)); 85 return keysOut; 86 } 87 finally 88 { 89 try { 90 resultSet.close(); 91 } 92 catch (Exception e) 93 { 94 log.warn("An Exception was encountered while attempting to close " + 95 "the Find TModel ResultSet: "+e.getMessage(),e); 96 } 97 98 try { 99 statement.close(); 100 } 101 catch (Exception e) 102 { 103 log.warn("An Exception was encountered while attempting to close " + 104 "the Find TModel Statement: "+e.getMessage(),e); 105 } 106 } 107 } 108 109 112 private static void appendWhere(DynamicQuery sql,CategoryBag categoryBag,FindQualifiers qualifiers) 113 { 114 sql.append("WHERE M.TMODEL_KEY = C.TMODEL_KEY "); 115 116 if (categoryBag != null) 117 { 118 Vector keyedRefVector = categoryBag.getKeyedReferenceVector(); 119 120 if (keyedRefVector != null) 121 { 122 int vectorSize = keyedRefVector.size(); 123 if (vectorSize > 0) 124 { 125 sql.append("AND ("); 126 127 for (int i=0; i<vectorSize; i++) 128 { 129 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i); 130 String key = keyedRef.getTModelKey(); 131 String name = keyedRef.getKeyName(); 132 String value = keyedRef.getKeyValue(); 133 134 if (name == null) 135 name = ""; 136 137 if (value == null) 138 value = ""; 139 140 if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY)) 146 { 147 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)"); 148 sql.addValue(key); 149 sql.addValue(name); 150 sql.addValue(value); 151 152 if (i+1 < vectorSize) 153 sql.append(" OR "); 154 } 155 else 156 { 157 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)"); 158 sql.addValue(key); 159 sql.addValue(value); 160 161 if (i+1 < vectorSize) 162 sql.append(" OR "); 163 } 164 } 165 166 sql.append(") "); 167 } 168 } 169 } 170 } 171 172 178 public static Vector select(KeyedReference keyedRef,Vector keysIn,FindQualifiers qualifiers,Connection connection) 179 throws java.sql.SQLException 180 { 181 if ((keysIn != null) && (keysIn.size() == 0)) 186 return keysIn; 187 188 Vector keysOut = new Vector (); 189 PreparedStatement statement = null; 190 ResultSet resultSet = null; 191 192 DynamicQuery sql = new DynamicQuery(selectSQL); 194 appendWhere(sql,keyedRef,qualifiers); 195 appendIn(sql,keysIn); 196 appendOrderBy(sql,qualifiers); 197 198 try 199 { 200 log.debug(sql.toString()); 201 202 statement = sql.buildPreparedStatement(connection); 203 resultSet = statement.executeQuery(); 204 205 while (resultSet.next()) 206 keysOut.addElement(resultSet.getString(1)); 208 return keysOut; 209 } 210 finally 211 { 212 try { 213 resultSet.close(); 214 } 215 catch (Exception e) 216 { 217 log.warn("An Exception was encountered while attempting to close " + 218 "the Find TModel ResultSet: "+e.getMessage(),e); 219 } 220 221 try { 222 statement.close(); 223 } 224 catch (Exception e) 225 { 226 log.warn("An Exception was encountered while attempting to close " + 227 "the Find TModel Statement: "+e.getMessage(),e); 228 } 229 } 230 } 231 232 235 private static void appendWhere(DynamicQuery sql,KeyedReference keyedRef,FindQualifiers qualifiers) 236 { 237 sql.append("WHERE M.TMODEL_KEY = C.TMODEL_KEY "); 238 239 if (keyedRef != null) 240 { 241 sql.append("AND ("); 242 243 String key = keyedRef.getTModelKey(); 244 String name = keyedRef.getKeyName(); 245 String value = keyedRef.getKeyValue(); 246 247 if (name == null) 248 name = ""; 249 250 if (value == null) 251 value = ""; 252 253 if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY)) 259 { 260 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)"); 261 sql.addValue(key); 262 sql.addValue(name); 263 sql.addValue(value); 264 } 265 else 266 { 267 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)"); 268 sql.addValue(key); 269 sql.addValue(value); 270 } 271 272 sql.append(") "); 273 } 274 } 275 276 285 private static void appendIn(DynamicQuery sql,Vector keysIn) 286 { 287 if (keysIn == null) 288 return; 289 290 sql.append("AND M.TMODEL_KEY IN ("); 291 292 int keyCount = keysIn.size(); 293 for (int i=0; i<keyCount; i++) 294 { 295 String key = (String )keysIn.elementAt(i); 296 sql.append("?"); 297 sql.addValue(key); 298 299 if ((i+1) < keyCount) 300 sql.append(","); 301 } 302 303 sql.append(") "); 304 } 305 306 309 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 310 { 311 sql.append("ORDER BY "); 312 313 if (qualifiers == null) 314 sql.append("M.LAST_UPDATE DESC"); 315 else if (qualifiers.sortByDateAsc) 316 sql.append("M.LAST_UPDATE ASC"); 317 else 318 sql.append("M.LAST_UPDATE DESC"); 319 } 320 } 321 | Popular Tags |