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.IdentifierBag; 26 import org.apache.juddi.datatype.KeyedReference; 27 import org.apache.juddi.datatype.request.FindQualifiers; 28 import org.apache.juddi.util.jdbc.DynamicQuery; 29 30 33 class FindBusinessByIdentifierQuery 34 { 35 private static Log log = LogFactory.getLog(FindBusinessByIdentifierQuery.class); 37 38 static String selectSQL; 39 static 40 { 41 StringBuffer sql = new StringBuffer (200); 43 sql.append("SELECT B.BUSINESS_KEY,B.LAST_UPDATE "); 44 sql.append("FROM BUSINESS_ENTITY B,BUSINESS_IDENTIFIER I "); 45 selectSQL = sql.toString(); 46 } 47 48 54 public static Vector select(IdentifierBag identifierBag,Vector keysIn,FindQualifiers qualifiers,Connection connection) 55 throws java.sql.SQLException 56 { 57 if ((keysIn != null) && (keysIn.size() == 0)) 61 return keysIn; 62 63 Vector keysOut = new Vector (); 64 PreparedStatement statement = null; 65 ResultSet resultSet = null; 66 67 DynamicQuery sql = new DynamicQuery(selectSQL); 69 appendWhere(sql,identifierBag,qualifiers); 70 appendIn(sql,keysIn); 71 appendOrderBy(sql,qualifiers); 72 73 try 74 { 75 log.debug(sql.toString()); 76 77 statement = sql.buildPreparedStatement(connection); 78 resultSet = statement.executeQuery(); 79 80 while (resultSet.next()) 81 keysOut.addElement(resultSet.getString(1)); 83 return keysOut; 84 } 85 finally 86 { 87 try { 88 resultSet.close(); 89 } 90 catch (Exception e) 91 { 92 log.warn("An Exception was encountered while attempting to close " + 93 "the Find BusinessEntity ResultSet: "+e.getMessage(),e); 94 } 95 96 try { 97 statement.close(); 98 } 99 catch (Exception e) 100 { 101 log.warn("An Exception was encountered while attempting to close " + 102 "the Find BusinessEntity Statement: "+e.getMessage(),e); 103 } 104 } 105 } 106 107 110 private static void appendWhere(DynamicQuery sql,IdentifierBag identifierBag,FindQualifiers qualifiers) 111 { 112 sql.append("WHERE B.BUSINESS_KEY = I.BUSINESS_KEY "); 113 114 if(identifierBag != null) 115 { 116 Vector keyedRefVector = identifierBag.getKeyedReferenceVector(); 117 if(keyedRefVector != null) 118 { 119 int vectorSize = keyedRefVector.size(); 120 if (vectorSize > 0) 121 { 122 sql.append("AND ("); 123 124 for (int i=0; i<vectorSize; i++) 125 { 126 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i); 133 134 String key = keyedRef.getTModelKey(); 135 if (key == null) 136 key = ""; 137 138 String value = keyedRef.getKeyValue(); 139 if (value == null) 140 value = ""; 141 142 sql.append("(I.TMODEL_KEY_REF = ? AND I.KEY_VALUE = ?)"); 143 sql.addValue(key); 144 sql.addValue(value); 145 146 if (i+1 < vectorSize) 147 sql.append(" OR "); 148 } 149 150 sql.append(") "); 151 } 152 } 153 } 154 } 155 156 165 private static void appendIn(DynamicQuery sql,Vector keysIn) 166 { 167 if (keysIn == null) 168 return; 169 170 sql.append("AND B.BUSINESS_KEY IN ("); 171 172 int keyCount = keysIn.size(); 173 for (int i=0; i<keyCount; i++) 174 { 175 String key = (String )keysIn.elementAt(i); 176 sql.append("?"); 177 sql.addValue(key); 178 179 if ((i+1) < keyCount) 180 sql.append(","); 181 } 182 183 sql.append(") "); 184 } 185 186 189 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 190 { 191 sql.append("ORDER BY "); 192 193 if (qualifiers == null) 194 sql.append("B.LAST_UPDATE DESC"); 195 else if (qualifiers.sortByDateAsc) 196 sql.append("B.LAST_UPDATE ASC"); 197 else 198 sql.append("B.LAST_UPDATE DESC"); 199 } 200 } 201 | Popular Tags |