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