KickJava   Java API By Example, From Geeks To Geeks.

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


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
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.dataobjects.DataFieldMetaData;
76 import com.jcorporate.expresso.core.dataobjects.DataObject;
77 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData;
78 import com.jcorporate.expresso.core.dataobjects.Securable;
79 import com.jcorporate.expresso.core.db.DBException;
80 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
81 import com.jcorporate.expresso.core.misc.StringUtil;
82 import com.jcorporate.expresso.core.security.User;
83 import com.jcorporate.expresso.services.controller.ui.DefaultAutoElement;
84 import com.jcorporate.expresso.services.dbobj.Setup;
85
86 import java.util.HashMap JavaDoc;
87 import java.util.Map JavaDoc;
88
89
90 /**
91  * Displays a form for a new DBObject record.
92  * <p>This state does not actually perform the adding of the dbobject to
93  * the database, instead it creates a blank form that is pre-populated with
94  * the default values of the given DBObject.</p>
95  * <p>The actual class that does the updating is <code>AddUpdate</code></p>
96  *
97  * @author Michael Nash, contributions by Kevin King
98  * @see com.jcorporate.expresso.services.controller.dbmaint.AddUpdate
99  */

100 public class Add
101         extends GetBase {
102     private Map JavaDoc fixedFields = new HashMap JavaDoc();
103
104     public Add() {
105
106     }
107
108     /**
109      * Default Constructor
110      *
111      * @param stateName Name of the state
112      * @param descrip Friendly name of the state
113      */

114     public Add(String JavaDoc stateName, String JavaDoc descrip) {
115         super(stateName, descrip);
116     } /* Add(String, String) */
117
118
119     /**
120      * Automatically handle a single field on the form.
121      * <p>This function is called by the base class' autoList function, so
122      * it provides, in essence, a callback for rendering each DBObject field</p>
123      *
124      * @param oneFieldName The name of the field
125      * @throws DBException If a problem occurs getting field info from the
126      * database object
127      * @throws ControllerException upon error
128      */

129     protected void autoField(String JavaDoc oneFieldName)
130             throws DBException, ControllerException {
131         DataObject myDBObj = this.getDataObject();
132         StringUtil.assertNotBlank(oneFieldName, "Field Name must not be null");
133
134         if (myDBObj == null) {
135             throw new DBException("Database object must be " +
136                     "initialized before calling autoField");
137         }
138
139         DataFieldMetaData metadata = myDBObj.getFieldMetaData(oneFieldName);
140         DataObjectMetaData objMetadata = myDBObj.getMetaData();
141
142         if (metadata.isVirtual()) {
143             return;
144         }
145
146
147         boolean readOnly = false;
148
149         /* If the field is explicitly set to read only, then only offer */
150         /* input to it in search mode */
151         if (metadata.isReadOnly()) {
152             readOnly = true;
153         }
154
155
156         String JavaDoc fixedValue = (String JavaDoc) fixedFields.get(oneFieldName);
157
158         if (fixedValue != null) {
159             readOnly = true;
160         }
161
162
163         String JavaDoc cachedValue = StringUtil.notNull(getFormCache(oneFieldName));
164
165         //
166
//Zero out cached value so that rendering of Mapped objects takes place
167
//
168
if (cachedValue != null && cachedValue.length() == 0) {
169             cachedValue = null;
170         }
171
172         //Hack. Make the ui think that the dataobject is new so it renders
173
//correctly even though we've set some fields for 'child' data objects
174
myDBObj.setStatus(DataObject.STATUS_NEW);
175         Input i = DefaultAutoElement.getAutoControllerElement()
176                 .renderDBObjectField(getControllerResponse(),
177                         myDBObj,
178                         oneFieldName,
179                         cachedValue,
180                         readOnly);
181
182         if (i == null) {
183             return;
184         }
185
186         /* Now, if there is a lookup object specified for the field */
187         /* add an icon for the user to search this object in a new window */
188         String JavaDoc lookupObjectName = StringUtil.notNull(objMetadata.getLookupObject(oneFieldName));
189
190         if (!lookupObjectName.equals("")) {
191             Transition lookup = new Transition();
192             lookup.setName("lookup");
193             lookup.addParam("dbobj", lookupObjectName);
194             lookup.addParam(Controller.STATE_PARAM_KEY, "Search");
195             lookup.setDescription("Look up Values");
196             i.addNested(lookup);
197         } /* if there was a lookup object */
198
199         this.addInput(i);
200
201     } /* autoField(String) */
202
203
204     /**
205      * Actually perform the setup and display of the form
206      *
207      * @param req The <code>ControllerRequest</code> object
208      * @param res The <code>ControllerResponse</code> object
209      * @throws NonHandleableException upon error
210      * @throws ControllerException upon error
211      */

212     public void run(ControllerRequest req, ControllerResponse res)
213             throws NonHandleableException, ControllerException {
214         super.run(req, res);
215         fixedFields = getFixedFields();
216
217         DataObject myDBObj = this.getDataObject();
218
219         try {
220             if (myDBObj instanceof Securable) {
221                 ((Securable) myDBObj).isAllowed("A");
222             } else {
223                 if (getUid() == SecuredDBObject.SYSTEM_ACCOUNT
224                         || User.getUserFromId(getUid(), this.getControllerRequest().getDataContext()).isAdmin()) {
225                     // all access ok
226
} else {
227                     String JavaDoc allowInsecure = Setup.getValue(req.getDataContext(),
228                             com.jcorporate.expresso.core.ExpressoSchema.class.getName(),
229                             "insecureDBMaint");
230                     if (!(StringUtil.toBoolean(allowInsecure))) {
231                         throw new SecurityException JavaDoc("Access to unsecured Objects not allowed");
232                     }
233                 }
234             }
235
236             showNext = false;
237             showPrev = false;
238             showForm();
239
240             Transition addUpdate = new Transition("AddUpdate", getController());
241             addParams(addUpdate);
242             add(addUpdate);
243         } catch (DBException de) {
244             throw new ControllerException(de);
245         }
246     } /* run(ControllerRequest, ControllerResponse) */
247 } /* Add */
Popular Tags