KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > event > BlobInterceptor


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/BlobInterceptor.java,v 1.8 2005/02/19 21:26:29 hkollmann Exp $
3  * $Revision: 1.8 $
4  * $Date: 2005/02/19 21:26: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;
25
26 import org.dbforms.config.DbFormsConfig;
27 import org.dbforms.config.FieldValue;
28 import org.dbforms.config.FieldValues;
29 import org.dbforms.config.Table;
30 import org.dbforms.config.ValidationException;
31
32 import org.dbforms.util.FileHolder;
33 import java.sql.Connection JavaDoc;
34
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40
41
42
43 /**
44  * This Interceptor can be used to automatically store the filenames, content
45  * types and file sizes of BLOBS in some table column specified, so that it is
46  * not lost. Currently, the interceptor must be configured manually in
47  * dbforms-config.xml inside the adequate table definition (see user guide)
48  * The interceptor MUST be initialized with the following parameters:
49  *
50  * <ul>
51  * <li>
52  * <b>blob-column </b>- the name of the BLOB field
53  * </li>
54  * <li>
55  * <b>name-column </b>- a character field to store the FILENAME of the uploaded
56  * file
57  * </li>
58  * </ul>
59  *
60  * Optionaly may be specified:
61  *
62  * <ul>
63  * <li>
64  * <b>mime-column </b>- a character field to store the content typpe of the
65  * uploaded file
66  * </li>
67  * <li>
68  * <b>size-column </b>- an integer field to store the file size of the uploaded
69  * file
70  * </li>
71  * </ul>
72  *
73  * if the table contains multiple BLOBs, then a unique integer n has to be
74  * appended to blob-column and blob-name to associate the correct pairs of
75  * BLOB, NAME, MIME and SIZE fields. Usage Examples:
76  *
77  * <p>
78  * if one BLOB:
79  * </p>
80  *
81  * <p>
82  * &lt;interceptor className="org.dbforms.event.BlobInterceptor"&gt; <br>
83  * &nbsp;&nbsp;&lt;param name="blob-column" value="file"/&gt; <br>
84  * &nbsp;&nbsp;&lt;param name="name-column" value="filename"/&gt; <br>
85  * &nbsp;&nbsp;&lt;param name="mime-column" value="mime_type"/&gt; <br>
86  * &nbsp;&nbsp;&lt;param name="size-column" value="file_size"/&gt; <br>
87  * &lt;/interceptor&gt; <br>
88  * </p>
89  *
90  * <p>
91  * if mulitple BLOBs:
92  * </p>
93  *
94  * <p>
95  * &lt;interceptor className="org.dbforms.event.BlobInterceptor"&gt; <br>
96  * &nbsp;&nbsp;&lt;param name="blob-column1" value="file"/&gt; <br>
97  * &nbsp;&nbsp;&lt;param name="name-column1" value="filename"/&gt; <br>
98  * &nbsp;&nbsp;&lt;param name="mime-column1" value="mime_type"/&gt; <br>
99  * &nbsp;&nbsp;&lt;param name="size-column1" value="file_size"/&gt; <br>
100  * &nbsp;&nbsp;&lt;param name="blob-column2" value="otherfile"/&gt; <br>
101  * &nbsp;&nbsp;&lt;param name="name-column2" value="otherfilename"/&gt; <br>
102  * &nbsp;&nbsp;&lt;param name="mime-column2" value="other_mime_type"/&gt; <br>
103  * &nbsp;&nbsp;&lt;param name="size-column2" value="other_file_size"/&gt; <br>
104  * &nbsp;&nbsp;&lt;param name="blob-column3" value="foofile"/&gt; <br>
105  * &nbsp;&nbsp;&lt;param name="name-column3" value="foofilename"/&gt; <br>
106  * &nbsp;&nbsp;&lt;param name="mime-column3" value="foo_mime_type"/&gt; <br>
107  * &nbsp;&nbsp;&lt;param name="size-column3" value="foo_file_size"/&gt; <br>
108  * &lt;/interceptor&gt; <br>
109  * </p>
110  *
111  * @author Joe Peer
112  */

