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.dbobj.ValidValue;
081: import com.jcorporate.expresso.core.misc.StringUtil;
082: import com.jcorporate.expresso.core.security.User;
083: import com.jcorporate.expresso.services.controller.ui.DefaultAutoElement;
084: import com.jcorporate.expresso.services.dbobj.Setup;
085:
086: import java.util.Vector;
087:
088: /**
089: * allows the user to search for a record. This class requires
090: * a match on all supplied fields of a DBObject. Search can be extended
091: * to allow a search on one or more fields e.g. a foreign key field
092: *
093: * @author Michael Nash, contributions by Kevin King
094: */
095: public class Search extends GetBase {
096:
097: public Search() {
098:
099: }
100:
101: /**
102: * Constructor
103: *
104: * @param code The name of the state.
105: * @param descrip The friendly name of the state
106: */
107: public Search(String code, String descrip) {
108: super (code, descrip);
109: } /* Search(String, String) */
110:
111: /**
112: * @param oneFieldName The name of the field
113: * @throws DBException If a problem occurs getting field info from the
114: * database object
115: */
116: protected void autoField(String oneFieldName) throws DBException,
117: ControllerException {
118: boolean readOnly = false;
119:
120: if (oneFieldName == null) {
121: throw new ControllerException("Field Name must not be null");
122: }
123:
124: DataObject myDBObj = this .getDataObject();
125: DataObjectMetaData objectMetadata = myDBObj.getMetaData();
126:
127: if (myDBObj == null) {
128: throw new DBException("Database object must be "
129: + "initialized before calling autoField");
130: }
131:
132: String oneFieldValue = myDBObj.getDataField(oneFieldName)
133: .asString();
134:
135: if (oneFieldValue == null) {
136: oneFieldValue = ("");
137: }
138:
139: String fixedValue = (String) getFixedFields().get(oneFieldName);
140: boolean skipField = false;
141:
142: if (fixedValue != null) {
143: oneFieldValue = fixedValue;
144: skipField = true;
145: readOnly = true;
146: }
147:
148: /* Now, if there is a lookup object specified for the field */
149: Input oneField = null;
150: DataFieldMetaData metaData = myDBObj
151: .getFieldMetaData(oneFieldName);
152:
153: if (metaData.isBinaryObjectType()) {
154: return;
155: }
156:
157: if (metaData.isReadOnly()) {
158: //
159: //We have a special case here in that we DO want to display all
160: //read only fields for 'editing' so to speak now.
161: //
162: oneField = new Input(oneFieldName);
163: oneField.setLabel(objectMetadata.getDescription(this
164: .getControllerRequest().getLocale(), oneFieldName));
165: oneField.setDefaultValue(oneFieldValue);
166: String oneFieldSize = Integer.toString(metaData
167: .getLengthInt());
168:
169: if (oneFieldSize.equals("0")) {
170: oneFieldSize = ("30");
171: }
172:
173: int fieldSize = metaData.getLengthInt();
174: if (fieldSize == 0 && metaData.isNumericType()) {
175: fieldSize = 12;
176: } else {
177: fieldSize = fieldSize + 2;
178: }
179: /* Make the longest display field size 60 characters */
180: int displayFieldSize = fieldSize;
181:
182: //
183: //Numeric fields are too small since integers which may, in text
184: //take at least 6 characters are listed as only 2 byte in size via
185: //the database information
186: //
187: if (displayFieldSize > 60) {
188: displayFieldSize = 60;
189: }
190: if (displayFieldSize < 20 && metaData.isNumericType()) {
191: displayFieldSize = 22;
192: }
193:
194: //We make the max length large since range strings could be
195: //conceivably set for a search.
196: oneField.setMaxLength(128);
197: oneField.setDisplayLength(displayFieldSize);
198:
199: if (objectMetadata.isMultiValued(oneFieldName)) {
200: oneField.setValidValues(new Vector(myDBObj
201: .getValidValuesList(oneFieldName)));
202: oneField.setAttribute(Input.ATTRIBUTE_MULTIVALUED, "Y");
203: oneField.setAttribute(Input.ATTRIBUTE_DROPDOWN, "Y");
204: oneField.setType(Input.ATTRIBUTE_DROPDOWN);
205: }
206: } else {
207: oneField = DefaultAutoElement.getAutoControllerElement()
208: .renderDBObjectField(this .getControllerResponse(),
209: myDBObj, oneFieldName, oneFieldValue,
210: readOnly);
211: }
212:
213: if (oneField == null) {
214: return;
215: }
216:
217: addInput(oneField);
218:
219: if (!skipField) {
220: oneField.setDefaultValue("");
221:
222: /* if the field is multi-valued, present a drop-down list */
223: /* instead of just a text field */
224: if (objectMetadata.isMultiValued(oneFieldName)) {
225: Vector values = oneField.getValidValues();
226:
227: if (values == null) {
228: throw new DBException("Valid values for field "
229: + oneFieldName + " from object "
230: + objectMetadata.getName() + " were null");
231: }
232:
233: values.add(new ValidValue("", "All Values"));
234: oneField.setValidValues(values);
235: oneField.setDefaultValue("");
236: } else {
237: //Set the size larger since search can retrieve ranges
238: oneField.setMaxLength(128);
239: if (oneField.getDisplayLength() < 20) {
240: oneField.setDisplayLength(20);
241: }
242: }
243:
244: /* Now, if there is a lookup object specified for the field */
245:
246: /* add an icon for the user to search this object in a new window */
247: String lookupObjectName = StringUtil.notNull(objectMetadata
248: .getLookupObject(oneFieldName));
249:
250: if (!lookupObjectName.equals("")) {
251: Transition lookup = new Transition();
252: lookup.setName("lookup");
253: lookup.addParam("dbobj", lookupObjectName);
254: lookup.addParam(Controller.STATE_PARAM_KEY, "Search");
255: lookup.setDescription("Look up Values");
256: oneField.addNested(lookup);
257: } /* if there was a lookup object */
258:
259: } /* if not skip field */
260: } /* autoField(String) */
261:
262: /**
263: * Perform the actions of this state.
264: *
265: * @param req The <code>ControllerRequest</code> object
266: * @param res The <code>ControllerResponse</code> object
267: */
268: public void run(ControllerRequest req, ControllerResponse res)
269: throws NonHandleableException, ControllerException {
270: super .run(req, res);
271:
272: DataObject myDBObj = this .getDataObject();
273:
274: try {
275: if (myDBObj instanceof Securable) {
276: ((Securable) myDBObj).isAllowed("S");
277: } else {
278: if (getUid() == SecuredDBObject.SYSTEM_ACCOUNT
279: || User.getUserFromId(
280: getUid(),
281: this .getControllerRequest()
282: .getDataContext()).isAdmin()) {
283: // all access ok
284: } else {
285: String allowInsecure = Setup
286: .getValue(
287: req.getDataContext(),
288: com.jcorporate.expresso.core.ExpressoSchema.class
289: .getName(),
290: "insecureDBMaint");
291: if (!(StringUtil.toBoolean(allowInsecure))) {
292: throw new SecurityException(
293: "Access to unsecured Objects not allowed");
294: }
295: }
296: }
297: myDBObj.clear();
298: showNext = false;
299: showPrev = false;
300: showForm();
301:
302: Transition searchList = new Transition("SearchList",
303: getController());
304: addParams(searchList);
305: add(searchList);
306: } catch (DBException de) {
307: throw new ControllerException(de);
308: }
309: } /* execute() */
310:
311: } /* Search */
|