KickJava   Java API By Example, From Geeks To Geeks.

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


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.TModelBag;
26 import org.apache.juddi.datatype.request.FindQualifiers;
27 import org.apache.juddi.util.jdbc.DynamicQuery;
28
29 /**
30  * @author Steve Viens (sviens@apache.org)
31  */

32 class FindBindingByTModelKeyQuery
33 {
34   // private reference to the jUDDI logger
35
private static Log log = LogFactory.getLog(FindBindingByTModelKeyQuery.class);
36
37   static String JavaDoc selectSQL;
38   static
39   {
40     // build selectSQL
41
StringBuffer JavaDoc sql = new StringBuffer JavaDoc(200);
42     sql.append("SELECT T.BINDING_KEY,T.LAST_UPDATE ");
43     sql.append("FROM BINDING_TEMPLATE T,TMODEL_INSTANCE_INFO I ");
44     selectSQL = sql.toString();
45   }
46
47   /**
48    * Select ...
49    *
50    * @param connection JDBC connection
51    * @throws java.sql.SQLException
52    */

53   public static Vector JavaDoc select(String JavaDoc serviceKey,TModelBag tModelBag,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
54     throws java.sql.SQLException JavaDoc
55   {
56     // if there is a keysIn vector but it doesn't contain
57
// any keys then the previous query has exhausted
58
// all possibilities of a match so skip this call.
59
if ((keysIn != null) && (keysIn.size() == 0))
60       return keysIn;
61
62     Vector JavaDoc keysOut = new Vector JavaDoc();
63     PreparedStatement JavaDoc statement = null;
64     ResultSet JavaDoc resultSet = null;
65
66     // construct the SQL statement
67
DynamicQuery sql = new DynamicQuery(selectSQL);
68     appendWhere(sql,serviceKey,tModelBag,qualifiers);
69     appendIn(sql,keysIn);
70     appendOrderBy(sql,qualifiers);
71
72     try
73     {
74       log.debug(sql.toString());
75       
76       statement = sql.buildPreparedStatement(connection);
77       resultSet = statement.executeQuery();
78
79       while (resultSet.next())
80         keysOut.addElement(resultSet.getString(1));//("BINDING_KEY"));
81

82       return keysOut;
83     }
84     finally
85     {
86       try {
87         resultSet.close();
88       }
89       catch (Exception JavaDoc e)
90       {
91         log.warn("An Exception was encountered while attempting to close " +
92           "the Find BindingTemplate ResultSet: "+e.getMessage(),e);
93       }
94
95       try {
96         statement.close();
97       }
98       catch (Exception JavaDoc e)
99       {
100         log.warn("An Exception was encountered while attempting to close " +
101           "the Find BindingTemplate Statement: "+e.getMessage(),e);
102       }
103     }
104   }
105
106   /**
107    *
108    */

109   private static void appendWhere(DynamicQuery sql,String JavaDoc serviceKey,TModelBag tModelBag,FindQualifiers qualifiers)
110   {
111     sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY ");
112     sql.append("AND T.SERVICE_KEY = ? ");
113     sql.addValue(serviceKey);
114
115     Vector JavaDoc keyVector = tModelBag.getTModelKeyVector();
116
117     int vectorSize = keyVector.size();
118     if (vectorSize > 0)
119     {
120       sql.append("AND (");
121
122       for (int i=0; i<vectorSize; i++)
123       {
124         String JavaDoc key = (String JavaDoc)keyVector.elementAt(i);
125
126         sql.append("I.TMODEL_KEY = ? ");
127         sql.addValue(key);
128
129         if (i+1 < vectorSize)
130           sql.append(" OR ");
131       }
132
133       sql.append(") ");
134     }
135   }
136
137   /**
138    * Select ...
139    *
140    * @param connection JDBC connection
141    * @throws java.sql.SQLException
142    */

143   public static Vector JavaDoc select(String JavaDoc serviceKey,String JavaDoc tModelKey,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
144     throws java.sql.SQLException JavaDoc
145   {
146     // if there is a keysIn vector but it doesn't contain
147
// any keys then the previous query has exhausted
148
// all possibilities of a match so skip this call.
149
if ((keysIn != null) && (keysIn.size() == 0))
150       return keysIn;
151
152     Vector JavaDoc keysOut = new Vector JavaDoc();
153     PreparedStatement JavaDoc statement = null;
154     ResultSet JavaDoc resultSet = null;
155
156     // construct the SQL statement
157
DynamicQuery sql = new DynamicQuery(selectSQL);
158     appendWhere(sql,serviceKey,tModelKey,qualifiers);
159     appendIn(sql,keysIn);
160     appendOrderBy(sql,qualifiers);
161
162     try
163     {
164       log.debug(sql.toString());
165
166       statement = sql.buildPreparedStatement(connection);
167       resultSet = statement.executeQuery();
168
169       while (resultSet.next())
170         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
171

172       return keysOut;
173     }
174     finally
175     {
176       try {
177         resultSet.close();
178       }
179       catch (Exception JavaDoc e)
180       {
181         log.warn("An Exception was encountered while attempting to close " +
182           "the Find BindingTemplate ResultSet: "+e.getMessage(),e);
183       }
184
185       try {
186         statement.close();
187       }
188       catch (Exception JavaDoc e)
189       {
190         log.warn("An Exception was encountered while attempting to close " +
191           "the Find BindingTemplate Statement: "+e.getMessage(),e);
192       }
193     }
194   }
195
196   /**
197    *
198    */

199   private static void appendWhere(DynamicQuery sql,String JavaDoc serviceKey,String JavaDoc tModelKey,FindQualifiers qualifiers)
200   {
201     sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY ");
202
203     if ((serviceKey != null) && (serviceKey.trim().length() > 0))
204     {
205       sql.append("AND T.SERVICE_KEY = ? ");
206       sql.addValue(serviceKey);
207     }
208     
209     if ((tModelKey != null) && (tModelKey.trim().length() > 0))
210     {
211       sql.append("AND I.TMODEL_KEY = ? ");
212       sql.addValue(tModelKey);
213     }
214   }
215
216   /**
217    * Utility method used to construct SQL "IN" statements such as
218    * the following SQL example:
219    *
220    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
221    *
222    * @param sql StringBuffer to append the final results to
223    * @param keysIn Vector of Strings used to construct the "IN" clause
224    */

225   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
226   {
227     if (keysIn == null)
228       return;
229
230     sql.append("AND T.BINDING_KEY IN (");
231
232     int keyCount = keysIn.size();
233     for (int i=0; i<keyCount; i++)
234     {
235       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
236       sql.append("?");
237       sql.addValue(key);
238       
239       if ((i+1) < keyCount)
240         sql.append(",");
241     }
242
243     sql.append(") ");
244   }
245
246   /**
247    *
248    */

249   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
250   {
251     sql.append("ORDER BY ");
252
253     if (qualifiers == null)
254       sql.append("T.LAST_UPDATE DESC");
255     else if (qualifiers.sortByDateAsc)
256       sql.append("T.LAST_UPDATE ASC");
257     else
258       sql.append("T.LAST_UPDATE DESC");
259   }
260 }
261
Popular Tags