KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > controller > DBMaint


1 package com.jcorporate.expresso.services.controller;
2
3 /* ====================================================================
4  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
5  *
6  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by Jcorporate Ltd.
23  * (http://www.jcorporate.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. "Jcorporate" and product names such as "Expresso" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written permission,
30  * please contact info@jcorporate.com.
31  *
32  * 5. Products derived from this software may not be called "Expresso",
33  * or other Jcorporate product names; nor may "Expresso" or other
34  * Jcorporate product names appear in their name, without prior
35  * written permission of Jcorporate Ltd.
36  *
37  * 6. No product derived from this software may compete in the same
38  * market space, i.e. framework, without prior written permission
39  * of Jcorporate Ltd. For written permission, please contact
40  * partners@jcorporate.com.
41  *
42  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
43  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
46  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
48  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
49  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
51  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  * ====================================================================
55  *
56  * This software consists of voluntary contributions made by many
57  * individuals on behalf of the Jcorporate Ltd. Contributions back
58  * to the project(s) are encouraged when you make modifications.
59  * Please send them to support@jcorporate.com. For more information
60  * on Jcorporate Ltd. and its products, please see
61  * <http://www.jcorporate.com/>.
62  *
63  * Portions of this software are based upon other open source
64  * products and are subject to their respective licenses.
65  */

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.DBController;
71 import com.jcorporate.expresso.core.controller.Input;
72 import com.jcorporate.expresso.core.controller.State;
73 import com.jcorporate.expresso.core.controller.Transition;
74 import com.jcorporate.expresso.core.db.DBException;
75 import com.jcorporate.expresso.core.dbobj.DBObject;
76 import com.jcorporate.expresso.core.dbobj.Schema;
77 import com.jcorporate.expresso.core.dbobj.SchemaFactory;
78 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
79 import com.jcorporate.expresso.core.dbobj.ValidValue;
80 import com.jcorporate.expresso.services.controller.dbmaint.Add;
81 import com.jcorporate.expresso.services.controller.dbmaint.AddUpdate;
82 import com.jcorporate.expresso.services.controller.dbmaint.List;
83 import com.jcorporate.expresso.services.controller.dbmaint.Search;
84 import com.jcorporate.expresso.services.controller.dbmaint.SearchList;
85 import com.jcorporate.expresso.services.controller.dbmaint.Update;
86 import com.jcorporate.expresso.services.controller.dbmaint.UpdateDelete;
87 import com.jcorporate.expresso.services.controller.dbmaint.UpdateUpdate;
88 import com.jcorporate.expresso.services.controller.dbmaint.ViewBlob;
89 import com.jcorporate.expresso.services.dbobj.DBOtherMap;
90 import com.jcorporate.expresso.services.dbobj.SchemaList;
91 import org.apache.log4j.Logger;
92
93 import java.util.Enumeration JavaDoc;
94 import java.util.Iterator JavaDoc;
95 import java.util.Vector JavaDoc;
96
97
98 /**
99  * Controller that enables basic maintenance (add/update/delete) of any DBObject
100  * DBMaint acts more as a controller for allowing operations on DBObjects.
101  * The real work for adding, deleting, searching and updating is carried out by
102  * a series of classes that are dynamically instantiated and live in the
103  * com.jcorporate.expresso.core.servlet.commands package. These classes
104  * can be extended to add additional functionality.
105  * <p/>
106  * When tracking down code functionality, the core states are in package:<br/>
107  * &nbsp;<code>com.jcorporate.expresso.services.dbmaint</code><br/>
108  * </p> <p>
109  * The only state handlers embedded in the class are minimalistic classes
110  * for allowing admins to select the DBObjects to administer from dropdown lists.
111  * </p>
112  *
113  * @author Michael Nash
114  * @version $Revision: 1.19 $ $Date: 2004/11/17 20:48:17 $
115  */

116 public class DBMaint
117         extends DBController {
118
119     /**
120      * The log4j Logger
121      */

122     private static Logger log = Logger.getLogger(DBMaint.class);
123     public static final String JavaDoc UPDATE = "Update";
124     public static final String JavaDoc LIST = "List";
125     public static final String JavaDoc ADD = "Add";
126     public static final String JavaDoc SEARCH = "Search";
127     public static final String JavaDoc PROMPT = "prompt";
128
129     /**
130      * Default Constructor. It constructs all the state
131      */

132     public DBMaint() {
133         super();
134         State prompt = new State(PROMPT, "Prompt to Select Schema");
135         addState(prompt);
136         setInitialState("prompt");
137
138         State selDBObj = new State("selDBObj", "Select Database Object");
139         selDBObj.addRequiredParameter("SchemaClass");
140         addState(selDBObj);
141
142         State add = new Add(ADD, "Prompt for New Record");
143         addState(add);
144
145         State addUpdate = new AddUpdate("AddUpdate", "Save New Record");
146         addState(addUpdate);
147
148         State list = new List(LIST, "List Records");
149         addState(list);
150
151         State search = new Search(SEARCH, "Prompt for Search Criteria");
152         addState(search);
153
154         State searchList = new SearchList("SearchList",
155                 "Display records found by Search");
156         addState(searchList);
157
158         State update = new Update(UPDATE, "Present Record for Update");
159         addState(update);
160
161         State updateDelete = new UpdateDelete("UpdateDelete", "Delete Record");
162         addState(updateDelete);
163
164         State updateUpdate = new UpdateUpdate("UpdateUpdate", "Update Record");
165         addState(updateUpdate);
166
167         this.addState(new ViewBlob());
168
169
170         this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class);
171     } /* Constructor */
172
173
174     /**
175      * Retrieve the title of this Controller
176      *
177      * @return java.lang.String The Title of the controller
178      */

