KickJava   Java API By Example, From Geeks To Geeks.

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


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.IdentifierBag;
26 import org.apache.juddi.datatype.KeyedReference;
27 import org.apache.juddi.datatype.request.FindQualifiers;
28 import org.apache.juddi.util.jdbc.DynamicQuery;
29
30 /**
31  * @author Steve Viens (sviens@apache.org)
32  */

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

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

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

110   private static void appendWhere(DynamicQuery sql,IdentifierBag identifierBag,FindQualifiers qualifiers)
111   {
112     sql.append("WHERE M.TMODEL_KEY = I.TMODEL_KEY ");
113     if(identifierBag != null)
114     {
115
116       Vector JavaDoc keyedRefVector = identifierBag.getKeyedReferenceVector();
117       if(keyedRefVector != null)
118       {
119         int vectorSize = keyedRefVector.size();
120         if (vectorSize > 0)
121         {
122           sql.append("AND (");
123
124           for (int i=0; i<vectorSize; i++)
125           {
126             // When determining whether a keyedReference matches
127
// a passed keyedReference, a match occurs if and only
128
// if 1) the tModelKeys refer to the same tModel and 2)
129
// the keyValues are identical. The keyNames are not
130
// significant. - UDDI Programmers API v2.04, Pg 25
131
//
132
KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i);
133             
134             String JavaDoc key = keyedRef.getTModelKey();
135             if (key == null)
136               key = "";
137             
138             String JavaDoc value = keyedRef.getKeyValue();
139             if (value == null)
140               value = "";
141
142             sql.append("(I.TMODEL_KEY_REF = ? AND I.KEY_VALUE = ?)");
143             sql.addValue(key);
144             sql.addValue(value);
145
146             if (i+1 < vectorSize)
147               sql.append(" OR ");
148           }
149
150           sql.append(") ");
151         }
152       }
153     }
154   }
155
156   /**
157    * Utility method used to construct SQL "IN" statements such as
158    * the following SQL example:
159    *
160    * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
161    *
162    * @param sql StringBuffer to append the final results to
163    * @param keysIn Vector of Strings used to construct the "IN" clause
164    */

165   private static void appendIn(DynamicQuery sql,Vector JavaDoc keysIn)
166   {
167     if (keysIn == null)
168       return;
169
170     sql.append("AND M.TMODEL_KEY IN (");
171
172     int keyCount = keysIn.size();
173     for (int i=0; i<keyCount; i++)
174     {
175       String JavaDoc key = (String JavaDoc)keysIn.elementAt(i);
176       sql.append("?");
177       sql.addValue(key);
178
179       if ((i+1) < keyCount)
180         sql.append(",");
181     }
182
183     sql.append(") ");
184   }
185
186   /**
187    *
188    */

189   private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers)
190   {
191     sql.append("ORDER BY ");
192
193     if (qualifiers == null)
194       sql.append("M.LAST_UPDATE DESC");
195     else if (qualifiers.sortByDateAsc)
196       sql.append("M.LAST_UPDATE ASC");
197     else
198       sql.append("M.LAST_UPDATE DESC");
199   }
200 }
201
Popular Tags