113 public class BlobInterceptor extends DbEventInterceptorSupport {
114    private static final int BLOB_COL = 0;
115    private static final int NAME_COL = 1;
116    private static final int MIME_COL = 2;
117    private static final int SIZE_COL = 3;
118    private HashMap JavaDoc blobFieldData;
119
120    /**
121     * takes params, sorts/groups and stores for later use
122     *
123     * @param params DOCUMENT ME!
124     */

125    public void setParameterMap(Map JavaDoc params) {
126       super.setParameterMap(params);
127       blobFieldData = new HashMap JavaDoc();
128
129       for (Iterator JavaDoc iter = params.entrySet()
130                                  .iterator(); iter.hasNext();) {
131          Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
132          String JavaDoc key = (String JavaDoc) me.getKey();
133          String JavaDoc value = (String JavaDoc) me.getValue();
134
135          if (key.startsWith("blob-column")) {
136             Integer JavaDoc ii = getSuffixAsInteger(key, "blob-column");
137             String JavaDoc[] s = (String JavaDoc[]) blobFieldData.get(ii);
138
139             if (s == null) {
140                s = new String JavaDoc[4];
141                blobFieldData.put(ii, s);
142             }
143
144             s[BLOB_COL] = value;
145          } else if (key.startsWith("name-column")) {
146             Integer JavaDoc ii = getSuffixAsInteger(key, "name-column");
147             String JavaDoc[] s = (String JavaDoc[]) blobFieldData.get(ii);
148
149             if (s == null) {
150                s = new String JavaDoc[4];
151                blobFieldData.put(ii, s);
152             }
153
154             s[NAME_COL] = value;
155          } else if (key.startsWith("mime-column")) {
156             Integer JavaDoc ii = getSuffixAsInteger(key, "mime-column");
157             String JavaDoc[] s = (String JavaDoc[]) blobFieldData.get(ii);
158
159             if (s == null) {
160                s = new String JavaDoc[4];
161                blobFieldData.put(ii, s);
162             }
163
164             s[MIME_COL] = value;
165          } else if (key.startsWith("size-column")) {
166             Integer JavaDoc ii = getSuffixAsInteger(key, "size-column");
167             String JavaDoc[] s = (String JavaDoc[]) blobFieldData.get(ii);
168
169             if (s == null) {
170                s = new String JavaDoc[4];
171                blobFieldData.put(ii, s);
172             }
173
174             s[SIZE_COL] = value;
175          }
176       }
177    }
178
179
180    /**
181     * stores filenames, content types and file sizes of all blobs in the
182     * NAME_COLUMNs, MIME_COLUMNs and SIZE_COLUMNs specified
183     *
184     * @param request DOCUMENT ME!
185     * @param table DOCUMENT ME!
186     * @param fieldValues DOCUMENT ME!
187     * @param config DOCUMENT ME!
188     * @param con DOCUMENT ME!
189     *
190     * @return DOCUMENT ME!
191     */

192    public int preInsert(HttpServletRequest JavaDoc request,
193                         Table table,
194                         FieldValues fieldValues,
195                         DbFormsConfig config,
196                         Connection JavaDoc con) throws ValidationException {
197       assignBlobData(table, fieldValues);
198
199       return GRANT_OPERATION;
200    }
201
202
203    /**
204     * stores filenames, content types and file sizes of all blobs in the
205     * NAME_COLUMNs, MIME_COLUMNs and SIZE_COLUMNs specified
206     *
207     * @param request DOCUMENT ME!
208     * @param table DOCUMENT ME!
209     * @param fieldValues DOCUMENT ME!
210     * @param config DOCUMENT ME!
211     * @param con DOCUMENT ME!
212     *
213     * @return DOCUMENT ME!
214     */

215    public int preUpdate(HttpServletRequest JavaDoc request,
216                         Table table,
217                         FieldValues fieldValues,
218                         DbFormsConfig config,
219                         Connection JavaDoc con) throws ValidationException {
220       assignBlobData(table, fieldValues);
221
222       return GRANT_OPERATION;
223    }
224
225
226    /**
227     * utility function
228     *
229     * @param value DOCUMENT ME!
230     * @param stub DOCUMENT ME!
231     *
232     * @return DOCUMENT ME!
233     */

234    private Integer JavaDoc getSuffixAsInteger(String JavaDoc value,
235                                       String JavaDoc stub) {
236       String JavaDoc suffix = value.substring(stub.length());
237
238       if (suffix.length() == 0) {
239          return new Integer JavaDoc(Integer.MIN_VALUE);
240       } else {
241          return new Integer JavaDoc(suffix);
242       }
243    }
244
245
246    /**
247     * goes through params and makes sure that the fileholder's file name info
248     * associated to the blob field BLOB_COLUMN is stored in the NAME_COLUMN
249     * field. The same is with MIME_COLUMN and SIZE_COLUMN fields.
250     *
251     * @param table DOCUMENT ME!
252     * @param fieldValues DOCUMENT ME!
253     */

254    private void assignBlobData(Table table,
255                                FieldValues fieldValues) {
256       for (Iterator JavaDoc iter = blobFieldData.values()
257                                         .iterator(); iter.hasNext();) {
258          String JavaDoc[] s = (String JavaDoc[]) iter.next();
259          FieldValue fv = fieldValues.get(s[BLOB_COL]);
260
261          if (fv != null) {
262             Object JavaDoc o = fv.getFieldValueAsObject();
263
264             if ((o != null) && o instanceof FileHolder) {
265                String JavaDoc fileName = ((FileHolder) o).getFileName();
266                String JavaDoc contentType = ((FileHolder) o).getContentType();
267                int fileLength = ((FileHolder) o).getFileLength();
268                setValue(table, fieldValues, s[NAME_COL], fileName);
269
270                if (s[MIME_COL] != null) {
271                   setValue(table, fieldValues, s[MIME_COL], contentType);
272                }
273
274                if (s[SIZE_COL] != null) {
275                   setValue(table, fieldValues, s[SIZE_COL],
276                            String.valueOf(fileLength));
277                }
278             }
279          }
280       }
281    }
282 }
283
Popular Tags