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