KickJava   Java API By Example, From Geeks To Geeks.

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


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.Name;
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 FindServiceByNameQuery
33 {
34   // private reference to the jUDDI logger
35
private static Log log = LogFactory.getLog(FindServiceByNameQuery.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 S.SERVICE_KEY,S.LAST_UPDATE,N.NAME ");
43     sql.append("FROM BUSINESS_SERVICE S,SERVICE_NAME N ");
44     selectSQL = sql.toString();
45   }
46
47   /**
48    * Select ...
49    *
50    * @param businessKey primary key value
51    * @param names
52    * @param keysIn
53    * @param qualifiers
54    * @param connection JDBC connection
55    * @throws java.sql.SQLException
56    */

57   public static Vector JavaDoc select(String JavaDoc businessKey,Vector JavaDoc names,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
58     throws java.sql.SQLException JavaDoc
59   {
60     // if there is a keysIn vector but it doesn't contain
61
// any keys then the previous query has exhausted
62
// all possibilities of a match so skip this call.
63
if ((keysIn != null) && (keysIn.size() == 0))
64       return keysIn;
65
66     Vector JavaDoc keysOut = new Vector JavaDoc();
67     PreparedStatement JavaDoc statement = null;
68     ResultSet JavaDoc resultSet = null;
69
70     // construct the SQL statement
71
DynamicQuery sql = new DynamicQuery(selectSQL);
72     appendWhere(sql,businessKey,names,qualifiers);
73     appendIn(sql,keysIn);
74     appendOrderBy(sql,qualifiers);
75
76     try
77     {
78       log.debug(sql.toString());
79
80       statement = sql.buildPreparedStatement(connection);
81       resultSet = statement.executeQuery();
82
83       while (resultSet.next())
84         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
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 BusinessService 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 BusinessService Statement: "+e.getMessage(),e);
106       }
107     }
108   }
109
110   /**
111    *
112    */

113   private static void appendWhere(DynamicQuery sql,String JavaDoc businessKey,Vector JavaDoc names,FindQualifiers qualifiers)
114   {
115     sql.append("WHERE N.SERVICE_KEY = S.SERVICE_KEY ");
116
117     // per UDDI v2.0 Programmers API Errata (pg. 14), businessKey is
118
// is no longer a required attribute of the find_service server.
119
if((businessKey != null) && (businessKey.length() > 0))
120     {
121       sql.append("AND S.BUSINESS_KEY = ? ");
122       sql.addValue(businessKey);
123     }
124
125     if (names != null)
126     {
127       int nameSize = names.size();
128       if (nameSize > 0)
129       {
130         sql.append("AND (");
131
132         for (int i=0; i<nameSize; i++)
133         {
134           Name name = (Name)names.elementAt(i);
135           String JavaDoc text = name.getValue();
136           String JavaDoc lang = name.getLanguageCode();
137
138           if ((text != null) && (text.length() > 0))
139           {
140             if (qualifiers == null) // default
141
{
142               sql.append("(UPPER(NAME) LIKE ?");
143               sql.addValue(text.endsWith("%") ? text.toUpperCase() : text.toUpperCase()+"%");
144             }
145             else if ((qualifiers.caseSensitiveMatch) && (qualifiers.exactNameMatch))
146             {
147               sql.append("(NAME = ?");
148               sql.addValue(text);
149             }
150             else if ((!qualifiers.caseSensitiveMatch) && (qualifiers.exactNameMatch))
151             {
152               sql.append("(UPPER(NAME) = ?");
153               sql.addValue(text.toUpperCase());
154             }
155             else if ((qualifiers.caseSensitiveMatch) && (!qualifiers.exactNameMatch))
156             {
157               sql.append("(NAME LIKE ?");
158               sql.addValue(text.endsWith("%") ? text : text+"%");
159             }
160             else if ((!qualifiers.caseSensitiveMatch) && (!qualifiers.exactNameMatch))
161             {
162               sql.append("(UPPER(NAME) LIKE ?");
163               sql.addValue(text.endsWith("%") ? text.toUpperCase() : text.toUpperCase()+"%");
164             }
165
166             // If lang is "en" we'll need to match with "en", "en_US" or "en_UK"
167
if ((lang != null) && (lang.length() > 0))
168             {
169               sql.append(" AND (UPPER(LANG_CODE) LIKE ?)");
170               sql.addValue(lang.toUpperCase()+"%");
171             }
172             
173             sql.append(")");
174
175             if (i+1 < nameSize)
176               sql.append(" OR ");
177           }
178         }
179       }
180
181       sql.append(") ");
182     }
183   }
184
185   /**
186    * Utility method used to construct SQL "IN" statements such as
187    * the following SQL example:
188    *
189    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
190    *
191    * @param sql StringBuffer to append the final results to
192    * @param keysIn Vector of Strings used to construct the "IN" clause
193    */

194   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
195   {
196     if (keysIn == null)
197       return;
198
199     sql.append("AND S.SERVICE_KEY IN (");
200
201     int keyCount = keysIn.size();
202     for (int i=0; i<keyCount; i++)
203     {
204       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
205       sql.append("?");
206       sql.addValue(key);
207
208       if ((i+1) < keyCount)
209         sql.append(",");
210     }
211
212     sql.append(") ");
213   }
214
215   /**
216    *
217    */

218   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
219   {
220     sql.append("ORDER BY ");
221
222     if ((qualifiers == null) ||
223        ((!qualifiers.sortByNameAsc) && (!qualifiers.sortByNameDesc) &&
224         (!qualifiers.sortByDateAsc) && (!qualifiers.sortByDateDesc)))
225     {
226       sql.append("N.NAME ASC,S.LAST_UPDATE DESC");
227     }
228     else if (qualifiers.sortByNameAsc || qualifiers.sortByNameDesc)
229     {
230       if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc)
231       {
232         if (qualifiers.sortByNameAsc && qualifiers.sortByDateDesc)
233           sql.append("N.NAME ASC,S.LAST_UPDATE DESC");
234         else if (qualifiers.sortByNameAsc && qualifiers.sortByDateAsc)
235           sql.append("N.NAME ASC,S.LAST_UPDATE ASC");
236         else if (qualifiers.sortByNameDesc && qualifiers.sortByDateDesc)
237           sql.append("N.NAME DESC,S.LAST_UPDATE DESC");
238         else
239           sql.append("N.NAME DESC,S.LAST_UPDATE ASC");
240       }
241       else
242       {
243         if (qualifiers.sortByNameAsc)
244           sql.append("N.NAME ASC,S.LAST_UPDATE DESC");
245         else
246           sql.append("N.NAME DESC,S.LAST_UPDATE DESC");
247       }
248     }
249     else if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc)
250     {
251       if (qualifiers.sortByDateDesc)
252         sql.append("S.LAST_UPDATE ASC,N.NAME ASC");
253       else
254         sql.append("S.LAST_UPDATE DESC,N.NAME ASC");
255     }
256   }
257 }
258
Popular Tags