View Javadoc

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;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.util.Vector;
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.registry.RegistryEngine;
29  import org.apache.juddi.util.Config;
30  import org.apache.juddi.util.jdbc.DynamicQuery;
31  
32  /***
33   * @author Steve Viens (sviens@apache.org)
34   */
35  class FindBusinessByDiscoveryURLQuery
36  {
37    // private reference to the jUDDI logger
38    private static Log log = LogFactory.getLog(FindBusinessByDiscoveryURLQuery.class);
39  
40    static String selectSQL;
41    static String tablePrefix;
42    static
43    {
44      tablePrefix = Config.getStringProperty(
45          RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
46      // build selectSQL
47      StringBuffer sql = new StringBuffer(200);
48      sql.append("SELECT B.BUSINESS_KEY,B.LAST_UPDATE ");
49      sql.append("FROM ").append(tablePrefix).append("BUSINESS_ENTITY B,").append(tablePrefix).append("DISCOVERY_URL U ");
50      selectSQL = sql.toString();
51    }
52  
53    /***
54     * Select ...
55     *
56     * @param discoveryURLs
57     * @param keysIn
58     * @param qualifiers
59     * @param connection JDBC connection
60     * @throws java.sql.SQLException
61     */
62    public static Vector select(DiscoveryURLs discoveryURLs,Vector keysIn,FindQualifiers qualifiers,Connection connection)
63      throws java.sql.SQLException
64    {
65      // if there is a keysIn vector but it doesn't contain
66      // any keys then the previous query has exhausted
67      // all possibilities of a match so skip this call.
68      if ((keysIn != null) && (keysIn.size() == 0))
69        return keysIn;
70  
71      Vector keysOut = new Vector();
72      PreparedStatement statement = null;
73      ResultSet resultSet = null;
74  
75      // construct the SQL statement
76      DynamicQuery sql = new DynamicQuery(selectSQL);
77      appendWhere(sql,discoveryURLs,qualifiers);
78      appendIn(sql,keysIn);
79      appendOrderBy(sql,qualifiers);
80  
81      try
82      {
83        log.debug(sql.toString());
84  
85        statement = sql.buildPreparedStatement(connection);
86        resultSet = statement.executeQuery();
87  
88        while (resultSet.next())
89          keysOut.addElement(resultSet.getString(1));//("BUSINESS_KEY"));
90  
91        return keysOut;
92      }
93      finally
94      {
95        try {
96          resultSet.close();
97        }
98        catch (Exception e)
99        {
100         log.warn("An Exception was encountered while attempting to close " +
101           "the Find BusinessEntity ResultSet: "+e.getMessage(),e);
102       }
103 
104       try {
105         statement.close();
106       }
107       catch (Exception e)
108       {
109         log.warn("An Exception was encountered while attempting to close " +
110           "the Find BusinessEntity Statement: "+e.getMessage(),e);
111       }
112     }
113   }
114 
115   /***
116    *
117    */
118   private static void appendWhere(DynamicQuery sql,DiscoveryURLs discoveryURLs,FindQualifiers qualifiers)
119   {
120     sql.append("WHERE B.BUSINESS_KEY = U.BUSINESS_KEY ");
121 
122     Vector urlVector = discoveryURLs.getDiscoveryURLVector();
123 
124     int vectorSize = urlVector.size();
125     if (vectorSize > 0)
126     {
127       sql.append("AND (");
128 
129       for (int i=0; i<vectorSize; i++)
130       {
131         DiscoveryURL discoveryURL = (DiscoveryURL)urlVector.elementAt(i);
132         String url = discoveryURL.getValue();
133         String useType = discoveryURL.getUseType();
134 
135         if ((url != null) && (url.length() > 0))
136         {
137           sql.append("(U.URL = ?");
138           sql.addValue(url);
139 
140           if ((useType != null) && (useType.length() > 0))
141           {
142             sql.append(" AND U.USE_TYPE = ?");
143             sql.addValue(useType);
144           }
145           
146           sql.append(")");
147 
148           if (i+1 < vectorSize)
149             sql.append(" OR ");
150         }
151       }
152 
153       sql.append(") ");
154     }
155   }
156 
157   /***
158    * Utility method used to construct SQL "IN" statements such as
159    * the following SQL example:
160    *
161    *   SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
162    *
163    * @param sql StringBuffer to append the final results to
164    * @param keysIn Vector of Strings used to construct the "IN" clause
165    */
166   private static void appendIn(DynamicQuery sql,Vector keysIn)
167   {
168     if (keysIn == null)
169       return;
170 
171     sql.append("AND B.BUSINESS_KEY IN (");
172 
173     int keyCount = keysIn.size();
174     for (int i=0; i<keyCount; i++)
175     {
176       String key = (String)keysIn.elementAt(i);
177       sql.append("?");
178       sql.addValue(key);
179 
180       if ((i+1) < keyCount)
181         sql.append(",");
182     }
183 
184     sql.append(") ");
185   }
186 
187   /***
188    *
189    */
190   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
191   {
192     sql.append("ORDER BY ");
193 
194     if (qualifiers == null)
195       sql.append("B.LAST_UPDATE DESC");
196     else if (qualifiers.sortByDateAsc)
197       sql.append("B.LAST_UPDATE ASC");
198     else
199       sql.append("B.LAST_UPDATE DESC");
200   }
201 }