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.request.FindQualifiers; 26 import org.apache.juddi.util.jdbc.DynamicQuery; 27 28 31 class FindServiceByBusinessKeyQuery 32 { 33 private static Log log = LogFactory.getLog(FindServiceByBusinessKeyQuery.class); 35 36 static String selectSQL; 37 static 38 { 39 StringBuffer sql = new StringBuffer (200); 41 sql.append("SELECT S.SERVICE_KEY,S.LAST_UPDATE "); 42 sql.append("FROM BUSINESS_SERVICE S "); 43 selectSQL = sql.toString(); 44 } 45 46 55 public static Vector select(String businessKey,Vector keysIn,FindQualifiers qualifiers,Connection connection) 56 throws java.sql.SQLException 57 { 58 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,businessKey); 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 FindBusinessService 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 FindBusinessService Statement: "+e.getMessage(),e); 105 } 106 } 107 } 108 109 112 private static void appendWhere(DynamicQuery sql,String businessKey) 113 { 114 sql.append("WHERE S.BUSINESS_KEY = ? "); 115 sql.addValue(businessKey); 116 } 117 118 127 private static void appendIn(DynamicQuery sql,Vector keysIn) 128 { 129 if (keysIn == null) 130 return; 131 132 sql.append("AND S.SERVICE_KEY IN ("); 133 134 int keyCount = keysIn.size(); 135 for (int i=0; i<keyCount; i++) 136 { 137 String key = (String )keysIn.elementAt(i); 138 sql.append("?"); 139 sql.addValue(key); 140 141 if ((i+1) < keyCount) 142 sql.append(","); 143 } 144 145 sql.append(") "); 146 } 147 148 151 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 152 { 153 sql.append("ORDER BY "); 154 155 if (qualifiers == null) 156 sql.append("S.LAST_UPDATE DESC"); 157 else if (qualifiers.sortByDateAsc) 158 sql.append("S.LAST_UPDATE ASC"); 159 else 160 sql.append("S.LAST_UPDATE DESC"); 161 } 162 } 163 | Popular Tags |