KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > event > datalist > DeleteEvent


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/DeleteEvent.java,v 1.20 2004/10/20 10:51:30 hkollmann Exp $
3  * $Revision: 1.20 $
4  * $Date: 2004/10/20 10:51:30 $
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.DbEventInterceptor;
28 import org.dbforms.config.DbEventInterceptorData;
29 import org.dbforms.config.DbFormsConfig;
30 import org.dbforms.config.FieldValues;
31 import org.dbforms.config.GrantedPrivileges;
32
33 import org.dbforms.event.DatabaseEvent;
34 import org.dbforms.event.datalist.dao.DataSourceFactory;
35 import org.dbforms.event.datalist.dao.DataSourceSessionList;
36
37 import org.dbforms.util.MessageResourcesInternal;
38 import org.dbforms.util.StringUtil;
39 import org.dbforms.util.Util;
40
41 import java.sql.Connection JavaDoc;
42 import java.sql.SQLException JavaDoc;
43
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45
46
47
48 /**
49  * This event prepares and performs a SQL-Delete operation. <br>
50  * Works with new factory classes.
51  *
52  * @author Henner Kollmann
53  */

54 public class DeleteEvent extends DatabaseEvent {
55    private static Log logCat = LogFactory.getLog(DeleteEvent.class.getName());
56
57    /**
58     * Creates a new DeleteEvent object.
59     *
60     * @param tableId the table id
61     * @param keyId the key id
62     * @param request the request object
63     * @param config the configuration object
64     */

65    public DeleteEvent(Integer JavaDoc tableId, String JavaDoc keyId,
66       HttpServletRequest JavaDoc request, DbFormsConfig config) {
67       super(tableId.intValue(), keyId, request, config);
68    }
69
70
71    /**
72     * Creates a new DeleteEvent object.
73     *
74     * @param action the action string
75     * @param request the request object
76     * @param config the configuration object
77     */

78    public DeleteEvent(String JavaDoc action, HttpServletRequest JavaDoc request,
79       DbFormsConfig config) {
80       super(StringUtil.getEmbeddedStringAsInteger(action, 2, '_'),
81          StringUtil.getEmbeddedString(action, 3, '_'), request, config);
82    }
83
84    /**
85     * Get the FieldValues attribute.
86     *
87     * @return the FieldValues attribute
88     */

89    public FieldValues getFieldValues() {
90       return getFieldValues(true);
91    }
92
93
94    /**
95     * Process this event.
96     *
97     * @param con the connection object
98     *
99     * @throws SQLException if any SQL error occurs
100     * @throws MultipleValidationException if any validation error occurs
101     */

102    public void processEvent(Connection JavaDoc con) throws SQLException JavaDoc {
103       // Apply given security contraints (as defined in dbforms-config.xml)
104
if (!hasUserPrivileg(GrantedPrivileges.PRIVILEG_DELETE)) {
105          String JavaDoc s = MessageResourcesInternal.getMessage("dbforms.events.delete.nogrant",
106                getRequest().getLocale(), new String JavaDoc[] {
107                   getTable().getName()
108                });
109          throw new SQLException JavaDoc(s);
110       }
111
112       // in order to process an update, we need the key of the dataset to update;
113
String JavaDoc keyValuesStr = getKeyValues();
114
115       if (Util.isNull(keyValuesStr)) {
116          logCat.error(
117             "::processEvent - At least one key is required per table, check your dbforms-config.xml");
118
119          return;
120       }
121
122       // which values do we find in request
123
FieldValues fieldValues = getFieldValues();
124       DbEventInterceptorData interceptorData = new DbEventInterceptorData(getRequest(),
125             getConfig(), con, getTable());
126       interceptorData.setAttribute(DbEventInterceptorData.FIELDVALUES,
127          fieldValues);
128       interceptorData.setAttribute(DbEventInterceptorData.KEYVALUES,
129          keyValuesStr);
130
131       // part 2: check if there are interceptors to be processed (as definied by
132
// "interceptor" element embedded in table element in dbforms-config xml file)
133
// process the interceptors associated to this table
134
int operation = getTable().processInterceptors(DbEventInterceptor.PRE_DELETE,
135             interceptorData);
136
137       if (operation == DbEventInterceptor.GRANT_OPERATION) {
138          // DELETE operation;
139
DataSourceSessionList ds = DataSourceSessionList.getInstance(getRequest());
140          DataSourceFactory qry = ds.get(getTable(), getRequest());
141          boolean own = false;
142
143          if (qry == null) {
144             qry = new DataSourceFactory((String JavaDoc) interceptorData
145                   .getAttribute(DbEventInterceptorData.CONNECTIONNAME),
146                   interceptorData.getConnection(), getTable());
147             own = true;
148          }
149
150          qry.doDelete(interceptorData, keyValuesStr);
151
152          if (own) {
153             qry.close();
154          } else {
155             ds.remove(getTable(), getRequest());
156          }
157
158          // finally, we process interceptor again (post-delete)
159
// process the interceptors associated to this table
160
getTable().processInterceptors(DbEventInterceptor.POST_DELETE,
161             interceptorData);
162       }
163
164       // End of interceptor processing
165
}
166 }
167
Popular Tags