179     public String JavaDoc getTitle() {
180         return ("Database Maintenance");
181     }
182
183     /**
184      * This particular state is for helping users to select DBObjects through
185      * the UI. It should normally be only accessible to Administrators in
186      * a live environment.
187      * <p/>
188      * This state creates an Input list of Schema objects that will be later
189      * user to select DBObjects from.
190      * </p>
191      *
192      * @param request The <code>ControllerRequest</code> object
193      * @param response The <code>ControllerResponse</code> object
194      * @throws ControllerException upon error.
195      */

196     private void runPromptState(ControllerRequest request, ControllerResponse response)
197             throws ControllerException {
198         try {
199             Input chooseSchema = new Input();
200             chooseSchema.setLabel("Choose Schema");
201             chooseSchema.setName("SchemaClass");
202
203             Vector JavaDoc v2 = new Vector JavaDoc(2);
204             v2.addElement(new ValidValue("com.jcorporate.expresso." + "core.ExpressoSchema",
205                     "General"));
206
207             SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT);
208             sl.setDataContext(request.getDataContext());
209
210             SchemaList oneSchema = null;
211
212             for (Iterator JavaDoc e = sl.searchAndRetrieveList("Descrip").iterator();
213                  e.hasNext();) {
214                 oneSchema = (SchemaList) e.next();
215                 v2.addElement(new ValidValue(oneSchema.getField("SchemaClass"),
216                         oneSchema.getField("Descrip")));
217             }
218
219             chooseSchema.setValidValues(v2);
220             response.addInput(chooseSchema);
221
222             Transition doDBObj = new Transition("Choose Database Object",
223                     getClass().getName());
224             doDBObj.setName("selDBObj");
225             doDBObj.addParam(STATE_PARAM_KEY, "selDBObj");
226             response.addTransition(doDBObj);
227         } catch (DBException de) {
228             throw new ControllerException(de.getMessage());
229         }
230     } /* promptState() */
231
232
233     /**
234      * Prompt for a database object to maintain from the specified schema. The
235      * schema is either a direct parameter or selected from the Prompt State.
236      *
237      * @param request The <code>ControllerRequest</code> object
238      * @param response The <code>ControllerResponse</code> object
239      * @throws ControllerException upon error.
240      */

241     private void runSelDBObjState(ControllerRequest request, ControllerResponse response)
242             throws ControllerException {
243         Input chooseDBobj = new Input();
244         chooseDBobj.setLabel("Choose Database Object");
245         chooseDBobj.setName("dbobj");
246
247         Vector JavaDoc v2 = new Vector JavaDoc();
248
249         try {
250             DBObject oneDBObject = null;
251             Schema mySchema = SchemaFactory.getInstance().getSchema(request.getParameter("SchemaClass"));
252             if (mySchema == null) {
253                 throw new ControllerException("Error instantiating Schema: " +
254                         request.getParameter("SchemaClass"));
255             }
256
257             for (Enumeration JavaDoc e = mySchema.getMembers(); e.hasMoreElements();) {
258                 oneDBObject = (DBObject) e.nextElement();
259                 String JavaDoc descrip = oneDBObject.getMetaData().getDescription(request.getLocale());
260                 if (log.isDebugEnabled()) {
261                     log.debug("Adding dataobject: " + descrip);
262                 }
263
264                 v2.addElement(new ValidValue(oneDBObject.getClass().getName(),
265                         oneDBObject.getMetaData().getDescription(request.getLocale())));
266             }
267
268             DBOtherMap otherDbobj = new DBOtherMap(SecuredDBObject.SYSTEM_ACCOUNT);
269             otherDbobj.setDataContext(request.getDataContext());
270
271         } catch (DBException de) {
272             throw new ControllerException(de);
273         }
274
275         chooseDBobj.setValidValues(v2);
276         response.addInput(chooseDBobj);
277         response.addTransition(new Transition(ADD, this));
278         response.addTransition(new Transition(LIST, this));
279         response.addTransition(new Transition(SEARCH, this));
280     } /* selDBObj(ControllerRequest, ControllerResponse) */
281
282 } /* DBMaint */
283
Popular Tags