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.KeyedReference;
26  import org.apache.juddi.registry.RegistryEngine;
27  import org.apache.juddi.util.Config;
28  
29  /***
30   * @author Steve Viens (sviens@apache.org)
31   */
32  class FindRelatedBusinessQuery
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(FindRelatedBusinessQuery.class);
36  
37    static String selectSQL;
38    static String selectWithKeyedRefSQL;
39  
40    static String tablePrefix;
41    static
42    {
43     tablePrefix = Config.getStringProperty(
44          RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
45      StringBuffer sql = null;
46  
47      // build selectSQL
48      sql = new StringBuffer(300);
49      sql.append("SELECT FROM_KEY,TO_KEY,TMODEL_KEY,KEY_NAME,KEY_VALUE ");
50      sql.append("FROM ").append(tablePrefix).append("PUBLISHER_ASSERTION ");
51      sql.append("WHERE (FROM_KEY=? OR TO_KEY=?) ");
52      sql.append("AND FROM_CHECK='true' ");
53      sql.append("AND TO_CHECK='true'");
54      selectSQL = sql.toString();
55  
56      // build selectWithKeyedRefSQL
57      sql = new StringBuffer(300);
58      sql.append("SELECT FROM_KEY,TO_KEY,TMODEL_KEY,KEY_NAME,KEY_VALUE ");
59      sql.append("FROM ").append(tablePrefix).append("PUBLISHER_ASSERTION ");
60      sql.append("WHERE (FROM_KEY=? OR TO_KEY=?) ");
61      sql.append("AND TMODEL_KEY=? ");
62      sql.append("AND KEY_NAME=? ");
63      sql.append("AND KEY_VALUE=? ");
64      sql.append("AND FROM_CHECK='true' ");
65      sql.append("AND TO_CHECK='true'");
66      selectWithKeyedRefSQL = sql.toString();
67    }
68  
69    /***
70     * Return a Vector of business keys that - together with the business key
71     * parameter passed in - represent a valid (ie: status:complete) PublisherAssertion.
72     * This is done by inspecting both the FROM_KEY and TO_KEY values returned from the
73     * query and adding the businessKey that IS NOT equal to the businessKey passed in.
74     *
75     * @param businessKey The BusinessKey to find relations for
76     * @param connection JDBC connection
77     * @throws java.sql.SQLException
78     */
79    public static Vector select(String businessKey,Connection connection)
80      throws java.sql.SQLException
81    {
82      Vector keysOut = new Vector();
83      PreparedStatement statement = null;
84      ResultSet resultSet = null;
85  
86      try
87      {
88        statement = connection.prepareStatement(selectSQL);
89        statement.setString(1,businessKey.toString());
90        statement.setString(2,businessKey.toString());
91  
92        if (log.isDebugEnabled()) {
93            log.debug("select from " + tablePrefix + "PUBLISHER_ASSERTION table:\n\n\t" + selectSQL +
94              "\n\t FROM_KEY=" + businessKey+
95              "\n\t TO_KEY=" + businessKey + "\n");
96        }
97  
98        resultSet = statement.executeQuery();
99        while (resultSet.next())
100       {
101         String fromKey = resultSet.getString(1);//("FROM_KEY");
102         String toKey = resultSet.getString(2);//("TO_KEY");
103 
104         if (!fromKey.equalsIgnoreCase(businessKey))
105           keysOut.addElement(fromKey);
106         else if (!toKey.equalsIgnoreCase(businessKey))
107           keysOut.addElement(toKey);
108       }
109 
110       return keysOut;
111     }
112     finally
113     {
114       try {
115         resultSet.close();
116       }
117       catch (Exception e)
118       {
119         log.warn("An Exception was encountered while attempting to close " +
120           "the Find BusinessEntity ResultSet: "+e.getMessage(),e);
121       }
122 
123       try {
124         statement.close();
125       }
126       catch (Exception e)
127       {
128         log.warn("An Exception was encountered while attempting to close " +
129           "the Find BusinessEntity Statement: "+e.getMessage(),e);
130       }
131     }
132   }
133 
134   /***
135    * Return a Vector of business keys that - together with the business key
136    * parameter passed in - represent a valid (ie: status:complete) PublisherAssertion.
137    * This is done by inspecting both the FROM_KEY and TO_KEY values returned from the
138    * query and adding the businessKey that IS NOT equal to the businessKey passed in.
139    *
140    * @param businessKey The BusinessKey to find relations for
141    * @param keyedRef A KeyedReference instance to using when searching
142    * @param connection JDBC connection
143    * @throws java.sql.SQLException
144    */
145   public static Vector selectWithKeyedRef(String businessKey,KeyedReference keyedRef,Connection connection)
146     throws java.sql.SQLException
147   {
148     Vector keysOut = new Vector();
149     PreparedStatement statement = null;
150     ResultSet resultSet = null;
151 
152     try
153     {
154       statement = connection.prepareStatement(selectWithKeyedRefSQL);
155       statement.setString(1,businessKey);
156       statement.setString(2,businessKey);
157       statement.setString(3,keyedRef.getTModelKey());
158       statement.setString(4,keyedRef.getKeyName());
159       statement.setString(5,keyedRef.getKeyValue());
160 
161       if (log.isDebugEnabled()) {
162           log.debug("select from " + tablePrefix + "PUBLISHER_ASSERTION table:\n\n\t" + selectWithKeyedRefSQL +
163             "\n\t FROM_KEY=" + businessKey +
164             "\n\t TO_KEY=" + businessKey +
165             "\n\t TMODEL_KEY=" + keyedRef.getTModelKey() +
166             "\n\t KEY_NAME=" + keyedRef.getKeyName() +
167             "\n\t KEY_VALUE=" + keyedRef.getKeyValue() + "\n");
168       }
169 
170       resultSet = statement.executeQuery();
171       while (resultSet.next())
172       {
173         String fromKey = resultSet.getString(1);//("FROM_KEY");
174         String toKey = resultSet.getString(2);//("TO_KEY");
175 
176         if (!fromKey.equalsIgnoreCase(businessKey))
177           keysOut.addElement(fromKey);
178         else if (!toKey.equalsIgnoreCase(businessKey))
179           keysOut.addElement(toKey);
180       }
181 
182       if (keysOut.size() > 0)
183         log.info("select successful, at least one matching row was found");
184       else
185         log.info("select executed successfully but no matching rows were found");
186 
187       return keysOut;
188     }
189     finally
190     {
191       try {
192         resultSet.close();
193       }
194       catch (Exception e)
195       {
196         log.warn("An Exception was encountered while attempting to close " +
197           "the Find BusinessEntity ResultSet: "+e.getMessage(),e);
198       }
199 
200       try {
201         statement.close();
202       }
203       catch (Exception e)
204       {
205         log.warn("An Exception was encountered while attempting to close " +
206           "the Find BusinessEntity Statement: "+e.getMessage(),e);
207       }
208     }
209   }
210 }