View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/UpdateEvent.java,v 1.31 2005/11/30 20:31:17 hkollmann Exp $
3    * $Revision: 1.31 $
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  
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.DbFormsConfig;
31  import org.dbforms.config.FieldValues;
32  import org.dbforms.config.GrantedPrivileges;
33  import org.dbforms.config.MultipleValidationException;
34  
35  import org.dbforms.event.AbstractValidationEvent;
36  import org.dbforms.event.datalist.dao.DataSourceFactory;
37  import org.dbforms.event.datalist.dao.DataSourceSessionList;
38  import org.dbforms.interfaces.DbEventInterceptorData;
39  import org.dbforms.interfaces.IDbEventInterceptor;
40  
41  import org.dbforms.util.MessageResourcesInternal;
42  import org.dbforms.util.ParseUtil;
43  import org.dbforms.util.StringUtil;
44  import org.dbforms.util.Util;
45  
46  import java.sql.Connection;
47  import java.sql.SQLException;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  
53  /***
54   * This event prepares and performs a SQL-Update operation. <br>
55   * Works with new factory classes.
56   *
57   * @author Henner Kollmann
58   */
59  public class UpdateEvent extends AbstractValidationEvent {
60     // logging category for this class
61     private static Log logCat = LogFactory.getLog(UpdateEvent.class.getName());
62  
63     /***
64      * Creates a new UpdateEvent object.
65      *
66      * @param tableId the table identifier
67      * @param keyId the key
68      * @param request the request object
69      * @param config the configuration object
70      */
71     public UpdateEvent(Integer            tableId,
72                        String             keyId,
73                        HttpServletRequest request,
74                        DbFormsConfig      config) {
75        super(tableId.intValue(), keyId, request, config);
76     }
77  
78  
79     /***
80      * Creates a new UpdateEvent object.
81      *
82      * @param action the action string
83      * @param request the request object
84      * @param config the configuration object
85      */
86     public UpdateEvent(String             action,
87                        HttpServletRequest request,
88                        DbFormsConfig      config) {
89        super(StringUtil.getEmbeddedStringAsInteger(action, 2, '_'),
90              StringUtil.getEmbeddedString(action, 3, '_'), request, config);
91     }
92  
93     /***
94      * Get the FieldValues object.
95      *
96      * @return the FieldValues object
97      */
98  
99     // must be public because protected will break cactus testing!
100    public FieldValues getFieldValues() {
101       String s = ParseUtil.getParameter(getRequest(),
102                                         Constants.FIELDNAME_OVERRIDEFIELDTEST
103                                         + getTable().getId());
104       boolean flag = "true".equalsIgnoreCase(s);
105 
106       return getFieldValues(flag);
107    }
108 
109 
110    /***
111     * Process this event.
112     *
113     * @param con the connection object
114     *
115     * @throws SQLException if any SQL error occurs
116     * @throws MultipleValidationException if any validation error occurs
117     */
118    public void processEvent(Connection con) throws SQLException, MultipleValidationException {
119       // Apply given security contraints (as defined in dbforms-config.xml)
120       if (!hasUserPrivileg(GrantedPrivileges.PRIVILEG_UPDATE)) {
121          String s = MessageResourcesInternal.getMessage("dbforms.events.update.nogrant",
122                                                         getRequest().getLocale(),
123                                                         new String[] {
124                                                            getTable()
125                                                               .getName()
126                                                         });
127          throw new SQLException(s);
128       }
129 
130       // End of interceptor processing
131       // in order to process an update, we need the key of the dataset to update
132       String keyValuesStr = getKeyValues();
133 
134       if (Util.isNull(keyValuesStr)) {
135          logCat.error("::processEvent - at least one key is required per table, check your dbforms-config.xml");
136 
137          return;
138       }
139 
140       // 2003-08-05-HKK: first check if update is necessary before check security
141       // which values do we find in request
142       FieldValues fieldValues = getFieldValues();
143 
144       if (fieldValues.size() == 0) {
145          logCat.info("no parameters to update found");
146 
147          return;
148       }
149 
150       DbEventInterceptorData interceptorData = new DbEventInterceptorData(getRequest(),
151                                                                getConfig(), con, getTable());
152       interceptorData.setAttribute(DbEventInterceptorData.FIELDVALUES, fieldValues);
153       interceptorData.setAttribute(DbEventInterceptorData.KEYVALUES, keyValuesStr);
154 
155       // process the interceptors associated to this table
156       int operation = getTable()
157                          .processInterceptors(IDbEventInterceptor.PRE_UPDATE,
158                                               interceptorData);
159 
160       if ((operation == IDbEventInterceptor.GRANT_OPERATION)
161                 && (fieldValues.size() > 0)) {
162          // UPDATE operation;
163          DataSourceSessionList    ds  = DataSourceSessionList.getInstance(getRequest());
164          DataSourceFactory qry = ds.get(getTable(), getRequest());
165          boolean           own = false;
166 
167          if (qry == null) {
168             qry = new DataSourceFactory((String)interceptorData.getAttribute(DbEventInterceptorData.CONNECTIONNAME), interceptorData.getConnection(), getTable());
169             own = true;
170          }
171 
172          getRequest().setAttribute("forceUpdate", "true");
173          int i = qry.doUpdate(interceptorData, fieldValues, keyValuesStr);
174          interceptorData.setAttribute(DbEventInterceptorData.ROWSAFFECTED, new Integer(i));
175 
176          if (own) {
177             qry.close();
178          } else {
179             ds.remove(getTable(), getRequest());
180          }
181 
182          // finally, we process interceptor again (post-update)
183          // process the interceptors associated to this table
184          getTable()
185             .processInterceptors(IDbEventInterceptor.POST_UPDATE, interceptorData);
186       }
187 
188       // End of interceptor processing
189    }
190 }