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 FindBindingByServiceKeyQuery
34  {
35    // private reference to the jUDDI logger
36    private static Log log = LogFactory.getLog(FindBindingByServiceKeyQuery.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 B.BINDING_KEY,B.LAST_UPDATE ");
47      sql.append("FROM ").append(tablePrefix).append("BINDING_TEMPLATE B ");
48      selectSQL = sql.toString();
49    }
50  
51    /***
52     * Select ...
53     *
54     * @param serviceKey
55     * @param keysIn
56     * @param qualifiers
57     * @param connection JDBC connection
58     * @throws java.sql.SQLException
59     */
60    public static Vector select(String serviceKey,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      DynamicQuery sql = new DynamicQuery(selectSQL);
75      appendWhere(sql,serviceKey,qualifiers);
76      appendIn(sql,keysIn);
77      appendOrderBy(sql,qualifiers);
78      
79      try
80      {
81        log.debug(sql.toString());
82        
83        statement = sql.buildPreparedStatement(connection);
84        resultSet = statement.executeQuery();      
85        while (resultSet.next())
86        {
87          keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
88        }
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 Find BindingTemplate 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 Find BindingTemplate Statement: "+e.getMessage(),e);
110       }
111     }
112   }
113 
114   /***
115    *
116    */
117   private static void appendWhere(DynamicQuery sql,String serviceKey,FindQualifiers qualifiers)
118   {
119     sql.append("WHERE B.SERVICE_KEY = ? ");
120     sql.addValue(serviceKey);
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 B.BINDING_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       
144       sql.append("?");
145       sql.addValue(key);
146 
147       if ((i+1) < keyCount)
148         sql.append(",");
149     }
150 
151     sql.append(") ");
152   }
153 
154   /***
155    *
156    */
157   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
158   {
159     sql.append("ORDER BY ");
160 
161     if (qualifiers == null)
162       sql.append("B.LAST_UPDATE DESC");
163     else if (qualifiers.sortByDateAsc)
164       sql.append("B.LAST_UPDATE ASC");
165     else
166       sql.append("B.LAST_UPDATE DESC");
167   }
168 }