Search in sources :

Example 86 with InvalidArgumentException

use of org.alfresco.rest.framework.core.exceptions.InvalidArgumentException in project alfresco-remote-api by Alfresco.

the class ProcessesImpl method updateVariableInProcess.

protected Variable updateVariableInProcess(String processId, String processDefinitionId, Variable variable) {
    if (variable.getName() == null) {
        throw new InvalidArgumentException("Variable name is required.");
    }
    // Get start-task definition for explicit typing of variables submitted at the start
    String formKey = null;
    StartFormData startFormData = activitiProcessEngine.getFormService().getStartFormData(processDefinitionId);
    if (startFormData != null) {
        formKey = startFormData.getFormKey();
    }
    DataTypeDefinition dataTypeDefinition = null;
    TypeDefinition startTaskTypeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(formKey, true);
    TypeDefinitionContext context = new TypeDefinitionContext(startTaskTypeDefinition, getQNameConverter());
    if (context.getPropertyDefinition(variable.getName()) != null) {
        dataTypeDefinition = context.getPropertyDefinition(variable.getName()).getDataType();
        if (variable.getType() != null && dataTypeDefinition.getName().toPrefixString(namespaceService).equals(variable.getType()) == false) {
            throw new InvalidArgumentException("type of variable " + variable.getName() + " should be " + dataTypeDefinition.getName().toPrefixString(namespaceService));
        }
    } else if (context.getAssociationDefinition(variable.getName()) != null) {
        dataTypeDefinition = dictionaryService.getDataType(DataTypeDefinition.NODE_REF);
    }
    if (dataTypeDefinition == null && variable.getType() != null) {
        try {
            QName dataType = QName.createQName(variable.getType(), namespaceService);
            dataTypeDefinition = dictionaryService.getDataType(dataType);
        } catch (InvalidQNameException iqne) {
            throw new InvalidArgumentException("Unsupported type of variable: '" + variable.getType() + "'.");
        }
    } else if (dataTypeDefinition == null) {
        // Fallback to raw value when no type has been passed and not present in model
        dataTypeDefinition = dictionaryService.getDataType(restVariableHelper.extractTypeFromValue(variable.getValue()));
    }
    if (dataTypeDefinition == null) {
        throw new InvalidArgumentException("Unsupported type of variable: '" + variable.getType() + "'.");
    }
    Object actualValue = null;
    if ("java.util.Date".equalsIgnoreCase(dataTypeDefinition.getJavaClassName())) {
        // fix for different ISO 8601 Date format classes in Alfresco (org.alfresco.util and Spring Surf)
        actualValue = ISO8601DateFormat.parse((String) variable.getValue());
    } else if (variable.getName().equals(WorkflowConstants.PROP_INITIATOR)) {
        // update the initiator if exists
        NodeRef initiator = getNodeRef((String) variable.getValue());
        if (nodeService.exists(initiator)) {
            actualValue = getNodeConverter().convertNode(initiator);
            // Also update the initiator home reference, if one exists
            NodeRef initiatorHome = (NodeRef) nodeService.getProperty(initiator, ContentModel.PROP_HOMEFOLDER);
            if (initiatorHome != null) {
                Variable initiatorHomeVar = new Variable();
                initiatorHomeVar.setName(WorkflowConstants.PROP_INITIATOR_HOME);
                initiatorHomeVar.setValue(initiatorHome);
                updateVariableInProcess(processId, processDefinitionId, initiatorHomeVar);
            }
        } else {
            throw new InvalidArgumentException("Variable value should be a valid person NodeRef.");
        }
    } else {
        if (context.getAssociationDefinition(variable.getName()) != null) {
            actualValue = convertAssociationDefinitionValue(context.getAssociationDefinition(variable.getName()), variable.getName(), variable.getValue());
        } else {
            actualValue = DefaultTypeConverter.INSTANCE.convert(dataTypeDefinition, variable.getValue());
        }
    }
    activitiProcessEngine.getRuntimeService().setVariable(processId, variable.getName(), actualValue);
    // Variable value needs to be of type NodeRef
    if (actualValue instanceof ActivitiScriptNode) {
        variable.setValue(((ActivitiScriptNode) actualValue).getNodeRef());
    } else {
        variable.setValue(actualValue);
    }
    // Set actual used type before returning
    variable.setType(dataTypeDefinition.getName().toPrefixString(namespaceService));
    return variable;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Variable(org.alfresco.rest.workflow.api.model.Variable) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) InvalidQNameException(org.alfresco.service.namespace.InvalidQNameException) QName(org.alfresco.service.namespace.QName) StartFormData(org.activiti.engine.form.StartFormData) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition)

