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 TModelDocDescTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(TModelDocDescTable.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_DOC_DESCR (");
52      sql.append("TMODEL_KEY,");
53      sql.append("TMODEL_DOC_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_DOC_DESCR_ID ");
65      sql.append("FROM ").append(tablePrefix).append("TMODEL_DOC_DESCR ");
66      sql.append("WHERE TMODEL_KEY=? ");
67      sql.append("ORDER BY TMODEL_DOC_DESCR_ID");
68      selectSQL = sql.toString();
69  
70      // build deleteSQL
71      sql = new StringBuffer(100);
72      sql.append("DELETE FROM ").append(tablePrefix).append("TMODEL_DOC_DESCR ");
73      sql.append("WHERE TMODEL_KEY=?");
74      deleteSQL = sql.toString();
75    }
76  
77    /***
78     * Insert new row into the TMODEL_DOC_DESCR table.
79     *
80     * @param tModelKey TModelKey to the TModel object that owns the info 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         // okay, set the values to be inserted
107         statement.setInt(2, descID); // Sequence Number aka Description ID
108         statement.setString(3, desc.getLanguageCode());
109         statement.setString(4, desc.getValue());
110 
111         if (log.isDebugEnabled()) {
112             log.debug(
113               "insert into " + tablePrefix + "TMODEL_DOC_DESCR table:\n\n\t"
114                 + insertSQL
115                 + "\n\t TMODEL_KEY="
116                 + tModelKey.toString()
117                 + "\n\t TMODEL_DOC_DESCR_ID="
118                 + descID
119                 + "\n\t LANG_CODE="
120                 + desc.getLanguageCode()
121                 + "\n\t DESCR="
122                 + desc.getValue()
123                 + "\n");
124         }
125 
126         statement.executeUpdate();
127       }
128     }
129     finally
130     {
131       try
132       {
133         statement.close();
134       }
135       catch (Exception e)
136       { /* ignored */
137       }
138     }
139   }
140 
141   /***
142    * Select all rows from the TMODEL_DOC_DESCR table for a given TModelKey.
143    *
144    * @param tModelKey TModelKey
145    * @param connection JDBC connection
146    * @throws java.sql.SQLException
147    */
148   public static Vector select(String tModelKey, Connection connection)
149     throws java.sql.SQLException
150   {
151     Vector descList = new Vector();
152     PreparedStatement statement = null;
153     ResultSet resultSet = null;
154 
155     try
156     {
157       // create a statement to query with
158       statement = connection.prepareStatement(selectSQL);
159       statement.setString(1, tModelKey.toString());
160 
161       if (log.isDebugEnabled()) {
162       log.debug(
163         "select from " + tablePrefix + "TMODEL_DOC_DESCR table:\n\n\t"
164           + selectSQL
165           + "\n\t TMODEL_KEY="
166           + tModelKey.toString()
167           + "\n\t TMODEL_DOC_DESCR_ID="
168           + "\n");
169       }
170 
171       // execute the statement
172       resultSet = statement.executeQuery();
173       while (resultSet.next())
174       {
175         Description desc = new Description();
176         desc.setLanguageCode(resultSet.getString(1));//("LANG_CODE"));
177         desc.setValue(resultSet.getString(2));//("DESCR"));
178         descList.add(desc);
179       }
180 
181       return descList;
182     }
183     finally
184     {
185       try
186       {
187         resultSet.close();
188         statement.close();
189       }
190       catch (Exception e)
191       { /* ignored */
192       }
193     }
194   }
195 
196   /***
197    * Delete multiple rows from the TMODEL_DOC_DESCR table that are assigned to the
198    * TModelKey specified.
199    *
200    * @param tModelKey TModelKey
201    * @param connection JDBC connection
202    * @throws java.sql.SQLException
203    */
204   public static void delete(String tModelKey, Connection connection)
205     throws java.sql.SQLException
206   {
207     PreparedStatement statement = null;
208 
209     try
210     {
211       // prepare the delete
212       statement = connection.prepareStatement(deleteSQL);
213       statement.setString(1, tModelKey.toString());
214 
215       if (log.isDebugEnabled()) {
216           log.debug(
217             "delete from " + tablePrefix + "TMODEL_DOC_DESCR table:\n\n\t"
218               + deleteSQL
219               + "\n\t TMODEL_KEY="
220               + tModelKey.toString()
221               + "\n");
222       }
223 
224       // execute
225       statement.executeUpdate();
226     }
227     finally
228     {
229       try
230       {
231         statement.close();
232       }
233       catch (Exception e)
234       { /* ignored */
235       }
236     }
237   }
238 }