KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/classic/DeleteEvent.java,v 1.20 2004/10/20 10:51:29 hkollmann Exp $
3  * $Revision: 1.20 $
4  * $Date: 2004/10/20 10:51:29 $
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.classic;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.config.*;
30
31 import org.dbforms.event.DatabaseEvent;
32
33 import org.dbforms.util.*;
34
35 import java.io.*;
36
37 import java.sql.*;
38
39 import java.util.*;
40
41 import javax.servlet.http.*;
42
43
44
45 /**
46  * DOCUMENT ME!
47  *
48  * @author Joe Peer
49  *
50  * @deprecated
51  * <p>
52  */

53 public class DeleteEvent extends DatabaseEvent {
54    // logging category for this class
55
static Log logCat = LogFactory.getLog(DeleteEvent.class.getName());
56
57    /**
58     * Creates a new DeleteEvent object.
59     *
60     * @param tableId
61     * DOCUMENT ME!
62     * @param keyId
63     * DOCUMENT ME!
64     * @param request
65     * DOCUMENT ME!
66     * @param config
67     * DOCUMENT ME!
68     */

69    public DeleteEvent(Integer JavaDoc tableId,
70                       String JavaDoc keyId,
71                       HttpServletRequest request,
72                       DbFormsConfig config) {
73       super(tableId.intValue(), keyId, request, config);
74    }
75
76
77    /**
78     * Creates a new DeleteEvent object.
79     *
80     * @param action
81     * DOCUMENT ME!
82     * @param request
83     * DOCUMENT ME!
84     * @param config
85     * DOCUMENT ME!
86     */

87    public DeleteEvent(String JavaDoc action,
88                       HttpServletRequest request,
89                       DbFormsConfig config) {
90       super(StringUtil.getEmbeddedStringAsInteger(action, 2, '_'),
91             StringUtil.getEmbeddedString(action, 3, '_'), request, config);
92    }
93
94    /**
95     * DOCUMENT ME!
96     *
97     * @return DOCUMENT ME!
98     */

99    public FieldValues getFieldValues() {
100       return getFieldValues(true);
101    }
102
103
104    /**
105     * DOCUMENT ME!
106     *
107     * @param con
108     * DOCUMENT ME!
109     *
110     * @throws SQLException
111     * DOCUMENT ME!
112     * @throws MultipleValidationException
113     * DOCUMENT ME!
114     */

115    public void processEvent(Connection con) throws SQLException {
116       // Apply given security contraints (as defined in dbforms-config.xml)
117
if (!hasUserPrivileg(GrantedPrivileges.PRIVILEG_DELETE)) {
118          String JavaDoc s = MessageResourcesInternal.getMessage("dbforms.events.delete.nogrant",
119                                                         getRequest().getLocale(),
120                                                         new String JavaDoc[] {
121                                                            getTable()
122                                                               .getName()
123                                                         });
124          throw new SQLException(s);
125       }
126
127       // in order to process an delete, we need the key of the dataset to
128
// delete
129
//
130
// new since version 0.9:
131
// key format: FieldID ":" Length ":" Value
132
// example: if key id = 121 and field id=2 then keyValueStr contains
133
// "2:3:121"
134
//
135
// if the key consists of more than one fields, the key values are
136
// seperated through "-"
137
// example: value of field 1=12, value of field 3=1992, then we'll get
138
// "1:2:12-3:4:1992"
139
String JavaDoc keyValuesStr = getKeyValues();
140
141       if (Util.isNull(keyValuesStr)) {
142          logCat.error("At least one key is required per table, check your dbforms-config.xml");
143
144          return;
145       }
146
147       // which values do we find in request
148
FieldValues fieldValues = getFieldValues();
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       // part 2b: process the interceptors associated to this table
156
int operation = getTable()
157                          .processInterceptors(DbEventInterceptor.PRE_DELETE,
158                                               interceptorData);
159
160       // End of interceptor processing
161
if (operation == DbEventInterceptor.GRANT_OPERATION) {
162          // we check if the table the delete should be applied to contains
163
// field(s)
164
// of the type "DISKBLOB"
165
// if so, we have to select the filename+dirs from the db before we
166
// can delete
167
ResultSet diskblobs = null;
168
169          if (getTable()
170                       .containsDiskblob()) {
171             StringBuffer JavaDoc queryBuf = new StringBuffer JavaDoc();
172             queryBuf.append(getTable().getDisblobSelectStatement());
173             queryBuf.append(" WHERE ");
174             queryBuf.append(getTable().getWhereClauseForKeyFields());
175
176             PreparedStatement diskblobsPs = con.prepareStatement(queryBuf
177                                                                  .toString());
178             getTable()
179                .populateWhereClauseWithKeyFields(keyValuesStr, diskblobsPs, 1);
180             diskblobs = diskblobsPs.executeQuery();
181             diskblobsPs.close();
182          }
183
184          // 20021031-HKK: build in table!!
185
PreparedStatement ps = con.prepareStatement(getTable().getDeleteStatement());
186
187          // now we provide the values
188
// of the key-fields, so that the WHERE clause matches the right
189
// dataset!
190
getTable()
191             .populateWhereClauseWithKeyFields(keyValuesStr, ps, 1);
192
193          // finally execute the query
194
ps.executeUpdate();
195          ps.close();
196
197          // if we came here, we can delete the diskblob files (if any)
198
// #checkme: rollback if file problem (?? not sure!)
199
if (diskblobs != null) { // if resultset exists
200

201             if (diskblobs.next()) {
202                // if a row in the resultset exists (can be only 1 row !)
203
Vector diskblobFields = getTable()
204                                           .getDiskblobs();
205
206                // get fields we're interested in
207
for (int i = 0; i < diskblobFields.size(); i++) {
208                   Field aField = (Field) diskblobFields.elementAt(i);
209                   String JavaDoc fileName = diskblobs.getString(i + 1);
210
211                   // get a filename
212
if (fileName != null) {
213                      // may be SQL NULL, in that case we'll skip it
214
fileName = fileName.trim(); // remove whitespace
215

216                      if (fileName.length() > 0) {
217                         String JavaDoc directory = null;
218
219                         try {
220                            directory = Util.replaceRealPath(aField.getDirectory(),
221                                                             DbFormsConfigRegistry.instance().lookup().getRealPath());
222                         } catch (Exception JavaDoc e) {
223                            throw new SQLException(e.getMessage());
224                         }
225
226                         // remember: every field may have its own
227
// storing dir!
228
File file = new File(directory, fileName);
229
230                         if (file.exists()) {
231                            file.delete();
232                            logCat.info("deleted file " + fileName
233                                        + " from dir " + directory);
234                         } else {
235                            logCat.info("delete of file " + fileName
236                                        + " from dir " + directory
237                                        + " failed because file not found");
238                         }
239                      }
240                   }
241                }
242             }
243          }
244
245          // finally, we process interceptor again (post-delete)
246
// process the interceptors associated to this table
247
getTable()
248             .processInterceptors(DbEventInterceptor.POST_DELETE, interceptorData);
249       }
250
251       // End of interceptor processing
252
}
253 }
254
Popular Tags