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 FindBusinessByNameQuery 33 { 34 private static Log log = LogFactory.getLog(FindBusinessByNameQuery.class); 36 37 static String selectSQL; 38 static 39 { 40 StringBuffer sql = new StringBuffer (200); 42 sql.append("SELECT B.BUSINESS_KEY,B.LAST_UPDATE,N.NAME "); 43 sql.append("FROM BUSINESS_ENTITY B,BUSINESS_NAME N "); 44 selectSQL = sql.toString(); 45 } 46 47 56 public static Vector select(Vector names,Vector keysIn,FindQualifiers qualifiers,Connection connection) 57 throws java.sql.SQLException 58 { 59 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,names,qualifiers); 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 Find BusinessEntity 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 Find BusinessEntity Statement: "+e.getMessage(),e); 105 } 106 } 107 } 108 109 112 private static void appendWhere(DynamicQuery sql,Vector names,FindQualifiers qualifiers) 113 { 114 sql.append("WHERE B.BUSINESS_KEY = N.BUSINESS_KEY "); 115 116 if (names != null) 117 { 118 int nameSize = names.size(); 119 if (nameSize > 0) 120 { 121 sql.append("AND ("); 122 123 for (int i=0; i<nameSize; i++) 124 { 125 Name name = (Name)names.elementAt(i); 126 String text = name.getValue(); 127 String lang = name.getLanguageCode(); 128 129 if ((text != null) && (text.length() > 0)) 130 { 131 if (qualifiers == null) { 133 sql.append("(UPPER(NAME) LIKE ?"); 134 sql.addValue(text.endsWith("%") ? text.toUpperCase() : text.toUpperCase()+"%"); 135 } 136 else if ((qualifiers.caseSensitiveMatch) && (qualifiers.exactNameMatch)) 137 { 138 sql.append("(NAME = ?"); 139 sql.addValue(text); 140 } 141 else if ((!qualifiers.caseSensitiveMatch) && (qualifiers.exactNameMatch)) 142 { 143 sql.append("(UPPER(NAME) = ?"); 144 sql.addValue(text.toUpperCase()); 145 } 146 else if ((qualifiers.caseSensitiveMatch) && (!qualifiers.exactNameMatch)) 147 { 148 sql.append("(NAME LIKE ?"); 149 sql.addValue(text.endsWith("%") ? text : text+"%"); 150 } 151 else if ((!qualifiers.caseSensitiveMatch) && (!qualifiers.exactNameMatch)) 152 { 153 sql.append("(UPPER(NAME) LIKE ?"); 154 sql.addValue(text.endsWith("%") ? text.toUpperCase() : text.toUpperCase()+"%"); 155 } 156 157 if ((lang != null) && (lang.length() > 0)) 159 { 160 sql.append(" AND (UPPER(LANG_CODE) LIKE ?)"); 161 sql.addValue(lang.toUpperCase()+"%"); 162 } 163 164 sql.append(")"); 165 166 if (i+1 < nameSize) 167 sql.append(" OR "); 168 } 169 } 170 } 171 172 sql.append(") "); 173 } 174 } 175 176 185 private static void appendIn(DynamicQuery sql,Vector keysIn) 186 { 187 if (keysIn == null) 188 return; 189 190 sql.append("AND B.BUSINESS_KEY IN ("); 191 192 int keyCount = keysIn.size(); 193 for (int i=0; i<keyCount; i++) 194 { 195 String key = (String )keysIn.elementAt(i); 196 sql.append("?"); 197 sql.addValue(key); 198 199 if ((i+1) < keyCount) 200 sql.append(","); 201 } 202 203 sql.append(") "); 204 } 205 206 209 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 210 { 211 sql.append("ORDER BY "); 212 213 if ((qualifiers == null) || 214 ((!qualifiers.sortByNameAsc) && (!qualifiers.sortByNameDesc) && 215 (!qualifiers.sortByDateAsc) && (!qualifiers.sortByDateDesc))) 216 { 217 sql.append("N.NAME ASC,B.LAST_UPDATE DESC"); 218 } 219 else if (qualifiers.sortByNameAsc || qualifiers.sortByNameDesc) 220 { 221 if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc) 222 { 223 if (qualifiers.sortByNameAsc && qualifiers.sortByDateDesc) 224 sql.append("N.NAME ASC,B.LAST_UPDATE DESC"); 225 else if (qualifiers.sortByNameAsc && qualifiers.sortByDateAsc) 226 sql.append("N.NAME ASC,B.LAST_UPDATE ASC"); 227 else if (qualifiers.sortByNameDesc && qualifiers.sortByDateDesc) 228 sql.append("N.NAME DESC,B.LAST_UPDATE DESC"); 229 else 230 sql.append("N.NAME DESC,B.LAST_UPDATE ASC"); 231 } 232 else 233 { 234 if (qualifiers.sortByNameAsc) 235 sql.append("N.NAME ASC,B.LAST_UPDATE DESC"); 236 else 237 sql.append("N.NAME DESC,B.LAST_UPDATE DESC"); 238 } 239 } 240 else if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc) 241 { 242 if (qualifiers.sortByDateDesc) 243 sql.append("B.LAST_UPDATE ASC,N.NAME ASC"); 244 else 245 sql.append("B.LAST_UPDATE DESC,N.NAME ASC"); 246 } 247 } 248 } 249 | Popular Tags |