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.DiscoveryURL; 26 import org.apache.juddi.datatype.DiscoveryURLs; 27 import org.apache.juddi.datatype.request.FindQualifiers; 28 import org.apache.juddi.util.jdbc.DynamicQuery; 29 30 33 class FindBusinessByDiscoveryURLQuery 34 { 35 private static Log log = LogFactory.getLog(FindBusinessByDiscoveryURLQuery.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,DISCOVERY_URL U "); 45 selectSQL = sql.toString(); 46 } 47 48 57 public static Vector select(DiscoveryURLs discoveryURLs,Vector keysIn,FindQualifiers qualifiers,Connection connection) 58 throws java.sql.SQLException 59 { 60 if ((keysIn != null) && (keysIn.size() == 0)) 64 return keysIn; 65 66 Vector keysOut = new Vector (); 67 PreparedStatement statement = null; 68 ResultSet resultSet = null; 69 70 DynamicQuery sql = new DynamicQuery(selectSQL); 72 appendWhere(sql,discoveryURLs,qualifiers); 73 appendIn(sql,keysIn); 74 appendOrderBy(sql,qualifiers); 75 76 try 77 { 78 log.debug(sql.toString()); 79 80 statement = sql.buildPreparedStatement(connection); 81 resultSet = statement.executeQuery(); 82 83 while (resultSet.next()) 84 keysOut.addElement(resultSet.getString(1)); 86 return keysOut; 87 } 88 finally 89 { 90 try { 91 resultSet.close(); 92 } 93 catch (Exception e) 94 { 95 log.warn("An Exception was encountered while attempting to close " + 96 "the Find BusinessEntity ResultSet: "+e.getMessage(),e); 97 } 98 99 try { 100 statement.close(); 101 } 102 catch (Exception e) 103 { 104 log.warn("An Exception was encountered while attempting to close " + 105 "the Find BusinessEntity Statement: "+e.getMessage(),e); 106 } 107 } 108 } 109 110 113 private static void appendWhere(DynamicQuery sql,DiscoveryURLs discoveryURLs,FindQualifiers qualifiers) 114 { 115 sql.append("WHERE B.BUSINESS_KEY = U.BUSINESS_KEY "); 116 117 Vector urlVector = discoveryURLs.getDiscoveryURLVector(); 118 119 int vectorSize = urlVector.size(); 120 if (vectorSize > 0) 121 { 122 sql.append("AND ("); 123 124 for (int i=0; i<vectorSize; i++) 125 { 126 DiscoveryURL discoveryURL = (DiscoveryURL)urlVector.elementAt(i); 127 String url = discoveryURL.getValue(); 128 String useType = discoveryURL.getUseType(); 129 130 if ((url != null) && (url.length() > 0)) 131 { 132 sql.append("(U.URL = ?"); 133 sql.addValue(url); 134 135 if ((useType != null) && (useType.length() > 0)) 136 { 137 sql.append(" AND U.USE_TYPE = ?"); 138 sql.addValue(useType); 139 } 140 141 sql.append(")"); 142 143 if (i+1 < vectorSize) 144 sql.append(" OR "); 145 } 146 } 147 148 sql.append(") "); 149 } 150 } 151 152 161 private static void appendIn(DynamicQuery sql,Vector keysIn) 162 { 163 if (keysIn == null) 164 return; 165 166 sql.append("AND B.BUSINESS_KEY IN ("); 167 168 int keyCount = keysIn.size(); 169 for (int i=0; i<keyCount; i++) 170 { 171 String key = (String )keysIn.elementAt(i); 172 sql.append("?"); 173 sql.addValue(key); 174 175 if ((i+1) < keyCount) 176 sql.append(","); 177 } 178 179 sql.append(") "); 180 } 181 182 185 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 186 { 187 sql.append("ORDER BY "); 188 189 if (qualifiers == null) 190 sql.append("B.LAST_UPDATE DESC"); 191 else if (qualifiers.sortByDateAsc) 192 sql.append("B.LAST_UPDATE ASC"); 193 else 194 sql.append("B.LAST_UPDATE DESC"); 195 } 196 } 197 | Popular Tags |