KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > controller > dbmaint > Update


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.Controller;
69 import com.jcorporate.expresso.core.controller.ControllerException;
70 import com.jcorporate.expresso.core.controller.ControllerRequest;
71 import com.jcorporate.expresso.core.controller.ControllerResponse;
72 import com.jcorporate.expresso.core.controller.Input;
73 import com.jcorporate.expresso.core.controller.NonHandleableException;
74 import com.jcorporate.expresso.core.controller.Transition;
75 import com.jcorporate.expresso.core.controller.session.PersistentSession;
76 import com.jcorporate.expresso.core.dataobjects.DataObject;
77 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData;
78 import com.jcorporate.expresso.core.dataobjects.Defineable;
79 import com.jcorporate.expresso.core.dataobjects.Securable;
80 import com.jcorporate.expresso.core.db.DBException;
81 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
82 import com.jcorporate.expresso.core.misc.StringUtil;
83 import com.jcorporate.expresso.core.misc.URLUTF8Encoder;
84 import com.jcorporate.expresso.core.security.User;
85 import com.jcorporate.expresso.services.controller.ui.DefaultAutoElement;
86 import com.jcorporate.expresso.services.dbobj.Setup;
87
88 import java.util.Iterator JavaDoc;
89 import java.util.StringTokenizer JavaDoc;
90
91
92 /**
93  * Provides a standard form for user to update all fields
94  * (except primary key). Extend this class if only updating
95  * one or several but not all fields.
96  *
97  * @author Michael Nash, contributions by Kevin King
98  */

