use of org.alfresco.service.cmr.action.Action in project alfresco-remote-api by Alfresco.
the class AbstractRuleWebScript method parseJsonRule.
protected Rule parseJsonRule(JSONObject jsonRule) throws JSONException {
Rule result = new Rule();
if (jsonRule.has("title") == false || jsonRule.getString("title").length() == 0) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Title missing when creating rule");
}
result.setTitle(jsonRule.getString("title"));
result.setDescription(jsonRule.has("description") ? jsonRule.getString("description") : "");
if (jsonRule.has("ruleType") == false || jsonRule.getJSONArray("ruleType").length() == 0) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Rule type missing when creating rule");
}
JSONArray types = jsonRule.getJSONArray("ruleType");
List<String> ruleTypes = new ArrayList<String>();
for (int i = 0; i < types.length(); i++) {
ruleTypes.add(types.getString(i));
}
result.setRuleTypes(ruleTypes);
result.applyToChildren(jsonRule.has("applyToChildren") ? jsonRule.getBoolean("applyToChildren") : false);
result.setExecuteAsynchronously(jsonRule.has("executeAsynchronously") ? jsonRule.getBoolean("executeAsynchronously") : false);
result.setRuleDisabled(jsonRule.has("disabled") ? jsonRule.getBoolean("disabled") : false);
JSONObject jsonAction = jsonRule.getJSONObject("action");
// parse action object
Action ruleAction = parseJsonAction(jsonAction);
result.setAction(ruleAction);
return result;
}
use of org.alfresco.service.cmr.action.Action in project alfresco-remote-api by Alfresco.
the class RulePut method updateRuleFromJSON.
protected void updateRuleFromJSON(JSONObject jsonRule, Rule ruleToUpdate) throws JSONException {
if (jsonRule.has("title")) {
ruleToUpdate.setTitle(jsonRule.getString("title"));
}
if (jsonRule.has("description")) {
ruleToUpdate.setDescription(jsonRule.getString("description"));
}
if (jsonRule.has("ruleType")) {
JSONArray jsonTypes = jsonRule.getJSONArray("ruleType");
List<String> types = new ArrayList<String>();
for (int i = 0; i < jsonTypes.length(); i++) {
types.add(jsonTypes.getString(i));
}
ruleToUpdate.setRuleTypes(types);
}
if (jsonRule.has("applyToChildren")) {
ruleToUpdate.applyToChildren(jsonRule.getBoolean("applyToChildren"));
}
if (jsonRule.has("executeAsynchronously")) {
ruleToUpdate.setExecuteAsynchronously(jsonRule.getBoolean("executeAsynchronously"));
}
if (jsonRule.has("disabled")) {
ruleToUpdate.setRuleDisabled(jsonRule.getBoolean("disabled"));
}
if (jsonRule.has("action")) {
JSONObject jsonAction = jsonRule.getJSONObject("action");
// update rule action
Action action = updateActionFromJson(jsonAction, (ActionImpl) ruleToUpdate.getAction());
ruleToUpdate.setAction(action);
}
}
use of org.alfresco.service.cmr.action.Action in project alfresco-remote-api by Alfresco.
the class RulePut method updateActionFromJson.
protected Action updateActionFromJson(JSONObject jsonAction, ActionImpl actionToUpdate) throws JSONException {
ActionImpl result = null;
if (jsonAction.has("id")) {
// update existing action
result = actionToUpdate;
} else {
// create new object as id was not sent by client
result = parseJsonAction(jsonAction);
return result;
}
if (jsonAction.has("description")) {
result.setDescription(jsonAction.getString("description"));
}
if (jsonAction.has("title")) {
result.setTitle(jsonAction.getString("title"));
}
if (jsonAction.has("parameterValues")) {
JSONObject jsonParameterValues = jsonAction.getJSONObject("parameterValues");
result.setParameterValues(parseJsonParameterValues(jsonParameterValues, result.getActionDefinitionName(), true));
}
if (jsonAction.has("executeAsync")) {
result.setExecuteAsynchronously(jsonAction.getBoolean("executeAsync"));
}
if (jsonAction.has("runAsUser")) {
result.setRunAsUser(jsonAction.getString("runAsUser"));
}
if (jsonAction.has("actions")) {
JSONArray jsonActions = jsonAction.getJSONArray("actions");
if (jsonActions.length() == 0) {
// empty array was sent -> clear list
((CompositeActionImpl) result).getActions().clear();
} else {
List<Action> existingActions = ((CompositeActionImpl) result).getActions();
List<Action> newActions = new ArrayList<Action>();
for (int i = 0; i < jsonActions.length(); i++) {
JSONObject innerJsonAction = jsonActions.getJSONObject(i);
if (innerJsonAction.has("id")) {
// update existing object
String actionId = innerJsonAction.getString("id");
Action existingAction = getAction(existingActions, actionId);
existingActions.remove(existingAction);
Action updatedAction = updateActionFromJson(innerJsonAction, (ActionImpl) existingAction);
newActions.add(updatedAction);
} else {
// create new action as id was not sent
newActions.add(parseJsonAction(innerJsonAction));
}
}
existingActions.clear();
for (Action action : newActions) {
existingActions.add(action);
}
}
}
if (jsonAction.has("conditions")) {
JSONArray jsonConditions = jsonAction.getJSONArray("conditions");
if (jsonConditions.length() == 0) {
// empty array was sent -> clear list
result.getActionConditions().clear();
} else {
List<ActionCondition> existingConditions = result.getActionConditions();
List<ActionCondition> newConditions = new ArrayList<ActionCondition>();
for (int i = 0; i < jsonConditions.length(); i++) {
JSONObject jsonCondition = jsonConditions.getJSONObject(i);
if (jsonCondition.has("id")) {
// update existing object
String conditionId = jsonCondition.getString("id");
ActionCondition existingCondition = getCondition(existingConditions, conditionId);
existingConditions.remove(existingCondition);
ActionCondition updatedActionCondition = updateActionConditionFromJson(jsonCondition, (ActionConditionImpl) existingCondition);
newConditions.add(updatedActionCondition);
} else {
// create new object as id was not sent
newConditions.add(parseJsonActionCondition(jsonCondition));
}
}
existingConditions.clear();
for (ActionCondition condition : newConditions) {
existingConditions.add(condition);
}
}
}
if (jsonAction.has("compensatingAction")) {
JSONObject jsonCompensatingAction = jsonAction.getJSONObject("compensatingAction");
Action compensatingAction = updateActionFromJson(jsonCompensatingAction, (ActionImpl) actionToUpdate.getCompensatingAction());
actionToUpdate.setCompensatingAction(compensatingAction);
}
return result;
}
use of org.alfresco.service.cmr.action.Action in project records-management by Alfresco.
the class RM2190Test method testUploadDocumentsSimultaneouslyWithRules.
public void testUploadDocumentsSimultaneouslyWithRules() {
doTestInTransaction(new Test<Void>() {
@Override
public Void run() {
rootFolder = fileFolderService.create(documentLibrary, generate(), TYPE_FOLDER).getNodeRef();
Action createAction = actionService.createAction(CreateRecordAction.NAME);
createAction.setParameterValue(CreateRecordAction.PARAM_FILE_PLAN, filePlan);
Rule declareRule = new Rule();
declareRule.setRuleType(INBOUND);
declareRule.setTitle(generate());
declareRule.setAction(createAction);
declareRule.setExecuteAsynchronously(true);
declareRule.applyToChildren(true);
ruleService.saveRule(rootFolder, declareRule);
folder1 = fileFolderService.create(rootFolder, generate(), TYPE_FOLDER).getNodeRef();
folder2 = fileFolderService.create(rootFolder, generate(), TYPE_FOLDER).getNodeRef();
Action fileAction = actionService.createAction(FileToAction.NAME);
fileAction.setParameterValue(FileToAction.PARAM_PATH, PATH);
fileAction.setParameterValue(FileToAction.PARAM_CREATE_RECORD_PATH, true);
Rule fileRule = new Rule();
fileRule.setRuleType(INBOUND);
fileRule.setTitle(generate());
fileRule.setAction(fileAction);
fileRule.setExecuteAsynchronously(true);
ruleService.saveRule(unfiledContainer, fileRule);
return null;
}
@Override
public void test(Void result) throws Exception {
assertFalse(ruleService.getRules(rootFolder).isEmpty());
assertFalse(ruleService.getRules(unfiledContainer).isEmpty());
}
});
doTestInTransaction(new Test<Void>() {
@Override
public Void run() throws FileNotFoundException, InterruptedException {
Thread thread1 = new Thread() {
public void run() {
List<NodeRef> files = addFilesToFolder(folder1);
waitForFilesToBeDeclared(files);
}
};
Thread thread2 = new Thread() {
public void run() {
List<NodeRef> files = addFilesToFolder(folder2);
waitForFilesToBeDeclared(files);
}
};
thread1.start();
thread2.start();
thread1.join(300000);
thread2.join(300000);
return null;
}
@Override
public void test(Void result) throws Exception {
FileInfo category = fileFolderService.resolveNamePath(filePlan, asList(tokenizeToStringArray(PATH, "/", false, true)));
assertEquals(NUMBER_IN_BATCH * 2, nodeService.getChildAssocs(category.getNodeRef()).size());
}
});
}
use of org.alfresco.service.cmr.action.Action in project records-management by Alfresco.
the class RM4163Test method testDeclareRecordsConcurently.
public void testDeclareRecordsConcurently() throws Exception {
doTestInTransaction(new Test<Void>() {
@Override
public Void run() {
// create the folder
ruleFolder = fileFolderService.create(documentLibrary, "mytestfolder", ContentModel.TYPE_FOLDER).getNodeRef();
// create record category
nodeRefCategory1 = filePlanService.createRecordCategory(filePlan, "category1");
// define declare as record rule and apply it to the created folder from documentLibrary
Action action = actionService.createAction(CreateRecordAction.NAME);
action.setParameterValue(CreateRecordAction.PARAM_FILE_PLAN, filePlan);
Rule rule = new Rule();
rule.setRuleType(RuleType.INBOUND);
rule.setTitle("declareAsRecordRule");
rule.setAction(action);
rule.setExecuteAsynchronously(true);
ruleService.saveRule(ruleFolder, rule);
// define filing rule and apply it to unfiled record container
Action fileAction = actionService.createAction(FileToAction.NAME);
fileAction.setParameterValue(FileToAction.PARAM_PATH, "/category1/{node.cm:description}");
fileAction.setParameterValue(FileToAction.PARAM_CREATE_RECORD_PATH, true);
Rule fileRule = new Rule();
fileRule.setRuleType(RuleType.INBOUND);
fileRule.setTitle("filingRule");
fileRule.setAction(fileAction);
fileRule.setExecuteAsynchronously(true);
ruleService.saveRule(filePlanService.getUnfiledContainer(filePlan), fileRule);
return null;
}
@Override
public void test(Void result) throws Exception {
assertFalse(ruleService.getRules(ruleFolder).isEmpty());
}
});
// create 4 documents in documentLibrary
List<NodeRef> documents = new ArrayList<NodeRef>(4);
documents.addAll(doTestInTransaction(new Test<List<NodeRef>>() {
@Override
public List<NodeRef> run() throws Exception {
List<NodeRef> documents = new ArrayList<NodeRef>(4);
NodeRef document = createFile(documentLibrary, "document1.txt", "desc1", ContentModel.TYPE_CONTENT);
documents.add(document);
document = createFile(documentLibrary, "document2.txt", "desc2", ContentModel.TYPE_CONTENT);
documents.add(document);
document = createFile(documentLibrary, "document3.txt", "desc1", ContentModel.TYPE_CONTENT);
documents.add(document);
document = createFile(documentLibrary, "document4.txt", "desc1", ContentModel.TYPE_CONTENT);
documents.add(document);
return documents;
}
}));
// move created documents in the folder that has Declare as Record rule
final Iterator<NodeRef> temp = documents.iterator();
doTestInTransaction(new Test<Void>() {
@Override
public Void run() throws Exception {
while (temp.hasNext()) {
NodeRef document = temp.next();
fileFolderService.move(document, ruleFolder, null);
}
return null;
}
});
// give enough time for filing all records
Thread.sleep(5000);
// check that target category has in created record folders 4 records
Integer numberOfRecords = AuthenticationUtil.runAsSystem(new RunAsWork<Integer>() {
@Override
public Integer doWork() throws Exception {
List<NodeRef> containedRecordFolders = filePlanService.getContainedRecordFolders(nodeRefCategory1);
int numberOfRecords = 0;
for (NodeRef recordFolder : containedRecordFolders) {
numberOfRecords = numberOfRecords + fileFolderService.list(recordFolder).size();
}
return numberOfRecords;
}
});
assertEquals(4, numberOfRecords.intValue());
}
Aggregations