KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > AbstractDatabaseAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.acting;
17
18 import java.io.BufferedInputStream JavaDoc;
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.math.BigDecimal JavaDoc;
24 import java.sql.Array JavaDoc;
25 import java.sql.Clob JavaDoc;
26 import java.sql.Date JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Time JavaDoc;
31 import java.sql.Timestamp JavaDoc;
32 import java.sql.Types JavaDoc;
33 import java.text.DateFormat JavaDoc;
34 import java.text.SimpleDateFormat JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
40 import org.apache.avalon.framework.activity.Disposable;
41 import org.apache.avalon.framework.configuration.Configurable;
42 import org.apache.avalon.framework.configuration.Configuration;
43 import org.apache.avalon.framework.configuration.ConfigurationException;
44 import org.apache.avalon.framework.parameters.Parameters;
45 import org.apache.avalon.framework.service.ServiceException;
46 import org.apache.avalon.framework.service.ServiceManager;
47 import org.apache.avalon.framework.service.ServiceSelector;
48 import org.apache.cocoon.environment.Request;
49 import org.apache.cocoon.util.ImageProperties;
50 import org.apache.cocoon.util.ImageUtils;
51
52 /**
53  * Set up environment for configurable form handling data. It is
54  * important to note that all DatabaseActions use a common configuration
55  * format. This group of actions are unique in that they employ a
56  * terciary mapping. There is the Form parameter, the database column,
57  * and the type.
58  *
59  * Each configuration file must use the same format in order to be
60  * effective. The name of the root configuration element is irrelevant.
61  *
62  * <pre>
63  * &lt;root&gt;
64  * &lt;connection&gt;personnel&lt;connection&gt;
65  * &lt;table&gt;
66  * &lt;keys&gt;
67  * &lt;key param="id" dbcol="id" type="int"/&gt;
68  * &lt;/keys&gt;
69  * &lt;values&gt;
70  * &lt;value param="name" dbcol="name" type="string"/&gt;
71  * &lt;value param="department" dbcol="department_id" type="int"/&gt;
72  * &lt;/values&gt;
73  * &lt;/table&gt;
74  * &lt;/root&gt;
75  * </pre>
76  *
77  * The types recognized by this system are:
78  *
79  * <table>
80  * <tr>
81  * <th>Type</th>
82  * <th>Description</th>
83  * </tr>
84  * <tr>
85  * <td>ascii</td>
86  * <td>ASCII Input Stream, a CLOB input</td>
87  * </tr>
88  * <tr>
89  * <td>big-decimal</td>
90  * <td>a <code>java.math.BigDecimal</code> value</td>
91  * </tr>
92  * <tr>
93  * <td>binary</td>
94  * <td>Binary Input Stream, a BLOB input</td>
95  * </tr>
96  * <tr>
97  * <td>byte</td>
98  * <td>a Byte</td>
99  * </tr>
100  * <tr>
101  * <td>string</td>
102  * <td>a String</td>
103  * </tr>
104  * <tr>
105  * <td>date</td>
106  * <td>a Date</td>
107  * </tr>
108  * <tr>
109  * <td>double</td>
110  * <td>a Double</td>
111  * </tr>
112  * <tr>
113  * <td>float</td>
114  * <td>a Float</td>
115  * </tr>
116  * <tr>
117  * <td>int</td>
118  * <td>an Integer</td>
119  * </tr>
120  * <tr>
121  * <td>long</td>
122  * <td>a Long</td>
123  * </tr>
124  * <tr>
125  * <td>short</td>
126  * <td>a Short</td>
127  * </tr>
128  * <tr>
129  * <td>time</td>
130  * <td>a Time</td>
131  * </tr>
132  * <tr>
133  * <td>time-stamp</td>
134  * <td>a Timestamp</td>
135  * </tr>
136  * <tr>
137  * <td>now</td>
138  * <td>a Timestamp with the current day/time--the form value is ignored.</td>
139  * </tr>
140  * <tr>
141  * <td>image</td>
142  * <td>a binary image file, we cache the attribute information</td>
143  * </tr>
144  * <tr>
145  * <td>image-width</td>
146  * <td>
147  * the width attribute of the cached file attribute. NOTE:
148  * param attribute must equal the param for image with a
149  * "-width" suffix.
150  * </td>
151  * </tr>
152  * <tr>
153  * <td>image-height</td>
154  * <td>
155  * the width attribute of the cached file attribute NOTE:
156  * param attribute must equal the param for image with a
157  * "-height" suffix.
158  * </td>
159  * </tr>
160  * <tr>
161  * <td>image-size</td>
162  * <td>
163  * the size attribute of the cached file attribute NOTE:
164  * param attribute must equal the param for image with a
165  * "-size" suffix.
166  * </td>
167  * </tr>
168  * </table>
169  *
170  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
171  * @author <a HREF="mailto:balld@apache.org">Donald Ball</a>
172  * @version CVS $Id: AbstractDatabaseAction.java 76148 2004-11-17 16:23:35Z antonio $
173  */

