View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2ACL library.
13   *
14   *  The Initial Owner of the Original Code is Power Of Two S.R.L.
15   *  Portions created by Power Of Two S.R.L. are Copyright (C) Power Of Two S.R.L.
16   *  All Rights Reserved.
17   *
18   * Contributor(s):
19   */
20  
21  package com.pow2.dbforms;
22  
23  
24  import java.sql.Connection;
25  
26  import org.apache.log4j.Category;
27  
28  import org.dbforms.config.FieldValue;
29  import org.dbforms.config.FieldValues;
30  import org.dbforms.config.ValidationException;
31  import org.dbforms.event.DbEventInterceptorSupport;
32  
33  import com.pow2.dao.DAO;
34  import com.pow2.util.Util;
35  
36  /***
37   *  DbForm interceptor (hook up class).
38   *  <br>
39   *  <b>Note: do not close</b> connection objects used in DbEventInterceptor methods.
40   *
41   * @author  Luca Fossato
42   * @created  10 maggio 2002
43   */
44  public class Interceptor extends DbEventInterceptorSupport
45  {
46    /*** Log4j category */
47    protected static Category cat = Category.getInstance(Interceptor.class);
48  
49    private static DAO dao = DAO.instance();
50  
51  
52    /***
53     *  Delete a record with a non composite key from the input table.
54     * 
55     * @param con         the JDBC connection object
56     * @param fieldValues the fieldValues object taken from the interceptor interface
57     * @param tableName   the table name of the record to delete
58     * @param keyFieldName  the name of the NON composite key field 
59     * @param isKeyAString  yes if the key value type is string; false otherwise
60     * 
61     * @return the opertaion code value specified by the dbForms' interceptor interface
62     * @throws ValidationException if any error occurs
63     */
64    protected int delete(Connection  con, 
65                         FieldValues fieldValues, 
66                         String      tableName, 
67                         String      keyFieldName, 
68                         boolean     isKeyAString) 
69      throws ValidationException
70    {
71      FieldValue keyFieldValue = null;
72      int returnCode = GRANT_OPERATION;
73      
74      keyFieldValue = fieldValues.get(keyFieldName);
75      
76      if (keyFieldValue != null)
77      {  
78        final String keyValue = keyFieldValue.getFieldValue();
79        String cond = (keyFieldName + " = ");
80        cond += isKeyAString ? Util.dbString(keyValue) : keyValue;
81        returnCode = delete(tableName, cond, con);
82      }
83      else
84      {
85        String errStr = ("cannot retrieve a keyFieldValue using keyField " + keyFieldName);
86        cat.error(errStr);
87        throw new ValidationException(errStr);
88      }
89      
90      return returnCode;
91    }
92    
93    
94    /***
95     *  Delete all the records of the input table that match the input condition.
96     *
97     * @param  tableName the name of the database table
98     * @param  cond      the SQL condition string (a WHERE clause without the 'where' world)
99     * @param  con       the connection object
100    * @return GRANT_OPERATION if the delete function is successfull
101    * @exception  ValidationException if any error occurs
102    */
103   protected int delete(String     tableName,
104                        String     cond,
105                        Connection con)
106     throws ValidationException
107   {
108     try
109     {
110       dao.delete(tableName, cond, con);
111     }
112     catch (Exception e)
113     {
114       throw new ValidationException(e.getMessage());
115     }
116 
117     return GRANT_OPERATION;
118   }
119 
120 
121   /***
122    *  Return a new unique key.
123    *
124    * @return  a new unique key
125    * @exception  ValidationException if any error occurs
126    */
127   protected long getNewKey() throws ValidationException
128   {
129     try
130     {
131       return dao.getNewKey();
132     }
133     catch (Exception e)
134     {
135       throw new ValidationException(e.getMessage());
136     }
137   }
138 }