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.IdentifierBag;
26  import org.apache.juddi.datatype.KeyedReference;
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 FindBusinessByIdentifierQuery
36  {
37    // private reference to the jUDDI logger
38    private static Log log = LogFactory.getLog(FindBusinessByIdentifierQuery.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("BUSINESS_IDENTIFIER I ");
50      selectSQL = sql.toString();
51    }
52  
53    /***
54     * Select ...
55     *
56     * @param connection JDBC connection
57     * @throws java.sql.SQLException
58     */
59    public static Vector select(IdentifierBag identifierBag,Vector keysIn,FindQualifiers qualifiers,Connection connection)
60      throws java.sql.SQLException
61    {
62      // if there is a keysIn vector but it doesn't contain
63      // any keys then the previous query has exhausted
64      // all possibilities of a match so skip this call.
65      if ((keysIn != null) && (keysIn.size() == 0))
66        return keysIn;
67  
68      Vector keysOut = new Vector();
69      PreparedStatement statement = null;
70      ResultSet resultSet = null;
71  
72      // construct the SQL statement
73      DynamicQuery sql = new DynamicQuery(selectSQL);
74      appendWhere(sql,identifierBag,qualifiers);
75      appendIn(sql,keysIn);
76      appendOrderBy(sql,qualifiers);
77  
78      try
79      {
80        log.debug(sql.toString());
81  
82        statement = sql.buildPreparedStatement(connection);
83        resultSet = statement.executeQuery();
84  
85        while (resultSet.next())
86          keysOut.addElement(resultSet.getString(1));//("BUSINESS_KEY"));
87  
88        return keysOut;
89      }
90      finally
91      {
92        try {
93          resultSet.close();
94        }
95        catch (Exception e)
96        {
97          log.warn("An Exception was encountered while attempting to close " +
98            "the Find BusinessEntity ResultSet: "+e.getMessage(),e);
99        }
100 
101       try {
102         statement.close();
103       }
104       catch (Exception e)
105       {
106         log.warn("An Exception was encountered while attempting to close " +
107           "the Find BusinessEntity Statement: "+e.getMessage(),e);
108       }
109     }
110   }
111 
112   /***
113    *
114    */
115   private static void appendWhere(DynamicQuery sql,IdentifierBag identifierBag,FindQualifiers qualifiers)
116   {
117     sql.append("WHERE B.BUSINESS_KEY = I.BUSINESS_KEY ");
118 
119     if(identifierBag != null)
120     {
121       Vector keyedRefVector = identifierBag.getKeyedReferenceVector();
122       if(keyedRefVector != null)
123       {
124         int vectorSize = keyedRefVector.size();
125         if (vectorSize > 0)
126         {
127           sql.append("AND (");
128 
129           for (int i=0; i<vectorSize; i++)
130           {
131             // When determining whether a keyedReference matches
132             // a passed keyedReference, a match occurs if and only 
133             // if 1) the tModelKeys refer to the same tModel and 2) 
134             // the keyValues are identical. The keyNames are not 
135             // significant. - UDDI Programmers API v2.04, Pgs 18, 19
136             //
137             KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i);
138             
139             String key = keyedRef.getTModelKey();
140             if (key == null)
141               key = "";
142             
143             String value = keyedRef.getKeyValue();
144             if (value == null)
145               value = "";
146   
147             sql.append("(I.TMODEL_KEY_REF = ? AND I.KEY_VALUE = ?)");
148             sql.addValue(key);
149             sql.addValue(value);
150 
151             if (i+1 < vectorSize)
152               sql.append(" OR ");
153           }
154 
155           sql.append(") ");
156         }
157       }
158     }
159   }
160 
161   /***
162    * Utility method used to construct SQL "IN" statements such as
163    * the following SQL example:
164    *
165    *   SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
166    *
167    * @param sql StringBuffer to append the final results to
168    * @param keysIn Vector of Strings used to construct the "IN" clause
169    */
170   private static void appendIn(DynamicQuery sql,Vector keysIn)
171   {
172     if (keysIn == null)
173       return;
174 
175     sql.append("AND B.BUSINESS_KEY IN (");
176 
177     int keyCount = keysIn.size();
178     for (int i=0; i<keyCount; i++)
179     {
180       String key = (String)keysIn.elementAt(i);
181       sql.append("?");
182       sql.addValue(key);
183 
184       if ((i+1) < keyCount)
185         sql.append(",");
186     }
187 
188     sql.append(") ");
189   }
190 
191   /***
192    *
193    */
194   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
195   {
196     sql.append("ORDER BY ");
197 
198     if (qualifiers == null)
199       sql.append("B.LAST_UPDATE DESC");
200     else if (qualifiers.sortByDateAsc)
201       sql.append("B.LAST_UPDATE ASC");
202     else
203       sql.append("B.LAST_UPDATE DESC");
204   }
205 }