From 779718755c22b6a6ad1c8a4b5d040f8a65cc4058 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 25 Jun 2013 17:03:15 +0200 Subject: More intelligent import of module resources. If resources or outcomes have been modified by a different user, do no overwrite them. Reset the properties and workflow though (should be fairly stateless), but leave the collections. --- .../predefined/entitycreation/NewItem.java | 89 ++++++++++++++++++---- 1 file changed, 73 insertions(+), 16 deletions(-) (limited to 'src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java') diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java index dfb19b8..1a9e2fa 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java @@ -3,8 +3,12 @@ package com.c2kernel.lifecycle.instance.predefined.entitycreation; import java.util.ArrayList; +import org.custommonkey.xmlunit.Diff; +import org.custommonkey.xmlunit.XMLUnit; + import com.c2kernel.collection.MembershipException; import com.c2kernel.common.CannotManageException; +import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.ObjectAlreadyExistsException; import com.c2kernel.common.ObjectCannotBeUpdated; import com.c2kernel.common.ObjectNotFoundException; @@ -16,6 +20,7 @@ import com.c2kernel.lifecycle.instance.stateMachine.States; import com.c2kernel.lifecycle.instance.stateMachine.Transitions; import com.c2kernel.lookup.DomainPath; import com.c2kernel.lookup.EntityPath; +import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.persistency.ClusterStorageException; import com.c2kernel.persistency.outcome.Viewpoint; import com.c2kernel.process.Gateway; @@ -60,16 +65,21 @@ public class NewItem extends ModuleImport { return ns; } - public void create(int agentId) throws ObjectCannotBeUpdated, ObjectNotFoundException, CannotManageException, ObjectAlreadyExistsException { + public void create(int agentId, boolean reset) throws ObjectCannotBeUpdated, ObjectNotFoundException, CannotManageException, ObjectAlreadyExistsException { DomainPath domPath = new DomainPath(new DomainPath(initialPath), name); - if (domPath.exists()) - throw new ObjectAlreadyExistsException(domPath+" already exists!", ""); - - // create item - EntityPath entPath = Gateway.getLDAPLookup().getNextKeyManager().generateNextEntityKey(); - TraceableEntity newItem = (TraceableEntity)Gateway.getCorbaServer().createEntity(entPath); - Gateway.getLDAPLookup().add(entPath); + EntityPath entPath; TraceableEntity newItem; + if (domPath.exists()) { + entPath = domPath.getEntity(); + newItem = Gateway.getCorbaServer().getItem(entPath.getSysKey()); + } + else { + // create item + entPath = Gateway.getLDAPLookup().getNextKeyManager().generateNextEntityKey(); + newItem = (TraceableEntity)Gateway.getCorbaServer().createEntity(entPath); + Gateway.getLDAPLookup().add(entPath); + } + // set the name property properties.add(new Property("Name", name)); @@ -89,27 +99,66 @@ public class NewItem extends ModuleImport { } // import outcomes - + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setIgnoreComments(true); History hist = new History(entPath.getSysKey(), null); for (Outcome thisOutcome : outcomes) { - Event newEvent = hist.addEvent("system", "Admin", Transitions.DONE, "Import", "Import", "Import", thisOutcome.viewname.equals("last")?null:thisOutcome.viewname, States.FINISHED); - com.c2kernel.persistency.outcome.Outcome newOutcome = new com.c2kernel.persistency.outcome.Outcome(newEvent.getID(), thisOutcome.getData(ns), thisOutcome.schema, thisOutcome.version); - Viewpoint newLastView = new Viewpoint(entPath.getSysKey(), thisOutcome.schema, thisOutcome.viewname, thisOutcome.version, newEvent.getID()); + com.c2kernel.persistency.outcome.Outcome newOutcome = new com.c2kernel.persistency.outcome.Outcome(-1, thisOutcome.getData(ns), thisOutcome.schema, thisOutcome.version); + Viewpoint impView; + try { + impView = (Viewpoint)Gateway.getStorage().get(entPath.getSysKey(), ClusterStorage.VIEWPOINT+"/"+thisOutcome.schema+"/"+thisOutcome.viewname, null); + String oldData = impView.getOutcome().getData(); + + Diff xmlDiff = new Diff(newOutcome.getDOM(), impView.getOutcome().getDOM()); + if (xmlDiff.identical()) { + Logger.msg(5, "NewItem.create() - View "+thisOutcome.schema+"/"+thisOutcome.viewname+" in "+name+" identical, no update required"); + return; + } + else { + Logger.msg("NewItem.create() - Difference found in view "+thisOutcome.schema+"/"+thisOutcome.viewname+" in "+name+": "+xmlDiff.toString()); + if (!reset && !impView.getEvent().getStepPath().equals("Import")) { + Logger.msg("Last edit was not done by import, and reset not requested. Not overwriting."); + continue; + } + } + } catch (ObjectNotFoundException ex) { + Logger.msg(3, "View "+thisOutcome.schema+"/"+thisOutcome.viewname+" not found in "+name+". Creating."); + impView = new Viewpoint(entPath.getSysKey(), thisOutcome.schema, thisOutcome.viewname, thisOutcome.version, -1); + } catch (ClusterStorageException e) { + throw new ObjectCannotBeUpdated("Could not check data for view "+thisOutcome.schema+"/"+thisOutcome.viewname+" in "+name); + } catch (InvalidDataException e) { + throw new ObjectCannotBeUpdated("Could not check previous event for view "+thisOutcome.schema+"/"+thisOutcome.viewname+" in "+name); + } + + // write new view/outcome/event + Event newEvent = hist.addEvent("system", "Admin", Transitions.DONE, "Import", "Import", "Import", thisOutcome.viewname, States.FINISHED); + newOutcome.setID(newEvent.getID()); + impView.setEventId(newEvent.getID()); try { Gateway.getStorage().put(entPath.getSysKey(), newOutcome, null); - Gateway.getStorage().put(entPath.getSysKey(), newLastView, null); + Gateway.getStorage().put(entPath.getSysKey(), impView, null); } catch (ClusterStorageException e) { throw new ObjectCannotBeUpdated("Could not store data for view "+thisOutcome.schema+"/"+thisOutcome.viewname+" in "+name); } } // register domain path (before collections in case of recursive collections) - domPath.setEntity(entPath); - Gateway.getLDAPLookup().add(domPath); + if (!domPath.exists()) { + domPath.setEntity(entPath); + Gateway.getLDAPLookup().add(domPath); + } // create collections for (Dependency element: dependencyList) { try { + com.c2kernel.collection.Dependency newDep = element.create(); + if (!reset) { + try { + Gateway.getStorage().get(entPath.getSysKey(), ClusterStorage.COLLECTION+"/"+newDep.getName(), null); + Logger.msg("Not overwriting dependency "+newDep.getName()); + continue; // TODO: a proper compare here + } catch (ObjectNotFoundException ex) { } // doesn't exist, ok to create + } Gateway.getStorage().put(entPath.getSysKey(), element.create(), null); } catch (ClusterStorageException ex) { Logger.error(ex); @@ -122,7 +171,15 @@ public class NewItem extends ModuleImport { for (Aggregation element : aggregationList) { try { - Gateway.getStorage().put(entPath.getSysKey(), element.create(), null); + com.c2kernel.collection.Aggregation newAgg = element.create(); + if (!reset) { + try { + Gateway.getStorage().get(entPath.getSysKey(), ClusterStorage.COLLECTION+"/"+newAgg.getName(), null); + Logger.msg("Not overwriting aggregation "+newAgg.getName()); + continue; // TODO: a proper compare here + } catch (ObjectNotFoundException ex) { } // doesn't exist, ok to create + } + Gateway.getStorage().put(entPath.getSysKey(), newAgg, null); } catch (ClusterStorageException ex) { Logger.error(ex); throw new CannotManageException("Could not create Aggregation "+element.name, ""); -- cgit v1.2.3