View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/classic/DeleteEvent.java,v 1.27 2006/01/25 16:35:23 hkollmann Exp $
3    * $Revision: 1.27 $
4    * $Date: 2006/01/25 16:35:23 $
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.AbstractDatabaseEvent;
32  import org.dbforms.interfaces.DbEventInterceptorData;
33  import org.dbforms.interfaces.IDbEventInterceptor;
34  
35  import org.dbforms.util.*;
36  
37  import java.io.*;
38  
39  import java.sql.*;
40  
41  import java.util.*;
42  
43  import javax.servlet.http.*;
44  
45  /***
46   * DOCUMENT ME!
47   * 
48   * @author Joe Peer
49   * 
50   * @deprecated
51   * <p>
52   */
53  public class DeleteEvent extends AbstractDatabaseEvent {
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 tableId, String keyId,
70  			HttpServletRequest request, DbFormsConfig config) {
71  		super(tableId.intValue(), keyId, request, config);
72  	}
73  
74  	/***
75  	 * Creates a new DeleteEvent object.
76  	 * 
77  	 * @param action
78  	 *            DOCUMENT ME!
79  	 * @param request
80  	 *            DOCUMENT ME!
81  	 * @param config
82  	 *            DOCUMENT ME!
83  	 */
84  	public DeleteEvent(String action, HttpServletRequest request,
85  			DbFormsConfig config) {
86  		super(StringUtil.getEmbeddedStringAsInteger(action, 2, '_'), StringUtil
87  				.getEmbeddedString(action, 3, '_'), request, config);
88  	}
89  
90  	/***
91  	 * DOCUMENT ME!
92  	 * 
93  	 * @return DOCUMENT ME!
94  	 */
95  	public FieldValues getFieldValues() {
96  		return getFieldValues(true);
97  	}
98  
99  	/***
100 	 * DOCUMENT ME!
101 	 * 
102 	 * @param con
103 	 *            DOCUMENT ME!
104 	 * 
105 	 * @throws MultipleValidationException
106 	 *             DOCUMENT ME!
107 	 */
108 public void processEvent(Connection con) throws SQLException, MultipleValidationException {
109       // Apply given security contraints (as defined in dbforms-config.xml)
110       if (!hasUserPrivileg(GrantedPrivileges.PRIVILEG_DELETE)) {
111          String s = MessageResourcesInternal.getMessage("dbforms.events.delete.nogrant",
112                                                         getRequest().getLocale(),
113                                                         new String[] {
114                                                            getTable()
115                                                               .getName()
116                                                         });
117          throw new MultipleValidationException(s);
118       }
119 
120       // in order to process an delete, we need the key of the dataset to
121       // delete
122       //
123       // new since version 0.9:
124       // key format: FieldID ":" Length ":" Value
125       // example: if key id = 121 and field id=2 then keyValueStr contains
126       // "2:3:121"
127       //
128       // if the key consists of more than one fields, the key values are
129       // seperated through "-"
130       // example: value of field 1=12, value of field 3=1992, then we'll get
131       // "1:2:12-3:4:1992"
132       String keyValuesStr = getKeyValues();
133 
134       if (Util.isNull(keyValuesStr)) {
135          logCat.error("At least one key is required per table, check your dbforms-config.xml");
136 
137          return;
138       }
139 
140       // which values do we find in request
141       FieldValues            fieldValues = getFieldValues();
142 
143       DbEventInterceptorData interceptorData = new DbEventInterceptorData(getRequest(),
144                                                                getConfig(), con, getTable());
145       interceptorData.setAttribute(DbEventInterceptorData.FIELDVALUES, fieldValues);
146       interceptorData.setAttribute(DbEventInterceptorData.KEYVALUES, keyValuesStr);
147 
148       // part 2b: process the interceptors associated to this table
149       int operation = getTable()
150                          .processInterceptors(IDbEventInterceptor.PRE_DELETE,
151                                               interceptorData);
152 
153       // End of interceptor processing
154       if (operation == IDbEventInterceptor.GRANT_OPERATION) {
155          // we check if the table the delete should be applied to contains
156          // field(s)
157          // of the type "DISKBLOB"
158          // if so, we have to select the filename+dirs from the db before we
159          // can delete
160          ResultSet diskblobs = null;
161          PreparedStatement diskblobsPs = null;
162          if (getTable()
163                       .containsDiskblob()) {
164             StringBuffer queryBuf = new StringBuffer();
165             queryBuf.append(getTable().getDisblobSelectStatement());
166             queryBuf.append(" WHERE ");
167             queryBuf.append(getTable().getWhereClauseForKeyFields(keyValuesStr));
168 
169             diskblobsPs = con.prepareStatement(queryBuf.toString());
170             getTable()
171                .populateWhereClauseWithKeyFields(keyValuesStr, diskblobsPs, 1);
172             diskblobs = diskblobsPs.executeQuery();
173          }
174 
175          // 20021031-HKK: build in table!!
176          PreparedStatement ps = con.prepareStatement(getTable().getDeleteStatement(keyValuesStr));
177 
178          // now we provide the values
179          // of the key-fields, so that the WHERE clause matches the right
180          // dataset!
181          getTable()
182             .populateWhereClauseWithKeyFields(keyValuesStr, ps, 1);
183 
184          // finally execute the query
185          ps.executeUpdate();
186          ps.close();
187 
188          // if we came here, we can delete the diskblob files (if any)
189          // #checkme: rollback if file problem (?? not sure!)
190          if (diskblobs != null) { // if resultset exists
191 
192             if (diskblobs.next()) {
193                // if a row in the resultset exists (can be only 1 row !)
194                Vector diskblobFields = getTable()
195                                           .getDiskblobs();
196 
197                // get fields we're interested in
198                for (int i = 0; i < diskblobFields.size(); i++) {
199                   Field  aField   = (Field) diskblobFields.elementAt(i);
200                   String fileName = diskblobs.getString(i + 1);
201 
202                   // get a filename
203                   if (fileName != null) {
204                      // may be SQL NULL, in that case we'll skip it
205                      fileName = fileName.trim(); // remove whitespace
206 
207                      if (fileName.length() > 0) {
208                         String directory = null;
209 
210                         try {
211                            directory = DbFormsConfigRegistry.instance().lookup().replaceVariables(aField.getDirectory());
212                         } catch (Exception e) {
213                            throw new SQLException(e.getMessage());
214                         }
215 
216                         // remember: every field may have its own
217                         // storing dir!
218                         File file = new File(directory, fileName);
219 
220                         if (file.exists()) {
221                            file.delete();
222                            logCat.info("deleted file " + fileName
223                                        + " from dir " + directory);
224                         } else {
225                            logCat.info("delete of file " + fileName
226                                        + " from dir " + directory
227                                        + " failed because file not found");
228                         }
229                      }
230                   }
231                }
232             }
233             diskblobsPs.close();
234             
235          }
236 
237          // finally, we process interceptor again (post-delete)
238          // process the interceptors associated to this table
239          getTable()
240             .processInterceptors(IDbEventInterceptor.POST_DELETE, interceptorData);
241       }
242 
243       // End of interceptor processing
244    }}