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.Description;
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 TModelDescTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(TModelDescTable.class);
36  
37    static String insertSQL = null;
38    static String selectSQL = null;
39    static String deleteSQL = null;
40    static String tablePrefix = "";
41    
42    static
43    {
44     tablePrefix = Config.getStringProperty(
45         RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
46      // buffer used to build SQL statements
47      StringBuffer sql = null;
48  
49      // build insertSQL
50      sql = new StringBuffer(150);
51      sql.append("INSERT INTO ").append(tablePrefix).append("TMODEL_DESCR (");
52      sql.append("TMODEL_KEY,");
53      sql.append("TMODEL_DESCR_ID,");
54      sql.append("LANG_CODE,");
55      sql.append("DESCR) ");
56      sql.append("VALUES (?,?,?,?)");
57      insertSQL = sql.toString();
58  
59      // build selectSQL
60      sql = new StringBuffer(200);
61      sql.append("SELECT ");
62      sql.append("LANG_CODE,");
63      sql.append("DESCR, ");
64      sql.append("TMODEL_DESCR_ID ");
65      sql.append("FROM ").append(tablePrefix).append("TMODEL_DESCR ");
66      sql.append("WHERE TMODEL_KEY=? ");
67      sql.append("ORDER BY TMODEL_DESCR_ID");
68      selectSQL = sql.toString();
69  
70      // build deleteSQL
71      sql = new StringBuffer(100);
72      sql.append("DELETE FROM ").append(tablePrefix).append("TMODEL_DESCR ");
73      sql.append("WHERE TMODEL_KEY=?");
74      deleteSQL = sql.toString();
75    }
76  
77    /***
78     * Insert new row into the TMODEL_DESCR table.
79     *
80     * @param tModelKey BusinessKey to the BusinessEntity object that owns the Description to be inserted
81     * @param descList Vector of Description objects holding values to be inserted
82     * @param connection JDBC connection
83     * @throws java.sql.SQLException
84     */
85    public static void insert(
86      String tModelKey,
87      Vector descList,
88      Connection connection)
89      throws java.sql.SQLException
90    {
91      if ((descList == null) || (descList.size() == 0))
92        return; // everything is valid but no elements to insert
93  
94      PreparedStatement statement = null;
95  
96      try
97      {
98        statement = connection.prepareStatement(insertSQL);
99        statement.setString(1, tModelKey.toString());
100 
101       int listSize = descList.size();
102       for (int descID = 0; descID < listSize; descID++)
103       {
104         Description desc = (Description) descList.elementAt(descID);
105 
106         statement.setInt(2, descID);
107         statement.setString(3, desc.getLanguageCode());
108         statement.setString(4, desc.getValue());
109 
110         if (log.isDebugEnabled()) {
111             log.debug(
112               "insert into " + tablePrefix + "TMODEL_DESCR table:\n\n\t"
113                 + insertSQL
114                 + "\n\t TMODEL_KEY="
115                 + tModelKey.toString()
116                 + "\n\t TMODEL_DESCR_ID="
117                 + descID
118                 + "\n\t LANG_CODE="
119                 + desc.getLanguageCode()
120                 + "\n\t DESCR="
121                 + desc.getValue()
122                 + "\n");
123         }
124 
125         statement.executeUpdate();
126       }
127     }
128     finally
129     {
130       try
131       {
132         statement.close();
133       }
134       catch (Exception e)
135       { /* ignored */
136       }
137     }
138   }
139 
140   /***
141    * Select all rows from the TMODEL_DESCR table for a given TModelKey.
142    *
143    * @param  tModelKey String
144    * @param  connection JDBC connection
145    * @throws java.sql.SQLException
146    */
147   public static Vector select(String tModelKey, Connection connection)
148     throws java.sql.SQLException
149   {
150     Vector descList = new Vector();
151     PreparedStatement statement = null;
152     ResultSet resultSet = null;
153 
154     try
155     {
156       // create a statement to query with
157       statement = connection.prepareStatement(selectSQL);
158       statement.setString(1, tModelKey.toString());
159 
160       if (log.isDebugEnabled()) {
161           log.debug(
162             "select from " + tablePrefix + "TMODEL_DESCR table:\n\n\t"
163               + selectSQL
164               + "\n\t TMODEL_KEY="
165               + tModelKey.toString()
166               + "\n");
167       }
168 
169       // execute the statement
170       resultSet = statement.executeQuery();
171       while (resultSet.next())
172       {
173         Description desc = new Description();
174         desc.setLanguageCode(resultSet.getString(1));//("LANG_CODE"));
175         desc.setValue(resultSet.getString(2));//("DESCR"));
176         descList.add(desc);
177       }
178 
179       return descList;
180     }
181     finally
182     {
183       try
184       {
185         resultSet.close();
186         statement.close();
187       }
188       catch (Exception e)
189       { /* ignored */
190       }
191     }
192   }
193 
194   /***
195    * Delete multiple rows from the TMODEL_DESCR table that are assigned to the
196    * TModelKey specified.
197    *
198    * @param  tModelKey String
199    * @param  connection JDBC connection
200    * @throws java.sql.SQLException
201    */
202   public static void delete(String tModelKey, Connection connection)
203     throws java.sql.SQLException
204   {
205     PreparedStatement statement = null;
206 
207     try
208     {
209       // prepare the delete
210       statement = connection.prepareStatement(deleteSQL);
211       statement.setString(1, tModelKey.toString());
212 
213       if (log.isDebugEnabled()) {
214           log.debug(
215             "delete from " + tablePrefix + "TMODEL_DESCR table:\n\n\t"
216               + deleteSQL
217               + "\n\t TMODEL_KEY="
218               + tModelKey.toString()
219               + "\n");
220       }
221 
222       // execute
223       statement.executeUpdate();
224     }
225     finally
226     {
227       try
228       {
229         statement.close();
230       }
231       catch (Exception e)
232       { /* ignored */
233       }
234     }
235   }
236 }