99 public class Update
100         extends GetBase {
101
102     public Update() {
103     }
104
105     /**
106      * Constructor
107      *
108      * @param code The name of the state.
109      * @param descrip The friendly name of the state
110      */

111     public Update(String JavaDoc code, String JavaDoc descrip) {
112         super(code, descrip);
113     } /* Update(String, String) */
114
115     /**
116      * Create an input for one field specified by the field name. This is
117      * sort of a callback function generated by the base class' autoForm function.
118      *
119      * @param oneFieldName The name of the field
120      * @throws DBException If a problem occurs getting field info from the
121      * database object
122      * @throws ControllerException upon other errors
123      * @see com.jcorporate.expresso.services.controller.dbmaint.GetBase
124      */

125     protected void autoField(String JavaDoc oneFieldName)
126             throws DBException, ControllerException {
127         DataObject myDBObj = this.getDataObject();
128         DataObjectMetaData metadata = myDBObj.getMetaData();
129
130         if (oneFieldName == null) {
131             throw new ControllerException("Field Name must not be null");
132         }
133         if (myDBObj == null) {
134             throw new DBException("Database object must be " +
135                     "initialized before calling autoField");
136         }
137
138         boolean readOnly = false;
139
140         /* if it's a key field, it's read-only */
141         if (isKeyField(oneFieldName)) {
142             readOnly = true;
143         }
144         /* Virtual fields, never get input... */
145         if (metadata.isVirtual(oneFieldName)) {
146             readOnly = true;
147         }
148         /* If the field is explicitly set to read only, then only offer */
149         /* input to it in search mode */
150         if (metadata.isReadOnly(oneFieldName)) {
151             readOnly = true;
152         }
153         String JavaDoc cachedValue = StringUtil.notNull(getFormCache(oneFieldName));
154
155         //
156
//Zero out cached value so that rendering of Mapped objects takes place
157
//
158
if (cachedValue != null && cachedValue.length() == 0) {
159             cachedValue = null;
160         }
161         /* Now, if there is a lookup object specified for the field */
162         Input oneField = DefaultAutoElement.getAutoControllerElement()
163                 .renderDBObjectField(this.getControllerResponse(),
164                         myDBObj,
165                         oneFieldName,
166                         cachedValue,
167                         readOnly);
168
169         if (oneField == null) {
170             return;
171         }
172
173         addInput(oneField);
174
175
176         /* Now, if there is a lookup object specified for the field */
177
178         /* add an icon for the user to search this object in a new window */
179         String JavaDoc lookupObjectName = StringUtil.notNull(metadata.getLookupObject(oneFieldName));
180
181         if (!lookupObjectName.equals("")) {
182             Transition lookup = new Transition();
183             lookup.setName("lookup");
184             lookup.addParam("dbobj", lookupObjectName);
185             lookup.addParam(Controller.STATE_PARAM_KEY, "Search");
186             lookup.setDescription("Look up Values");
187             oneField.addNested(lookup);
188         } /* if there was a lookup object */
189
190         //
191
//Added by Mike R. to properly support boolean datatypes and checkboxes
192
//
193
if (metadata.getAttribute(oneFieldName, "checkbox") != null) {
194             if (metadata.getType(oneFieldName).equals("boolean")) {
195                 Boolean JavaDoc boolValue = myDBObj.getDataField(oneFieldName).asBoolean();
196                 if (boolValue == null) {
197                     oneField.setDefaultValue("N");
198                 } else {
199                     boolean b = boolValue.booleanValue();
200                     oneField.setDefaultValue(b ? "Y" : "N");
201                 }
202             }
203         }
204
205
206         //
207
//If we have a BLOB then the Input will contain an icon to the MimeType
208
//Icon, and we add a Transition to view the blob.
209
//
210
if ("file".equalsIgnoreCase(oneField.getType()) &&
211                 myDBObj.getFieldMetaData(oneFieldName).isLongObjectType()) {
212
213             Transition t = new Transition("view", "View"
214                     , com.jcorporate.expresso.services.controller.DBMaint.class,
215                     "ViewBlob");
216             t.addParam("fieldName", oneFieldName);
217             t.addParam("dbobj", ((Object JavaDoc) myDBObj).getClass().getName());
218             this.showBlobViewLink(oneFieldName, myDBObj, oneField,
219                     this.getController().getClass().getName());
220         }
221     } /* autoField(String) */
222
223
224     /**
225      * Perform the actions of this state.
226      *
227      * @param req The <code>ControllerRequest</code> object
228      * @param res The <code>ControllerResponse</code> object
229      */

230     public void run(ControllerRequest req, ControllerResponse res)
231             throws NonHandleableException, ControllerException {
232         super.run(req, res);
233
234         DataObject myDBObj = this.getDataObject();
235         DataObjectMetaData metadata = myDBObj.getMetaData();
236
237         String JavaDoc allKeys = URLUTF8Encoder.decode(StringUtil.notNull(getParameter("key")));
238
239         if (allKeys.equals("")) {
240             throw new ControllerException("Update command requires key value string to follow it");
241         }
242         try {
243
244             myDBObj = this.retrieveMyDBObject();
245
246
247             showNext = false;
248             showPrev = false;
249             if (myDBObj instanceof Securable) {
250                 ((Securable) myDBObj).isAllowed("U");
251             } else {
252                 if (getUid() == SecuredDBObject.SYSTEM_ACCOUNT
253                         || User.getUserFromId(getUid(), this.getControllerRequest().getDataContext()).isAdmin()) {
254                     // all access ok
255
} else {
256                     String JavaDoc allowInsecure = Setup.getValue(req.getDataContext(),
257                             com.jcorporate.expresso.core.ExpressoSchema.class.getName(),
258                             "insecureDBMaint");
259                     if (!(StringUtil.toBoolean(allowInsecure))) {
260                         throw new SecurityException JavaDoc("Access to unsecured Objects not allowed");
261                     }
262                 }
263             }
264             showForm();
265
266             String JavaDoc keyParam = this.getKeyParameter(getDataObject());
267
268             Transition update = new Transition("UpdateUpdate", getController());
269             addParams(update);
270             update.addParam("key", keyParam);
271             add(update);
272
273             Transition delete = new Transition("UpdateDelete", getController());
274             addParams(delete);
275             delete.addParam("key", keyParam);
276             add(delete);
277
278             if (!StringUtil.notNull(getParameter("details")).equals("y")) {
279
280                 /* If we are not already the detail record of some other parent... */
281                 /* Now add a special block called "details". It contains */
282                 /* a transition for each of the detail db objects to our */
283                 /* current object */
284                 Block detBlock = new Block("details");
285
286                 try {
287                     Transition masterTrans = new Transition("Update",
288                             getController());
289                     String JavaDoc className = ((Object JavaDoc) myDBObj).getClass().getName();
290                     masterTrans.setName("master");
291                     detBlock.add(masterTrans);
292                     masterTrans.setLabel(metadata.getDescription(req.getLocale()));
293                     masterTrans.setAttribute("dbobj", className);
294                     masterTrans.addParam("dbobj", className);
295                     masterTrans.addParam(Controller.STATE_PARAM_KEY, "Update");
296                     masterTrans.addParam("key",
297                             URLUTF8Encoder.encode(keyParam));
298                     masterTrans.addParam("fields",
299                             URLUTF8Encoder.encode(getFieldsParam()));
300                     masterTrans.addParam("search",
301                             URLUTF8Encoder.encode(getSearchParam()));
302
303                     if (myDBObj instanceof com.jcorporate.expresso.core.dataobjects.Defineable) {
304                         masterTrans.addParam("definition", ((Defineable) myDBObj).getDefinitionName());
305                     }
306
307                     String JavaDoc oneDet = null;
308
309                     for (Iterator JavaDoc ee = metadata.getDetailSet().iterator();
310                          ee.hasNext();) {
311                         oneDet = (String JavaDoc) ee.next();
312
313                         DataObject detDBObj = null;
314
315                         try {
316                             detDBObj = (DataObject) Class.forName(oneDet).newInstance();
317                             detDBObj.setLocale(getControllerRequest().getLocale());
318                         } catch (Exception JavaDoc e) {
319                             throw new ControllerException("Unable to instantiate " +
320                                     "detail db object '" + oneDet + "'", e);
321                         }
322
323                         Transition oneTrans = new Transition("SearchList",
324                                 getController());
325                         detBlock.add(oneTrans);
326                         oneTrans.setLabel(detDBObj.getMetaData().getDescription(req.getLocale()));
327                         oneTrans.setAttribute("dbobj",
328                                 ((Object JavaDoc) detDBObj).getClass().getName());
329                         oneTrans.addParam("dbobj", oneDet);
330                         oneTrans.addParam(Controller.STATE_PARAM_KEY, "SearchList");
331                         oneTrans.addParam("details", "y");
332
333                         StringBuffer JavaDoc fieldsString = new StringBuffer JavaDoc();
334                         String JavaDoc localFields = metadata.getDetailFieldsLocal(oneDet);
335                         String JavaDoc foreignFields = metadata.getDetailFieldsForeign(oneDet);
336                         StringTokenizer JavaDoc stkLocal = new StringTokenizer JavaDoc(localFields, "|");
337                         StringTokenizer JavaDoc stkForeign = new StringTokenizer JavaDoc(foreignFields,
338                                 "|");
339                         boolean needsPipe = false;
340
341                         while (stkLocal.hasMoreTokens()) {
342                             if (needsPipe) {
343                                 fieldsString.append("|");
344                             } else {
345                                 needsPipe = true;
346                             }
347
348                             String JavaDoc localField = stkLocal.nextToken();
349                             String JavaDoc foreignField = stkForeign.nextToken();
350                             fieldsString.append(foreignField);
351                             fieldsString.append("|");
352                             fieldsString.append(myDBObj.getDataField(localField).asString());
353                         }
354
355                         oneTrans.addParam("search", fieldsString.toString());
356                         oneTrans.addParam("fields", fieldsString.toString());
357                     } /* for each detail record */
358
359                 } catch (DBException de) {
360                     throw new ControllerException(de);
361                 }
362                 if (detBlock.getNumContents() > 1) {
363                     add(detBlock);
364
365                     PersistentSession mySession = getSession();
366                     mySession.setPersistentAttribute(masterObjKey, detBlock);
367                 }
368             }
369         } catch (DBException de) {
370             throw new ControllerException(de);
371         }
372     } /* run(ControllerRequest, ControllerResponse) */
373
374 }
375
376 /* Update */
Popular Tags