Search in sources :

Example 1 with CompositeActionImpl

use of org.alfresco.repo.action.CompositeActionImpl in project alfresco-remote-api by Alfresco.

the class AbstractRuleWebScript method parseJsonAction.

protected ActionImpl parseJsonAction(JSONObject jsonAction) throws JSONException {
    ActionImpl result = null;
    String actionId = jsonAction.has("id") ? jsonAction.getString("id") : GUID.generate();
    if (jsonAction.getString("actionDefinitionName").equalsIgnoreCase("composite-action")) {
        result = new CompositeActionImpl(null, actionId);
    } else {
        result = new ActionImpl(null, actionId, jsonAction.getString("actionDefinitionName"));
    }
    // Post Action Queue parameter
    if (jsonAction.has("actionedUponNode")) {
        NodeRef actionedUponNode = new NodeRef(jsonAction.getString("actionedUponNode"));
        result.setNodeRef(actionedUponNode);
    }
    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");
        for (int i = 0; i < jsonActions.length(); i++) {
            JSONObject innerJsonAction = jsonActions.getJSONObject(i);
            Action innerAction = parseJsonAction(innerJsonAction);
            // we assume that only composite-action contains actions json array, so should be no cast exception
            ((CompositeActionImpl) result).addAction(innerAction);
        }
    }
    if (jsonAction.has("conditions")) {
        JSONArray jsonConditions = jsonAction.getJSONArray("conditions");
        for (int i = 0; i < jsonConditions.length(); i++) {
            JSONObject jsonCondition = jsonConditions.getJSONObject(i);
            // parse action conditions
            ActionCondition actionCondition = parseJsonActionCondition(jsonCondition);
            result.getActionConditions().add(actionCondition);
        }
    }
    if (jsonAction.has("compensatingAction")) {
        Action compensatingAction = parseJsonAction(jsonAction.getJSONObject("compensatingAction"));
        result.setCompensatingAction(compensatingAction);
    }
    return result;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Action(org.alfresco.service.cmr.action.Action) JSONObject(org.json.JSONObject) ActionImpl(org.alfresco.repo.action.ActionImpl) CompositeActionImpl(org.alfresco.repo.action.CompositeActionImpl) JSONArray(org.json.JSONArray) CompositeActionImpl(org.alfresco.repo.action.CompositeActionImpl) ActionCondition(org.alfresco.service.cmr.action.ActionCondition)

Example 2 with CompositeActionImpl

use of org.alfresco.repo.action.CompositeActionImpl 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;
}
Also used : Action(org.alfresco.service.cmr.action.Action) JSONObject(org.json.JSONObject) CompositeActionImpl(org.alfresco.repo.action.CompositeActionImpl) ActionImpl(org.alfresco.repo.action.ActionImpl) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) CompositeActionImpl(org.alfresco.repo.action.CompositeActionImpl) ActionCondition(org.alfresco.service.cmr.action.ActionCondition)

Aggregations

ActionImpl (org.alfresco.repo.action.ActionImpl)2 CompositeActionImpl (org.alfresco.repo.action.CompositeActionImpl)2 Action (org.alfresco.service.cmr.action.Action)2 ActionCondition (org.alfresco.service.cmr.action.ActionCondition)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 ArrayList (java.util.ArrayList)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1