Example 87 with InvalidArgumentException

use of org.alfresco.rest.framework.core.exceptions.InvalidArgumentException in project alfresco-remote-api by Alfresco.

the class ProcessesImpl method convertAssociationDefinitionValue.

protected Object convertAssociationDefinitionValue(AssociationDefinition associationDef, String variableName, Object variableValue) {
    if (variableValue != null && ContentModel.TYPE_PERSON.equals(associationDef.getTargetClass().getName())) {
        if (associationDef.isTargetMany()) {
            if (variableValue instanceof List<?>) {
                List<NodeRef> personList = new ArrayList<NodeRef>();
                List<?> values = (List<?>) variableValue;
                for (Object value : values) {
                    NodeRef personRef = getPersonNodeRef(value.toString());
                    if (personRef == null) {
                        throw new InvalidArgumentException(value.toString() + " is not a valid person user id");
                    }
                    personList.add(personRef);
                }
                variableValue = personList;
            } else {
                throw new InvalidArgumentException(variableName + " should have an array value");
            }
        } else {
            NodeRef personRef = getPersonNodeRef(variableValue.toString());
            if (personRef == null) {
                throw new InvalidArgumentException(variableValue.toString() + " is not a valid person user id");
            }
            variableValue = personRef;
        }
    } else if (variableValue != null && ContentModel.TYPE_AUTHORITY_CONTAINER.equals(associationDef.getTargetClass().getName())) {
        if (associationDef.isTargetMany()) {
            if (variableValue instanceof List<?>) {
                List<NodeRef> authorityList = new ArrayList<NodeRef>();
                List<?> values = (List<?>) variableValue;
                for (Object value : values) {
                    NodeRef authorityRef = authorityService.getAuthorityNodeRef(value.toString());
                    if (authorityRef == null) {
                        throw new InvalidArgumentException(value.toString() + " is not a valid authority id");
                    }
                    authorityList.add(authorityRef);
                }
                variableValue = authorityList;
            } else {
                throw new InvalidArgumentException(variableName + " should have an array value");
            }
        } else {
            NodeRef authorityRef = authorityService.getAuthorityNodeRef(variableValue.toString());
            if (authorityRef == null) {
                throw new InvalidArgumentException(variableValue.toString() + " is not a valid authority id");
            }
            variableValue = authorityRef;
        }
    }
    return variableValue;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 88 with InvalidArgumentException

use of org.alfresco.rest.framework.core.exceptions.InvalidArgumentException in project alfresco-remote-api by Alfresco.

the class InfoWebScriptGet method execute.

@Override
public void execute(final Api api, WebScriptRequest req, WebScriptResponse res) throws IOException {
    ResourceDictionary resourceDic = lookupDictionary.getDictionary();
    final Map<String, ResourceWithMetadata> apiResources = resourceDic.getAllResources().get(api);
    if (apiResources == null) {
        throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API);
    }
    assistant.getJsonHelper().withWriter(res.getOutputStream(), new Writer() {

        @Override
        public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
            List<ExecutionResult> entities = new ArrayList<ExecutionResult>();
            for (ResourceWithMetadata resource : apiResources.values()) {
                entities.add(new ExecutionResult(resource.getMetaData(), null));
            }
            Collections.sort(entities, new Comparator<ExecutionResult>() {

                public int compare(ExecutionResult r1, ExecutionResult r2) {
                    return ((ResourceMetadata) r1.getRoot()).getUniqueId().compareTo(((ResourceMetadata) r2.getRoot()).getUniqueId());
                }
            });
            objectMapper.writeValue(generator, CollectionWithPagingInfo.asPaged(Paging.DEFAULT, entities));
        }
    });
}
Also used : ResourceDictionary(org.alfresco.rest.framework.core.ResourceDictionary) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) IOException(java.io.IOException) Comparator(java.util.Comparator) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ArrayList(java.util.ArrayList) List(java.util.List) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Writer(org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 89 with InvalidArgumentException

use of org.alfresco.rest.framework.core.exceptions.InvalidArgumentException in project alfresco-remote-api by Alfresco.

the class DeploymentsImpl method getDeployment.

@Override
public Deployment getDeployment(String deploymentId) {
    // Only admin-user is allowed to get deployments
    if (!authorityService.isAdminAuthority(AuthenticationUtil.getRunAsUser())) {
        throw new PermissionDeniedException();
    }
    RepositoryService repositoryService = activitiProcessEngine.getRepositoryService();
    DeploymentQuery query = repositoryService.createDeploymentQuery().deploymentId(deploymentId);
    if (tenantService.isEnabled() && deployWorkflowsInTenant) {
        query.processDefinitionKeyLike("@" + TenantUtil.getCurrentDomain() + "@%");
    }
    org.activiti.engine.repository.Deployment deployment = null;
    try {
        deployment = query.singleResult();
    } catch (ActivitiException e) {
        // The next exception will cause a response status 400: Bad request
        throw new InvalidArgumentException("Invalid deployment id: " + deploymentId);
    }
    if (deployment == null) {
        // The next exception will cause a response status 404: Not found
        throw new EntityNotFoundException(deploymentId);
    }
    Deployment deploymentRest = new Deployment(deployment);
    return deploymentRest;
}
Also used : DeploymentQuery(org.activiti.engine.repository.DeploymentQuery) ActivitiException(org.activiti.engine.ActivitiException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Deployment(org.alfresco.rest.workflow.api.model.Deployment) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) RepositoryService(org.activiti.engine.RepositoryService)

Example 90 with InvalidArgumentException

use of org.alfresco.rest.framework.core.exceptions.InvalidArgumentException in project alfresco-remote-api by Alfresco.

the class WorkflowRestImpl method deleteItemFromProcess.

/**
 *  Delete an item from the process package variable
 */
public void deleteItemFromProcess(String itemId, String processId) {
    NodeRef nodeRef = getNodeRef(itemId);
    ActivitiScriptNode packageScriptNode = null;
    try {
        packageScriptNode = (ActivitiScriptNode) activitiProcessEngine.getRuntimeService().getVariable(processId, BPM_PACKAGE);
    } catch (ActivitiObjectNotFoundException e) {
        throw new EntityNotFoundException(processId);
    }
    if (packageScriptNode == null) {
        throw new InvalidArgumentException("process doesn't contain a workflow package variable");
    }
    boolean itemIdFoundInPackage = false;
    List<ChildAssociationRef> documentList = nodeService.getChildAssocs(packageScriptNode.getNodeRef());
    for (ChildAssociationRef childAssociationRef : documentList) {
        if (childAssociationRef.getChildRef().equals(nodeRef)) {
            itemIdFoundInPackage = true;
            break;
        }
    }
    if (itemIdFoundInPackage == false) {
        throw new EntityNotFoundException("Item " + itemId + " not found in the process package variable");
    }
    try {
        nodeService.removeChild(packageScriptNode.getNodeRef(), nodeRef);
        activitiWorkflowEngine.dispatchPackageUpdatedEvent(packageScriptNode, null, null, processId, null);
    } catch (InvalidNodeRefException e) {
        throw new EntityNotFoundException("Item " + itemId + " not found");
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Aggregations

InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)132 NodeRef (org.alfresco.service.cmr.repository.NodeRef)39 QName (org.alfresco.service.namespace.QName)37 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)31 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)24 ArrayList (java.util.ArrayList)22 Test (org.junit.Test)20 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)17 Serializable (java.io.Serializable)12 SortColumn (org.alfresco.rest.framework.resource.parameters.SortColumn)12 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)11 Paging (org.alfresco.rest.framework.resource.parameters.Paging)10 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)9 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)9 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)9 Pair (org.alfresco.util.Pair)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Params (org.alfresco.rest.framework.resource.parameters.Params)8 RelationshipResourceNotFoundException (org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException)7