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 FindTModelByNameQuery 32 { 33 private static Log log = LogFactory.getLog(FindTModelByNameQuery.class); 35 36 static String selectSQL; 37 static 38 { 39 StringBuffer sql = new StringBuffer (200); 41 sql.append("SELECT M.TMODEL_KEY,M.LAST_UPDATE,M.NAME,M.DELETED "); 42 sql.append("FROM TMODEL M "); 43 selectSQL = sql.toString(); 44 } 45 46 55 public static Vector select(String name,Vector keysIn,FindQualifiers qualifiers,Connection connection) 56 throws java.sql.SQLException 57 { 58 if ((keysIn != null) && (keysIn.size() == 0)) 62 return keysIn; 63 64 Vector keysOut = new Vector (); 65 PreparedStatement statement = null; 66 ResultSet resultSet = null; 67 68 DynamicQuery sql = new DynamicQuery(selectSQL); 70 appendWhere(sql,name,qualifiers); 71 appendIn(sql,name,keysIn); 72 appendOrderBy(sql,qualifiers); 73 74 try 75 { 76 log.debug(sql.toString()); 77 78 statement = sql.buildPreparedStatement(connection); 79 resultSet = statement.executeQuery(); 80 81 while (resultSet.next()) 82 keysOut.addElement(resultSet.getString(1)); 84 return keysOut; 85 } 86 finally 87 { 88 try { 89 resultSet.close(); 90 } 91 catch (Exception e) 92 { 93 log.warn("An Exception was encountered while attempting to close " + 94 "the Find TModel ResultSet: "+e.getMessage(),e); 95 } 96 97 try { 98 statement.close(); 99 } 100 catch (Exception e) 101 { 102 log.warn("An Exception was encountered while attempting to close " + 103 "the Find TModel Statement: "+e.getMessage(),e); 104 } 105 } 106 } 107 108 111 private static void appendWhere(DynamicQuery sql,String name,FindQualifiers qualifiers) 112 { 113 if ((name == null) || (name.length() == 0)) 114 return; 115 116 sql.append("WHERE M.DELETED IS NULL "); 120 121 if ((qualifiers != null) && (qualifiers.exactNameMatch)) 122 { 123 sql.append("AND M.NAME = ? "); 124 sql.addValue(name); 125 } 126 else 127 { 128 sql.append("AND M.NAME LIKE ? "); 129 sql.addValue(name.endsWith("%") ? name : name+"%"); 130 } 131 } 132 133 142 private static void appendIn(DynamicQuery sql,String name,Vector keysIn) 143 { 144 if (keysIn == null) 145 return; 146 147 if ((name == null) || (name.length() == 0)) 148 sql.append("WHERE M.TMODEL_KEY IN ("); 149 else 150 sql.append("AND M.TMODEL_KEY IN ("); 151 152 int keyCount = keysIn.size(); 153 for (int i=0; i<keyCount; i++) 154 { 155 String key = (String )keysIn.elementAt(i); 156 sql.append("?"); 157 sql.addValue(key); 158 159 if ((i+1) < keyCount) 160 sql.append(","); 161 } 162 163 sql.append(") "); 164 } 165 166 169 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 170 { 171 sql.append("ORDER BY "); 172 173 if ((qualifiers == null) || 174 ((!qualifiers.sortByNameAsc) && (!qualifiers.sortByNameDesc) && 175 (!qualifiers.sortByDateAsc) && (!qualifiers.sortByDateDesc))) 176 { 177 sql.append("M.NAME ASC,M.LAST_UPDATE DESC"); 178 } 179 else if (qualifiers.sortByNameAsc || qualifiers.sortByNameDesc) 180 { 181 if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc) 182 { 183 if (qualifiers.sortByNameAsc && qualifiers.sortByDateDesc) 184 sql.append("M.NAME ASC,M.LAST_UPDATE DESC"); 185 else if (qualifiers.sortByNameAsc && qualifiers.sortByDateAsc) 186 sql.append("M.NAME ASC,M.LAST_UPDATE ASC"); 187 else if (qualifiers.sortByNameDesc && qualifiers.sortByDateDesc) 188 sql.append("M.NAME DESC,M.LAST_UPDATE DESC"); 189 else 190 sql.append("M.NAME DESC,M.LAST_UPDATE ASC"); 191 } 192 else 193 { 194 if (qualifiers.sortByNameAsc) 195 sql.append("M.NAME ASC,M.LAST_UPDATE DESC"); 196 else 197 sql.append("M.NAME DESC,M.LAST_UPDATE DESC"); 198 } 199 } 200 else if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc) 201 { 202 if (qualifiers.sortByDateDesc) 203 sql.append("M.LAST_UPDATE ASC,M.NAME ASC"); 204 else 205 sql.append("M.LAST_UPDATE DESC,M.NAME ASC"); 206 } 207 } 208 } 209
| Popular Tags
|