KickJava   Java API By Example, From Geeks To Geeks.

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


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 FindBindingByServiceKeyQuery
32 {
33   // private reference to the jUDDI logger
34
private static Log log = LogFactory.getLog(FindBindingByServiceKeyQuery.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 B.BINDING_KEY,B.LAST_UPDATE ");
42     sql.append("FROM BINDING_TEMPLATE B ");
43     selectSQL = sql.toString();
44   }
45
46   /**
47    * Select ...
48    *
49    * @param serviceKey
50    * @param keysIn
51    * @param qualifiers
52    * @param connection JDBC connection
53    * @throws java.sql.SQLException
54    */

55   public static Vector JavaDoc select(String JavaDoc serviceKey,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
56     throws java.sql.SQLException JavaDoc
57   {
58     // If there is a keysIn vector but it doesn't contain
59
// any keys then the previous query has exhausted
60
// all possibilities of a match so skip this call.
61
//
62
if ((keysIn != null) && (keysIn.size() == 0))
63       return keysIn;
64
65     Vector JavaDoc keysOut = new Vector JavaDoc();
66     PreparedStatement JavaDoc statement = null;
67     ResultSet JavaDoc resultSet = null;
68     DynamicQuery dynStmt = new DynamicQuery();
69     
70     DynamicQuery sql = new DynamicQuery(selectSQL);
71     appendWhere(sql,serviceKey,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       while (resultSet.next())
82       {
83         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
84
}
85
86       return keysOut;
87     }
88     finally
89     {
90       try {
91         resultSet.close();
92       }
93       catch (Exception JavaDoc e)
94       {
95         log.warn("An Exception was encountered while attempting to close " +
96           "the Find BindingTemplate ResultSet: "+e.getMessage(),e);
97       }
98
99       try {
100         statement.close();
101       }
102       catch (Exception JavaDoc e)
103       {
104         log.warn("An Exception was encountered while attempting to close " +
105           "the Find BindingTemplate Statement: "+e.getMessage(),e);
106       }
107     }
108   }
109
110   /**
111    *
112    */

113   private static void appendWhere(DynamicQuery sql,String JavaDoc serviceKey,FindQualifiers qualifiers)
114   {
115     sql.append("WHERE B.SERVICE_KEY = ? ");
116     sql.addValue(serviceKey);
117   }
118
119   /**
120    * Utility method used to construct SQL "IN" statements such as
121    * the following SQL example:
122    *
123    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
124    *
125    * @param sql StringBuffer to append the final results to
126    * @param keysIn Vector of Strings used to construct the "IN" clause
127    */

128   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
129   {
130     if (keysIn == null)
131       return;
132
133     sql.append("AND B.BINDING_KEY IN (");
134
135     int keyCount = keysIn.size();
136     for (int i=0; i<keyCount; i++)
137     {
138       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
139       
140       sql.append("?");
141       sql.addValue(key);
142
143       if ((i+1) < keyCount)
144         sql.append(",");
145     }
146
147     sql.append(") ");
148   }
149
150   /**
151    *
152    */

153   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
154   {
155     sql.append("ORDER BY ");
156
157     if (qualifiers == null)
158       sql.append("B.LAST_UPDATE DESC");
159     else if (qualifiers.sortByDateAsc)
160       sql.append("B.LAST_UPDATE ASC");
161     else
162       sql.append("B.LAST_UPDATE DESC");
163   }
164 }
165
Popular Tags