use of org.cristalise.kernel.events.History 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.History in project kernel by cristal-ise.
the class Bootstrap method storeOutcomeEventAndViews.
/**
* @param item
* @param newOutcome
* @param version
* @throws PersistencyException
* @throws ObjectNotFoundException
* @throws InvalidDataException
*/
private static void storeOutcomeEventAndViews(ItemProxy item, Outcome newOutcome, int version) throws PersistencyException, ObjectNotFoundException, InvalidDataException {
Logger.msg("Bootstrap.storeOutcomeEventAndViews() - Writing new " + newOutcome.getSchema().getName() + " v" + version + " to " + item.getName());
History hist = new History(item.getPath(), item);
String viewName = String.valueOf(version);
int eventID = hist.addEvent(systemAgents.get("system").getPath(), null, "Admin", "Bootstrap", "Bootstrap", "Bootstrap", newOutcome.getSchema(), getPredefSM(), PredefinedStep.DONE, viewName).getID();
newOutcome.setID(eventID);
Viewpoint newLastView = new Viewpoint(item.getPath(), newOutcome.getSchema(), "last", eventID);
Viewpoint newNumberView = new Viewpoint(item.getPath(), newOutcome.getSchema(), viewName, eventID);
Gateway.getStorage().put(item.getPath(), newOutcome, item);
Gateway.getStorage().put(item.getPath(), newLastView, item);
Gateway.getStorage().put(item.getPath(), newNumberView, item);
}
use of org.cristalise.kernel.events.History in project kernel by cristal-ise.
the class ClusterStorageManager method get.
/**
* Internal get method. Retrieves clusters from ClusterStorages & maintains the memory cache.
* <br>
* There is a special case for Viewpoint. When path ends with /data it returns referenced Outcome instead of Viewpoint.
*
* @param itemPath current Iten
* @param path the cluster path
* @return the C2KObject located by path
*/
public C2KLocalObject get(ItemPath itemPath, String path) throws PersistencyException, ObjectNotFoundException {
// check cache first
Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(itemPath);
if (sysKeyMemCache != null) {
synchronized (sysKeyMemCache) {
C2KLocalObject obj = sysKeyMemCache.get(path);
if (obj != null) {
Logger.msg(7, "ClusterStorageManager.get() - found " + itemPath + "/" + path + " in memcache");
return obj;
}
}
}
// special case for Viewpoint- When path ends with /data it returns referenced Outcome instead of Viewpoint
if (path.startsWith(VIEWPOINT.getName()) && path.endsWith("/data")) {
StringTokenizer tok = new StringTokenizer(path, "/");
if (tok.countTokens() == 4) {
// to not catch viewpoints called 'data'
Viewpoint view = (Viewpoint) get(itemPath, path.substring(0, path.lastIndexOf("/")));
if (view != null)
return view.getOutcome();
else
return null;
}
}
C2KLocalObject result = null;
// deal out top level remote maps
if (path.indexOf('/') == -1) {
if (path.equals(HISTORY.getName())) {
result = new History(itemPath, null);
} else if (path.equals(JOB.getName())) {
if (itemPath instanceof AgentPath)
result = new JobList((AgentPath) itemPath, null);
else
throw new ObjectNotFoundException("Items do not have job lists");
}
}
if (result == null) {
// else try each reader in turn until we find it
ArrayList<ClusterStorage> readers = findStorages(ClusterStorage.getClusterType(path), false);
for (ClusterStorage thisReader : readers) {
try {
result = thisReader.get(itemPath, path);
Logger.msg(7, "ClusterStorageManager.get() - reading " + path + " from " + thisReader.getName() + " for item " + itemPath);
// got it!
if (result != null)
break;
} catch (PersistencyException e) {
Logger.msg(7, "ClusterStorageManager.get() - reader " + thisReader.getName() + " could not retrieve " + itemPath + "/" + path + ": " + e.getMessage());
}
}
}
// No result was found after reading the list of ClusterStorages
if (result == null) {
throw new ObjectNotFoundException("ClusterStorageManager.get() - Path " + path + " not found in " + itemPath);
}
putInMemoryCache(itemPath, path, result);
return result;
}
use of org.cristalise.kernel.events.History 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.History 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;
}
Aggregations