use of org.cristalise.kernel.events.Event in project kernel by cristal-ise.
the class ItemImplementation method initialise.
@Override
public void initialise(SystemKey agentId, String propString, String initWfString, String initCollsString) throws AccessRightsException, InvalidDataException, PersistencyException {
Logger.msg(5, "Item::initialise(" + mItemPath + ") - agent:" + agentId);
Object locker = new Object();
AgentPath agentPath;
try {
agentPath = new AgentPath(agentId);
} catch (InvalidItemPathException e) {
throw new AccessRightsException("Invalid Agent Id:" + agentId);
}
// must supply properties
if (propString == null || propString.length() == 0 || propString.equals("<NULL/>")) {
throw new InvalidDataException("No properties supplied");
}
// store properties
try {
PropertyArrayList props = (PropertyArrayList) Gateway.getMarshaller().unmarshall(propString);
for (Property thisProp : props.list) mStorage.put(mItemPath, thisProp, locker);
} catch (Throwable ex) {
Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Properties were invalid: " + propString);
Logger.error(ex);
mStorage.abort(locker);
throw new InvalidDataException("Properties were invalid");
}
// Store an event and the initial properties
try {
Schema initSchema = LocalObjectLoader.getSchema("ItemInitialization", 0);
Outcome initOutcome = new Outcome(0, propString, initSchema);
History hist = new History(mItemPath, locker);
Event newEvent = hist.addEvent(new AgentPath(agentId), null, "", "Initialize", "", "", initSchema, Bootstrap.getPredefSM(), PredefinedStep.DONE, "last");
initOutcome.setID(newEvent.getID());
Viewpoint newLastView = new Viewpoint(mItemPath, initSchema, "last", newEvent.getID());
mStorage.put(mItemPath, initOutcome, locker);
mStorage.put(mItemPath, newLastView, locker);
} catch (Throwable ex) {
Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Could not store event and outcome.");
Logger.error(ex);
mStorage.abort(locker);
throw new PersistencyException("Error storing event and outcome");
}
// init collections
if (initCollsString != null && initCollsString.length() > 0 && !initCollsString.equals("<NULL/>")) {
try {
CollectionArrayList colls = (CollectionArrayList) Gateway.getMarshaller().unmarshall(initCollsString);
for (Collection<?> thisColl : colls.list) {
mStorage.put(mItemPath, thisColl, locker);
}
} catch (Throwable ex) {
Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Collections were invalid: " + initCollsString);
Logger.error(ex);
mStorage.abort(locker);
throw new InvalidDataException("Collections were invalid");
}
}
// create wf
Workflow lc = null;
try {
if (initWfString == null || initWfString.length() == 0 || initWfString.equals("<NULL/>")) {
lc = new Workflow(new CompositeActivity(), getNewPredefStepContainer());
} else {
lc = new Workflow((CompositeActivity) Gateway.getMarshaller().unmarshall(initWfString), getNewPredefStepContainer());
}
mStorage.put(mItemPath, lc, locker);
} catch (Throwable ex) {
Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Workflow was invalid: " + initWfString);
Logger.error(ex);
mStorage.abort(locker);
throw new InvalidDataException("Workflow was invalid");
}
// All objects are in place, initialize the workflow to get the Item running
lc.initialise(mItemPath, agentPath, locker);
mStorage.put(mItemPath, lc, locker);
mStorage.commit(locker);
Logger.msg(3, "Initialisation of item " + mItemPath + " was successful");
}
use of org.cristalise.kernel.events.Event in project kernel by cristal-ise.
the class ImportItem method create.
/**
*/
@Override
public Path create(AgentPath agentPath, boolean reset) throws InvalidDataException, ObjectCannotBeUpdated, ObjectNotFoundException, CannotManageException, ObjectAlreadyExistsException, InvalidCollectionModification, PersistencyException {
domainPath = new DomainPath(new DomainPath(initialPath), name);
if (domainPath.exists()) {
ItemPath domItem = domainPath.getItemPath();
if (!getItemPath().equals(domItem)) {
throw new CannotManageException("Item " + domainPath + " was found with the wrong itemPath (" + domainPath.getItemPath() + " vs " + getItemPath() + ")");
}
} else
isDOMPathExists = false;
TraceableEntity newItem = getTraceableEntitiy();
// (re)initialise the new item with properties, workflow and collections
try {
newItem.initialise(agentPath.getSystemKey(), Gateway.getMarshaller().marshall(createItemProperties()), Gateway.getMarshaller().marshall(createCompositeActivity()), Gateway.getMarshaller().marshall(createCollections()));
} catch (Exception ex) {
Logger.error("Error initialising new item " + ns + "/" + name);
Logger.error(ex);
if (isNewItem)
Gateway.getLookupManager().delete(itemPath);
throw new CannotManageException("Problem initialising new item. See server log:" + ex.getMessage());
}
History hist = new History(getItemPath(), null);
// import outcomes
for (ImportOutcome thisOutcome : outcomes) {
String outcomeData = thisOutcome.getData(ns);
// load schema and state machine
Schema schema = LocalObjectLoader.getSchema(thisOutcome.schema, thisOutcome.version);
// parse new outcome and validate
Outcome newOutcome = new Outcome(-1, outcomeData, schema);
newOutcome.validateAndCheck();
Viewpoint impView;
try {
impView = (Viewpoint) Gateway.getStorage().get(getItemPath(), ClusterType.VIEWPOINT + "/" + thisOutcome.schema + "/" + thisOutcome.viewname, null);
if (newOutcome.isIdentical(impView.getOutcome())) {
Logger.msg(5, "ImportItem.create() - View " + thisOutcome.schema + "/" + thisOutcome.viewname + " in " + ns + "/" + name + " identical, no update required");
continue;
} else {
Logger.msg("ImportItem.create() - Difference found in view " + thisOutcome.schema + "/" + thisOutcome.viewname + " in " + ns + "/" + name);
if (!reset && !impView.getEvent().getStepPath().equals("Import")) {
Logger.msg("ImportItem.create() - Last edit was not done by import, and reset not requested. Not overwriting.");
continue;
}
}
} catch (ObjectNotFoundException ex) {
Logger.msg("ImportItem.create() - View " + thisOutcome.schema + "/" + thisOutcome.viewname + " not found in " + ns + "/" + name + ". Creating.");
impView = new Viewpoint(getItemPath(), schema, thisOutcome.viewname, -1);
}
// write new view/outcome/event
Event newEvent = hist.addEvent(agentPath, null, "Admin", "Import", "Import", "Import", schema, Bootstrap.getPredefSM(), PredefinedStep.DONE, thisOutcome.viewname);
newOutcome.setID(newEvent.getID());
impView.setEventId(newEvent.getID());
Gateway.getStorage().put(getItemPath(), newOutcome, null);
Gateway.getStorage().put(getItemPath(), impView, null);
}
// register domain path (before collections in case of recursive collections)
if (!isDOMPathExists) {
domainPath.setItemPath(getItemPath());
Gateway.getLookupManager().add(domainPath);
}
return domainPath;
}
use of org.cristalise.kernel.events.Event in project kernel by cristal-ise.
the class Import method runActivityLogic.
/**
* Params: Schemaname_version:Viewpoint (optional), Outcome, Timestamp (optional)
*/
@Override
protected String runActivityLogic(AgentPath agent, ItemPath item, int transitionID, String requestData, Object locker) throws InvalidDataException, PersistencyException, ObjectNotFoundException {
String[] params = getDataList(requestData);
if (Logger.doLog(3))
Logger.msg(3, "Import: called by " + agent + " on " + item + " with parameters " + Arrays.toString(params));
int split1 = params[0].indexOf('_');
int split2 = params[0].indexOf(':');
if (split1 == -1)
throw new InvalidDataException("Import: Invalid parameters " + Arrays.toString(params));
requestData = params[1];
Schema schema;
String viewpoint = null;
String schemaName = params[0].substring(0, split1);
int schemaVersion;
if (split2 > -1) {
schemaVersion = Integer.parseInt(params[0].substring(split1 + 1, split2));
viewpoint = params[0].substring(split2 + 1);
} else
schemaVersion = Integer.parseInt(params[0].substring(split1 + 1));
schema = LocalObjectLoader.getSchema(schemaName, schemaVersion);
String timestamp;
if (params.length == 3)
timestamp = params[2];
else
timestamp = DateUtility.timeToString(DateUtility.getNow());
// write event, outcome and viewpoints to storage
TransactionManager storage = Gateway.getStorage();
History hist = getWf().getHistory();
Event event = hist.addEvent(agent, null, getCurrentAgentRole(), getName(), getPath(), getType(), schema, getStateMachine(), transitionID, viewpoint, timestamp);
try {
storage.put(item, new Outcome(event.getID(), requestData, schema), locker);
storage.put(item, new Viewpoint(item, schema, viewpoint, event.getID()), locker);
if (!"last".equals(viewpoint))
storage.put(item, new Viewpoint(item, schema, "last", event.getID()), locker);
} catch (PersistencyException e) {
storage.abort(locker);
throw e;
}
storage.commit(locker);
return requestData;
}
use of org.cristalise.kernel.events.Event in project kernel by cristal-ise.
the class WriteViewpoint method write.
public static void write(ItemPath item, String schemaName, String viewName, int eventId, Object locker) throws PersistencyException, ObjectNotFoundException, InvalidDataException {
Event event = (Event) Gateway.getStorage().get(item, ClusterType.HISTORY + "/" + eventId, locker);
if (StringUtils.isBlank(event.getSchemaName())) {
throw new InvalidDataException("Event " + eventId + " does not reference an Outcome, so cannot be assigned to a Viewpoint.");
}
// checks Schema name/version
Schema thisSchema = LocalObjectLoader.getSchema(schemaName, event.getSchemaVersion());
if (!event.getSchemaName().equals(thisSchema.getItemID())) {
throw new InvalidDataException("Event outcome schema is " + event.getSchemaName() + ", and cannot be used for a " + schemaName + " Viewpoint");
}
Gateway.getStorage().put(item, new Viewpoint(item, thisSchema, viewName, eventId), locker);
}
Aggregations