Search in sources :

Example 1 with ParameterizedItemDefinition

use of org.alfresco.service.cmr.action.ParameterizedItemDefinition in project alfresco-remote-api by Alfresco.

the class AbstractRuleWebScript method parseJsonParameterValues.

protected Map<String, Serializable> parseJsonParameterValues(JSONObject jsonParameterValues, String name, boolean isAction) throws JSONException {
    Map<String, Serializable> parameterValues = new HashMap<String, Serializable>();
    // get parameters names
    JSONArray names = jsonParameterValues.names();
    if (names == null) {
        return null;
    }
    // Get the action or condition definition
    ParameterizedItemDefinition definition = null;
    if (isAction == true) {
        definition = actionService.getActionDefinition(name);
    } else {
        definition = actionService.getActionConditionDefinition(name);
    }
    if (definition == null) {
        throw new AlfrescoRuntimeException("Could not find defintion for action/condition " + name);
    }
    for (int i = 0; i < names.length(); i++) {
        String propertyName = names.getString(i);
        Object propertyValue = jsonParameterValues.get(propertyName);
        // Get the parameter definition we care about
        ParameterDefinition paramDef = definition.getParameterDefintion(propertyName);
        if (paramDef == null && !definition.getAdhocPropertiesAllowed()) {
            throw new AlfrescoRuntimeException("Invalid parameter " + propertyName + " for action/condition " + name);
        }
        if (paramDef != null) {
            QName typeQName = paramDef.getType();
            // Convert the property value
            Serializable value = convertValue(typeQName, propertyValue);
            parameterValues.put(propertyName, value);
        } else {
            // If there is no parameter definition we can only rely on the .toString() representation of the ad-hoc property
            parameterValues.put(propertyName, propertyValue.toString());
        }
    }
    return parameterValues;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ParameterizedItemDefinition(org.alfresco.service.cmr.action.ParameterizedItemDefinition) QName(org.alfresco.service.namespace.QName) JSONArray(org.json.JSONArray) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) JSONObject(org.json.JSONObject) ParameterDefinition(org.alfresco.service.cmr.action.ParameterDefinition)

Example 2 with ParameterizedItemDefinition

use of org.alfresco.service.cmr.action.ParameterizedItemDefinition in project alfresco-remote-api by Alfresco.

the class TestActions method canGetActionDefinitionsForNode.

