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.Input;
070: import com.jcorporate.expresso.core.controller.Output;
071: import com.jcorporate.expresso.core.controller.Transition;
072: import com.jcorporate.expresso.core.dataobjects.DataObject;
073: import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData;
074: import com.jcorporate.expresso.core.db.DBException;
075: import com.jcorporate.expresso.core.misc.Format;
076: import com.jcorporate.expresso.core.misc.StringUtil;
077: import com.jcorporate.expresso.core.security.User;
078: import com.jcorporate.expresso.core.security.UserInfo;
079:
080: import java.util.Iterator;
081:
082: /**
083: * Base class for search/add & update operations that
084: * were previously carried out in DBMaint.doGet().
085: *
086: * @author Michael Nash, contributions by Kevin King
087: */
088: public abstract class GetBase extends DynamicCmd {
089: public GetBase() {
090: }
091:
092: /**
093: * @param code internal state name constructor
094: * @param descrip Friendly name of the state.
095: */
096: public GetBase(String code, String descrip) {
097: super (code, descrip);
098: } /* GetBase(String, String) */
099:
100: /**
101: * implemented by subclass
102: *
103: * @param oneFieldName the field name to bring up the field
104: * @throws DBException upon error communicating with the DBObject.
105: * @throws ControllerException upon other errors
106: */
107: protected abstract void autoField(String oneFieldName)
108: throws DBException, ControllerException;
109:
110: /**
111: * Show an "auto" form - a default form that just lists all of the fields
112: * on a DBObject for simple data entry
113: *
114: * @throws DBException If an error occurs retrieving the form
115: * information from the database object
116: */
117: private void autoForm() throws DBException, ControllerException {
118: String oneFieldName = null;
119: DataObject myDBObj = this .getDataObject();
120:
121: if (myDBObj == null) {
122: throw new DBException("Database object must be "
123: + "initialized before calling autoField");
124: }
125:
126: DataObjectMetaData metadata = myDBObj.getMetaData();
127: showUserName(metadata.getDescription(this
128: .getControllerRequest().getLocale()));
129: addOutput(new Output("title", metadata.getDescription(this
130: .getControllerRequest().getLocale())));
131:
132: for (Iterator e = metadata.getFieldListArray().iterator(); e
133: .hasNext();) {
134: oneFieldName = (String) e.next();
135: autoField(oneFieldName);
136: }
137: } /* autoForm() */
138:
139: /**
140: * Format a value for display in the HTML being returned to the client
141: *
142: * @param fieldType The type of the field to format
143: * @param fieldValue The value of the field
144: * @return String The formatted field
145: * @throws DBException If the field format information could not be
146: * determined
147: */
148: protected String displayValue(String fieldType, String fieldValue)
149: throws DBException {
150: try {
151: if (fieldType.equalsIgnoreCase("money")) {
152: if (!fieldValue.equals("")) {
153: return new Format("%-10.2f").form(new Double(
154: fieldValue).doubleValue());
155: }
156: } else {
157: return fieldValue;
158: }
159: } catch (NumberFormatException ne) {
160: throw new DBException("Number for field not in a "
161: + "valid numeric format:" + fieldValue + ":"
162: + ne.getMessage());
163: }
164:
165: return null;
166: } /* displayValue(String, String) */
167:
168: /**
169: * Adds the lookup transition if the target is a DataObject. And a lookup
170: * object is specified. Otherwise, it skips the lookup item.
171: *
172: * @param metadata the object's metadata
173: * @param oneField the input to have a nested transition added to if valid.
174: * @param oneFieldName the name of the field to check
175: * @throws DBException upon error
176: */
177: protected void addLookupTransition(DataObjectMetaData metadata,
178: Input oneField, String oneFieldName) throws DBException {
179: /* Now, if there is a lookup object specified for the field */
180:
181: /* add an icon for the user to search this object in a new window */
182: String lookupObjectName = StringUtil.notNull(metadata
183: .getLookupObject(oneFieldName));
184:
185: String definitionName = StringUtil.notNull(metadata
186: .getFieldMetadata(oneFieldName).getLookupDefinition());
187:
188: if (!lookupObjectName.equals("")) {
189: boolean add = true;
190: //
191: //Special case for Users... the lookup is actually on the userinfo object
192: //IF the userinfo object implements the DataObject interface
193: //
194: if (com.jcorporate.expresso.core.security.User.class
195: .getName().equals(lookupObjectName)) {
196: User u = new User();
197: UserInfo ui = u.getUserInfo();
198: if (!(ui instanceof DataObject)) {
199: add = false;
200: }
201: }
202:
203: if (add) {
204: Transition lookup = new Transition();
205: lookup.setName("lookup");
206: lookup.addParam("dbobj", lookupObjectName);
207: if (definitionName.length() > 0) {
208: lookup.addParam("definition", definitionName);
209: }
210: lookup.addParam(Controller.STATE_PARAM_KEY, "Search");
211: lookup.setDescription("Look up Values");
212: oneField.addNested(lookup);
213: }
214: } /* if there was a lookup object */
215: }
216:
217: /**
218: * Show the HTML form to the user - this form may be for add/update or
219: * search and may or may not be populated with data when presented to the user.
220: * The descendant of this class must defined this method to show the
221: * necessary input form for the user to key in either record data or criteria f
222: * or a search. An existing record (if any) will be populated in myDBObj
223: *
224: * @throws DBException If a problem occurs with the database connection
225: */
226: protected void showForm() throws DBException, ControllerException {
227: autoForm();
228: showOptions();
229: } /* showForm() */
230:
231: } /* GetBase */
|