KickJava   Java API By Example, From Geeks To Geeks.

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


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.Controller;
68 import com.jcorporate.expresso.core.controller.ControllerException;
69 import com.jcorporate.expresso.core.controller.Input;
70 import com.jcorporate.expresso.core.controller.Output;
71 import com.jcorporate.expresso.core.controller.Transition;
72 import com.jcorporate.expresso.core.dataobjects.DataObject;
73 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData;
74 import com.jcorporate.expresso.core.db.DBException;
75 import com.jcorporate.expresso.core.misc.Format;
76 import com.jcorporate.expresso.core.misc.StringUtil;
77 import com.jcorporate.expresso.core.security.User;
78 import com.jcorporate.expresso.core.security.UserInfo;
79
80 import java.util.Iterator JavaDoc;
81
82
83 /**
84  * Base class for search/add & update operations that
85  * were previously carried out in DBMaint.doGet().
86  *
87  * @author Michael Nash, contributions by Kevin King
88  */

89 public abstract class GetBase
90         extends DynamicCmd {
91     public GetBase() {
92     }
93
94     /**
95      * @param code internal state name constructor
96      * @param descrip Friendly name of the state.
97      */

98     public GetBase(String JavaDoc code, String JavaDoc descrip) {
99         super(code, descrip);
100     } /* GetBase(String, String) */
101
102     /**
103      * implemented by subclass
104      *
105      * @param oneFieldName the field name to bring up the field
106      * @throws DBException upon error communicating with the DBObject.
107      * @throws ControllerException upon other errors
108      */

109     protected abstract void autoField(String JavaDoc oneFieldName)
110             throws DBException, ControllerException;
111
112     /**
113      * Show an "auto" form - a default form that just lists all of the fields
114      * on a DBObject for simple data entry
115      *
116      * @throws DBException If an error occurs retrieving the form
117      * information from the database object
118      */

119     private void autoForm()
120             throws DBException, ControllerException {
121         String JavaDoc oneFieldName = null;
122         DataObject myDBObj = this.getDataObject();
123
124         if (myDBObj == null) {
125             throw new DBException("Database object must be " +
126                     "initialized before calling autoField");
127         }
128
129         DataObjectMetaData metadata = myDBObj.getMetaData();
130         showUserName(metadata.getDescription(this.getControllerRequest().getLocale()));
131         addOutput(new Output("title", metadata.getDescription(this.getControllerRequest().getLocale())));
132
133         for (Iterator JavaDoc e = metadata.getFieldListArray().iterator(); e.hasNext();) {
134             oneFieldName = (String JavaDoc) e.next();
135             autoField(oneFieldName);
136         }
137     } /* autoForm() */
138
139
140     /**
141      * Format a value for display in the HTML being returned to the client
142      *
143      * @param fieldType The type of the field to format
144      * @param fieldValue The value of the field
145      * @return String The formatted field
146      * @throws DBException If the field format information could not be
147      * determined
148      */

149     protected String JavaDoc displayValue(String JavaDoc fieldType, String JavaDoc fieldValue)
150             throws DBException {
151         try {
152             if (fieldType.equalsIgnoreCase("money")) {
153                 if (!fieldValue.equals("")) {
154                     return new Format("%-10.2f").form(new Double JavaDoc(fieldValue).doubleValue());
155                 }
156             } else {
157                 return fieldValue;
158             }
159         } catch (NumberFormatException JavaDoc 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     /**
170      * Adds the lookup transition if the target is a DataObject. And a lookup
171      * object is specified. Otherwise, it skips the lookup item.
172      *
173      * @param metadata the object's metadata
174      * @param oneField the input to have a nested transition added to if valid.
175      * @param oneFieldName the name of the field to check
176      * @throws DBException upon error
177      */

178     protected void addLookupTransition(DataObjectMetaData metadata,
179                                        Input oneField,
180                                        String JavaDoc oneFieldName) throws DBException {
181         /* Now, if there is a lookup object specified for the field */
182
183         /* add an icon for the user to search this object in a new window */
184         String JavaDoc lookupObjectName = StringUtil.notNull(metadata.getLookupObject(oneFieldName));
185
186         String JavaDoc definitionName = StringUtil.notNull(metadata
187                 .getFieldMetadata(oneFieldName).getLookupDefinition());
188
189         if (!lookupObjectName.equals("")) {
190             boolean add = true;
191             //
192
//Special case for Users... the lookup is actually on the userinfo object
193
//IF the userinfo object implements the DataObject interface
194
//
195
if (com.jcorporate.expresso.core.security.User
196                     .class.getName().equals(lookupObjectName)) {
197                 User u = new User();
198                 UserInfo ui = u.getUserInfo();
199                 if (!(ui instanceof DataObject)) {
200                     add = false;
201                 }
202             }
203
204             if (add) {
205                 Transition lookup = new Transition();
206                 lookup.setName("lookup");
207                 lookup.addParam("dbobj", lookupObjectName);
208                 if (definitionName.length() > 0) {
209                     lookup.addParam("definition", definitionName);
210                 }
211                 lookup.addParam(Controller.STATE_PARAM_KEY, "Search");
212                 lookup.setDescription("Look up Values");
213                 oneField.addNested(lookup);
214             }
215         } /* if there was a lookup object */
216     }
217
218     /**
219      * Show the HTML form to the user - this form may be for add/update or
220      * search and may or may not be populated with data when presented to the user.
221      * The descendant of this class must defined this method to show the
222      * necessary input form for the user to key in either record data or criteria f
223      * or a search. An existing record (if any) will be populated in myDBObj
224      *
225      * @throws DBException If a problem occurs with the database connection
226      */

227     protected void showForm()
228             throws DBException, ControllerException {
229         autoForm();
230         showOptions();
231     } /* showForm() */
232
233
234 } /* GetBase */
235
236
Popular Tags