KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > controller > DataTransfer


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.ext.controller;
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.NonHandleableException;
73 import com.jcorporate.expresso.core.controller.Output;
74 import com.jcorporate.expresso.core.controller.State;
75 import com.jcorporate.expresso.core.controller.Transition;
76 import com.jcorporate.expresso.core.db.DBException;
77 import com.jcorporate.expresso.core.dbobj.DBObject;
78 import com.jcorporate.expresso.core.dbobj.Schema;
79 import com.jcorporate.expresso.core.dbobj.SchemaFactory;
80 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
81 import com.jcorporate.expresso.core.dbobj.ValidValue;
82 import com.jcorporate.expresso.core.misc.ConfigManager;
83 import com.jcorporate.expresso.core.misc.ConfigurationException;
84 import com.jcorporate.expresso.core.misc.StringUtil;
85 import com.jcorporate.expresso.services.dbobj.SchemaList;
86 import org.apache.log4j.Logger;
87
88 import java.util.Enumeration JavaDoc;
89 import java.util.Iterator JavaDoc;
90 import java.util.Vector JavaDoc;
91
92
93 /**
94  * DataTransfer is a controller used to move data from one
95  * db/context to another, with various options
96  *
97  * @author Michael Nash
98  */

99 public class DataTransfer
100         extends DBController {
101     private static Logger log = Logger.getLogger(DataTransfer.class);
102
103     /**
104      * Default constructor
105      */

106     public DataTransfer() {
107         super();
108         State selSchema = new State("selSchema", "Prompt for Schema");
109         addState(selSchema);
110         setInitialState("selSchema");
111
112         State prompt = new State("prompt", "Prompt for Tranfer Parameters");
113         addState(prompt);
114         prompt.addRequiredParameter("SchemaClass");
115
116         State transfer = new State("transfer", "Run Transfers");
117         transfer.addRequiredParameter("fromContext");
118         transfer.addRequiredParameter("toContext");
119         addState(transfer);
120         this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class);
121     } /* DataTransfer() */
122
123
124     protected void runSelSchemaState(ControllerRequest req,
125                                      ControllerResponse res)
126             throws ControllerException {
127         try {
128             Input chooseSchema = new Input();
129             chooseSchema.setLabel("Choose Schema");
130             chooseSchema.setName("SchemaClass");
131
132             Vector JavaDoc v2 = new Vector JavaDoc(2);
133             v2.addElement(new ValidValue("com.jcorporate.expresso." + "core.ExpressoSchema",
134                     "General"));
135
136             SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT);
137             sl.setDataContext(req.getDataContext());
138
139             SchemaList oneSchema = null;
140
141             for (Iterator JavaDoc e = sl.searchAndRetrieveList("Descrip").iterator();
142                  e.hasNext();) {
143                 oneSchema = (SchemaList) e.next();
144                 v2.addElement(new ValidValue(oneSchema.getField("SchemaClass"),
145                         oneSchema.getField("Descrip")));
146             }
147
148             chooseSchema.setValidValues(v2);
149             res.addInput(chooseSchema);
150             res.addTransition(new Transition("prompt", this));
151         } catch (DBException de) {
152             throw new ControllerException(de.getMessage());
153         }
154     } /* runSelSchemaState */
155
156
157     protected void runPromptState(ControllerRequest req, ControllerResponse res)
158             throws ControllerException, NonHandleableException {
159         Input chooseDBobj = new Input();
160         chooseDBobj.setLabel("Choose Database Object");
161         chooseDBobj.setName("dbobj");
162
163         Vector JavaDoc v2 = new Vector JavaDoc();
164         v2.addElement(new ValidValue("All", "All Objects in Schema"));
165
166         DBObject oneDBObject = null;
167         Schema mySchema = SchemaFactory.getInstance().getSchema(req.getParameter("SchemaClass"));
168
169         if (mySchema == null) {
170             throw new ControllerException("Unable to instantiate schema: " +
171                     req.getParameter("SchemaClass"));
172         }
173
174         for (Enumeration JavaDoc e = mySchema.getMembers(); e.hasMoreElements();) {
175             oneDBObject = (DBObject) e.nextElement();
176             v2.addElement(new ValidValue(oneDBObject.getClass().getName(),
177                     oneDBObject.getMetaData().getDescription()));
178         }
179
180         chooseDBobj.setValidValues(v2);
181         res.addInput(chooseDBobj);
182
183         Input fromContext = new Input("fromContext");
184         fromContext.setDefaultValue(req.getDataContext());
185         fromContext.setLabel("From " + getString("Context/Database_"));
186
187         Input toContext = new Input("toContext");
188         toContext.setDefaultValue(req.getDataContext());
189         toContext.setLabel("To " + getString("Context/Database_"));
190
191         String JavaDoc oneConfigKey = null;
192         String JavaDoc oneDescrip = null;
193         Vector JavaDoc v = new Vector JavaDoc();
194
195         for (Enumeration JavaDoc ie = ConfigManager.getAllConfigKeys();
196              ie.hasMoreElements();) {
197             oneConfigKey = (String JavaDoc) ie.nextElement();
198             oneDescrip = "";
199
200             try {
201                 oneDescrip = StringUtil.notNull(ConfigManager.getContext(oneConfigKey).getDescription());
202
203                 if (oneDescrip.equals("")) {
204                     oneDescrip = oneConfigKey;
205                 }
206                 /* If it's not an expresso context, you can't log in to it */
207                 if (ConfigManager.getContext(oneConfigKey).hasSetupTables()) {
208                     v.addElement(new ValidValue(oneConfigKey, oneDescrip));
209                 }
210             } catch (ConfigurationException ce) {
211                 throw new ControllerException(ce);
212             }
213         }
214
215         fromContext.setValidValues(v);
216         res.addInput(fromContext);
217         toContext.setValidValues(v);
218         res.addInput(toContext);
219
220         Input handleDups = new Input("handleDups", "Duplicate Key Handling");
221         Vector JavaDoc hdv = new Vector JavaDoc();
222         hdv.addElement(new ValidValue("S", "Skip Duplicate Records"));
223         hdv.addElement(new ValidValue("R", "Replace Duplicate Records"));
224         hdv.addElement(new ValidValue("A", "Add New Records"));
225         hdv.addElement(new ValidValue("I", "Insert Records"));
226         handleDups.setValidValues(hdv);
227         res.addInput(handleDups);
228
229         Input clearDestination = new Input("clearDestination",
230                 "Clear Destination Table before Copy");
231         res.addInput(clearDestination);
232
233         Input customWhere = new Input("customWhere", "Where clause for source");
234         res.addInput(customWhere);
235
236         Transition doTransfer = new Transition("transfer", this);
237         doTransfer.addParam("SchemaClass", req.getParameter("SchemaClass"));
238         res.addTransition(doTransfer);
239     } /* runPromptState */
240
241
242     protected void runTransferState(ControllerRequest req,
243                                     ControllerResponse res)
244             throws ControllerException {
245         String JavaDoc dbobj = req.getParameter("dbobj");
246         Vector JavaDoc transferList = new Vector JavaDoc();
247
248         if (dbobj.equals("All")) {
249             DBObject oneDBObject = null;
250             Schema mySchema = SchemaFactory.getInstance().getSchema(req.getParameter("SchemaClass"));
251
252             if (mySchema == null) {
253                 throw new ControllerException("Error instantiating schema: " +
254                         req.getParameter("SchemaClass"));
255             }
256
257             for (Enumeration JavaDoc e = mySchema.getMembers();
258                  e.hasMoreElements();) {
259                 oneDBObject = (DBObject) e.nextElement();
260                 transferList.addElement(oneDBObject.getClass().getName());
261             }
262         } else {
263             transferList.addElement(dbobj);
264         }
265         try {
266             String JavaDoc oneObj = null;
267
268             for (Enumeration JavaDoc e = transferList.elements(); e.hasMoreElements();) {
269                 oneObj = (String JavaDoc) e.nextElement();
270                 transferObject(res, req.getParameter("fromContext"),
271                         req.getParameter("toContext"), oneObj,
272                         req.getParameter("handleDups"),
273                         req.getParameter("clearDestination"),
274                         req.getParameter("customWhere"));
275             }
276         } catch (DBException de) {
277             throw new ControllerException(de);
278         }
279     } /* runTranferState */
280
281
282     private void transferObject(ControllerResponse res,
283                                 String JavaDoc fromContext, String JavaDoc toContext,
284                                 String JavaDoc objectClass, String JavaDoc handleDups,
285                                 String JavaDoc clearDestination, String JavaDoc customWhere)
286             throws DBException, ControllerException {
287         if (objectClass.equals("com.jcorporate.expresso.ext.dbobj.SingleDBUserInfo")) {
288             return;
289         }
290         if (clearDestination.equalsIgnoreCase("Y")) {
291             DBObject clearObj = createDBObj(objectClass, toContext);
292             clearObj.deleteAll();
293         }
294
295         DBObject fromList = createDBObj(objectClass, fromContext);
296
297         if (!customWhere.equals("")) {
298             fromList.setCustomWhereClause(customWhere);
299         }
300
301         int exceptionCount = 0;
302         int addedCount = 0;
303         int updatedCount = 0;
304         DBObject fromObj = createDBObj(objectClass, fromContext);
305
306         for (Iterator JavaDoc i = fromList.searchAndRetrieveList().iterator();
307              i.hasNext();) {
308             try {
309                 fromObj = (DBObject) i.next();
310
311                 if (handleDups.equals("S")) {
312                     DBObject searchObj = createDBObj(objectClass,
313                             toContext);
314
315                     /* set tke keys */
316                     copyKeys(fromObj, searchObj);
317
318                     if (!searchObj.find()) {
319                         DBObject newObj = createDBObj(objectClass,
320                                 toContext);
321                         copyAll(fromObj, newObj);
322                         newObj.add();
323                         addedCount++;
324                     }
325                 } else if (handleDups.equals("R")) {
326                     DBObject searchObj = createDBObj(objectClass,
327                             toContext);
328
329                     /* set tke keys */
330                     copyKeys(fromObj, searchObj);
331
332                     DBObject newObj = createDBObj(objectClass, toContext);
333                     copyAll(fromObj, newObj);
334
335                     if (searchObj.find()) {
336                         newObj.update();
337                         updatedCount++;
338                     } else {
339                         newObj.add();
340                         addedCount++;
341                     }
342                 } else if (handleDups.equals("A")) {
343                     DBObject newObj = createDBObj(objectClass, toContext);
344                     copyAll(fromObj, newObj);
345                     newObj.add();
346                     addedCount++;
347                 } else if (handleDups.equals("I")) {
348                     DBObject newObj = createDBObj(objectClass, toContext);
349                     copyAll(fromObj, newObj);
350                     newObj.basicAdd();
351                     addedCount++;
352                 }
353             } catch (DBException de) {
354                 log.error("Exception tranferring records for '" + objectClass +
355                         "':", de);
356                 exceptionCount++;
357             }
358         } /* for */
359
360
361         res.addOutput(new Output("eCount",
362                 "Object '" + objectClass + "' There were " +
363                 exceptionCount + " exceptions, " +
364                 addedCount + " records added, " +
365                 updatedCount +
366                 " records updated. See log for details"));
367     }
368
369     private void copyKeys(DBObject fromObj, DBObject toObj)
370             throws DBException {
371         String JavaDoc oneKey = null;
372
373         for (Iterator JavaDoc i = fromObj.getKeyFieldListIterator(); i.hasNext();) {
374             oneKey = (String JavaDoc) i.next();
375             toObj.setField(oneKey, fromObj.getField(oneKey));
376         }
377     }
378
379     private void copyAll(DBObject fromObj, DBObject toObj)
380             throws DBException {
381         String JavaDoc oneField = null;
382
383         for (Iterator JavaDoc i = fromObj.getMetaData().getFieldListArray().iterator(); i.hasNext();) {
384             oneField = (String JavaDoc) i.next();
385
386             if (!fromObj.getMetaData().getFieldMetadata(oneField).isVirtual()) {
387                 toObj.setField(oneField, fromObj.getField(oneField));
388             }
389         }
390     }
391
392     private DBObject createDBObj(String JavaDoc className,
393                                  String JavaDoc dbContext)
394             throws DBException {
395         DBObject myDBObj = null;
396
397         try {
398             myDBObj = (DBObject) Class.forName(className).newInstance();
399
400             if (myDBObj == null) {
401                 throw new DBException("Null object - " +
402                         "instantiate failed for database object " +
403                         className);
404             }
405         } catch (Exception JavaDoc eo) {
406             throw new DBException("Exception loading SecuredDBObject", eo);
407         }
408
409         myDBObj.setDataContext(dbContext);
410
411         return myDBObj;
412     }
413
414     /**
415      * @return java.lang.String The Title of the controller
416      */

417     public String JavaDoc getTitle() {
418         return "Data Transfer";
419     }
420 } /* DataTransfer */
421
422
Popular Tags