001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.services.controller.dbmaint;
066:
067: import com.jcorporate.expresso.core.controller.Block;
068: import com.jcorporate.expresso.core.controller.ControllerException;
069: import com.jcorporate.expresso.core.controller.Output;
070: import com.jcorporate.expresso.core.dataobjects.DataField;
071: import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData;
072: import com.jcorporate.expresso.core.dataobjects.DataObject;
073: import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData;
074: import com.jcorporate.expresso.core.dataobjects.NestableDataObject;
075: import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCDataObject;
076: import com.jcorporate.expresso.core.dataobjects.jdbc.LobField;
077: import com.jcorporate.expresso.core.db.DBException;
078: import com.jcorporate.expresso.core.dbobj.ValidValue;
079: import com.jcorporate.expresso.services.dbobj.MediaDBObject;
080: import org.apache.log4j.Logger;
081:
082: import java.util.Iterator;
083:
084: /**
085: * Base Class for posting add, delete and updating operations. This
086: * functionality was previously carried out in the DBMaint.doPost().
087: * Now extended by ADDsave, UPDATEsave and UPDATEdelete
088: * method.
089: *
090: * @author Michael Nash, contributions by Kevin King
091: * @version $Revision: 1.16 $ $Date: 2004/11/17 20:48:18 $
092: */
093: public abstract class UpdateBase extends DynamicCmd {
094:
095: private static Logger log = Logger.getLogger(UpdateBase.class);
096:
097: public UpdateBase() {
098: }
099:
100: /**
101: * Constructor
102: *
103: * @param code The name of the state.
104: * @param descrip The friendly name of the state
105: */
106: public UpdateBase(String code, String descrip) {
107: super (code, descrip);
108: } /* UpdateBase(String, String) */
109:
110: /**
111: * Display a table showing the key fields of this object and their
112: * values. Used to confirm add/delete/update operations to the user
113: *
114: * @param withEditLink true if you want the edit link displayed for the key
115: * @throws DBException If it is not possible to determine the keys
116: * or their values
117: */
118: protected void showKey(boolean withEditLink) throws DBException,
119: ControllerException {
120: DataObject myDBObj = this .getDataObject();
121: DataObjectMetaData metadata = myDBObj.getMetaData();
122: String controller = getControllerName();
123:
124: Block keyTable = new Block("key");
125: addBlock(keyTable);
126: keyTable.setAttribute("table", "Y");
127:
128: String oneKeyFieldName = null;
129:
130: for (Iterator e = myDBObj.getMetaData().getKeyFieldListArray()
131: .iterator(); e.hasNext();) {
132: oneKeyFieldName = (String) e.next();
133: DataFieldMetaData fieldMetadata = myDBObj
134: .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
140: .getDescription(this .getControllerRequest()
141: .getLocale(), oneKeyFieldName)));
142: // fieldMetadata.getDescription()));
143:
144: if (fieldMetadata.isMultiValued()) {
145: java.util.List values = myDBObj
146: .getValidValuesList(oneKeyFieldName);
147:
148: if (values == null) {
149: throw new DBException("Valid values for field "
150: + oneKeyFieldName + " from object "
151: + fieldMetadata.getName() + " were null");
152: }
153:
154: String fieldValue = null;
155: ValidValue oneVV = null;
156:
157: for (Iterator ve = values.iterator(); ve.hasNext();) {
158: oneVV = (ValidValue) ve.next();
159:
160: if (oneVV.getValue().equals(
161: myDBObj.getDataField(oneKeyFieldName)
162: .asString())) {
163: fieldValue = oneVV.getDescription();
164: }
165: }
166: if (fieldValue == null) {
167: Output fieldOutput = new Output("FieldValue",
168: myDBObj.getDataField(oneKeyFieldName)
169: .asString());
170:
171: if (withEditLink) {
172: showEditLink(oneKeyFieldName, myDBObj
173: .getDataField(oneKeyFieldName)
174: .asString(), myDBObj, fieldOutput,
175: controller);
176: }
177:
178: oneRow.add(fieldOutput);
179: } else {
180: Output fieldOutput = new Output("FieldValue",
181: fieldValue);
182:
183: if (withEditLink) {
184: showEditLink(oneKeyFieldName, fieldValue,
185: myDBObj, fieldOutput, controller);
186: }
187:
188: oneRow.add(fieldOutput);
189: }
190: } else { /* if field is multi_valued */
191: Output fieldOutput = new Output("FieldValue", myDBObj
192: .getDataField(oneKeyFieldName).asString());
193:
194: if (withEditLink) {
195: showEditLink(oneKeyFieldName, myDBObj.getDataField(
196: oneKeyFieldName).asString(), myDBObj,
197: fieldOutput, controller);
198: }
199:
200: oneRow.add(fieldOutput);
201: }
202: } /* each key field */
203:
204: } /* showKey() */
205:
206: /**
207: * Saves updated BLOB fields to the database table if there are any. The
208: * way to tell if the field needs updating is that the BLOB field attribute
209: * fileName equals something, which points to a new file that has been uploaded
210: * [As parsed and set by DefaultAutoElement]
211: *
212: * @param myDBObj The current DBObject to update/add For adding records, the
213: * DBObject should already be added by the time this method is called.
214: * @see com.jcorporate.expresso.services.controller.ui.DefaultAutoElement#parseSingleInput
215: */
216: protected void saveBlobFields(DataObject myDBObj)
217: throws DBException {
218:
219: //We can't deal with anything but JDBC data objects
220: if (!(myDBObj instanceof JDBCDataObject || myDBObj instanceof NestableDataObject)) {
221: return;
222: }
223:
224: //
225: //Check for BLOB fields. If they have a 'fileName' attribute set
226: //for any of the fields that are Binary types, then separately save
227: //that data to the database
228: //
229: for (Iterator i = myDBObj.getMetaData().getFieldListArray()
230: .iterator(); i.hasNext();) {
231: String nextField = (String) i.next();
232: DataFieldMetaData metadata = myDBObj
233: .getFieldMetaData(nextField);
234:
235: if (metadata.isBinaryObjectType()) {
236: DataField field = myDBObj.getDataField(nextField);
237: String uploadedFileName = (String) field
238: .getAttribute("fileName");
239:
240: if (uploadedFileName != null
241: && uploadedFileName.length() > 0) {
242: DataObject daoToUse = myDBObj;
243: if (daoToUse instanceof NestableDataObject) {
244: daoToUse = ((NestableDataObject) myDBObj)
245: .getNestedFromFieldName(nextField);
246: if (!(daoToUse instanceof JDBCDataObject)) {
247: log
248: .warn("Nested DataObject is not of type JDBCDataObject. Skipping update of file name");
249: continue;
250: }
251:
252: nextField = ((NestableDataObject) myDBObj)
253: .getFieldFromNestedName(nextField);
254: field = daoToUse.getDataField(nextField);
255: }
256:
257: if (daoToUse instanceof MediaDBObject) {
258: MediaDBObject mediaObj = (MediaDBObject) daoToUse;
259: mediaObj.saveBlob(nextField);
260: } else {
261: java.io.File f = new java.io.File(
262: uploadedFileName);
263: if (f == null) {
264: log.error("File: " + uploadedFileName
265: + " doesn't appear to exist.");
266: throw new DBException(
267: "Unable to retrieve uploaded file!");
268: }
269:
270: saveBlob(nextField, f,
271: (JDBCDataObject) daoToUse);
272:
273: }
274: }
275: }
276: } /* End For Iterator */
277: }
278:
279: /**
280: * Saves a BLOB field to the database
281: *
282: * @param fieldName the field Name to save
283: * @param value A java.io.File object representing the file to send to the
284: * database.
285: * @param criteria The current DBObject we are operating on.
286: */
287: protected void saveBlob(String fieldName, java.io.File value,
288: JDBCDataObject criteria) throws DBException {
289:
290: int fileSize = (int) value.length();
291: java.io.InputStream is;
292: try {
293: is = new java.io.FileInputStream(value);
294: } catch (java.io.FileNotFoundException ex) {
295: log.error("File: " + value.getName()
296: + " doesn't appear to exist.", ex);
297: throw new DBException("Unable to retrieve uploaded file!",
298: ex);
299: }
300:
301: LobField lf = new LobField();
302:
303: lf.setCriteria(criteria);
304: try {
305: lf.saveBlob(fieldName, is, fileSize);
306: } finally {
307: lf.close();
308: }
309: }
310:
311: }
312:
313: /* UpdateBase */
|