1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
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
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
133
134
135 int operation = getTable().processInterceptors(IDbEventInterceptor.PRE_DELETE,
136 interceptorData);
137
138 if (operation == IDbEventInterceptor.GRANT_OPERATION) {
139
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
162
163 getTable().processInterceptors(IDbEventInterceptor.POST_DELETE,
164 interceptorData);
165 }
166
167
168 }
169 }