Search in sources :

Example 11 with Rule

use of org.alfresco.service.cmr.rule.Rule 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;
}
Also used : Action(org.alfresco.service.cmr.action.Action) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Rule(org.alfresco.service.cmr.rule.Rule)

Example 12 with Rule

use of org.alfresco.service.cmr.rule.Rule in project alfresco-remote-api by Alfresco.

the class InheritedRulesGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    // get request parameters
    NodeRef nodeRef = parseRequestForNodeRef(req);
    String ruleType = req.getParameter("ruleType");
    RuleType type = ruleService.getRuleType(ruleType);
    if (type == null) {
        ruleType = null;
    }
    // get all rules (including inherited) filtered by rule type
    List<Rule> inheritedRules = ruleService.getRules(nodeRef, true, ruleType);
    // get owned rules (excluding inherited) filtered by rule type
    List<Rule> ownedRules = ruleService.getRules(nodeRef, false, ruleType);
    // remove owned rules
    inheritedRules.removeAll(ownedRules);
    List<RuleRef> inheritedRuleRefs = new ArrayList<RuleRef>();
    for (Rule rule : inheritedRules) {
        inheritedRuleRefs.add(new RuleRef(rule, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(rule))));
    }
    model.put("inheritedRuleRefs", inheritedRuleRefs);
    return model;
}
Also used : RuleRef(org.alfresco.repo.web.scripts.rule.ruleset.RuleRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RuleType(org.alfresco.service.cmr.rule.RuleType) Rule(org.alfresco.service.cmr.rule.Rule)

Example 13 with Rule

use of org.alfresco.service.cmr.rule.Rule in project alfresco-remote-api by Alfresco.

the class RulePost method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    // get request parameters
    NodeRef nodeRef = parseRequestForNodeRef(req);
    Rule rule = null;
    JSONObject json = null;
    try {
        // read request json
        json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // parse request json
        rule = parseJsonRule(json);
        // check the rule
        checkRule(rule);
        // create rule
        ruleService.saveRule(nodeRef, rule);
        RuleRef ruleRef = new RuleRef(rule, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(rule)));
        model.put("ruleRef", ruleRef);
    } catch (IOException iox) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
    }
    return model;
}
Also used : JSONTokener(org.json.JSONTokener) RuleRef(org.alfresco.repo.web.scripts.rule.ruleset.RuleRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Rule(org.alfresco.service.cmr.rule.Rule) IOException(java.io.IOException)

Example 14 with Rule

use of org.alfresco.service.cmr.rule.Rule in project alfresco-remote-api by Alfresco.

the class RulePut method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    // get request parameters
    NodeRef nodeRef = parseRequestForNodeRef(req);
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    String ruleId = templateVars.get("rule_id");
    Rule ruleToUpdate = null;
    // get all rules for given nodeRef
    List<Rule> rules = ruleService.getRules(nodeRef);
    // filter by rule id
    for (Rule rule : rules) {
        if (rule.getNodeRef().getId().equalsIgnoreCase(ruleId)) {
            ruleToUpdate = rule;
            break;
        }
    }
    if (ruleToUpdate == null) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find rule with id: " + ruleId);
    }
    JSONObject json = null;
    try {
        // read request json
        json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // parse request json
        updateRuleFromJSON(json, ruleToUpdate);
        // check the rule
        checkRule(ruleToUpdate);
        // save changes
        ruleService.saveRule(nodeRef, ruleToUpdate);
        RuleRef updatedRuleRef = new RuleRef(ruleToUpdate, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(ruleToUpdate)));
        model.put("ruleRef", updatedRuleRef);
    } catch (IOException iox) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
    }
    return model;
}
Also used : HashMap(java.util.HashMap) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONTokener(org.json.JSONTokener) RuleRef(org.alfresco.repo.web.scripts.rule.ruleset.RuleRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) Rule(org.alfresco.service.cmr.rule.Rule)

Example 15 with Rule

use of org.alfresco.service.cmr.rule.Rule in project alfresco-remote-api by Alfresco.

the class RulesGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    // get request parameters
    NodeRef nodeRef = parseRequestForNodeRef(req);
    String ruleType = req.getParameter("ruleType");
    RuleType type = ruleService.getRuleType(ruleType);
    if (type == null) {
        ruleType = null;
    }
    // get all rules (excluding inherited) filtered by rule type
    List<Rule> rules = ruleService.getRules(nodeRef, false, ruleType);
    List<RuleRef> ruleRefs = new ArrayList<RuleRef>();
    for (Rule rule : rules) {
        ruleRefs.add(new RuleRef(rule, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(rule))));
    }
    model.put("ruleRefs", ruleRefs);
    return model;
}
Also used : RuleRef(org.alfresco.repo.web.scripts.rule.ruleset.RuleRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RuleType(org.alfresco.service.cmr.rule.RuleType) Rule(org.alfresco.service.cmr.rule.Rule)

Aggregations

Rule (org.alfresco.service.cmr.rule.Rule)20 NodeRef (org.alfresco.service.cmr.repository.NodeRef)13 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 Action (org.alfresco.service.cmr.action.Action)7 RuleRef (org.alfresco.repo.web.scripts.rule.ruleset.RuleRef)6 CreateRecordAction (org.alfresco.module.org_alfresco_module_rm.action.dm.CreateRecordAction)5 FileToAction (org.alfresco.module.org_alfresco_module_rm.action.impl.FileToAction)5 JSONObject (org.json.JSONObject)5 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)5 List (java.util.List)3 RuleType (org.alfresco.service.cmr.rule.RuleType)3 IOException (java.io.IOException)2 CompositeAction (org.alfresco.service.cmr.action.CompositeAction)2 Node (org.alfresco.web.bean.repository.Node)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONTokener (org.json.JSONTokener)2 Arrays.asList (java.util.Arrays.asList)1 Date (java.util.Date)1