174 public abstract class AbstractDatabaseAction extends AbstractComplementaryConfigurableAction implements Configurable, Disposable {
175     protected Map JavaDoc files = new HashMap JavaDoc();
176     protected static final Map JavaDoc typeConstants;
177     protected ServiceSelector dbselector;
178
179     static {
180         /** Initialize the map of type names to jdbc column types.
181             Note that INTEGER, BLOB, and VARCHAR column types map to more than
182             one type name. **/

183         Map JavaDoc constants = new HashMap JavaDoc();
184         constants.put("ascii", new Integer JavaDoc(Types.CLOB));
185         constants.put("big-decimal", new Integer JavaDoc(Types.BIGINT));
186         constants.put("binary", new Integer JavaDoc(Types.BLOB));
187         constants.put("byte", new Integer JavaDoc(Types.TINYINT));
188         constants.put("string", new Integer JavaDoc(Types.VARCHAR));
189         constants.put("date", new Integer JavaDoc(Types.DATE));
190         constants.put("double", new Integer JavaDoc(Types.DOUBLE));
191         constants.put("float", new Integer JavaDoc(Types.FLOAT));
192         constants.put("int", new Integer JavaDoc(Types.INTEGER));
193         constants.put("long", new Integer JavaDoc(Types.NUMERIC));
194         constants.put("short", new Integer JavaDoc(Types.SMALLINT));
195         constants.put("time", new Integer JavaDoc(Types.TIME));
196         constants.put("time-stamp", new Integer JavaDoc(Types.TIMESTAMP));
197         constants.put("now", new Integer JavaDoc(Types.LONGVARBINARY));
198         //constants.put("image", new Integer(Types.DISTINCT));
199
//constants.put("image-width", new Integer(Types.ARRAY));
200
//constants.put("image-height", new Integer(Types.BIT));
201
//constants.put("image-size", new Integer(Types.CHAR));
202
constants.put("image",new Integer JavaDoc(Types.BLOB));
203         constants.put("image-width",new Integer JavaDoc(Types.INTEGER));
204         constants.put("image-height",new Integer JavaDoc(Types.INTEGER));
205         constants.put("image-size",new Integer JavaDoc(Types.INTEGER));
206         constants.put("row-index",new Integer JavaDoc(Types.INTEGER));
207         constants.put("image-mime-type",new Integer JavaDoc(Types.VARCHAR));
208         constants.put("array", new Integer JavaDoc(Types.ARRAY));
209         constants.put("row", new Integer JavaDoc(Types.STRUCT));
210         constants.put("object", new Integer JavaDoc(Types.OTHER));
211         typeConstants = Collections.unmodifiableMap(constants);
212     }
213
214     /**
215      * Compose the Actions so that we can select our databases.
216      */

217     public void service(ServiceManager manager) throws ServiceException {
218         super.service(manager);
219         this.dbselector = (ServiceSelector) manager.lookup(DataSourceComponent.ROLE + "Selector");
220     }
221     /**
222      * Get the Datasource we need.
223      */

224     protected final DataSourceComponent getDataSource(Configuration conf) throws ServiceException {
225         Configuration dsn = conf.getChild("connection");
226         return (DataSourceComponent) this.dbselector.select(dsn.getValue(""));
227     }
228
229     /**
230      * Return whether a type is a Large Object (BLOB/CLOB).
231      */

232     protected final boolean isLargeObject (String JavaDoc type) {
233         if ("ascii".equals(type)) return true;
234         if ("binary".equals(type)) return true;
235         if ("image".equals(type)) return true;
236
237         return false;
238     }
239
240     /**
241      * Get the Statement column so that the results are mapped correctly.
242      */

243     protected Object JavaDoc getColumn(ResultSet JavaDoc set, Request request, Configuration entry)
244     throws Exception JavaDoc {
245         Integer JavaDoc type = (Integer JavaDoc) AbstractDatabaseAction.typeConstants.get(entry.getAttribute("type"));
246         String JavaDoc attribute = entry.getAttribute("param", "");
247         String JavaDoc dbcol = entry.getAttribute("dbcol", "");
248         Object JavaDoc value = null;
249
250         switch (type.intValue()) {
251             case Types.CLOB:
252                 Clob JavaDoc dbClob = set.getClob(dbcol);
253                 int length = (int) dbClob.length();
254                 InputStream JavaDoc asciiStream = new BufferedInputStream JavaDoc(dbClob.getAsciiStream());
255                 byte[] buffer = new byte[length];
256                 asciiStream.read(buffer);
257                 String JavaDoc str = new String JavaDoc(buffer);
258                 asciiStream.close();
259                 value = str;
260                 break;
261             case Types.BIGINT:
262                 value = set.getBigDecimal(dbcol);
263                 break;
264             case Types.TINYINT:
265                 value = new Byte JavaDoc(set.getByte(dbcol));
266                 break;
267             case Types.VARCHAR:
268                 value = set.getString(dbcol);
269                 break;
270             case Types.DATE:
271                 value = set.getDate(dbcol);
272                 break;
273             case Types.DOUBLE:
274                 value = new Double JavaDoc(set.getDouble(dbcol));
275                 break;
276             case Types.FLOAT:
277                 value = new Float JavaDoc(set.getFloat(dbcol));
278                 break;
279             case Types.INTEGER:
280                 value = new Integer JavaDoc(set.getInt(dbcol));
281                 break;
282             case Types.NUMERIC:
283                 value = new Long JavaDoc(set.getLong(dbcol));
284                 break;
285             case Types.SMALLINT:
286                 value = new Short JavaDoc(set.getShort(dbcol));
287                 break;
288             case Types.TIME:
289                 value = set.getTime(dbcol);
290                 break;
291             case Types.TIMESTAMP:
292                 value = set.getTimestamp(dbcol);
293                 break;
294             case Types.ARRAY:
295                 value = set.getArray(dbcol);
296                 break;
297             case Types.BIT:
298                 value = new Integer JavaDoc(set.getInt(dbcol));
299                 break;
300             case Types.CHAR:
301                 value = new Integer JavaDoc(set.getInt(dbcol));
302                 break;
303         case Types.STRUCT:
304         value = set.getObject(dbcol);
305         break;
306         case Types.OTHER:
307         value = set.getObject(dbcol);
308         break;
309
310             default:
311                 // The blob types have to be requested separately, via a Reader.
312
value = "";
313                 break;
314         }
315
316         setRequestAttribute(request,attribute,value);
317
318         return value;
319     }
320
321     /**
322      * Set the Statement column so that the results are mapped correctly.
323      * The name of the parameter is retrieved from the configuration object.
324      *
325      * @param statement the prepared statement
326      * @param position the position of the column
327      * @param request the request
328      * @param entry the configuration object
329      */

330     protected void setColumn(PreparedStatement JavaDoc statement, int position, Request request, Configuration entry)
331     throws Exception JavaDoc {
332         setColumn(statement,position,request,entry,entry.getAttribute("param",""));
333     }
334
335     /**
336      * Set the Statement column so that the results are mapped correctly. The
337      * value of the column is retrieved from the request object. If the
338      * named parameter exists in the request object's parameters, that value
339      * is used. Otherwise if the named parameter exists in the request object's
340      * attributes, that value is used. Otherwise the request object is
341      * retrieved using Request.get(attribute), which is documented to be the
342      * same as Request.getAttribute(attribute), so something weird must be
343      * going on.
344      *
345      * @param statement the prepared statement
346      * @param position the position of the column
347      * @param request the request
348      * @param entry the configuration object
349      * @param param the name of the request parameter
350      */

351     protected void setColumn(PreparedStatement JavaDoc statement, int position, Request request, Configuration entry, String JavaDoc param)
352     throws Exception JavaDoc {
353         Object JavaDoc value = request.getParameter(param);
354         if (value == null) value = request.getAttribute(param);
355         if (value == null) value = request.get(param);
356         setColumn(statement,position,request,entry,param,value);
357     }
358
359     /**
360      * Set the Statement column so that the results are mapped correctly.
361      *
362      * @param statement the prepared statement
363      * @param position the position of the column
364      * @param request the request
365      * @param entry the configuration object
366      * @param param the name of the request parameter
367      * @param value the value of the column
368      */

369     protected void setColumn(PreparedStatement JavaDoc statement, int position, Request request, Configuration entry, String JavaDoc param, Object JavaDoc value) throws Exception JavaDoc {
370         setColumn(statement,position,request,entry,param,value,0);
371     }
372
373     /**
374      * Set the Statement column so that the results are mapped correctly.
375      *
376      * @param statement the prepared statement
377      * @param position the position of the column
378      * @param request the request
379      * @param entry the configuration object
380      * @param param the name of the request parameter
381      * @param value the value of the column
382      * @param rowIndex the index of the current row for manyrows inserts
383      */

384     protected void setColumn(PreparedStatement JavaDoc statement, int position, Request request, Configuration entry, String JavaDoc param, Object JavaDoc value, int rowIndex) throws Exception JavaDoc {
385         getLogger().debug("Setting column "+position+" named "+param+" with value "+value);
386         if (value instanceof String JavaDoc) {
387             value = ((String JavaDoc) value).trim();
388         }
389         String JavaDoc typeName = entry.getAttribute("type");
390         Integer JavaDoc typeObject = (Integer JavaDoc) AbstractDatabaseAction.typeConstants.get(typeName);
391         if (typeObject == null) {
392             throw new SQLException JavaDoc("Can't set column because the type "+typeName+" is unrecognized");
393         }
394         if (value == null) {
395             /** If the value is null, set the column value null and return **/
396             if (typeName.equals("image-width") || typeName.equals("image-height") || typeName.equals("image-size") || typeName.equals("row-index") || typeName.equals("image-mime-type")) {
397               /** these column types are automatically generated so it's ok **/
398             } else {
399               statement.setNull(position, typeObject.intValue());
400               return;
401             }
402         }
403         if ("".equals(value)) {
404             switch (typeObject.intValue()) {
405                 case Types.CHAR:
406                 case Types.CLOB:
407                 case Types.VARCHAR:
408                     /** If the value is an empty string and the column is
409                         a string type, we can continue **/

410                     break;
411                 case Types.INTEGER:
412                   if (typeName.equals("image-width") || typeName.equals("image-height") || typeName.equals("image-size") || typeName.equals("row-index")) {
413                     /** again, these types are okay to be absent **/
414                     break;
415                   }
416                 default:
417                     /** If the value is an empty string and the column
418                         is something else, we treat it as a null value **/

419                     statement.setNull(position, typeObject.intValue());
420                     return;
421             }
422         }
423
424         /** Store the column value in the request attribute
425             keyed by the request parameter name. we do this so possible future
426             actions can access this data. not sure about the key tho... **/

427         setRequestAttribute(request,param,value);
428         File JavaDoc file;
429
430         switch (typeObject.intValue()) {
431             case Types.CLOB:
432                 int length = -1;
433                 InputStream JavaDoc asciiStream = null;
434
435                 if (value instanceof File JavaDoc) {
436                     File JavaDoc asciiFile = (File JavaDoc) value;
437                     asciiStream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(asciiFile));
438                     length = (int) asciiFile.length();
439                 } else {
440                     String JavaDoc asciiText = (String JavaDoc) value;
441                     asciiStream = new BufferedInputStream JavaDoc(new ByteArrayInputStream JavaDoc(asciiText.getBytes()));
442                     length = asciiText.length();
443                 }
444
445                 statement.setAsciiStream(position, asciiStream, length);
446                 break;
447             case Types.BIGINT:
448                 BigDecimal JavaDoc bd = null;
449
450                 if (value instanceof BigDecimal JavaDoc) {
451                     bd = (BigDecimal JavaDoc) value;
452                 } else {
453                     bd = new BigDecimal JavaDoc((String JavaDoc) value);
454                 }
455
456                 statement.setBigDecimal(position, bd);
457                 break;
458             case Types.TINYINT:
459                 Byte JavaDoc b = null;
460
461                 if (value instanceof Byte JavaDoc) {
462                     b = (Byte JavaDoc) value;
463                 } else {
464                     b = new Byte JavaDoc((String JavaDoc) value);
465                 }
466
467                 statement.setByte(position, b.byteValue());
468                 break;
469             case Types.DATE:
470                 Date JavaDoc d = null;
471
472                 if (value instanceof Date JavaDoc) {
473                     d = (Date JavaDoc) value;
474                 } else if (value instanceof java.util.Date JavaDoc) {
475                     d = new Date JavaDoc(((java.util.Date JavaDoc) value).getTime());
476                 } else {
477                     d = new Date JavaDoc(this.dateValue((String JavaDoc) value, entry.getAttribute("format", "M/d/yyyy")));
478                 }
479
480                 statement.setDate(position, d);
481                 break;
482             case Types.DOUBLE:
483                 Double JavaDoc db = null;
484
485                 if (value instanceof Double JavaDoc) {
486                     db = (Double JavaDoc) value;
487                 } else {
488                     db = new Double JavaDoc((String JavaDoc) value);
489                 }
490
491                 statement.setDouble(position, db.doubleValue());
492                 break;
493             case Types.FLOAT:
494                 Float JavaDoc f = null;
495
496                 if (value instanceof Float JavaDoc) {
497                     f = (Float JavaDoc) value;
498                 } else {
499                     f = new Float JavaDoc((String JavaDoc) value);
500                 }
501
502                 statement.setFloat(position, f.floatValue());
503                 break;
504             case Types.NUMERIC:
505                 Long JavaDoc l = null;
506
507                 if (value instanceof Long JavaDoc) {
508                     l = (Long JavaDoc) value;
509                 } else {
510                     l = new Long JavaDoc((String JavaDoc) value);
511                 }
512
513                 statement.setLong(position, l.longValue());
514                 break;
515             case Types.SMALLINT:
516                 Short JavaDoc s = null;
517
518                 if (value instanceof Short JavaDoc) {
519                     s = (Short JavaDoc) value;
520                 } else {
521                     s = new Short JavaDoc((String JavaDoc) value);
522                 }
523
524                 statement.setShort(position, s.shortValue());
525                 break;
526             case Types.TIME:
527                 Time JavaDoc t = null;
528
529                 if (value instanceof Time JavaDoc) {
530                     t = (Time JavaDoc) value;
531                 } else {
532                     t = new Time JavaDoc(this.dateValue((String JavaDoc) value, entry.getAttribute("format", "h:m:s a")));
533                 }
534
535                 statement.setTime(position, t);
536                 break;
537             case Types.TIMESTAMP:
538                 Timestamp JavaDoc ts = null;
539
540                 if (value instanceof Time JavaDoc) {
541                     ts = (Timestamp JavaDoc) value;
542                 } else {
543                     ts = new Timestamp JavaDoc(this.dateValue((String JavaDoc) value, entry.getAttribute("format", "M/d/yyyy h:m:s a")));
544                 }
545
546                 statement.setTimestamp(position, ts);
547                 break;
548             case Types.ARRAY:
549                 statement.setArray(position, (Array JavaDoc) value); // no way to convert string to array
550
break;
551             case Types.STRUCT:
552             case Types.OTHER:
553                 statement.setObject(position, value);
554                 break;
555             case Types.LONGVARBINARY:
556                 statement.setTimestamp(position, new Timestamp JavaDoc((new java.util.Date JavaDoc()).getTime()));
557                 break;
558             case Types.VARCHAR:
559                 if ("string".equals(typeName)) {
560                    statement.setString(position, (String JavaDoc) value);
561                    break;
562                 } else if ("image-mime-type".equals(typeName)) {
563                     String JavaDoc imageAttr = param.substring(0, (param.length() - "-mime-type".length()));
564                     file = (File JavaDoc) request.get(imageAttr);
565                     synchronized (this.files) {
566                         Parameters parameters = (Parameters) this.files.get(file);
567                         String JavaDoc imageMimeType = parameters.getParameter("image-mime-type",
568                                                                        (String JavaDoc) settings.get("image-mime-type",""));
569                         statement.setString(position, imageMimeType);
570                         /** Store the image mime type in the request attributes.
571                             Why do we do this? **/

572                         setRequestAttribute(request, param, imageMimeType);
573                     }
574                     break;
575                 }
576             case Types.BLOB:
577                 if (value instanceof File JavaDoc) {
578                     file = (File JavaDoc)value;
579                 } else if (value instanceof String JavaDoc) {
580                     file = new File JavaDoc((String JavaDoc)value);
581                 } else {
582                     throw new SQLException JavaDoc("Invalid type for blob: "+value.getClass().getName());
583                 }
584                 //InputStream input = new BufferedInputStream(new FileInputStream(file));
585
FileInputStream JavaDoc input = new FileInputStream JavaDoc(file);
586                 statement.setBinaryStream(position, input, (int)file.length());
587                 if ("image".equals(typeName)) {
588                     /** If this column type is an image, store the
589                         size, width, and height in a static table **/

590                     Parameters parameters = new Parameters();
591                     parameters.setParameter("image-size", Long.toString(file.length()));
592                     ImageProperties prop = ImageUtils.getImageProperties(file);
593                     parameters.setParameter("image-width", Integer.toString(prop.width));
594                     parameters.setParameter("image-height", Integer.toString(prop.height));
595                     // TC: if it's really mime-type shouldn't we prepend "image/"?
596
parameters.setParameter("image-mime-type",prop.type);
597                     synchronized (this.files) {
598                         this.files.put(file, parameters);
599                     }
600                 }
601                 break;
602             case Types.INTEGER:
603                 if ("int".equals(typeName)) {
604                     Integer JavaDoc i = null;
605                     if (value instanceof Integer JavaDoc) {
606                         i = (Integer JavaDoc) value;
607                     } else {
608                         i = new Integer JavaDoc((String JavaDoc) value);
609                     }
610                     statement.setInt(position, i.intValue());
611                     break;
612                 } else if ("image-width".equals(typeName)) {
613                     /** Get the image width from the cached image data **/
614                     /** Is this why we store the values in the request
615                         attributes? **/

616                     String JavaDoc imageAttr = param.substring(0, (param.length() - "-width".length()));
617                     file = (File JavaDoc) request.get(imageAttr);
618                     synchronized (this.files) {
619                         Parameters parameters = (Parameters) this.files.get(file);
620                         statement.setInt(position, parameters.getParameterAsInteger("image-width",
621                          Integer.parseInt((String JavaDoc)settings.get("image-width","-1"))));
622                         /** Store the image width in the request attributes.
623                             Why do we do this? **/

624                         setRequestAttribute(request,
625                         param,
626                         parameters.getParameter("image-width",
627                                     (String JavaDoc) settings.get("image-width","")));
628                     }
629                     break;
630                 } else if ("image-height".equals(typeName)) {
631                     /** Get the image height from the cached image data **/
632                     String JavaDoc imageAttr = param.substring(0, (param.length() - "-height".length()));
633                     file = (File JavaDoc) request.get(imageAttr);
634                     synchronized (this.files) {
635                         Parameters parameters = (Parameters) this.files.get(file);
636                         statement.setInt(position, parameters.getParameterAsInteger("image-height",
637                          Integer.parseInt((String JavaDoc)settings.get("image-height","-1"))));
638                         setRequestAttribute(request,
639                         param,
640                         parameters.getParameter("image-height",
641                                     (String JavaDoc) settings.get("image-height","")));
642                     }
643                     break;
644                 } else if ("image-size".equals(typeName)) {
645                     /** Get the image file size from the cached image data **/
646                     String JavaDoc imageAttr = param.substring(0, (param.length() - "-size".length()));
647                     file = (File JavaDoc) request.get(imageAttr);
648                     synchronized (this.files) {
649                         Parameters parameters = (Parameters) this.files.get(file);
650                         statement.setInt(position, parameters.getParameterAsInteger("image-size",
651                          Integer.parseInt((String JavaDoc)settings.get("image-height","-1"))));
652                         setRequestAttribute(request,
653                         param,
654                         parameters.getParameter("image-size",
655                                     (String JavaDoc) settings.get("image-size","")));
656                     }
657                     break;
658                 } else if ("row-index".equals(typeName)) {
659                     statement.setInt(position,rowIndex);
660                     break;
661                 }
662             default:
663                 throw new SQLException JavaDoc("Impossible exception - invalid type "+typeName);
664         }
665     }
666
667     /**
668      * Convert a String to a long value.
669      */

