KickJava   Java API By Example, From Geeks To Geeks.

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


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.ControllerException;
68 import com.jcorporate.expresso.core.controller.ControllerRequest;
69 import com.jcorporate.expresso.core.controller.ControllerResponse;
70 import com.jcorporate.expresso.core.controller.ErrorCollection;
71 import com.jcorporate.expresso.core.controller.NonHandleableException;
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.StringUtil;
76 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
77 import com.jcorporate.expresso.services.controller.ui.DefaultAutoElement;
78 import org.apache.log4j.Logger;
79
80 import java.util.Iterator JavaDoc;
81
82
83 /**
84  * Processes the data passed via a standard form from SEARCH.
85  * This class requires a match on all specified fields of a DBObject
86  * for a successful outcome. SEARCHsearch can be extended
87  * to enabling a more restrictive search on only one or
88  * two fields e.g. a foreign key field
89  *
90  * @author Michael Nash, contributions by Kevin King
91  * @version $Revision: 1.17 $ $Date: 2004/11/17 20:48:17 $
92  */

93 public class SearchList
94         extends ListBase {
95     private static Logger log = Logger.getLogger(SearchList.class);
96
97     public SearchList() {
98
99     }
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 SearchList(String JavaDoc code, String JavaDoc descrip) {
108         super(code, descrip);
109     } /* SearchList(String, String) */
110
111     /**
112      * Perform the actions of this state.
113      *
114      * @param req The <code>ControllerRequest</code> object
115      * @param res The <code>ControllerResponse</code> object
116      */

117     public void run(ControllerRequest req, ControllerResponse res)
118             throws NonHandleableException, ControllerException {
119         super.run(req, res);
120
121         DataObject myDBObj = this.getDataObject();
122         DataObjectMetaData metadata = myDBObj.getMetaData();
123
124         ErrorCollection ee = new ErrorCollection();
125
126         myDBObj = DefaultAutoElement.getAutoControllerElement().parseDBObject(req, myDBObj, ee, false);
127
128         showNext = true;
129         showPrev = false;
130
131         String JavaDoc oneFieldName = null;
132         String JavaDoc fieldValue = null;
133         FastStringBuffer newFields = FastStringBuffer.getInstance();
134
135         try {
136             if (!StringUtil.notNull(getFieldsParam()).equals("")) {
137                 newFields.append(getFieldsParam());
138                 newFields.append("|");
139             }
140             try {
141
142                 /* Build the "search" param for subsequent pages, if any */
143                 for (Iterator JavaDoc e = myDBObj.getMetaData().getFieldListArray().iterator(); e.hasNext();) {
144                     oneFieldName = (String JavaDoc) e.next();
145
146                     if (!metadata.isVirtual(oneFieldName)) {
147                         fieldValue = StringUtil.notNull(myDBObj.getDataField(oneFieldName).asString());
148
149                         if (!fieldValue.equals("")) {
150                             if (metadata.getAttribute(oneFieldName, "checkbox") != null
151                                     && myDBObj.getDataField(oneFieldName).asBoolean().booleanValue() == false) {
152
153                                 //
154
//To actually get searches working, we only search
155
//on a checkbox value if it is checked, otherwise
156
//we ignore it. It's not the best, but otherwise
157
//folks don't get search results at all.
158
//
159
if (log.isDebugEnabled()) {
160                                     log.debug("Received false checkbox. " +
161                                             " Skipping value and resetting to false");
162                                 }
163                                 myDBObj.set(oneFieldName, null);
164                             } else {
165                                 newFields.append(oneFieldName);
166                                 newFields.append("|");
167                                 newFields.append(fieldValue);
168                                 newFields.append("|");
169                             }
170                         }
171                     }
172                 }
173
174                 setSearchParam(newFields.toString());
175                 showList();
176             } catch (DBException de) {
177                 ee.addError(de);
178                 res.saveErrors(ee);
179                 transition("Search", req, res);
180                 return;
181             } catch (Throwable JavaDoc t) {
182                 log.error("Error listing fields", t);
183                 throw new ControllerException("Error Listing Fields", t);
184             }
185         } finally {
186             newFields.release();
187         }
188     } /* run() */
189 }
190
191 /* SearchList */
Popular Tags