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 FindTModelByNameQuery
34  {
35    // private reference to the jUDDI logger
36    private static Log log = LogFactory.getLog(FindTModelByNameQuery.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 M.TMODEL_KEY,M.LAST_UPDATE,M.NAME,M.DELETED ");
47      sql.append("FROM ").append(tablePrefix).append("TMODEL M ");
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 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      if ((keysIn != null) && (keysIn.size() == 0))
67        return keysIn;
68  
69      Vector keysOut = 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,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  
86        while (resultSet.next())
87          keysOut.addElement(resultSet.getString(1));//("TMODEL_KEY"));
88  
89        return keysOut;
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 TModel 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 TModel 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     // When TModels are deleted they are only marked as
122     // deleted they're not actually removed from the database.
123     //
124     sql.append("WHERE M.DELETED IS NULL ");
125 
126     if ((qualifiers != null) && (qualifiers.exactNameMatch))
127     {
128       sql.append("AND M.NAME = ? ");
129       sql.addValue(name);
130     }
131     else
132     {
133       sql.append("AND M.NAME LIKE ? ");
134       sql.addValue(name.endsWith("%") ? name : name+"%");
135     }
136   }
137 
138   /***
139    * Utility method used to construct SQL "IN" statements such as
140    * the following SQL example:
141    *
142    *   SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
143    *
144    * @param sql StringBuffer to append the final results to
145    * @param keysIn Vector of Strings used to construct the "IN" clause
146    */
147   private static void appendIn(DynamicQuery sql,String name,Vector keysIn)
148   {
149     if (keysIn == null)
150       return;
151 
152     if ((name == null) || (name.length() == 0))
153       sql.append("WHERE M.TMODEL_KEY IN (");
154     else
155       sql.append("AND M.TMODEL_KEY IN (");
156 
157     int keyCount = keysIn.size();
158     for (int i=0; i<keyCount; i++)
159     {
160       String key = (String)keysIn.elementAt(i);
161       sql.append("?");
162       sql.addValue(key);
163 
164       if ((i+1) < keyCount)
165         sql.append(",");
166     }
167 
168     sql.append(") ");
169   }
170 
171   /***
172    *
173    */
174   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
175   {
176     sql.append("ORDER BY ");
177 
178     if ((qualifiers == null) ||
179        ((!qualifiers.sortByNameAsc) && (!qualifiers.sortByNameDesc) &&
180         (!qualifiers.sortByDateAsc) && (!qualifiers.sortByDateDesc)))
181     {
182       sql.append("M.NAME ASC,M.LAST_UPDATE DESC");
183     }
184     else if (qualifiers.sortByNameAsc || qualifiers.sortByNameDesc)
185     {
186       if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc)
187       {
188         if (qualifiers.sortByNameAsc && qualifiers.sortByDateDesc)
189           sql.append("M.NAME ASC,M.LAST_UPDATE DESC");
190         else if (qualifiers.sortByNameAsc && qualifiers.sortByDateAsc)
191           sql.append("M.NAME ASC,M.LAST_UPDATE ASC");
192         else if (qualifiers.sortByNameDesc && qualifiers.sortByDateDesc)
193           sql.append("M.NAME DESC,M.LAST_UPDATE DESC");
194         else
195           sql.append("M.NAME DESC,M.LAST_UPDATE ASC");
196       }
197       else
198       {
199         if (qualifiers.sortByNameAsc)
200           sql.append("M.NAME ASC,M.LAST_UPDATE DESC");
201         else
202           sql.append("M.NAME DESC,M.LAST_UPDATE DESC");
203       }
204     }
205     else if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc)
206     {
207       if (qualifiers.sortByDateDesc)
208         sql.append("M.LAST_UPDATE DESC,M.NAME ASC");
209       else
210         sql.append("M.LAST_UPDATE ASC,M.NAME ASC");
211     }
212   }
213 }