KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > jdbc > FindPublisherByNameQuery


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.datastore.jdbc;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.util.Vector JavaDoc;
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 /**
29  * @author Steve Viens (sviens@apache.org)
30  */

31 class FindPublisherByNameQuery
32 {
33   // private reference to the jUDDI logger
34
private static Log log = LogFactory.getLog(FindPublisherByNameQuery.class);
35
36   static String JavaDoc selectSQL;
37   static
38   {
39     // build selectSQL
40
StringBuffer JavaDoc sql = new StringBuffer JavaDoc(200);
41     sql.append("SELECT P.PUBLISHER_ID,P.PUBLISHER_NAME ");
42     sql.append("FROM PUBLISHER P ");
43     selectSQL = sql.toString();
44   }
45
46   /**
47    * Select ...
48    *
49    * @param name primary key value
50    * @param keysIn primary key value
51    * @param qualifiers primary key value
52    * @param connection JDBC connection
53    * @throws java.sql.SQLException
54    */

55   public static Vector JavaDoc select(String JavaDoc name,Vector JavaDoc idsIn,FindQualifiers qualifiers,Connection JavaDoc connection)
56     throws java.sql.SQLException JavaDoc
57   {
58     // if there is a idsIn vector but it doesn't contain
59
// any publisher IDs then the previous query has exhausted
60
// all possibilities of a match so skip this call.
61
if ((idsIn != null) && (idsIn.size() == 0))
62       return idsIn;
63
64     Vector JavaDoc idsOut = new Vector JavaDoc();
65     PreparedStatement JavaDoc statement = null;
66     ResultSet JavaDoc resultSet = null;
67
68     // construct the SQL statement
69
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));//("PUBLISHER_ID"));
83

84       return idsOut;
85     }
86     finally
87     {
88       try {
89         resultSet.close();
90       }
91       catch (Exception JavaDoc 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 JavaDoc 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   /**
109    *
110    */

111   private static void appendWhere(DynamicQuery sql,String JavaDoc 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   /**
129    * Utility method used to construct SQL "IN" statements such as
130    * the following SQL example:
131    *
132    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
133    *
134    * @param sql StringBuffer to append the final results to
135    * @param keysIn Vector of Strings used to construct the "IN" clause
136    */

137   private static void appendIn(DynamicQuery sql,String JavaDoc name,Vector JavaDoc 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 JavaDoc key = (String JavaDoc)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   /**
162    * if the qualifier parameter is null or if the
163    * sortByNameDesc qualifier is true or if neither of
164    * the sortByNameXxxx qualifiers are true then sort
165    * the result by name in decending order.
166    */

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