670     private final long dateValue(String JavaDoc value, String JavaDoc format) throws Exception JavaDoc {
671         DateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(format);
672         return formatter.parse(value).getTime();
673     }
674
675     /**
676      * dispose
677      */

678     public void dispose() {
679         this.manager.release(dbselector);
680     }
681
682     /**
683      * Store a key/value pair in the request attributes. We prefix the key
684      * with the name of this class to prevent potential name collisions.
685      */

686     protected void setRequestAttribute(Request request, String JavaDoc key, Object JavaDoc value) {
687       request.setAttribute("org.apache.cocoon.acting.AbstractDatabaseAction:"+key,value);
688     }
689
690     /**
691      * Retreive a value from the request attributes.
692      */

693     protected Object JavaDoc getRequestAttribute(Request request, String JavaDoc key) {
694       return request.getAttribute("org.apache.cocoon.acting.AbstractDatabaseAction:"+key);
695     }
696
697     /**
698      * Build a separed list with the Values of a Configuration Array
699      * @param values - build the list from
700      * @param separator - Put a separator between the values of the list
701      * @return - an StringBuffer with the builded List
702      * @throws ConfigurationException
703      */

704     protected StringBuffer JavaDoc buildList(Configuration[] values, String JavaDoc separator) throws ConfigurationException {
705         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
706         for (int i = 0; i < values.length; i++) {
707             if (i > 0) {
708                 buffer.append(separator);
709             }
710             buffer.append(values[i].getAttribute("dbcol"));
711             buffer.append(" = ?");
712         }
713         return buffer;
714     }
715
716     /**
717      * Build a separed list with the Values of a Configuration Array
718      * @param values - build the list from
719      * @param begin - Initial index
720      * @return - an StringBuffer with the builded List
721      * @throws ConfigurationException
722      */

723     protected StringBuffer JavaDoc buildList(Configuration[] values, int begin) throws ConfigurationException {
724         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
725         int length = values.length;
726         boolean prependComma = begin > 0;
727         for (int i = 0; i < length; i++) {
728             if (prependComma) {
729                 buffer.append(", ");
730             } else {
731                 prependComma = true;
732             }
733             buffer.append(values[i].getAttribute("dbcol"));
734         }
735         return buffer;
736     }
737 }
738
Popular Tags