1 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 ; 89 import java.util.Iterator ; 90 import java.util.Vector ; 91 92 93 99 public class DataTransfer 100 extends DBController { 101 private static Logger log = Logger.getLogger(DataTransfer.class); 102 103 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 } 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 v2 = new Vector (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 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 } 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 v2 = new Vector (); 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 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 oneConfigKey = null; 192 String oneDescrip = null; 193 Vector v = new Vector (); 194 195 for (Enumeration ie = ConfigManager.getAllConfigKeys(); 196 ie.hasMoreElements();) { 197 oneConfigKey = (String ) 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 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 hdv = new Vector (); 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 } 240 241 242 protected void runTransferState(ControllerRequest req, 243 ControllerResponse res) 244 throws ControllerException { 245 String dbobj = req.getParameter("dbobj"); 246 Vector transferList = new Vector (); 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 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 oneObj = null; 267 268 for (Enumeration e = transferList.elements(); e.hasMoreElements();) { 269 oneObj = (String ) 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 } 280 281 282 private void transferObject(ControllerResponse res, 283 String fromContext, String toContext, 284 String objectClass, String handleDups, 285 String clearDestination, String 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 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 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 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 } 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 oneKey = null; 372 373 for (Iterator i = fromObj.getKeyFieldListIterator(); i.hasNext();) { 374 oneKey = (String ) 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 oneField = null; 382 383 for (Iterator i = fromObj.getMetaData().getFieldListArray().iterator(); i.hasNext();) { 384 oneField = (String ) 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 className, 393 String 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 eo) { 406 throw new DBException("Exception loading SecuredDBObject", eo); 407 } 408 409 myDBObj.setDataContext(dbContext); 410 411 return myDBObj; 412 } 413 414 417 public String getTitle() { 418 return "Data Transfer"; 419 } 420 } 421 422 | Popular Tags |