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.TModelBag;
26  import org.apache.juddi.datatype.request.FindQualifiers;
27  import org.apache.juddi.registry.RegistryEngine;
28  import org.apache.juddi.util.Config;
29  import org.apache.juddi.util.jdbc.DynamicQuery;
30  
31  /***
32   * @author Steve Viens (sviens@apache.org)
33   */
34  class FindBindingByTModelKeyQuery
35  {
36    // private reference to the jUDDI logger
37    private static Log log = LogFactory.getLog(FindBindingByTModelKeyQuery.class);
38  
39    static String selectSQL;
40    static String tablePrefix = "";
41    static
42    {
43      tablePrefix = Config.getStringProperty(
44          RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
45      // build selectSQL
46      StringBuffer sql = new StringBuffer(200);
47      sql.append("SELECT T.BINDING_KEY,T.LAST_UPDATE ");
48      sql.append("FROM ").append(tablePrefix).append("BINDING_TEMPLATE T,").append(tablePrefix).append("TMODEL_INSTANCE_INFO I ");
49      selectSQL = sql.toString();
50    }
51  
52    /***
53     * Select ...
54     *
55     * @param connection JDBC connection
56     * @throws java.sql.SQLException
57     */
58    public static Vector select(String serviceKey,TModelBag tModelBag,Vector keysIn,FindQualifiers qualifiers,Connection connection)
59      throws java.sql.SQLException
60    {
61      // if there is a keysIn vector but it doesn't contain
62      // any keys then the previous query has exhausted
63      // all possibilities of a match so skip this call.
64      if ((keysIn != null) && (keysIn.size() == 0))
65        return keysIn;
66  
67      Vector keysOut = new Vector();
68      PreparedStatement statement = null;
69      ResultSet resultSet = null;
70  
71      // construct the SQL statement
72      DynamicQuery sql = new DynamicQuery(selectSQL);
73      appendWhere(sql,serviceKey,tModelBag,qualifiers);
74      appendIn(sql,keysIn);
75      appendOrderBy(sql,qualifiers);
76  
77      try
78      {
79        log.debug(sql.toString());
80        
81        statement = sql.buildPreparedStatement(connection);
82        resultSet = statement.executeQuery();
83  
84        while (resultSet.next())
85          keysOut.addElement(resultSet.getString(1));//("BINDING_KEY"));
86  
87        return keysOut;
88      }
89      finally
90      {
91        try {
92          resultSet.close();
93        }
94        catch (Exception e)
95        {
96          log.warn("An Exception was encountered while attempting to close " +
97            "the Find BindingTemplate ResultSet: "+e.getMessage(),e);
98        }
99  
100       try {
101         statement.close();
102       }
103       catch (Exception e)
104       {
105         log.warn("An Exception was encountered while attempting to close " +
106           "the Find BindingTemplate Statement: "+e.getMessage(),e);
107       }
108     }
109   }
110 
111   /***
112    *
113    */
114   private static void appendWhere(DynamicQuery sql,String serviceKey,TModelBag tModelBag,FindQualifiers qualifiers)
115   {
116     sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY ");
117     sql.append("AND T.SERVICE_KEY = ? ");
118     sql.addValue(serviceKey);
119 
120     Vector keyVector = tModelBag.getTModelKeyVector();
121 
122     int vectorSize = keyVector.size();
123     if (vectorSize > 0)
124     {
125       sql.append("AND (");
126 
127       for (int i=0; i<vectorSize; i++)
128       {
129         String key = (String)keyVector.elementAt(i);
130 
131         sql.append("I.TMODEL_KEY = ? ");
132         sql.addValue(key);
133 
134         if (i+1 < vectorSize)
135           sql.append(" OR ");
136       }
137 
138       sql.append(") ");
139     }
140   }
141 
142   /***
143    * Select ...
144    *
145    * @param connection JDBC connection
146    * @throws java.sql.SQLException
147    */
148   public static Vector select(String serviceKey,String tModelKey,Vector keysIn,FindQualifiers qualifiers,Connection connection)
149     throws java.sql.SQLException
150   {
151     // if there is a keysIn vector but it doesn't contain
152     // any keys then the previous query has exhausted
153     // all possibilities of a match so skip this call.
154     if ((keysIn != null) && (keysIn.size() == 0))
155       return keysIn;
156 
157     Vector keysOut = new Vector();
158     PreparedStatement statement = null;
159     ResultSet resultSet = null;
160 
161     // construct the SQL statement
162     DynamicQuery sql = new DynamicQuery(selectSQL);
163     appendWhere(sql,serviceKey,tModelKey,qualifiers);
164     appendIn(sql,keysIn);
165     appendOrderBy(sql,qualifiers);
166 
167     try
168     {
169       log.debug(sql.toString());
170 
171       statement = sql.buildPreparedStatement(connection);
172       resultSet = statement.executeQuery();
173 
174       while (resultSet.next())
175         keysOut.addElement(resultSet.getString(1));//("SERVICE_KEY"));
176 
177       return keysOut;
178     }
179     finally
180     {
181       try {
182         resultSet.close();
183       }
184       catch (Exception e)
185       {
186         log.warn("An Exception was encountered while attempting to close " +
187           "the Find BindingTemplate ResultSet: "+e.getMessage(),e);
188       }
189 
190       try {
191         statement.close();
192       }
193       catch (Exception e)
194       {
195         log.warn("An Exception was encountered while attempting to close " +
196           "the Find BindingTemplate Statement: "+e.getMessage(),e);
197       }
198     }
199   }
200 
201   /***
202    *
203    */
204   private static void appendWhere(DynamicQuery sql,String serviceKey,String tModelKey,FindQualifiers qualifiers)
205   {
206     sql.append("WHERE I.BINDING_KEY = T.BINDING_KEY ");
207 
208     if ((serviceKey != null) && (serviceKey.trim().length() > 0))
209     {
210       sql.append("AND T.SERVICE_KEY = ? ");
211       sql.addValue(serviceKey);
212     }
213     
214     if ((tModelKey != null) && (tModelKey.trim().length() > 0))
215     {
216       sql.append("AND I.TMODEL_KEY = ? ");
217       sql.addValue(tModelKey);
218     }
219   }
220 
221   /***
222    * Utility method used to construct SQL "IN" statements such as
223    * the following SQL example:
224    *
225    *   SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
226    *
227    * @param sql StringBuffer to append the final results to
228    * @param keysIn Vector of Strings used to construct the "IN" clause
229    */
230   private static void appendIn(DynamicQuery sql,Vector keysIn)
231   {
232     if (keysIn == null)
233       return;
234 
235     sql.append("AND T.BINDING_KEY IN (");
236 
237     int keyCount = keysIn.size();
238     for (int i=0; i<keyCount; i++)
239     {
240       String key = (String)keysIn.elementAt(i);
241       sql.append("?");
242       sql.addValue(key);
243       
244       if ((i+1) < keyCount)
245         sql.append(",");
246     }
247 
248     sql.append(") ");
249   }
250 
251   /***
252    *
253    */
254   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
255   {
256     sql.append("ORDER BY ");
257 
258     if (qualifiers == null)
259       sql.append("T.LAST_UPDATE DESC");
260     else if (qualifiers.sortByDateAsc)
261       sql.append("T.LAST_UPDATE ASC");
262     else
263       sql.append("T.LAST_UPDATE DESC");
264   }
265 }