1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
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
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
131
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
141
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
156 int operation = getTable()
157 .processInterceptors(IDbEventInterceptor.PRE_UPDATE,
158 interceptorData);
159
160 if ((operation == IDbEventInterceptor.GRANT_OPERATION)
161 && (fieldValues.size() > 0)) {
162
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
183
184 getTable()
185 .processInterceptors(IDbEventInterceptor.POST_UPDATE, interceptorData);
186 }
187
188
189 }
190 }