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 42 class FindServiceByTModelKeyQuery 43 { 44 private static Log log = LogFactory.getLog(FindServiceByTModelKeyQuery.class); 46 47 static String selectSQL; 48 static 49 { 50 StringBuffer sql = new StringBuffer (200); 52 sql.append("SELECT S.SERVICE_KEY,S.LAST_UPDATE "); 53 sql.append("FROM BUSINESS_SERVICE S,BINDING_TEMPLATE T,TMODEL_INSTANCE_INFO I "); 54 selectSQL = sql.toString(); 55 } 56 57 63 public static Vector select(String businessKey,TModelBag tModelBag,Vector keysIn,FindQualifiers qualifiers,Connection connection) 64 throws java.sql.SQLException 65 { 66 if ((keysIn != null) && (keysIn.size() == 0)) 70 return keysIn; 71 72 Vector keysOut = new Vector (); 73 PreparedStatement statement = null; 74 ResultSet resultSet = null; 75 76 DynamicQuery sql = new DynamicQuery(selectSQL); 78 appendWhere(sql,businessKey,tModelBag,qualifiers); 79 appendIn(sql,keysIn); 80 appendOrderBy(sql,qualifiers); 81 82 try 83 { 84 log.debug(sql.toString()); 85 86 statement = sql.buildPreparedStatement(connection); 87 resultSet = statement.executeQuery(); 88 89 while (resultSet.next()) 90 keysOut.addElement(resultSet.getString(1)); 92 return keysOut; 93 } 94 finally 95 { 96 try { 97 resultSet.close(); 98 } 99 catch (Exception e) 100 { 101 log.warn("An Exception was encountered while attempting to close " + 102 "the Find BusinessService ResultSet: "+e.getMessage(),e); 103 } 104 105 try { 106 statement.close(); 107 } 108 catch (Exception e) 109 { 110 log.warn("An Exception was encountered while attempting to close " + 111 "the Find BusinessService Statement: "+e.getMessage(),e); 112 } 113 } 114 } 115 116 119 private static void appendWhere(DynamicQuery sql,String businessKey,TModelBag tModelBag,FindQualifiers qualifiers) 120 { 121 sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY "); 122 sql.append("AND T.SERVICE_KEY = S.SERVICE_KEY "); 123 124 if (businessKey != null) 125 { 126 sql.append("AND S.BUSINESS_KEY = ? "); 127 sql.addValue(businessKey); 128 } 129 130 Vector keyVector = tModelBag.getTModelKeyVector(); 131 132 int vectorSize = keyVector.size(); 133 if (vectorSize > 0) 134 { 135 sql.append("AND ("); 136 137 for (int i=0; i<vectorSize; i++) 138 { 139 String key = (String )keyVector.elementAt(i); 140 141 sql.append("I.TMODEL_KEY = ? "); 142 sql.addValue(key); 143 144 if (i+1 < vectorSize) 145 sql.append(" OR "); 146 } 147 148 sql.append(") "); 149 } 150 } 151 152 158 public static Vector select(String businessKey,String tModelKey,Vector keysIn,FindQualifiers qualifiers,Connection connection) 159 throws java.sql.SQLException 160 { 161 if ((keysIn != null) && (keysIn.size() == 0)) 165 return keysIn; 166 167 Vector keysOut = new Vector (); 168 PreparedStatement statement = null; 169 ResultSet resultSet = null; 170 171 DynamicQuery sql = new DynamicQuery(selectSQL); 173 appendWhere(sql,businessKey,tModelKey,qualifiers); 174 appendIn(sql,keysIn); 175 appendOrderBy(sql,qualifiers); 176 177 try 178 { 179 log.debug(sql.toString()); 180 181 statement = sql.buildPreparedStatement(connection); 182 resultSet = statement.executeQuery(); 183 184 while (resultSet.next()) 185 keysOut.addElement(resultSet.getString(1)); 187 return keysOut; 188 } 189 finally 190 { 191 try { 192 resultSet.close(); 193 } 194 catch (Exception e) 195 { 196 log.warn("An Exception was encountered while attempting to close " + 197 "the Find BusinessService ResultSet: "+e.getMessage(),e); 198 } 199 200 try { 201 statement.close(); 202 } 203 catch (Exception e) 204 { 205 log.warn("An Exception was encountered while attempting to close " + 206 "the Find BusinessService Statement: "+e.getMessage(),e); 207 } 208 } 209 } 210 211 214 private static void appendWhere(DynamicQuery sql,String businessKey,String tModelKey,FindQualifiers qualifiers) 215 { 216 sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY "); 217 sql.append("AND T.SERVICE_KEY = S.SERVICE_KEY "); 218 219 if ((businessKey != null) && (businessKey.trim().length() > 0)) 220 { 221 sql.append("AND S.BUSINESS_KEY = ? "); 222 sql.addValue(businessKey); 223 } 224 225 if ((tModelKey != null) && (tModelKey.trim().length() > 0)) 226 { 227 sql.append("AND I.TMODEL_KEY = ? "); 228 sql.addValue(tModelKey); 229 } 230 } 231 232 241 private static void appendIn(DynamicQuery sql,Vector keysIn) 242 { 243 if (keysIn == null) 244 return; 245 246 sql.append("AND S.SERVICE_KEY IN ("); 247 248 int keyCount = keysIn.size(); 249 for (int i=0; i<keyCount; i++) 250 { 251 String key = (String )keysIn.elementAt(i); 252 sql.append("?"); 253 sql.addValue(key); 254 255 if ((i+1) < keyCount) 256 sql.append(","); 257 } 258 259 sql.append(") "); 260 } 261 262 265 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 266 { 267 sql.append("ORDER BY "); 268 269 if (qualifiers == null) 270 sql.append("S.LAST_UPDATE DESC"); 271 else if (qualifiers.sortByDateAsc) 272 sql.append("S.LAST_UPDATE ASC"); 273 else 274 sql.append("S.LAST_UPDATE DESC"); 275 } 276 } 277 | Popular Tags |