KickJava   Java API By Example, From Geeks To Geeks.

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


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.ControllerRequest;
70 import com.jcorporate.expresso.core.controller.ControllerResponse;
71 import com.jcorporate.expresso.core.controller.Input;
72 import com.jcorporate.expresso.core.controller.NonHandleableException;
73 import com.jcorporate.expresso.core.controller.Transition;
74 import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData;
75 import com.jcorporate.expresso.core.dataobjects.DataObject;
76 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData;
77 import com.jcorporate.expresso.core.dataobjects.Securable;
78 import com.jcorporate.expresso.core.db.DBException;
79 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
80 import com.jcorporate.expresso.core.dbobj.ValidValue;
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.Vector JavaDoc;
87
88
89 /**
90  * allows the user to search for a record. This class requires
91  * a match on all supplied fields of a DBObject. Search can be extended
92  * to allow a search on one or more fields e.g. a foreign key field
93  *
94  * @author Michael Nash, contributions by Kevin King
95  */

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

109     public Search(String JavaDoc code, String JavaDoc descrip) {
110         super(code, descrip);
111     } /* Search(String, String) */
112
113     /**
114      * @param oneFieldName The name of the field
115      * @throws DBException If a problem occurs getting field info from the
116      * database object
117      */

118     protected void autoField(String JavaDoc oneFieldName)
119             throws DBException, ControllerException {
120         boolean readOnly = false;
121
122         if (oneFieldName == null) {
123             throw new ControllerException("Field Name must not be null");
124         }
125
126         DataObject myDBObj = this.getDataObject();
127         DataObjectMetaData objectMetadata = myDBObj.getMetaData();
128
129         if (myDBObj == null) {
130             throw new DBException("Database object must be " +
131                     "initialized before calling autoField");
132         }
133
134
135         String JavaDoc oneFieldValue = myDBObj.getDataField(oneFieldName).asString();
136
137         if (oneFieldValue == null) {
138             oneFieldValue = ("");
139         }
140
141         String JavaDoc fixedValue = (String JavaDoc) getFixedFields().get(oneFieldName);
142         boolean skipField = false;
143
144         if (fixedValue != null) {
145             oneFieldValue = fixedValue;
146             skipField = true;
147             readOnly = true;
148         }
149
150
151
152         /* Now, if there is a lookup object specified for the field */
153         Input oneField = null;
154         DataFieldMetaData metaData = myDBObj.getFieldMetaData(oneFieldName);
155
156         if (metaData.isBinaryObjectType()) {
157             return;
158         }
159
160         if (metaData.isReadOnly()) {
161             //
162
//We have a special case here in that we DO want to display all
163
//read only fields for 'editing' so to speak now.
164
//
165
oneField = new Input(oneFieldName);
166             oneField.setLabel(objectMetadata.getDescription(this.getControllerRequest().getLocale(), oneFieldName));
167             oneField.setDefaultValue(oneFieldValue);
168             String JavaDoc oneFieldSize = Integer.toString(metaData.getLengthInt());
169
170             if (oneFieldSize.equals("0")) {
171                 oneFieldSize = ("30");
172             }
173
174             int fieldSize = metaData.getLengthInt();
175             if (fieldSize == 0 && metaData.isNumericType()) {
176                 fieldSize = 12;
177             } else {
178                 fieldSize = fieldSize + 2;
179             }
180             /* Make the longest display field size 60 characters */
181             int displayFieldSize = fieldSize;
182
183             //
184
//Numeric fields are too small since integers which may, in text
185
//take at least 6 characters are listed as only 2 byte in size via
186
//the database information
187
//
188
if (displayFieldSize > 60) {
189                 displayFieldSize = 60;
190             }
191             if (displayFieldSize < 20 && metaData.isNumericType()) {
192                 displayFieldSize = 22;
193             }
194
195
196             //We make the max length large since range strings could be
197
//conceivably set for a search.
198
oneField.setMaxLength(128);
199             oneField.setDisplayLength(displayFieldSize);
200
201             if (objectMetadata.isMultiValued(oneFieldName)) {
202                 oneField.setValidValues(new Vector JavaDoc(myDBObj.getValidValuesList(oneFieldName)));
203                 oneField.setAttribute(Input.ATTRIBUTE_MULTIVALUED, "Y");
204                 oneField.setAttribute(Input.ATTRIBUTE_DROPDOWN, "Y");
205                 oneField.setType(Input.ATTRIBUTE_DROPDOWN);
206             }
207         } else {
208             oneField = DefaultAutoElement.getAutoControllerElement()
209                     .renderDBObjectField(this.getControllerResponse(),
210                             myDBObj,
211                             oneFieldName,
212                             oneFieldValue,
213                             readOnly);
214         }
215
216         if (oneField == null) {
217             return;
218         }
219
220         addInput(oneField);
221
222
223         if (!skipField) {
224             oneField.setDefaultValue("");
225
226             /* if the field is multi-valued, present a drop-down list */
227             /* instead of just a text field */
228             if (objectMetadata.isMultiValued(oneFieldName)) {
229                 Vector JavaDoc values = oneField.getValidValues();
230
231                 if (values == null) {
232                     throw new DBException("Valid values for field " +
233                             oneFieldName + " from object " +
234                             objectMetadata.getName() + " were null");
235                 }
236
237                 values.add(new ValidValue("", "All Values"));
238                 oneField.setValidValues(values);
239                 oneField.setDefaultValue("");
240             } else {
241                 //Set the size larger since search can retrieve ranges
242
oneField.setMaxLength(128);
243                 if (oneField.getDisplayLength() < 20) {
244                     oneField.setDisplayLength(20);
245                 }
246             }
247
248             /* Now, if there is a lookup object specified for the field */
249
250             /* add an icon for the user to search this object in a new window */
251             String JavaDoc lookupObjectName = StringUtil.notNull(objectMetadata.getLookupObject(oneFieldName));
252
253             if (!lookupObjectName.equals("")) {
254                 Transition lookup = new Transition();
255                 lookup.setName("lookup");
256                 lookup.addParam("dbobj", lookupObjectName);
257                 lookup.addParam(Controller.STATE_PARAM_KEY, "Search");
258                 lookup.setDescription("Look up Values");
259                 oneField.addNested(lookup);
260             } /* if there was a lookup object */
261
262         } /* if not skip field */
263     } /* autoField(String) */
264
265
266     /**
267      * Perform the actions of this state.
268      *
269      * @param req The <code>ControllerRequest</code> object
270      * @param res The <code>ControllerResponse</code> object
271      */

