KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 class FindBusinessByDiscoveryURLQuery
34 {
35   // private reference to the jUDDI logger
36
private static Log log = LogFactory.getLog(FindBusinessByDiscoveryURLQuery.class);
37
38   static String JavaDoc selectSQL;
39   static
40   {
41     // build selectSQL
42
StringBuffer JavaDoc sql = new StringBuffer JavaDoc(200);
43     sql.append("SELECT B.BUSINESS_KEY,B.LAST_UPDATE ");
44     sql.append("FROM BUSINESS_ENTITY B,DISCOVERY_URL U ");
45     selectSQL = sql.toString();
46   }
47
48   /**
49    * Select ...
50    *
51    * @param discoveryURLs
52    * @param keysIn
53    * @param qualifiers
54    * @param connection JDBC connection
55    * @throws java.sql.SQLException
56    */

57   public static Vector JavaDoc select(DiscoveryURLs discoveryURLs,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,discoveryURLs,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));//("BUSINESS_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 BusinessEntity 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 BusinessEntity Statement: "+e.getMessage(),e);
106       }
107     }
108   }
109
110   /**
111    *
112    */

113   private static void appendWhere(DynamicQuery sql,DiscoveryURLs discoveryURLs,FindQualifiers qualifiers)
114   {
115     sql.append("WHERE B.BUSINESS_KEY = U.BUSINESS_KEY ");
116
117     Vector JavaDoc urlVector = discoveryURLs.getDiscoveryURLVector();
118
119     int vectorSize = urlVector.size();
120     if (vectorSize > 0)
121     {
122       sql.append("AND (");
123
124       for (int i=0; i<vectorSize; i++)
125       {
126         DiscoveryURL discoveryURL = (DiscoveryURL)urlVector.elementAt(i);
127         String JavaDoc url = discoveryURL.getValue();
128         String JavaDoc useType = discoveryURL.getUseType();
129
130         if ((url != null) && (url.length() > 0))
131         {
132           sql.append("(U.URL = ?");
133           sql.addValue(url);
134
135           if ((useType != null) && (useType.length() > 0))
136           {
137             sql.append(" AND U.USE_TYPE = ?");
138             sql.addValue(useType);
139           }
140           
141           sql.append(")");
142
143           if (i+1 < vectorSize)
144             sql.append(" OR ");
145         }
146       }
147
148       sql.append(") ");
149     }
150   }
151
152   /**
153    * Utility method used to construct SQL "IN" statements such as
154    * the following SQL example:
155    *
156    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
157    *
158    * @param sql StringBuffer to append the final results to
159    * @param keysIn Vector of Strings used to construct the "IN" clause
160    */

161   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
162   {
163     if (keysIn == null)
164       return;
165
166     sql.append("AND B.BUSINESS_KEY IN (");
167
168     int keyCount = keysIn.size();
169     for (int i=0; i<keyCount; i++)
170     {
171       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
172       sql.append("?");
173       sql.addValue(key);
174
175       if ((i+1) < keyCount)
176         sql.append(",");
177     }
178
179     sql.append(") ");
180   }
181
182   /**
183    *
184    */

185   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
186   {
187     sql.append("ORDER BY ");
188
189     if (qualifiers == null)
190       sql.append("B.LAST_UPDATE DESC");
191     else if (qualifiers.sortByDateAsc)
192       sql.append("B.LAST_UPDATE ASC");
193     else
194       sql.append("B.LAST_UPDATE DESC");
195   }
196 }
197
Popular Tags