KickJava   Java API By Example, From Geeks To Geeks.

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


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.KeyedReference;
26
27 /**
28  * @author Steve Viens (sviens@apache.org)
29  */

30 class FindRelatedBusinessQuery
31 {
32   // private reference to the jUDDI logger
33
private static Log log = LogFactory.getLog(FindRelatedBusinessQuery.class);
34
35   static String JavaDoc selectSQL;
36   static String JavaDoc selectWithKeyedRefSQL;
37
38   static
39   {
40     StringBuffer JavaDoc sql = null;
41
42     // build selectSQL
43
sql = new StringBuffer JavaDoc(300);
44     sql.append("SELECT FROM_KEY,TO_KEY,TMODEL_KEY,KEY_NAME,KEY_VALUE ");
45     sql.append("FROM PUBLISHER_ASSERTION ");
46     sql.append("WHERE (FROM_KEY=? OR TO_KEY=?) ");
47     sql.append("AND FROM_CHECK='true' ");
48     sql.append("AND TO_CHECK='true'");
49     selectSQL = sql.toString();
50
51     // build selectWithKeyedRefSQL
52
sql = new StringBuffer JavaDoc(300);
53     sql.append("SELECT FROM_KEY,TO_KEY,TMODEL_KEY,KEY_NAME,KEY_VALUE ");
54     sql.append("FROM PUBLISHER_ASSERTION ");
55     sql.append("WHERE (FROM_KEY=? OR TO_KEY=?) ");
56     sql.append("AND TMODEL_KEY=? ");
57     sql.append("AND KEY_NAME=? ");
58     sql.append("AND KEY_VALUE=? ");
59     sql.append("AND FROM_CHECK='true' ");
60     sql.append("AND TO_CHECK='true'");
61     selectWithKeyedRefSQL = sql.toString();
62   }
63
64   /**
65    * Return a Vector of business keys that - together with the business key
66    * parameter passed in - represent a valid (ie: status:complete) PublisherAssertion.
67    * This is done by inspecting both the FROM_KEY and TO_KEY values returned from the
68    * query and adding the businessKey that IS NOT equal to the businessKey passed in.
69    *
70    * @param businessKey The BusinessKey to find relations for
71    * @param connection JDBC connection
72    * @throws java.sql.SQLException
73    */

74   public static Vector JavaDoc select(String JavaDoc businessKey,Connection JavaDoc connection)
75     throws java.sql.SQLException JavaDoc
76   {
77     Vector JavaDoc keysOut = new Vector JavaDoc();
78     PreparedStatement JavaDoc statement = null;
79     ResultSet JavaDoc resultSet = null;
80
81     try
82     {
83       statement = connection.prepareStatement(selectSQL);
84       statement.setString(1,businessKey.toString());
85       statement.setString(2,businessKey.toString());
86
87       log.debug("select from PUBLISHER_ASSERTION table:\n\n\t" + selectSQL +
88         "\n\t FROM_KEY=" + businessKey+
89         "\n\t TO_KEY=" + businessKey + "\n");
90
91       resultSet = statement.executeQuery();
92       while (resultSet.next())
93       {
94         String JavaDoc fromKey = resultSet.getString(1);//("FROM_KEY");
95
String JavaDoc toKey = resultSet.getString(2);//("TO_KEY");
96

97         if (!fromKey.equalsIgnoreCase(businessKey))
98           keysOut.addElement(fromKey);
99         else if (!toKey.equalsIgnoreCase(businessKey))
100           keysOut.addElement(toKey);
101       }
102
103       return keysOut;
104     }
105     finally
106     {
107       try {
108         resultSet.close();
109       }
110       catch (Exception JavaDoc e)
111       {
112         log.warn("An Exception was encountered while attempting to close " +
113           "the Find BusinessEntity ResultSet: "+e.getMessage(),e);
114       }
115
116       try {
117         statement.close();
118       }
119       catch (Exception JavaDoc e)
120       {
121         log.warn("An Exception was encountered while attempting to close " +
122           "the Find BusinessEntity Statement: "+e.getMessage(),e);
123       }
124     }
125   }
126
127   /**
128    * Return a Vector of business keys that - together with the business key
129    * parameter passed in - represent a valid (ie: status:complete) PublisherAssertion.
130    * This is done by inspecting both the FROM_KEY and TO_KEY values returned from the
131    * query and adding the businessKey that IS NOT equal to the businessKey passed in.
132    *
133    * @param businessKey The BusinessKey to find relations for
134    * @param keyedRef A KeyedReference instance to using when searching
135    * @param connection JDBC connection
136    * @throws java.sql.SQLException
137    */

138   public static Vector JavaDoc selectWithKeyedRef(String JavaDoc businessKey,KeyedReference keyedRef,Connection JavaDoc connection)
139     throws java.sql.SQLException JavaDoc
140   {
141     Vector JavaDoc keysOut = new Vector JavaDoc();
142     PreparedStatement JavaDoc statement = null;
143     ResultSet JavaDoc resultSet = null;
144
145     try
146     {
147       statement = connection.prepareStatement(selectWithKeyedRefSQL);
148       statement.setString(1,businessKey);
149       statement.setString(2,businessKey);
150       statement.setString(3,keyedRef.getTModelKey());
151       statement.setString(4,keyedRef.getKeyName());
152       statement.setString(5,keyedRef.getKeyValue());
153
154       log.debug("select from PUBLISHER_ASSERTION table:\n\n\t" + selectWithKeyedRefSQL +
155         "\n\t FROM_KEY=" + businessKey +
156         "\n\t TO_KEY=" + businessKey +
157         "\n\t TMODEL_KEY=" + keyedRef.getTModelKey() +
158         "\n\t KEY_NAME=" + keyedRef.getKeyName() +
159         "\n\t KEY_VALUE=" + keyedRef.getKeyValue() + "\n");
160
161       resultSet = statement.executeQuery();
162       while (resultSet.next())
163       {
164         String JavaDoc fromKey = resultSet.getString(1);//("FROM_KEY");
165
String JavaDoc toKey = resultSet.getString(2);//("TO_KEY");
166

167         if (!fromKey.equalsIgnoreCase(businessKey))
168           keysOut.addElement(fromKey);
169         else if (!toKey.equalsIgnoreCase(businessKey))
170           keysOut.addElement(toKey);
171       }
172
173       if (keysOut.size() > 0)
174         log.info("select successful, at least one matching row was found");
175       else
176         log.info("select executed successfully but no matching rows were found");
177
178       return keysOut;
179     }
180     finally
181     {
182       try {
183         resultSet.close();
184       }
185       catch (Exception JavaDoc e)
186       {
187         log.warn("An Exception was encountered while attempting to close " +
188           "the Find BusinessEntity ResultSet: "+e.getMessage(),e);
189       }
190
191       try {
192         statement.close();
193       }
194       catch (Exception JavaDoc e)
195       {
196         log.warn("An Exception was encountered while attempting to close " +
197           "the Find BusinessEntity Statement: "+e.getMessage(),e);
198       }
199     }
200   }
201 }
Popular Tags