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.TModelBag; 26 import org.apache.juddi.datatype.request.FindQualifiers; 27 import org.apache.juddi.util.jdbc.DynamicQuery; 28 29 32 class FindBusinessByTModelKeyQuery 33 { 34 private static Log log = LogFactory.getLog(FindBusinessByTModelKeyQuery.class); 36 37 static String selectSQL; 38 static 39 { 40 StringBuffer sql = new StringBuffer (200); 42 sql.append("SELECT B.BUSINESS_KEY,B.LAST_UPDATE "); 43 sql.append("FROM BUSINESS_ENTITY B,BUSINESS_SERVICE S,BINDING_TEMPLATE T,TMODEL_INSTANCE_INFO I "); 44 selectSQL = sql.toString(); 45 } 46 47 56 public static Vector select(TModelBag tModelBag,Vector keysIn,FindQualifiers qualifiers,Connection connection) 57 throws java.sql.SQLException 58 { 59 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,tModelBag,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,TModelBag tModelBag,FindQualifiers qualifiers) 113 { 114 sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY "); 115 sql.append("AND T.SERVICE_KEY = S.SERVICE_KEY "); 116 sql.append("AND S.BUSINESS_KEY = B.BUSINESS_KEY "); 117 118 Vector keyVector = tModelBag.getTModelKeyVector(); 119 120 int vectorSize = keyVector.size(); 121 if (vectorSize > 0) 122 { 123 sql.append("AND ("); 124 125 for (int i=0; i<vectorSize; i++) 126 { 127 String key = (String ) keyVector.elementAt(i); 128 129 sql.append("I.TMODEL_KEY = ? "); 130 sql.addValue(key); 131 132 if (i+1 < vectorSize) 133 sql.append(" OR "); 134 } 135 136 sql.append(") "); 137 } 138 } 139 140 149 public static Vector select(String tModelKey,Vector keysIn,FindQualifiers qualifiers,Connection connection) 150 throws java.sql.SQLException 151 { 152 if ((keysIn != null) && (keysIn.size() == 0)) 156 return keysIn; 157 158 Vector keysOut = new Vector (); 159 PreparedStatement statement = null; 160 ResultSet resultSet = null; 161 162 DynamicQuery sql = new DynamicQuery(selectSQL); 164 appendWhere(sql,tModelKey,qualifiers); 165 appendIn(sql,keysIn); 166 appendOrderBy(sql,qualifiers); 167 168 try 169 { 170 log.debug(sql.toString()); 171 172 statement = sql.buildPreparedStatement(connection); 173 resultSet = statement.executeQuery(); 174 175 while (resultSet.next()) 176 keysOut.addElement(resultSet.getString(1)); 178 return keysOut; 179 } 180 finally 181 { 182 try { 183 resultSet.close(); 184 } 185 catch (Exception e) 186 { 187 log.warn("An Exception was encountered while attempting to close " + 188 "the Find BusinessEntity ResultSet: "+e.getMessage(),e); 189 } 190 191 try { 192 statement.close(); 193 } 194 catch (Exception e) 195 { 196 log.warn("An Exception was encountered while attempting to close " + 197 "the Find BusinessEntity Statement: "+e.getMessage(),e); 198 } 199 } 200 } 201 202 205 private static void appendWhere(DynamicQuery sql,String tModelKey,FindQualifiers qualifiers) 206 { 207 sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY "); 208 sql.append("AND T.SERVICE_KEY = S.SERVICE_KEY "); 209 sql.append("AND S.BUSINESS_KEY = B.BUSINESS_KEY "); 210 211 if ((tModelKey != null) && (tModelKey.trim().length() > 0)) 212 { 213 sql.append("AND I.TMODEL_KEY = ? "); 214 sql.addValue(tModelKey); 215 } 216 } 217 218 227 private static void appendIn(DynamicQuery sql,Vector keysIn) 228 { 229 if (keysIn == null) 230 return; 231 232 sql.append("AND B.BUSINESS_KEY IN ("); 233 234 int keyCount = keysIn.size(); 235 for (int i=0; i<keyCount; i++) 236 { 237 String key = (String )keysIn.elementAt(i); 238 sql.append("?"); 239 sql.addValue(key); 240 241 if ((i+1) < keyCount) 242 sql.append(","); 243 } 244 245 sql.append(") "); 246 } 247 248 251 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 252 { 253 sql.append("ORDER BY "); 254 255 if (qualifiers == null) 256 sql.append("B.LAST_UPDATE DESC"); 257 else if (qualifiers.sortByDateAsc) 258 sql.append("B.LAST_UPDATE ASC"); 259 else 260 sql.append("B.LAST_UPDATE DESC"); 261 } 262 } 263 | Popular Tags |