KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > jdbc > FindTModelByNameQuery


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 JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.util.Vector JavaDoc;
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.util.jdbc.DynamicQuery;
27
28 /**
29  * @author Steve Viens (sviens@apache.org)
30  */

31 class FindTModelByNameQuery
32 {
33   // private reference to the jUDDI logger
34
private static Log log = LogFactory.getLog(FindTModelByNameQuery.class);
35
36   static String JavaDoc selectSQL;
37   static
38   {
39     // build selectSQL
40
StringBuffer JavaDoc sql = new StringBuffer JavaDoc(200);
41     sql.append("SELECT M.TMODEL_KEY,M.LAST_UPDATE,M.NAME,M.DELETED ");
42     sql.append("FROM TMODEL M ");
43     selectSQL = sql.toString();
44   }
45
46   /**
47    * Select ...
48    *
49    * @param name primary key value
50    * @param keysIn primary key value
51    * @param qualifiers primary key value
52    * @param connection JDBC connection
53    * @throws java.sql.SQLException
54    */

55   public static Vector JavaDoc select(String JavaDoc name,Vector JavaDoc keysIn,FindQualifiers qualifiers,Connection JavaDoc connection)
56     throws java.sql.SQLException JavaDoc
57   {
58     // if there is a keysIn vector but it doesn't contain
59
// any keys then the previous query has exhausted
60
// all possibilities of a match so skip this call.
61
if ((keysIn != null) && (keysIn.size() == 0))
62       return keysIn;
63
64     Vector JavaDoc keysOut = new Vector JavaDoc();
65     PreparedStatement JavaDoc statement = null;
66     ResultSet JavaDoc resultSet = null;
67
68     // construct the SQL statement
69
DynamicQuery sql = new DynamicQuery(selectSQL);
70     appendWhere(sql,name,qualifiers);
71     appendIn(sql,name,keysIn);
72     appendOrderBy(sql,qualifiers);
73
74     try
75     {
76       log.debug(sql.toString());
77
78       statement = sql.buildPreparedStatement(connection);
79       resultSet = statement.executeQuery();
80
81       while (resultSet.next())
82         keysOut.addElement(resultSet.getString(1));//("TMODEL_KEY"));
83

84       return keysOut;
85     }
86     finally
87     {
88       try {
89         resultSet.close();
90       }
91       catch (Exception JavaDoc e)
92       {
93         log.warn("An Exception was encountered while attempting to close " +
94           "the Find TModel ResultSet: "+e.getMessage(),e);
95       }
96
97       try {
98         statement.close();
99       }
100       catch (Exception JavaDoc e)
101       {
102         log.warn("An Exception was encountered while attempting to close " +
103           "the Find TModel Statement: "+e.getMessage(),e);
104       }
105     }
106   }
107
108   /**
109    *
110    */

111   private static void appendWhere(DynamicQuery sql,String JavaDoc name,FindQualifiers qualifiers)
112   {
113     if ((name == null) || (name.length() == 0))
114       return;
115
116     // When TModels are deleted they are only marked as
117
// deleted they're not actually removed from the database.
118
//
119
sql.append("WHERE M.DELETED IS NULL ");
120
121     if ((qualifiers != null) && (qualifiers.exactNameMatch))
122     {
123       sql.append("AND M.NAME = ? ");
124       sql.addValue(name);
125     }
126     else
127     {
128       sql.append("AND M.NAME LIKE ? ");
129       sql.addValue(name.endsWith("%") ? name : 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 JavaDoc name,Vector JavaDoc keysIn)
143   {
144     if (keysIn == null)
145       return;
146
147     if ((name == null) || (name.length() == 0))
148       sql.append("WHERE M.TMODEL_KEY IN (");
149     else
150       sql.append("AND M.TMODEL_KEY IN (");
151
152     int keyCount = keysIn.size();
153     for (int i=0; i<keyCount; i++)
154     {
155       String JavaDoc key = (String JavaDoc)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    *
168    */

169   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
170   {
171     sql.append("ORDER BY ");
172
173     if ((qualifiers == null) ||
174        ((!qualifiers.sortByNameAsc) && (!qualifiers.sortByNameDesc) &&
175         (!qualifiers.sortByDateAsc) && (!qualifiers.sortByDateDesc)))
176     {
177       sql.append("M.NAME ASC,M.LAST_UPDATE DESC");
178     }
179     else if (qualifiers.sortByNameAsc || qualifiers.sortByNameDesc)
180     {
181       if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc)
182       {
183         if (qualifiers.sortByNameAsc && qualifiers.sortByDateDesc)
184           sql.append("M.NAME ASC,M.LAST_UPDATE DESC");
185         else if (qualifiers.sortByNameAsc && qualifiers.sortByDateAsc)
186           sql.append("M.NAME ASC,M.LAST_UPDATE ASC");
187         else if (qualifiers.sortByNameDesc && qualifiers.sortByDateDesc)
188           sql.append("M.NAME DESC,M.LAST_UPDATE DESC");
189         else
190           sql.append("M.NAME DESC,M.LAST_UPDATE ASC");
191       }
192       else
193       {
194         if (qualifiers.sortByNameAsc)
195           sql.append("M.NAME ASC,M.LAST_UPDATE DESC");
196         else
197           sql.append("M.NAME DESC,M.LAST_UPDATE DESC");
198       }
199     }
200     else if (qualifiers.sortByDateAsc || qualifiers.sortByDateDesc)
201     {
202       if (qualifiers.sortByDateDesc)
203         sql.append("M.LAST_UPDATE ASC,M.NAME ASC");
204       else
205         sql.append("M.LAST_UPDATE DESC,M.NAME ASC");
206     }
207   }
208 }
209
Popular Tags