View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/DeleteEvent.java,v 1.25 2005/11/30 20:31:17 hkollmann Exp $
3    * $Revision: 1.25 $
4    * $Date: 2005/11/30 20:31:17 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  package org.dbforms.event.datalist;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import org.dbforms.config.DbFormsConfig;
28  import org.dbforms.config.FieldValues;
29  import org.dbforms.config.GrantedPrivileges;
30  import org.dbforms.config.MultipleValidationException;
31  
32  import org.dbforms.event.AbstractDatabaseEvent;
33  import org.dbforms.event.datalist.dao.DataSourceFactory;
34  import org.dbforms.event.datalist.dao.DataSourceSessionList;
35  import org.dbforms.interfaces.DbEventInterceptorData;
36  import org.dbforms.interfaces.IDbEventInterceptor;
37  
38  import org.dbforms.util.MessageResourcesInternal;
39  import org.dbforms.util.StringUtil;
40  import org.dbforms.util.Util;
41  
42  import java.sql.Connection;
43  import java.sql.SQLException;
44  
45  import javax.servlet.http.HttpServletRequest;
46  
47  
48  
49  /***
50   * This event prepares and performs a SQL-Delete operation. <br>
51   * Works with new factory classes.
52   *
53   * @author Henner Kollmann
54   */
55  public class DeleteEvent extends AbstractDatabaseEvent {
56     private static Log logCat = LogFactory.getLog(DeleteEvent.class.getName());
57  
58     /***
59      * Creates a new DeleteEvent object.
60      *
61      * @param tableId the table id
62      * @param keyId the key id
63      * @param request the request object
64      * @param config the configuration object
65      */
66     public DeleteEvent(Integer tableId, String keyId,
67        HttpServletRequest request, DbFormsConfig config) {
68        super(tableId.intValue(), keyId, request, config);
69     }
70  
71  
72     /***
73      * Creates a new DeleteEvent object.
74      *
75      * @param action the action string
76      * @param request the request object
77      * @param config the configuration object
78      */
79     public DeleteEvent(String action, HttpServletRequest request,
80        DbFormsConfig config) {
81        super(StringUtil.getEmbeddedStringAsInteger(action, 2, '_'),
82           StringUtil.getEmbeddedString(action, 3, '_'), request, config);
83     }
84  
85     /***
86      * Get the FieldValues attribute.
87      *
88      * @return the FieldValues attribute
89      */
90     public FieldValues getFieldValues() {
91        return getFieldValues(true);
92     }
93  
94  
95     /***
96      * Process this event.
97      *
98      * @param con the connection object
99      *
100     * @throws SQLException if any SQL error occurs
101     * @throws MultipleValidationException if any validation error occurs
102     */
103    public void processEvent(Connection con) throws SQLException, MultipleValidationException {
104       // Apply given security contraints (as defined in dbforms-config.xml)
105       if (!hasUserPrivileg(GrantedPrivileges.PRIVILEG_DELETE)) {
106          String s = MessageResourcesInternal.getMessage("dbforms.events.delete.nogrant",
107                getRequest().getLocale(), new String[] {
108                   getTable().getName()
109                });
110          throw new SQLException(s);
111       }
112 
113       // in order to process an update, we need the key of the dataset to update;
114       String keyValuesStr = getKeyValues();
115 
116       if (Util.isNull(keyValuesStr)) {
117          logCat.error(
118             "::processEvent - At least one key is required per table, check your dbforms-config.xml");
119 
120          return;
121       }
122 
123       // which values do we find in request
124       FieldValues            fieldValues     = getFieldValues();
125       DbEventInterceptorData interceptorData = new DbEventInterceptorData(getRequest(),
126             getConfig(), con, getTable());
127       interceptorData.setAttribute(DbEventInterceptorData.FIELDVALUES,
128          fieldValues);
129       interceptorData.setAttribute(DbEventInterceptorData.KEYVALUES,
130          keyValuesStr);
131 
132       // part 2: check if there are interceptors to be processed (as definied by
133       // "interceptor" element embedded in table element in dbforms-config xml file)
134       // process the interceptors associated to this table
135       int operation = getTable().processInterceptors(IDbEventInterceptor.PRE_DELETE,
136             interceptorData);
137 
138       if (operation == IDbEventInterceptor.GRANT_OPERATION) {
139          // DELETE operation;
140          DataSourceSessionList ds  = DataSourceSessionList.getInstance(getRequest());
141          DataSourceFactory     qry = ds.get(getTable(), getRequest());
142          boolean               own = false;
143 
144          if (qry == null) {
145             qry    = new DataSourceFactory((String) interceptorData
146                   .getAttribute(DbEventInterceptorData.CONNECTIONNAME),
147                   interceptorData.getConnection(), getTable());
148             own    = true;
149          }
150 
151          getRequest().setAttribute("forceUpdate", "true");
152          int i = qry.doDelete(interceptorData, keyValuesStr);
153          interceptorData.setAttribute(DbEventInterceptorData.ROWSAFFECTED, new Integer(i));
154 
155          if (own) {
156             qry.close();
157          } else {
158             ds.remove(getTable(), getRequest());
159          }
160 
161          // finally, we process interceptor again (post-delete)
162          // process the interceptors associated to this table
163          getTable().processInterceptors(IDbEventInterceptor.POST_DELETE,
164             interceptorData);
165       }
166 
167       // End of interceptor processing
168    }
169 }