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 FindPublisherByNameQuery
34  {
35    // private reference to the jUDDI logger
36    private static Log log = LogFactory.getLog(FindPublisherByNameQuery.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 P.PUBLISHER_ID,P.PUBLISHER_NAME ");
47      sql.append("FROM ").append(tablePrefix).append("PUBLISHER P ");
48      selectSQL = sql.toString();
49    }
50  
51    /***
52     * Select ...
53     *
54     * @param name primary key value
55     * @param keysIn primary key value
56     * @param qualifiers primary key value
57     * @param connection JDBC connection
58     * @throws java.sql.SQLException
59     */
60    public static Vector select(String name,Vector idsIn,FindQualifiers qualifiers,Connection connection)
61      throws java.sql.SQLException
62    {
63      // if there is a idsIn vector but it doesn't contain
64      // any publisher IDs then the previous query has exhausted
65      // all possibilities of a match so skip this call.
66      if ((idsIn != null) && (idsIn.size() == 0))
67        return idsIn;
68  
69      Vector idsOut = new Vector();
70      PreparedStatement statement = null;
71      ResultSet resultSet = null;
72  
73      // construct the SQL statement
74      DynamicQuery sql = new DynamicQuery(selectSQL);
75      appendWhere(sql,name,qualifiers);
76      appendIn(sql,name,idsIn);
77      appendOrderBy(sql,qualifiers);
78  
79      try
80      {
81        log.debug(sql.toString());
82  
83        statement = sql.buildPreparedStatement(connection);
84        resultSet = statement.executeQuery();
85  
86        while (resultSet.next())
87          idsOut.addElement(resultSet.getString(1));//("PUBLISHER_ID"));
88  
89        return idsOut;
90      }
91      finally
92      {
93        try {
94          resultSet.close();
95        }
96        catch (Exception e)
97        {
98          log.warn("An Exception was encountered while attempting to close " +
99            "the Find Publisher ResultSet: "+e.getMessage(),e);
100       }
101 
102       try {
103         statement.close();
104       }
105       catch (Exception e)
106       {
107         log.warn("An Exception was encountered while attempting to close " +
108           "the Find Publisher Statement: "+e.getMessage(),e);
109       }
110     }
111   }
112 
113   /***
114    *
115    */
116   private static void appendWhere(DynamicQuery sql,String name,FindQualifiers qualifiers)
117   {
118     if ((name == null) || (name.length() == 0))
119       return;
120 
121     if ((qualifiers != null) && (qualifiers.exactNameMatch))
122     {
123       sql.append("WHERE P.PUBLISHER_NAME = ? ");
124       sql.addValue(name);
125     }
126     else
127     {
128       sql.append("WHERE P.PUBLISHER_NAME LIKE ? ");
129       sql.addValue(name+"%");
130     }
131   }
132 
133   /***
134    * Utility method used to construct SQL "IN" statements such as
135    * the following SQL example:
136    *
137    *   SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
138    *
139    * @param sql StringBuffer to append the final results to
140    * @param keysIn Vector of Strings used to construct the "IN" clause
141    */
142   private static void appendIn(DynamicQuery sql,String name,Vector keysIn)
143   {
144     if (keysIn == null)
145       return;
146 
147     if ((name == null) || (name.length() == 0))
148       sql.append("WHERE P.PUBLISHER_ID IN (");
149     else
150       sql.append("AND P.PUBLISHER_ID IN (");
151 
152     int keyCount = keysIn.size();
153     for (int i=0; i<keyCount; i++)
154     {
155       String key = (String)keysIn.elementAt(i);
156       sql.append("?");
157       sql.addValue(key);
158 
159       if ((i+1) < keyCount)
160         sql.append(",");
161     }
162 
163     sql.append(") ");
164   }
165 
166   /***
167    * if the qualifier parameter is null or if the
168    * sortByNameDesc qualifier is true or if neither of
169    * the sortByNameXxxx qualifiers are true then sort
170    * the result by name in decending order.
171    */
172   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
173   {
174     sql.append("ORDER BY ");
175 
176     if ((qualifiers == null) ||
177         (qualifiers.sortByNameDesc) ||
178         ((!qualifiers.sortByNameAsc) && (!qualifiers.sortByNameDesc)))
179     {
180       sql.append("P.PUBLISHER_NAME DESC");
181     }
182     else if (qualifiers.sortByNameAsc)
183     {
184       sql.append("P.PUBLISHER_NAME ASC");
185     }
186   }
187 }