@Test
public void canGetActionDefinitionsForNode() throws Exception {
    final String person1 = account1PersonIt.next();
    publicApiClient.setRequestContext(new RequestContext(account1.getId(), person1));
    // Get the actions available on the -my- node-ref alias
    {
        ListResponse<ActionDefinition> actionDefs = actions.getActionDefinitionsForNode("-my-", emptyParams, 200);
        assertNotNull("Action definition list should not be null", actionDefs);
        assertFalse("Action definition list should not be empty", actionDefs.getList().isEmpty());
        // Check defaults, given that no paging params were sent in the request
        assertEquals(Paging.DEFAULT_MAX_ITEMS, actionDefs.getPaging().getMaxItems().intValue());
        assertEquals(Paging.DEFAULT_SKIP_COUNT, actionDefs.getPaging().getSkipCount().intValue());
        // Check ActionDefinition fields
        List<ActionDefinition> actionDefinitions = actionDefs.getList().stream().filter(ad -> ad.getName().equals("add-features")).collect(Collectors.toList());
        assertEquals(1, actionDefinitions.size());
        ActionDefinition action = actionDefinitions.get(0);
        assertEquals("add-features", action.getId());
        assertEquals("add-features", action.getName());
        assertEquals("Add aspect", action.getTitle());
        assertEquals("This will add an aspect to the matched item.", action.getDescription());
        // Applicable types
        assertEquals(0, action.getApplicableTypes().size());
        assertEquals(false, action.isTrackStatus());
        // Parameter definitions
        assertEquals(1, action.getParameterDefinitions().size());
        ActionDefinition.ParameterDefinition paramDefs = action.getParameterDefinitions().get(0);
        assertEquals(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, paramDefs.getName());
        assertEquals("d:qname", paramDefs.getType());
        assertEquals(true, paramDefs.isMandatory());
        assertEquals("Aspect", paramDefs.getDisplayLabel());
        assertEquals(false, paramDefs.isMultiValued());
        assertEquals("ac-aspects", paramDefs.getParameterConstraintName());
    }
    AuthenticationUtil.setFullyAuthenticatedUser(person1);
    // Get the actions for a "checked out" node - there should be a "check-in" action present.
    // Inspect the fields, to make sure that they're all there. Especially applicableTypes, as
    // this isn't available on any of the actions that appear for the "-my-" alias in the test above.
    {
        NodeRef nodeForCheckout = nodeService.createNode(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, getMyNodeId()), ContentModel.ASSOC_CONTAINS, QName.createQName("test", "test-node-checkedout"), ContentModel.TYPE_CONTENT).getChildRef();
        CheckOutCheckInService coci = applicationContext.getBean("CheckOutCheckInService", CheckOutCheckInService.class);
        coci.checkout(nodeForCheckout);
        ListResponse<ActionDefinition> actionDefs = actions.getActionDefinitionsForNode(nodeForCheckout.getId(), emptyParams, 200);
        List<ActionDefinition> actionDefinitions = actionDefs.getList().stream().filter(ad -> ad.getName().equals("check-in")).collect(Collectors.toList());
        assertEquals(1, actionDefinitions.size());
        ActionDefinition action = actionDefinitions.get(0);
        assertEquals("check-in", action.getId());
        assertEquals("check-in", action.getName());
        assertEquals("Check in", action.getTitle());
        assertEquals("This will check in the matched content.", action.getDescription());
        // Applicable types
        assertEquals(1, action.getApplicableTypes().size());
        assertEquals("cm:content", action.getApplicableTypes().get(0));
        assertEquals(false, action.isTrackStatus());
        // Parameter definitions
        assertEquals(2, action.getParameterDefinitions().size());
        // "description"
        ActionDefinition.ParameterDefinition paramDefs = action.getParameterDefinitions().get(0);
        assertEquals(CheckInActionExecuter.PARAM_DESCRIPTION, paramDefs.getName());
        assertEquals("d:text", paramDefs.getType());
        assertEquals(false, paramDefs.isMandatory());
        assertEquals("Description", paramDefs.getDisplayLabel());
        assertEquals(false, paramDefs.isMultiValued());
        assertEquals(null, paramDefs.getParameterConstraintName());
        // "minorChange"
        paramDefs = action.getParameterDefinitions().get(1);
        assertEquals(CheckInActionExecuter.PARAM_MINOR_CHANGE, paramDefs.getName());
        assertEquals("d:boolean", paramDefs.getType());
        assertEquals(false, paramDefs.isMandatory());
        assertEquals("Minor change", paramDefs.getDisplayLabel());
        assertEquals(false, paramDefs.isMultiValued());
        assertEquals(null, paramDefs.getParameterConstraintName());
    }
    String myNode = getMyNodeId();
    NodeRef validNode = nodeService.createNode(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, myNode), ContentModel.ASSOC_CONTAINS, QName.createQName("test", "test-node"), ContentModel.TYPE_CONTENT).getChildRef();
    // Get the actions available using a specific node ID
    {
        ListResponse<ActionDefinition> actionDefs = actions.getActionDefinitionsForNode(validNode.getId(), emptyParams, 200);
        assertNotNull("Action definition list should not be null", actionDefs);
        assertFalse("Action definition list should not be empty", actionDefs.getList().isEmpty());
    }
    // Basic/default paging and sorting
    checkBasicPagingAndSorting(// Expected
    () -> actionService.getActionDefinitions(validNode).stream().sorted(comparing(org.alfresco.service.cmr.action.ActionDefinition::getName)).map(ParameterizedItemDefinition::getName).collect(Collectors.toList()), // Actual results
    paging -> actions.getActionDefinitionsForNode(validNode.getId(), createParams(paging, null), 200));
    // Test explicit sorting by title
    checkSorting(// Expected
    () -> actionService.getActionDefinitions(validNode).stream().sorted(comparing(org.alfresco.service.cmr.action.ActionDefinition::getTitle, nullsFirst(naturalOrder()))).map(act -> new Pair<>(act.getName(), act.getTitle())).collect(Collectors.toList()), // Actual results
    (paging, orderBy) -> actions.getActionDefinitionsForNode(validNode.getId(), createParams(paging, orderBy), 200), "title");
    // Test explicit sorting by name
    checkSorting(// Expected
    () -> actionService.getActionDefinitions(validNode).stream().sorted(comparing(org.alfresco.service.cmr.action.ActionDefinition::getName, nullsFirst(naturalOrder()))).map(act -> new Pair<>(act.getName(), act.getTitle())).collect(Collectors.toList()), // Actual results
    (paging, orderBy) -> actions.getActionDefinitionsForNode(validNode.getId(), createParams(paging, orderBy), 200), "name");
    // Badly formed request -> 400
    {
        // -1 is not acceptable
        PublicApiClient.Paging paging = getPaging(0, -1);
        actions.getActionDefinitionsForNode(validNode.getId(), createParams(paging, null), 400);
    }
    // Non-existent node ID
    {
        NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "750a2867-ecfa-478c-8343-fa0e39d27be3");
        assertFalse("Test pre-requisite: node must not exist", nodeService.exists(nodeRef));
        actions.getActionDefinitionsForNode(nodeRef.getId(), emptyParams, 404);
    }
    // Unauthorized -> 401
    {
        publicApiClient.setRequestContext(new RequestContext(account1.getId(), person1, "invalid-password"));
        actions.getActionDefinitionsForNode(validNode.getId(), emptyParams, 401);
    }
}
Also used : ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) Paging(org.alfresco.rest.framework.resource.parameters.Paging) ActionDefinition(org.alfresco.rest.api.model.ActionDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) CheckOutCheckInService(org.alfresco.service.cmr.coci.CheckOutCheckInService) ParameterizedItemDefinition(org.alfresco.service.cmr.action.ParameterizedItemDefinition) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 3 with ParameterizedItemDefinition

