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 FindBusinessByCategoryQuery 35 { 36 private static Log log = LogFactory.getLog(FindBusinessByCategoryQuery.class); 38 39 static String selectSQL; 40 static 41 { 42 StringBuffer sql = new StringBuffer (200); 44 sql.append("SELECT B.BUSINESS_KEY,B.LAST_UPDATE "); 45 sql.append("FROM BUSINESS_ENTITY B,BUSINESS_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 BusinessEntity 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 BusinessEntity 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 B.BUSINESS_KEY = C.BUSINESS_KEY "); 115 116 if (categoryBag != null) 117 { 118 Vector keyedRefVector = categoryBag.getKeyedReferenceVector(); 119 if (keyedRefVector != null) 120 { 121 int vectorSize = keyedRefVector.size(); 122 if (vectorSize > 0) 123 { 124 sql.append("AND ("); 125 126 for (int i=0; i<vectorSize; i++) 127 { 128 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i); 129 String key = keyedRef.getTModelKey(); 130 String name = keyedRef.getKeyName(); 131 String value = keyedRef.getKeyValue(); 132 133 if (name == null) 134 name = ""; 135 136 if (value == null) 137 value = ""; 138 139 if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY)) 145 { 146 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)"); 147 sql.addValue(key); 148 sql.addValue(name); 149 sql.addValue(value); 150 151 if (i+1 < vectorSize) 152 sql.append(" OR "); 153 } 154 else 155 { 156 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)"); 157 sql.addValue(key); 158 sql.addValue(value); 159 160 if (i+1 < vectorSize) 161 sql.append(" OR "); 162 } 163 } 164 165 sql.append(") "); 166 } 167 } 168 } 169 } 170 171 177 public static Vector select(KeyedReference keyedRef,Vector keysIn,FindQualifiers qualifiers,Connection connection) 178 throws java.sql.SQLException 179 { 180 if ((keysIn != null) && (keysIn.size() == 0)) 185 return keysIn; 186 187 Vector keysOut = new Vector (); 188 PreparedStatement statement = null; 189 ResultSet resultSet = null; 190 191 DynamicQuery sql = new DynamicQuery(selectSQL); 193 appendWhere(sql,keyedRef,qualifiers); 194 appendIn(sql,keysIn); 195 appendOrderBy(sql,qualifiers); 196 197 try 198 { 199 log.debug(sql.toString()); 200 201 statement = sql.buildPreparedStatement(connection); 202 resultSet = statement.executeQuery(); 203 204 while (resultSet.next()) 205 keysOut.addElement(resultSet.getString(1)); 207 return keysOut; 208 } 209 finally 210 { 211 try { 212 resultSet.close(); 213 } 214 catch (Exception e) 215 { 216 log.warn("An Exception was encountered while attempting to close " + 217 "the Find BusinessEntity ResultSet: "+e.getMessage(),e); 218 } 219 220 try { 221 statement.close(); 222 } 223 catch (Exception e) 224 { 225 log.warn("An Exception was encountered while attempting to close " + 226 "the Find BusinessEntity Statement: "+e.getMessage(),e); 227 } 228 } 229 } 230 231 234 private static void appendWhere(DynamicQuery sql,KeyedReference keyedRef,FindQualifiers qualifiers) 235 { 236 sql.append("WHERE B.BUSINESS_KEY = C.BUSINESS_KEY "); 237 238 if (keyedRef != null) 239 { 240 sql.append("AND ("); 241 242 String key = keyedRef.getTModelKey(); 243 String name = keyedRef.getKeyName(); 244 String value = keyedRef.getKeyValue(); 245 246 if (name == null) 247 name = ""; 248 249 if (value == null) 250 value = ""; 251 252 if (key.equals(TModel.GENERAL_KEYWORDS_TMODEL_KEY)) 258 { 259 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_NAME = ? AND C.KEY_VALUE = ?)"); 260 sql.addValue(key); 261 sql.addValue(name); 262 sql.addValue(value); 263 } 264 else 265 { 266 sql.append("(C.TMODEL_KEY_REF = ? AND C.KEY_VALUE = ?)"); 267 sql.addValue(key); 268 sql.addValue(value); 269 } 270 271 sql.append(") "); 272 } 273 } 274 275 284 private static void appendIn(DynamicQuery sql,Vector keysIn) 285 { 286 if (keysIn == null) 287 return; 288 289 sql.append("AND B.BUSINESS_KEY IN ("); 290 291 int keyCount = keysIn.size(); 292 for (int i=0; i<keyCount; i++) 293 { 294 String key = (String )keysIn.elementAt(i); 295 sql.append("?"); 296 sql.addValue(key); 297 298 if ((i+1) < keyCount) 299 sql.append(","); 300 } 301 302 sql.append(") "); 303 } 304 305 308 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 309 { 310 sql.append("ORDER BY "); 311 312 if (qualifiers == null) 313 sql.append("B.LAST_UPDATE DESC"); 314 else if (qualifiers.sortByDateAsc) 315 sql.append("B.LAST_UPDATE ASC"); 316 else 317 sql.append("B.LAST_UPDATE DESC"); 318 } 319 } 320 | Popular Tags |