Search in sources :

Example 6 with ActionImpl

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

the class ActionQueuePost 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
    boolean async = Boolean.parseBoolean(req.getParameter("async"));
    ActionImpl action = null;
    JSONObject json = null;
    try {
        // read request json
        json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // parse request json
        action = parseJsonAction(json);
        NodeRef actionedUponNode = action.getNodeRef();
        // clear nodeRef for action
        action.setNodeRef(null);
        json.remove("actionedUponNode");
        if (async) {
            model.put(STATUS, STATUS_QUEUED);
        } else {
            model.put(STATUS, STATUS_SUCCESS);
        }
        // Execute action
        actionService.executeAction(action, actionedUponNode, true, async);
        // Prepair model
        model.put("actionedUponNode", actionedUponNode.toString());
        model.put("action", json);
    } 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) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) ActionImpl(org.alfresco.repo.action.ActionImpl) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Example 7 with ActionImpl

use of org.alfresco.repo.action.ActionImpl 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)7 JSONObject (org.json.JSONObject)7 JSONArray (org.json.JSONArray)6 ReplicationDefinition (org.alfresco.service.cmr.replication.ReplicationDefinition)4 Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)4 UserTransaction (javax.transaction.UserTransaction)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 GetRequest (org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)3 CompositeActionImpl (org.alfresco.repo.action.CompositeActionImpl)2 Action (org.alfresco.service.cmr.action.Action)2 ActionCondition (org.alfresco.service.cmr.action.ActionCondition)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1 JSONTokener (org.json.JSONTokener)1 PutRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1