use of org.alfresco.service.cmr.action.ParameterizedItemDefinition in project alfresco-remote-api by Alfresco.

the class TestActions method canGetActionDefinitions.

@Test
public void canGetActionDefinitions() throws PublicApiException {
    final String person1 = account1PersonIt.next();
    publicApiClient.setRequestContext(new RequestContext(account1.getId(), person1));
    {
        ListResponse<ActionDefinition> actionDefs = actions.getActionDefinitions(emptyParams, 200);
        assertNotNull("Action definition list should not be null", actionDefs);
        assertFalse("Action definition list should not be empty", actionDefs.getList().isEmpty());
        // Check defaults, given that no paging params were sent in the request
        assertEquals(Paging.DEFAULT_MAX_ITEMS, actionDefs.getPaging().getMaxItems().intValue());
        assertEquals(Paging.DEFAULT_SKIP_COUNT, actionDefs.getPaging().getSkipCount().intValue());
        // Check ActionDefinition fields
        List<ActionDefinition> actionDefinitions = actionDefs.getList().stream().filter(ad -> ad.getName().equals("add-features")).collect(Collectors.toList());
        assertEquals(1, actionDefinitions.size());
        ActionDefinition action = actionDefinitions.get(0);
        assertEquals("add-features", action.getId());
        assertEquals("add-features", action.getName());
        assertEquals("Add aspect", action.getTitle());
        assertEquals("This will add an aspect to the matched item.", action.getDescription());
        // Applicable types
        assertEquals(0, action.getApplicableTypes().size());
        assertEquals(false, action.isTrackStatus());
        // Parameter definitions
        assertEquals(1, action.getParameterDefinitions().size());
        ActionDefinition.ParameterDefinition paramDefs = action.getParameterDefinitions().get(0);
        assertEquals(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, paramDefs.getName());
        assertEquals("d:qname", paramDefs.getType());
        assertEquals(true, paramDefs.isMandatory());
        assertEquals("Aspect", paramDefs.getDisplayLabel());
        assertEquals(false, paramDefs.isMultiValued());
        assertEquals("ac-aspects", paramDefs.getParameterConstraintName());
    }
    checkBasicPagingAndSorting(// Expected
    () -> actionService.getActionDefinitions().stream().sorted(comparing(org.alfresco.service.cmr.action.ActionDefinition::getName)).map(ParameterizedItemDefinition::getName).collect(Collectors.toList()), // Actual results
    paging -> actions.getActionDefinitions(createParams(paging, null), 200));
    // Explicit sorting by title
    checkSorting(// Expected
    () -> actionService.getActionDefinitions().stream().sorted(comparing(org.alfresco.service.cmr.action.ActionDefinition::getTitle, nullsFirst(naturalOrder()))).map(act -> new Pair<>(act.getName(), act.getTitle())).collect(Collectors.toList()), // Actual results
    (paging, orderBy) -> actions.getActionDefinitions(createParams(paging, orderBy), 200), "title");
    // Explicit sorting by name
    checkSorting(// Expected
    () -> actionService.getActionDefinitions().stream().sorted(comparing(org.alfresco.service.cmr.action.ActionDefinition::getName, nullsFirst(naturalOrder()))).map(act -> new Pair<>(act.getName(), act.getTitle())).collect(Collectors.toList()), // Actual results
    (paging, orderBy) -> actions.getActionDefinitions(createParams(paging, orderBy), 200), "name");
    // Badly formed request -> 400
    {
        // -1 is not acceptable
        PublicApiClient.Paging paging = getPaging(0, -1);
        actions.getActionDefinitions(createParams(paging, null), 400);
    }
    // Unauthorized -> 401
    {
        publicApiClient.setRequestContext(new RequestContext(account1.getId(), person1, "invalid-password"));
        actions.getActionDefinitions(emptyParams, 401);
    }
}
Also used : ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) ParameterizedItemDefinition(org.alfresco.service.cmr.action.ParameterizedItemDefinition) Paging(org.alfresco.rest.framework.resource.parameters.Paging) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) ActionDefinition(org.alfresco.rest.api.model.ActionDefinition) Test(org.junit.Test)

Aggregations

ParameterizedItemDefinition (org.alfresco.service.cmr.action.ParameterizedItemDefinition)3 List (java.util.List)2 ActionDefinition (org.alfresco.rest.api.model.ActionDefinition)2 ListResponse (org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse)2 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)2 Paging (org.alfresco.rest.framework.resource.parameters.Paging)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 ParameterDefinition (org.alfresco.service.cmr.action.ParameterDefinition)1 CheckOutCheckInService (org.alfresco.service.cmr.coci.CheckOutCheckInService)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 QName (org.alfresco.service.namespace.QName)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1