KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

185   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
186   {
187     if (keysIn == null)
188       return;
189
190     sql.append("AND B.BUSINESS_KEY IN (");
191
192     int keyCount = keysIn.size();
193     for (int i=0; i<keyCount; i++)
194     {
195       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
196       sql.append("?");
197       sql.addValue(key);
198
199       if ((i+1) < keyCount)
200         sql.append(",");
201     }
202
203     sql.append(") ");
204   }
205
206   /**
207    *
208    */

209   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
210   {
211     sql.append("ORDER BY ");
212
213     if ((qualifiers == null) ||
214        ((!qualifiers.sortByNameAsc) && (!qualifiers.sortByNameDesc) &&
215         (!qualifiers.sortByDateAsc) && (!qualifiers.sortByDateDesc)))
216     {
217       sql.append("N.NAME ASC,B.LAST_UPDATE DESC");
218     }
219     else if (qualifiers.sortByNameAsc || qualifiers.sortByNameDesc)
220     {
221       if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc)
222       {
223         if (qualifiers.sortByNameAsc && qualifiers.sortByDateDesc)
224           sql.append("N.NAME ASC,B.LAST_UPDATE DESC");
225         else if (qualifiers.sortByNameAsc && qualifiers.sortByDateAsc)
226           sql.append("N.NAME ASC,B.LAST_UPDATE ASC");
227         else if (qualifiers.sortByNameDesc && qualifiers.sortByDateDesc)
228           sql.append("N.NAME DESC,B.LAST_UPDATE DESC");
229         else
230           sql.append("N.NAME DESC,B.LAST_UPDATE ASC");
231       }
232       else
233       {
234         if (qualifiers.sortByNameAsc)
235           sql.append("N.NAME ASC,B.LAST_UPDATE DESC");
236         else
237           sql.append("N.NAME DESC,B.LAST_UPDATE DESC");
238       }
239     }
240     else if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc)
241     {
242       if (qualifiers.sortByDateDesc)
243         sql.append("B.LAST_UPDATE ASC,N.NAME ASC");
244       else
245         sql.append("B.LAST_UPDATE DESC,N.NAME ASC");
246     }
247   }
248 }
249
Popular Tags