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.Name; 26 import org.apache.juddi.datatype.request.FindQualifiers; 27 import org.apache.juddi.util.jdbc.DynamicQuery; 28 29 32 class FindServiceByNameQuery 33 { 34 private static Log log = LogFactory.getLog(FindServiceByNameQuery.class); 36 37 static String selectSQL; 38 static 39 { 40 StringBuffer sql = new StringBuffer (200); 42 sql.append("SELECT S.SERVICE_KEY,S.LAST_UPDATE,N.NAME "); 43 sql.append("FROM BUSINESS_SERVICE S,SERVICE_NAME N "); 44 selectSQL = sql.toString(); 45 } 46 47 57 public static Vector select(String businessKey,Vector names,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,businessKey,names,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 BusinessService 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 BusinessService Statement: "+e.getMessage(),e); 106 } 107 } 108 } 109 110 113 private static void appendWhere(DynamicQuery sql,String businessKey,Vector names,FindQualifiers qualifiers) 114 { 115 sql.append("WHERE N.SERVICE_KEY = S.SERVICE_KEY "); 116 117 if((businessKey != null) && (businessKey.length() > 0)) 120 { 121 sql.append("AND S.BUSINESS_KEY = ? "); 122 sql.addValue(businessKey); 123 } 124 125 if (names != null) 126 { 127 int nameSize = names.size(); 128 if (nameSize > 0) 129 { 130 sql.append("AND ("); 131 132 for (int i=0; i<nameSize; i++) 133 { 134 Name name = (Name)names.elementAt(i); 135 String text = name.getValue(); 136 String lang = name.getLanguageCode(); 137 138 if ((text != null) && (text.length() > 0)) 139 { 140 if (qualifiers == null) { 142 sql.append("(UPPER(NAME) LIKE ?"); 143 sql.addValue(text.endsWith("%") ? text.toUpperCase() : text.toUpperCase()+"%"); 144 } 145 else if ((qualifiers.caseSensitiveMatch) && (qualifiers.exactNameMatch)) 146 { 147 sql.append("(NAME = ?"); 148 sql.addValue(text); 149 } 150 else if ((!qualifiers.caseSensitiveMatch) && (qualifiers.exactNameMatch)) 151 { 152 sql.append("(UPPER(NAME) = ?"); 153 sql.addValue(text.toUpperCase()); 154 } 155 else if ((qualifiers.caseSensitiveMatch) && (!qualifiers.exactNameMatch)) 156 { 157 sql.append("(NAME LIKE ?"); 158 sql.addValue(text.endsWith("%") ? text : text+"%"); 159 } 160 else if ((!qualifiers.caseSensitiveMatch) && (!qualifiers.exactNameMatch)) 161 { 162 sql.append("(UPPER(NAME) LIKE ?"); 163 sql.addValue(text.endsWith("%") ? text.toUpperCase() : text.toUpperCase()+"%"); 164 } 165 166 if ((lang != null) && (lang.length() > 0)) 168 { 169 sql.append(" AND (UPPER(LANG_CODE) LIKE ?)"); 170 sql.addValue(lang.toUpperCase()+"%"); 171 } 172 173 sql.append(")"); 174 175 if (i+1 < nameSize) 176 sql.append(" OR "); 177 } 178 } 179 } 180 181 sql.append(") "); 182 } 183 } 184 185 194 private static void appendIn(DynamicQuery sql,Vector keysIn) 195 { 196 if (keysIn == null) 197 return; 198 199 sql.append("AND S.SERVICE_KEY IN ("); 200 201 int keyCount = keysIn.size(); 202 for (int i=0; i<keyCount; i++) 203 { 204 String key = (String )keysIn.elementAt(i); 205 sql.append("?"); 206 sql.addValue(key); 207 208 if ((i+1) < keyCount) 209 sql.append(","); 210 } 211 212 sql.append(") "); 213 } 214 215 218 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 219 { 220 sql.append("ORDER BY "); 221 222 if ((qualifiers == null) || 223 ((!qualifiers.sortByNameAsc) && (!qualifiers.sortByNameDesc) && 224 (!qualifiers.sortByDateAsc) && (!qualifiers.sortByDateDesc))) 225 { 226 sql.append("N.NAME ASC,S.LAST_UPDATE DESC"); 227 } 228 else if (qualifiers.sortByNameAsc || qualifiers.sortByNameDesc) 229 { 230 if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc) 231 { 232 if (qualifiers.sortByNameAsc && qualifiers.sortByDateDesc) 233 sql.append("N.NAME ASC,S.LAST_UPDATE DESC"); 234 else if (qualifiers.sortByNameAsc && qualifiers.sortByDateAsc) 235 sql.append("N.NAME ASC,S.LAST_UPDATE ASC"); 236 else if (qualifiers.sortByNameDesc && qualifiers.sortByDateDesc) 237 sql.append("N.NAME DESC,S.LAST_UPDATE DESC"); 238 else 239 sql.append("N.NAME DESC,S.LAST_UPDATE ASC"); 240 } 241 else 242 { 243 if (qualifiers.sortByNameAsc) 244 sql.append("N.NAME ASC,S.LAST_UPDATE DESC"); 245 else 246 sql.append("N.NAME DESC,S.LAST_UPDATE DESC"); 247 } 248 } 249 else if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc) 250 { 251 if (qualifiers.sortByDateDesc) 252 sql.append("S.LAST_UPDATE ASC,N.NAME ASC"); 253 else 254 sql.append("S.LAST_UPDATE DESC,N.NAME ASC"); 255 } 256 } 257 } 258 | Popular Tags |