KickJava   Java API By Example, From Geeks To Geeks.

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


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 FindBusinessByTModelKeyQuery
33 {
34   // private reference to the jUDDI logger
35
private static Log log = LogFactory.getLog(FindBusinessByTModelKeyQuery.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 B.BUSINESS_KEY,B.LAST_UPDATE ");
43     sql.append("FROM BUSINESS_ENTITY B,BUSINESS_SERVICE S,BINDING_TEMPLATE T,TMODEL_INSTANCE_INFO I ");
44     selectSQL = sql.toString();
45   }
46
47   /**
48    * Select ...
49    *
50    * @param tModelBag
51    * @param keysIn
52    * @param qualifiers
53    * @param connection JDBC connection
54    * @throws java.sql.SQLException
55    */

56   public static Vector JavaDoc select(TModelBag tModelBag,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
57     throws java.sql.SQLException JavaDoc
58   {
59     // if there is a keysIn vector but it doesn't contain
60
// any keys then the previous query has exhausted
61
// all possibilities of a match so skip this call.
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
69     // construct the SQL statement
70
DynamicQuery sql = new DynamicQuery(selectSQL);
71     appendWhere(sql,tModelBag,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
82       while (resultSet.next())
83         keysOut.addElement(resultSet.getString(1));//("BUSINESS_KEY"));
84

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

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

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

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

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

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

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