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 FindPublisherByNameQuery 32 { 33 private static Log log = LogFactory.getLog(FindPublisherByNameQuery.class); 35 36 static String selectSQL; 37 static 38 { 39 StringBuffer sql = new StringBuffer (200); 41 sql.append("SELECT P.PUBLISHER_ID,P.PUBLISHER_NAME "); 42 sql.append("FROM PUBLISHER P "); 43 selectSQL = sql.toString(); 44 } 45 46 55 public static Vector select(String name,Vector idsIn,FindQualifiers qualifiers,Connection connection) 56 throws java.sql.SQLException 57 { 58 if ((idsIn != null) && (idsIn.size() == 0)) 62 return idsIn; 63 64 Vector idsOut = 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,idsIn); 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 idsOut.addElement(resultSet.getString(1)); 84 return idsOut; 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 Publisher 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 Publisher 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 if ((qualifiers != null) && (qualifiers.exactNameMatch)) 117 { 118 sql.append("WHERE P.PUBLISHER_NAME = ? "); 119 sql.addValue(name); 120 } 121 else 122 { 123 sql.append("WHERE P.PUBLISHER_NAME LIKE ? "); 124 sql.addValue(name+"%"); 125 } 126 } 127 128 137 private static void appendIn(DynamicQuery sql,String name,Vector keysIn) 138 { 139 if (keysIn == null) 140 return; 141 142 if ((name == null) || (name.length() == 0)) 143 sql.append("WHERE P.PUBLISHER_ID IN ("); 144 else 145 sql.append("AND P.PUBLISHER_ID IN ("); 146 147 int keyCount = keysIn.size(); 148 for (int i=0; i<keyCount; i++) 149 { 150 String key = (String )keysIn.elementAt(i); 151 sql.append("?"); 152 sql.addValue(key); 153 154 if ((i+1) < keyCount) 155 sql.append(","); 156 } 157 158 sql.append(") "); 159 } 160 161 167 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 168 { 169 sql.append("ORDER BY "); 170 171 if ((qualifiers == null) || 172 (qualifiers.sortByNameDesc) || 173 ((!qualifiers.sortByNameAsc) && (!qualifiers.sortByNameDesc))) 174 { 175 sql.append("P.PUBLISHER_NAME DESC"); 176 } 177 else if (qualifiers.sortByNameAsc) 178 { 179 sql.append("P.PUBLISHER_NAME ASC"); 180 } 181 } 182 } | Popular Tags |