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 FindBindingByTModelKeyQuery 33 { 34 private static Log log = LogFactory.getLog(FindBindingByTModelKeyQuery.class); 36 37 static String selectSQL; 38 static 39 { 40 StringBuffer sql = new StringBuffer (200); 42 sql.append("SELECT T.BINDING_KEY,T.LAST_UPDATE "); 43 sql.append("FROM BINDING_TEMPLATE T,TMODEL_INSTANCE_INFO I "); 44 selectSQL = sql.toString(); 45 } 46 47 53 public static Vector select(String serviceKey,TModelBag tModelBag,Vector keysIn,FindQualifiers qualifiers,Connection connection) 54 throws java.sql.SQLException 55 { 56 if ((keysIn != null) && (keysIn.size() == 0)) 60 return keysIn; 61 62 Vector keysOut = new Vector (); 63 PreparedStatement statement = null; 64 ResultSet resultSet = null; 65 66 DynamicQuery sql = new DynamicQuery(selectSQL); 68 appendWhere(sql,serviceKey,tModelBag,qualifiers); 69 appendIn(sql,keysIn); 70 appendOrderBy(sql,qualifiers); 71 72 try 73 { 74 log.debug(sql.toString()); 75 76 statement = sql.buildPreparedStatement(connection); 77 resultSet = statement.executeQuery(); 78 79 while (resultSet.next()) 80 keysOut.addElement(resultSet.getString(1)); 82 return keysOut; 83 } 84 finally 85 { 86 try { 87 resultSet.close(); 88 } 89 catch (Exception e) 90 { 91 log.warn("An Exception was encountered while attempting to close " + 92 "the Find BindingTemplate ResultSet: "+e.getMessage(),e); 93 } 94 95 try { 96 statement.close(); 97 } 98 catch (Exception e) 99 { 100 log.warn("An Exception was encountered while attempting to close " + 101 "the Find BindingTemplate Statement: "+e.getMessage(),e); 102 } 103 } 104 } 105 106 109 private static void appendWhere(DynamicQuery sql,String serviceKey,TModelBag tModelBag,FindQualifiers qualifiers) 110 { 111 sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY "); 112 sql.append("AND T.SERVICE_KEY = ? "); 113 sql.addValue(serviceKey); 114 115 Vector keyVector = tModelBag.getTModelKeyVector(); 116 117 int vectorSize = keyVector.size(); 118 if (vectorSize > 0) 119 { 120 sql.append("AND ("); 121 122 for (int i=0; i<vectorSize; i++) 123 { 124 String key = (String )keyVector.elementAt(i); 125 126 sql.append("I.TMODEL_KEY = ? "); 127 sql.addValue(key); 128 129 if (i+1 < vectorSize) 130 sql.append(" OR "); 131 } 132 133 sql.append(") "); 134 } 135 } 136 137 143 public static Vector select(String serviceKey,String tModelKey,Vector keysIn,FindQualifiers qualifiers,Connection connection) 144 throws java.sql.SQLException 145 { 146 if ((keysIn != null) && (keysIn.size() == 0)) 150 return keysIn; 151 152 Vector keysOut = new Vector (); 153 PreparedStatement statement = null; 154 ResultSet resultSet = null; 155 156 DynamicQuery sql = new DynamicQuery(selectSQL); 158 appendWhere(sql,serviceKey,tModelKey,qualifiers); 159 appendIn(sql,keysIn); 160 appendOrderBy(sql,qualifiers); 161 162 try 163 { 164 log.debug(sql.toString()); 165 166 statement = sql.buildPreparedStatement(connection); 167 resultSet = statement.executeQuery(); 168 169 while (resultSet.next()) 170 keysOut.addElement(resultSet.getString(1)); 172 return keysOut; 173 } 174 finally 175 { 176 try { 177 resultSet.close(); 178 } 179 catch (Exception e) 180 { 181 log.warn("An Exception was encountered while attempting to close " + 182 "the Find BindingTemplate ResultSet: "+e.getMessage(),e); 183 } 184 185 try { 186 statement.close(); 187 } 188 catch (Exception e) 189 { 190 log.warn("An Exception was encountered while attempting to close " + 191 "the Find BindingTemplate Statement: "+e.getMessage(),e); 192 } 193 } 194 } 195 196 199 private static void appendWhere(DynamicQuery sql,String serviceKey,String tModelKey,FindQualifiers qualifiers) 200 { 201 sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY "); 202 203 if ((serviceKey != null) && (serviceKey.trim().length() > 0)) 204 { 205 sql.append("AND T.SERVICE_KEY = ? "); 206 sql.addValue(serviceKey); 207 } 208 209 if ((tModelKey != null) && (tModelKey.trim().length() > 0)) 210 { 211 sql.append("AND I.TMODEL_KEY = ? "); 212 sql.addValue(tModelKey); 213 } 214 } 215 216 225 private static void appendIn(DynamicQuery sql,Vector keysIn) 226 { 227 if (keysIn == null) 228 return; 229 230 sql.append("AND T.BINDING_KEY IN ("); 231 232 int keyCount = keysIn.size(); 233 for (int i=0; i<keyCount; i++) 234 { 235 String key = (String )keysIn.elementAt(i); 236 sql.append("?"); 237 sql.addValue(key); 238 239 if ((i+1) < keyCount) 240 sql.append(","); 241 } 242 243 sql.append(") "); 244 } 245 246 249 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 250 { 251 sql.append("ORDER BY "); 252 253 if (qualifiers == null) 254 sql.append("T.LAST_UPDATE DESC"); 255 else if (qualifiers.sortByDateAsc) 256 sql.append("T.LAST_UPDATE ASC"); 257 else 258 sql.append("T.LAST_UPDATE DESC"); 259 } 260 } 261 | Popular Tags |