KickJava   Java API By Example, From Geeks To Geeks.

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


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

58 public class UpdateEvent extends ValidationEvent {
59    // logging category for this class
60
private static Log logCat = LogFactory.getLog(UpdateEvent.class.getName());
61
62    /**
63     * Creates a new UpdateEvent object.
64     *
65     * @param tableId the table identifier
66     * @param keyId the key
67     * @param request the request object
68     * @param config the configuration object
69     */

70    public UpdateEvent(Integer JavaDoc tableId,
71                       String JavaDoc keyId,
72                       HttpServletRequest JavaDoc request,
73                       DbFormsConfig config) {
74       super(tableId.intValue(), keyId, request, config);
75    }
76
77
78    /**
79     * Creates a new UpdateEvent object.
80     *
81     * @param action the action string
82     * @param request the request object
83     * @param config the configuration object
84     */

85    public UpdateEvent(String JavaDoc action,
86                       HttpServletRequest JavaDoc request,
87                       DbFormsConfig config) {
88       super(StringUtil.getEmbeddedStringAsInteger(action, 2, '_'),
89             StringUtil.getEmbeddedString(action, 3, '_'), request, config);
90    }
91
92    /**
93     * Get the FieldValues object.
94     *
95     * @return the FieldValues object
96     */

97
98    // must be public because protected will break cactus testing!
99
public FieldValues getFieldValues() {
100       String JavaDoc s = ParseUtil.getParameter(getRequest(),
101                                         Constants.FIELDNAME_OVERRIDEFIELDTEST
102                                         + getTable().getId());
103       boolean flag = "true".equalsIgnoreCase(s);
104
105       return getFieldValues(flag);
106    }
107
108
109    /**
110     * Process this event.
111     *
112     * @param con the connection object
113     *
114     * @throws SQLException if any SQL error occurs
115     * @throws MultipleValidationException if any validation error occurs
116     */

117    public void processEvent(Connection JavaDoc con) throws SQLException JavaDoc {
118       // Apply given security contraints (as defined in dbforms-config.xml)
119
if (!hasUserPrivileg(GrantedPrivileges.PRIVILEG_UPDATE)) {
120          String JavaDoc s = MessageResourcesInternal.getMessage("dbforms.events.update.nogrant",
121                                                         getRequest().getLocale(),
122                                                         new String JavaDoc[] {
123                                                            getTable()
124                                                               .getName()
125                                                         });
126          throw new SQLException JavaDoc(s);
127       }
128
129       // End of interceptor processing
130
// in order to process an update, we need the key of the dataset to update
131
String JavaDoc keyValuesStr = getKeyValues();
132
133       if (Util.isNull(keyValuesStr)) {
134          logCat.error("::processEvent - at least one key is required per table, check your dbforms-config.xml");
135
136          return;
137       }
138
139       // 2003-08-05-HKK: first check if update is necessary before check security
140
// which values do we find in request
141
FieldValues fieldValues = getFieldValues();
142
143       if (fieldValues.size() == 0) {
144          logCat.info("no parameters to update found");
145
146          return;
147       }
148
149       DbEventInterceptorData interceptorData = new DbEventInterceptorData(getRequest(),
150                                                                getConfig(), con, getTable());
151       interceptorData.setAttribute(DbEventInterceptorData.FIELDVALUES, fieldValues);
152       interceptorData.setAttribute(DbEventInterceptorData.KEYVALUES, keyValuesStr);
153
154       // process the interceptors associated to this table
155
int operation = getTable()
156                          .processInterceptors(DbEventInterceptor.PRE_UPDATE,
157                                               interceptorData);
158
159       if ((operation == DbEventInterceptor.GRANT_OPERATION)
160                 && (fieldValues.size() > 0)) {
161          // UPDATE operation;
162
DataSourceSessionList ds = DataSourceSessionList.getInstance(getRequest());
163          DataSourceFactory qry = ds.get(getTable(), getRequest());
164          boolean own = false;
165
166          if (qry == null) {
167             qry = new DataSourceFactory((String JavaDoc)interceptorData.getAttribute(DbEventInterceptorData.CONNECTIONNAME), interceptorData.getConnection(), getTable());
168             own = true;
169          }
170
171          qry.doUpdate(interceptorData, fieldValues, keyValuesStr);
172
173          if (own) {
174             qry.close();
175          } else {
176             ds.remove(getTable(), getRequest());
177          }
178
179          // finally, we process interceptor again (post-update)
180
// process the interceptors associated to this table
181
getTable()
182             .processInterceptors(DbEventInterceptor.POST_UPDATE, interceptorData);
183       }
184
185       // End of interceptor processing
186
}
187 }
188
Popular Tags