Search in sources :

Example 1 with RecordsManagementActionResult

use of org.alfresco.module.org_alfresco_module_rm.action.RecordsManagementActionResult in project records-management by Alfresco.

the class RmActionPost method executeImpl.

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    String reqContentAsString;
    try {
        reqContentAsString = req.getContent().getContent();
    } catch (IOException iox) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
    }
    String actionName = null;
    List<NodeRef> targetNodeRefs = new ArrayList<NodeRef>(1);
    Map<String, Serializable> actionParams = new HashMap<String, Serializable>(3);
    try {
        JSONObject jsonObj = new JSONObject(new JSONTokener(reqContentAsString));
        // Get the action name
        if (jsonObj.has(PARAM_NAME)) {
            actionName = jsonObj.getString(PARAM_NAME);
        }
        // Get the target references
        if (jsonObj.has(PARAM_NODE_REF)) {
            NodeRef nodeRef = new NodeRef(jsonObj.getString(PARAM_NODE_REF));
            targetNodeRefs.add(nodeRef);
        }
        if (jsonObj.has(PARAM_NODE_REFS)) {
            JSONArray jsonArray = jsonObj.getJSONArray(PARAM_NODE_REFS);
            if (jsonArray.length() != 0) {
                targetNodeRefs = new ArrayList<NodeRef>(jsonArray.length());
                for (int i = 0; i < jsonArray.length(); i++) {
                    NodeRef nodeRef = new NodeRef(jsonArray.getString(i));
                    targetNodeRefs.add(nodeRef);
                }
            }
        }
        // params are optional.
        if (jsonObj.has(PARAM_PARAMS)) {
            JSONObject paramsObj = jsonObj.getJSONObject(PARAM_PARAMS);
            for (Iterator<String> iter = paramsObj.keys(); iter.hasNext(); ) {
                String nextKeyString = iter.next();
                Object nextValue = paramsObj.get(nextKeyString);
                // Check for date values
                if ((nextValue instanceof JSONObject) && ((JSONObject) nextValue).has("iso8601")) {
                    String dateStringValue = ((JSONObject) nextValue).getString("iso8601");
                    nextValue = ISO8601DateFormat.parse(dateStringValue);
                }
                actionParams.put(nextKeyString, (Serializable) nextValue);
            }
        }
    } catch (JSONException exception) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unable to parse request JSON.", exception);
    }
    // Some RM actions can be posted without a nodeRef.
    if (actionName == null) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "A mandatory parameter has not been provided in URL");
    }
    // Check that all the nodes provided exist and build report string
    StringBuilder targetNodeRefsString = new StringBuilder(30);
    boolean firstTime = true;
    for (NodeRef targetNodeRef : targetNodeRefs) {
        if (!nodeService.exists(targetNodeRef)) {
            throw new WebScriptException(Status.STATUS_NOT_FOUND, "The targetNode does not exist (" + targetNodeRef.toString() + ")");
        }
        // Build the string
        if (firstTime) {
            firstTime = false;
        } else {
            targetNodeRefsString.append(", ");
        }
        targetNodeRefsString.append(targetNodeRef.toString());
    }
    // Proceed to execute the specified action on the specified node.
    if (logger.isDebugEnabled()) {
        StringBuilder msg = new StringBuilder();
        msg.append("Executing Record Action ").append(actionName).append(", (").append(targetNodeRefsString.toString()).append("), ").append(actionParams);
        logger.debug(msg.toString());
    }
    Map<String, Object> model = new HashMap<String, Object>();
    if (targetNodeRefs.isEmpty()) {
        RecordsManagementActionResult result = this.rmActionService.executeRecordsManagementAction(actionName, actionParams);
        if (result.getValue() != null) {
            model.put("result", result.getValue().toString());
        }
    } else {
        Map<NodeRef, RecordsManagementActionResult> resultMap = this.rmActionService.executeRecordsManagementAction(targetNodeRefs, actionName, actionParams);
        Map<String, String> results = new HashMap<String, String>(resultMap.size());
        for (Map.Entry<NodeRef, RecordsManagementActionResult> entry : resultMap.entrySet()) {
            Object value = entry.getValue().getValue();
            if (value != null) {
                results.put(entry.getKey().toString(), value.toString());
            }
        }
        model.put("results", results);
    }
    model.put("message", "Successfully queued action [" + actionName + "] on " + targetNodeRefsString.toString());
    return model;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) RecordsManagementActionResult(org.alfresco.module.org_alfresco_module_rm.action.RecordsManagementActionResult) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with RecordsManagementActionResult

use of org.alfresco.module.org_alfresco_module_rm.action.RecordsManagementActionResult in project records-management by Alfresco.

the class ReportServiceImplTest method getTransferObject.

/**
 * Helper method for creating a transfer object
 *
 * @return Node reference of the transfer object
 */
private NodeRef getTransferObject() {
    NodeRef recordCategory = filePlanService.createRecordCategory(filePlan, GUID.generate());
    utils.createDispositionSchedule(recordCategory, CommonRMTestUtils.DEFAULT_DISPOSITION_INSTRUCTIONS, CommonRMTestUtils.DEFAULT_DISPOSITION_AUTHORITY, // record level
    false, // set the default actions
    true, // extended disposition schedule
    true);
    NodeRef recordFolder = recordFolderService.createRecordFolder(recordCategory, GUID.generate());
    // Set the record folder identifier
    String identifier = identifierService.generateIdentifier(TYPE_RECORD_FOLDER, recordCategory);
    nodeService.setProperty(recordFolder, PROP_IDENTIFIER, identifier);
    // Complete event
    Map<String, Serializable> params = new HashMap<String, Serializable>(1);
    params.put(CompleteEventAction.PARAM_EVENT_NAME, CommonRMTestUtils.DEFAULT_EVENT_NAME);
    rmActionService.executeRecordsManagementAction(recordFolder, CompleteEventAction.NAME, params);
    // Cut off folder
    rmActionService.executeRecordsManagementAction(recordFolder, CutOffAction.NAME);
    // Transfer folder
    RecordsManagementActionResult transferAction = rmActionService.executeRecordsManagementAction(recordFolder, TransferAction.NAME);
    NodeRef transferObject = (NodeRef) transferAction.getValue();
    assertTrue(transferObject != null);
    return transferObject;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) HashMap(java.util.HashMap) RecordsManagementActionResult(org.alfresco.module.org_alfresco_module_rm.action.RecordsManagementActionResult)

Aggregations

Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 RecordsManagementActionResult (org.alfresco.module.org_alfresco_module_rm.action.RecordsManagementActionResult)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1