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.Controller;
068: import com.jcorporate.expresso.core.controller.ControllerException;
069: import com.jcorporate.expresso.core.controller.ControllerRequest;
070: import com.jcorporate.expresso.core.controller.ControllerResponse;
071: import com.jcorporate.expresso.core.controller.Input;
072: import com.jcorporate.expresso.core.controller.NonHandleableException;
073: import com.jcorporate.expresso.core.controller.Transition;
074: import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData;
075: import com.jcorporate.expresso.core.dataobjects.DataObject;
076: import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData;
077: import com.jcorporate.expresso.core.dataobjects.Securable;
078: import com.jcorporate.expresso.core.db.DBException;
079: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
080: import com.jcorporate.expresso.core.misc.StringUtil;
081: import com.jcorporate.expresso.core.security.User;
082: import com.jcorporate.expresso.services.controller.ui.DefaultAutoElement;
083: import com.jcorporate.expresso.services.dbobj.Setup;
084:
085: import java.util.HashMap;
086: import java.util.Map;
087:
088: /**
089: * Displays a form for a new DBObject record.
090: * <p>This state does not actually perform the adding of the dbobject to
091: * the database, instead it creates a blank form that is pre-populated with
092: * the default values of the given DBObject.</p>
093: * <p>The actual class that does the updating is <code>AddUpdate</code></p>
094: *
095: * @author Michael Nash, contributions by Kevin King
096: * @see com.jcorporate.expresso.services.controller.dbmaint.AddUpdate
097: */
098: public class Add extends GetBase {
099: private Map fixedFields = new HashMap();
100:
101: public Add() {
102:
103: }
104:
105: /**
106: * Default Constructor
107: *
108: * @param stateName Name of the state
109: * @param descrip Friendly name of the state
110: */
111: public Add(String stateName, String descrip) {
112: super (stateName, descrip);
113: } /* Add(String, String) */
114:
115: /**
116: * Automatically handle a single field on the form.
117: * <p>This function is called by the base class' autoList function, so
118: * it provides, in essence, a callback for rendering each DBObject field</p>
119: *
120: * @param oneFieldName The name of the field
121: * @throws DBException If a problem occurs getting field info from the
122: * database object
123: * @throws ControllerException upon error
124: */
125: protected void autoField(String oneFieldName) throws DBException,
126: ControllerException {
127: DataObject myDBObj = this .getDataObject();
128: StringUtil.assertNotBlank(oneFieldName,
129: "Field Name must not be null");
130:
131: if (myDBObj == null) {
132: throw new DBException("Database object must be "
133: + "initialized before calling autoField");
134: }
135:
136: DataFieldMetaData metadata = myDBObj
137: .getFieldMetaData(oneFieldName);
138: DataObjectMetaData objMetadata = myDBObj.getMetaData();
139:
140: if (metadata.isVirtual()) {
141: return;
142: }
143:
144: boolean readOnly = false;
145:
146: /* If the field is explicitly set to read only, then only offer */
147: /* input to it in search mode */
148: if (metadata.isReadOnly()) {
149: readOnly = true;
150: }
151:
152: String fixedValue = (String) fixedFields.get(oneFieldName);
153:
154: if (fixedValue != null) {
155: readOnly = true;
156: }
157:
158: String cachedValue = StringUtil
159: .notNull(getFormCache(oneFieldName));
160:
161: //
162: //Zero out cached value so that rendering of Mapped objects takes place
163: //
164: if (cachedValue != null && cachedValue.length() == 0) {
165: cachedValue = null;
166: }
167:
168: //Hack. Make the ui think that the dataobject is new so it renders
169: //correctly even though we've set some fields for 'child' data objects
170: myDBObj.setStatus(DataObject.STATUS_NEW);
171: Input i = DefaultAutoElement.getAutoControllerElement()
172: .renderDBObjectField(getControllerResponse(), myDBObj,
173: oneFieldName, cachedValue, readOnly);
174:
175: if (i == null) {
176: return;
177: }
178:
179: /* Now, if there is a lookup object specified for the field */
180: /* add an icon for the user to search this object in a new window */
181: String lookupObjectName = StringUtil.notNull(objMetadata
182: .getLookupObject(oneFieldName));
183:
184: if (!lookupObjectName.equals("")) {
185: Transition lookup = new Transition();
186: lookup.setName("lookup");
187: lookup.addParam("dbobj", lookupObjectName);
188: lookup.addParam(Controller.STATE_PARAM_KEY, "Search");
189: lookup.setDescription("Look up Values");
190: i.addNested(lookup);
191: } /* if there was a lookup object */
192:
193: this .addInput(i);
194:
195: } /* autoField(String) */
196:
197: /**
198: * Actually perform the setup and display of the form
199: *
200: * @param req The <code>ControllerRequest</code> object
201: * @param res The <code>ControllerResponse</code> object
202: * @throws NonHandleableException upon error
203: * @throws ControllerException upon error
204: */
205: public void run(ControllerRequest req, ControllerResponse res)
206: throws NonHandleableException, ControllerException {
207: super .run(req, res);
208: fixedFields = getFixedFields();
209:
210: DataObject myDBObj = this .getDataObject();
211:
212: try {
213: if (myDBObj instanceof Securable) {
214: ((Securable) myDBObj).isAllowed("A");
215: } else {
216: if (getUid() == SecuredDBObject.SYSTEM_ACCOUNT
217: || User.getUserFromId(
218: getUid(),
219: this .getControllerRequest()
220: .getDataContext()).isAdmin()) {
221: // all access ok
222: } else {
223: String allowInsecure = Setup
224: .getValue(
225: req.getDataContext(),
226: com.jcorporate.expresso.core.ExpressoSchema.class
227: .getName(),
228: "insecureDBMaint");
229: if (!(StringUtil.toBoolean(allowInsecure))) {
230: throw new SecurityException(
231: "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",
241: getController());
242: addParams(addUpdate);
243: add(addUpdate);
244: } catch (DBException de) {
245: throw new ControllerException(de);
246: }
247: } /* run(ControllerRequest, ControllerResponse) */
248: } /* Add */
|