Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/jcorporate/expresso/services/controller/dbmaint/UpdateBase.java


1   /* ====================================================================
2    * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3    *
4    * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in
15   *    the documentation and/or other materials provided with the
16   *    distribution.
17   *
18   * 3. The end-user documentation included with the redistribution,
19   *    if any, must include the following acknowledgment:
20   *       "This product includes software developed by Jcorporate Ltd.
21   *        (http://www.jcorporate.com/)."
22   *    Alternately, this acknowledgment may appear in the software itself,
23   *    if and wherever such third-party acknowledgments normally appear.
24   *
25   * 4. "Jcorporate" and product names such as "Expresso" must
26   *    not be used to endorse or promote products derived from this
27   *    software without prior written permission. For written permission,
28   *    please contact info@jcorporate.com.
29   *
30   * 5. Products derived from this software may not be called "Expresso",
31   *    or other Jcorporate product names; nor may "Expresso" or other
32   *    Jcorporate product names appear in their name, without prior
33   *    written permission of Jcorporate Ltd.
34   *
35   * 6. No product derived from this software may compete in the same
36   *    market space, i.e. framework, without prior written permission
37   *    of Jcorporate Ltd. For written permission, please contact
38   *    partners@jcorporate.com.
39   *
40   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43   * DISCLAIMED.  IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51   * SUCH DAMAGE.
52   * ====================================================================
53   *
54   * This software consists of voluntary contributions made by many
55   * individuals on behalf of the Jcorporate Ltd. Contributions back
56   * to the project(s) are encouraged when you make modifications.
57   * Please send them to support@jcorporate.com. For more information
58   * on Jcorporate Ltd. and its products, please see
59   * <http://www.jcorporate.com/>.
60   *
61   * Portions of this software are based upon other open source
62   * products and are subject to their respective licenses.
63   */
64  
65  package com.jcorporate.expresso.services.controller.dbmaint;
66  
67  import com.jcorporate.expresso.core.controller.Block;
68  import com.jcorporate.expresso.core.controller.ControllerException;
69  import com.jcorporate.expresso.core.controller.Output;
70  import com.jcorporate.expresso.core.dataobjects.DataField;
71  import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData;
72  import com.jcorporate.expresso.core.dataobjects.DataObject;
73  import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData;
74  import com.jcorporate.expresso.core.dataobjects.NestableDataObject;
75  import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCDataObject;
76  import com.jcorporate.expresso.core.dataobjects.jdbc.LobField;
77  import com.jcorporate.expresso.core.db.DBException;
78  import com.jcorporate.expresso.core.dbobj.ValidValue;
79  import com.jcorporate.expresso.services.dbobj.MediaDBObject;
80  import org.apache.log4j.Logger;
81  
82  import java.util.Iterator;
83  
84  
85  /**
86   * Base Class for posting add, delete and updating operations. This
87   * functionality was previously carried out in the DBMaint.doPost().
88   * Now extended by ADDsave, UPDATEsave and UPDATEdelete
89   * method.
90   *
91   * @author Michael Nash, contributions by Kevin King
92   * @version $Revision: 1.16 $  $Date: 2004/11/17 20:48:18 $
93   */
94  public abstract class UpdateBase
95          extends DynamicCmd {
96  
97      private static Logger log = Logger.getLogger(UpdateBase.class);
98  
99      public UpdateBase() {
100     }
101 
102     /**
103      * Constructor
104      *
105      * @param code    The name of the state.
106      * @param descrip The friendly name of the state
107      */
108     public UpdateBase(String code, String descrip) {
109         super(code, descrip);
110     } /* UpdateBase(String, String) */
111 
112     /**
113      * Display a table showing the key fields of this object and their
114      * values. Used to confirm add/delete/update operations to the user
115      *
116      * @param withEditLink true if you want the edit link displayed for the key
117      * @throws DBException If it is not possible to determine the keys
118      *                     or their values
119      */
120     protected void showKey(boolean withEditLink)
121             throws DBException, ControllerException {
122         DataObject myDBObj = this.getDataObject();
123         DataObjectMetaData metadata = myDBObj.getMetaData();
124         String controller = getControllerName();
125 
126         Block keyTable = new Block("key");
127         addBlock(keyTable);
128         keyTable.setAttribute("table", "Y");
129 
130         String oneKeyFieldName = null;
131 
132         for (Iterator e = myDBObj.getMetaData().getKeyFieldListArray().iterator(); e.hasNext();) {
133             oneKeyFieldName = (String) e.next();
134             DataFieldMetaData fieldMetadata = myDBObj.getFieldMetaData(oneKeyFieldName);
135 
136             Block oneRow = new Block("oneRow");
137             oneRow.setAttribute("row", "Y");
138             keyTable.add(oneRow);
139             oneRow.add(new Output("FieldDescrip", metadata.getDescription(this
140                     .getControllerRequest().getLocale(), oneKeyFieldName)));
141 //                                  fieldMetadata.getDescription()));
142 
143             if (fieldMetadata.isMultiValued()) {
144                 java.util.List values = myDBObj.getValidValuesList(oneKeyFieldName);
145 
146                 if (values == null) {
147                     throw new DBException("Valid values for field " +
148                             oneKeyFieldName + " from object " +
149                             fieldMetadata.getName() + " were null");
150                 }
151 
152                 String fieldValue = null;
153                 ValidValue oneVV = null;
154 
155                 for (Iterator ve = values.iterator(); ve.hasNext();) {
156                     oneVV = (ValidValue) ve.next();
157 
158                     if (oneVV.getValue().equals(myDBObj.getDataField(oneKeyFieldName).asString())) {
159                         fieldValue = oneVV.getDescription();
160                     }
161                 }
162                 if (fieldValue == null) {
163                     Output fieldOutput = new Output("FieldValue",
164                             myDBObj.getDataField(oneKeyFieldName).asString());
165 
166                     if (withEditLink) {
167                         showEditLink(oneKeyFieldName,
168                                 myDBObj.getDataField(oneKeyFieldName).asString(),
169                                 myDBObj, fieldOutput, controller);
170                     }
171 
172                     oneRow.add(fieldOutput);
173                 } else {
174                     Output fieldOutput = new Output("FieldValue", fieldValue);
175 
176                     if (withEditLink) {
177                         showEditLink(oneKeyFieldName, fieldValue, myDBObj,
178                                 fieldOutput, controller);
179                     }
180 
181                     oneRow.add(fieldOutput);
182                 }
183             } else { /* if field is multi_valued */
184                 Output fieldOutput = new Output("FieldValue",
185                         myDBObj.getDataField(oneKeyFieldName).asString());
186 
187                 if (withEditLink) {
188                     showEditLink(oneKeyFieldName,
189                             myDBObj.getDataField(oneKeyFieldName).asString(), myDBObj,
190                             fieldOutput, controller);
191                 }
192 
193                 oneRow.add(fieldOutput);
194             }
195         } /* each key field */
196 
197     } /* showKey() */
198 
199     /**
200      * Saves updated BLOB fields to the database table if there are any.  The
201      * way to tell if the field needs updating is that the BLOB field attribute
202      * fileName equals something, which points to a new file that has been uploaded
203      * [As parsed and set by DefaultAutoElement]
204      *
205      * @param myDBObj The current DBObject to update/add  For adding records, the
206      *                DBObject should already be added by the time this method is called.
207      * @see com.jcorporate.expresso.services.controller.ui.DefaultAutoElement#parseSingleInput
208      */
209     protected void saveBlobFields(DataObject myDBObj) throws DBException {
210 
211 
212         //We can't deal with anything but JDBC data objects
213         if (!(myDBObj instanceof JDBCDataObject || myDBObj instanceof NestableDataObject)) {
214             return;
215         }
216 
217 
218 
219         //
220         //Check for BLOB fields.  If they have a 'fileName' attribute set
221         //for any of the fields that are Binary types, then separately save
222         //that data to the database
223         //
224         for (Iterator i = myDBObj.getMetaData().getFieldListArray().iterator(); i.hasNext();) {
225             String nextField = (String) i.next();
226             DataFieldMetaData metadata = myDBObj.getFieldMetaData(nextField);
227 
228             if (metadata.isBinaryObjectType()) {
229                 DataField field = myDBObj.getDataField(nextField);
230                 String uploadedFileName = (String) field.getAttribute("fileName");
231 
232                 if (uploadedFileName != null && uploadedFileName.length() > 0) {
233                     DataObject daoToUse = myDBObj;
234                     if (daoToUse instanceof NestableDataObject) {
235                         daoToUse = ((NestableDataObject) myDBObj).getNestedFromFieldName(nextField);
236                         if (!(daoToUse instanceof JDBCDataObject)) {
237                             log.warn("Nested DataObject is not of type JDBCDataObject. Skipping update of file name");
238                             continue;
239                         }
240 
241                         nextField = ((NestableDataObject) myDBObj).getFieldFromNestedName(nextField);
242                         field = daoToUse.getDataField(nextField);
243                     }
244 
245 
246                     if (daoToUse instanceof MediaDBObject) {
247                         MediaDBObject mediaObj = (MediaDBObject) daoToUse;
248                         mediaObj.saveBlob(nextField);
249                     } else {
250                         java.io.File f = new java.io.File(uploadedFileName);
251                         if (f == null) {
252                             log.error("File: " + uploadedFileName + " doesn't appear to exist.");
253                             throw new DBException("Unable to retrieve uploaded file!");
254                         }
255 
256                         saveBlob(nextField, f, (JDBCDataObject) daoToUse);
257 
258                     }
259                 }
260             }
261         }  /* End For Iterator */
262     }
263 
264     /**
265      * Saves a BLOB field to the database
266      *
267      * @param fieldName the field Name to save
268      * @param value     A java.io.File object representing the file to send to the
269      *                  database.
270      * @param criteria  The current DBObject we are operating on.
271      */
272     protected void saveBlob(String fieldName,
273                             java.io.File value,
274                             JDBCDataObject criteria) throws DBException {
275 
276         int fileSize = (int) value.length();
277         java.io.InputStream is;
278         try {
279             is = new java.io.FileInputStream(value);
280         } catch (java.io.FileNotFoundException ex) {
281             log.error("File: " + value.getName() + " doesn't appear to exist.", ex);
282             throw new DBException("Unable to retrieve uploaded file!", ex);
283         }
284 
285         LobField lf = new LobField();
286 
287         lf.setCriteria(criteria);
288         try {
289             lf.saveBlob(fieldName, is, fileSize);
290         } finally {
291             lf.close();
292         }
293     }
294 
295 
296 }
297 
298 /* UpdateBase */