272     public void run(ControllerRequest req, ControllerResponse res)
273             throws NonHandleableException, ControllerException {
274         super.run(req, res);
275
276         DataObject myDBObj = this.getDataObject();
277
278         try {
279             if (myDBObj instanceof Securable) {
280                 ((Securable) myDBObj).isAllowed("S");
281             } else {
282                 if (getUid() == SecuredDBObject.SYSTEM_ACCOUNT
283                         || User.getUserFromId(getUid(), this.getControllerRequest().getDataContext()).isAdmin()) {
284                     // all access ok
285
} else {
286                     String JavaDoc allowInsecure = Setup.getValue(req.getDataContext(),
287                             com.jcorporate.expresso.core.ExpressoSchema.class.getName(),
288                             "insecureDBMaint");
289                     if (!(StringUtil.toBoolean(allowInsecure))) {
290                         throw new SecurityException JavaDoc("Access to unsecured Objects not allowed");
291                     }
292                 }
293             }
294             myDBObj.clear();
295             showNext = false;
296             showPrev = false;
297             showForm();
298
299             Transition searchList = new Transition("SearchList",
300                     getController());
301             addParams(searchList);
302             add(searchList);
303         } catch (DBException de) {
304             throw new ControllerException(de);
305         }
306     } /* execute() */
307
308 } /* Search */
Popular Tags