Search in sources :

Example 36 with TypeDefinition

use of org.alfresco.service.cmr.dictionary.TypeDefinition 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 37 with TypeDefinition

use of org.alfresco.service.cmr.dictionary.TypeDefinition in project alfresco-remote-api by Alfresco.

the class TasksImpl method getTasks.

@Override
public CollectionWithPagingInfo<Task> getTasks(Parameters parameters) {
    Paging paging = parameters.getPaging();
    MapBasedQueryWalker propertyWalker = new MapBasedQueryWalker(TASK_COLLECTION_EQUALS_QUERY_PROPERTIES, TASK_COLLECTION_MATCHES_QUERY_PROPERTIES);
    propertyWalker.setSupportedGreaterThanParameters(TASK_COLLECTION_GREATERTHAN_QUERY_PROPERTIES);
    propertyWalker.setSupportedGreaterThanOrEqualParameters(TASK_COLLECTION_GREATERTHANOREQUAL_QUERY_PROPERTIES);
    propertyWalker.setSupportedLessThanParameters(TASK_COLLECTION_LESSTHAN_QUERY_PROPERTIES);
    propertyWalker.setSupportedLessThanOrEqualParameters(TASK_COLLECTION_LESSTHANOREQUAL_QUERY_PROPERTIES);
    propertyWalker.enableVariablesSupport(namespaceService, dictionaryService);
    if (parameters.getQuery() != null) {
        QueryHelper.walk(parameters.getQuery(), propertyWalker);
    }
    String status = propertyWalker.getProperty("status", WhereClauseParser.EQUALS);
    String assignee = propertyWalker.getProperty("assignee", WhereClauseParser.EQUALS);
    String assigneeLike = propertyWalker.getProperty("assignee", WhereClauseParser.MATCHES);
    String owner = propertyWalker.getProperty("owner", WhereClauseParser.EQUALS);
    String ownerLike = propertyWalker.getProperty("owner", WhereClauseParser.MATCHES);
    String candidateUser = propertyWalker.getProperty("candidateUser", WhereClauseParser.EQUALS);
    String candidateGroup = propertyWalker.getProperty("candidateGroup", WhereClauseParser.EQUALS);
    String name = propertyWalker.getProperty("name", WhereClauseParser.EQUALS);
    String nameLike = propertyWalker.getProperty("name", WhereClauseParser.MATCHES);
    String description = propertyWalker.getProperty("description", WhereClauseParser.EQUALS);
    String descriptionLike = propertyWalker.getProperty("description", WhereClauseParser.MATCHES);
    Integer priority = propertyWalker.getProperty("priority", WhereClauseParser.EQUALS, Integer.class);
    Integer priorityGreaterThanOrEquals = propertyWalker.getProperty("priority", WhereClauseParser.GREATERTHANOREQUALS, Integer.class);
    Integer priorityLessThanOrEquals = propertyWalker.getProperty("priority", WhereClauseParser.LESSTHANOREQUALS, Integer.class);
    String processInstanceId = propertyWalker.getProperty("processId", WhereClauseParser.EQUALS);
    String processInstanceBusinessKey = propertyWalker.getProperty("processBusinessKey", WhereClauseParser.EQUALS);
    String processInstanceBusinessKeyLike = propertyWalker.getProperty("processBusinessKey", WhereClauseParser.MATCHES);
    String activityDefinitionId = propertyWalker.getProperty("activityDefinitionId", WhereClauseParser.EQUALS);
    String activityDefinitionIdLike = propertyWalker.getProperty("activityDefinitionId", WhereClauseParser.MATCHES);
    String processDefinitionId = propertyWalker.getProperty("processDefinitionId", WhereClauseParser.EQUALS);
    String processDefinitionKey = propertyWalker.getProperty("processDefinitionKey", WhereClauseParser.EQUALS);
    String processDefinitionKeyLike = propertyWalker.getProperty("processDefinitionKey", WhereClauseParser.MATCHES);
    String processDefinitionName = propertyWalker.getProperty("processDefinitionName", WhereClauseParser.EQUALS);
    String processDefinitionNameLike = propertyWalker.getProperty("processDefinitionName", WhereClauseParser.MATCHES);
    Date startedAt = propertyWalker.getProperty("startedAt", WhereClauseParser.EQUALS, Date.class);
    Date startedAtGreaterThan = propertyWalker.getProperty("startedAt", WhereClauseParser.GREATERTHAN, Date.class);
    Date startedAtLessThan = propertyWalker.getProperty("startedAt", WhereClauseParser.LESSTHAN, Date.class);
    Date endedAt = propertyWalker.getProperty("endedAt", WhereClauseParser.EQUALS, Date.class);
    Date endedAtGreaterThan = propertyWalker.getProperty("endedAt", WhereClauseParser.GREATERTHAN, Date.class);
    Date endedAtLessThan = propertyWalker.getProperty("endedAt", WhereClauseParser.LESSTHAN, Date.class);
    Date dueAt = propertyWalker.getProperty("dueAt", WhereClauseParser.EQUALS, Date.class);
    Date dueAtGreaterThan = propertyWalker.getProperty("dueAt", WhereClauseParser.GREATERTHAN, Date.class);
    Date dueAtLessThan = propertyWalker.getProperty("dueAt", WhereClauseParser.LESSTHAN, Date.class);
    Boolean includeProcessVariables = propertyWalker.getProperty("includeProcessVariables", WhereClauseParser.EQUALS, Boolean.class);
    Boolean includeTaskVariables = propertyWalker.getProperty("includeTaskVariables", WhereClauseParser.EQUALS, Boolean.class);
    List<SortColumn> sortList = parameters.getSorting();
    SortColumn sortColumn = null;
    if (sortList != null && sortList.size() > 0) {
        if (sortList.size() != 1) {
            throw new InvalidArgumentException("Only one order by parameter is supported");
        }
        sortColumn = sortList.get(0);
    }
    List<Task> page = null;
    int totalCount = 0;
    if (status == null || STATUS_ACTIVE.equals(status)) {
        TaskQuery query = activitiProcessEngine.getTaskService().createTaskQuery();
        if (assignee != null)
            query.taskAssignee(assignee);
        if (assigneeLike != null)
            query.taskAssigneeLike(assigneeLike);
        if (owner != null)
            query.taskOwner(owner);
        if (ownerLike != null)
            query.taskOwner(ownerLike);
        if (candidateUser != null) {
            Set<String> parents = authorityService.getContainingAuthorities(AuthorityType.GROUP, candidateUser, false);
            if (parents != null) {
                List<String> authorities = new ArrayList<String>();
                authorities.addAll(parents);
                // there's a limitation in at least Oracle for using an IN statement with more than 1000 items
                if (parents.size() > 1000) {
                    authorities = authorities.subList(0, 1000);
                }
                if (authorities.size() > 0) {
                    query.taskCandidateGroupIn(authorities);
                } else {
                    query.taskCandidateUser(candidateUser);
                }
            }
        }
        if (candidateGroup != null)
            query.taskCandidateGroup(candidateGroup);
        if (name != null)
            query.taskName(name);
        if (nameLike != null)
            query.taskNameLike(nameLike);
        if (description != null)
            query.taskDescription(description);
        if (descriptionLike != null)
            query.taskDescriptionLike(descriptionLike);
        if (priority != null)
            query.taskPriority(priority);
        if (priorityGreaterThanOrEquals != null)
            query.taskMinPriority(priorityGreaterThanOrEquals);
        if (priorityLessThanOrEquals != null)
            query.taskMaxPriority(priorityLessThanOrEquals);
        if (processInstanceId != null)
            query.processInstanceId(processInstanceId);
        if (processInstanceBusinessKey != null)
            query.processInstanceBusinessKey(processInstanceBusinessKey);
        if (processInstanceBusinessKeyLike != null)
            query.processInstanceBusinessKeyLike(processInstanceBusinessKeyLike);
        if (activityDefinitionId != null)
            query.taskDefinitionKey(activityDefinitionId);
        if (activityDefinitionIdLike != null)
            query.taskDefinitionKey(activityDefinitionIdLike);
        if (processDefinitionId != null)
            query.processDefinitionId(processDefinitionId);
        if (processDefinitionKey != null)
            query.processDefinitionKey(processDefinitionKey);
        if (processDefinitionKeyLike != null)
            query.processDefinitionKeyLike(processDefinitionKeyLike);
        if (processDefinitionName != null)
            query.processDefinitionName(processDefinitionName);
        if (processDefinitionNameLike != null)
            query.processDefinitionNameLike(processDefinitionNameLike);
        if (dueAt != null)
            query.dueDate(dueAt);
        if (dueAtGreaterThan != null)
            query.dueAfter(dueAtGreaterThan);
        if (dueAtLessThan != null)
            query.dueBefore(dueAtLessThan);
        if (startedAt != null)
            query.taskCreatedOn(startedAt);
        if (startedAtGreaterThan != null)
            query.taskCreatedAfter(startedAtGreaterThan);
        if (startedAtLessThan != null)
            query.taskCreatedBefore(startedAtLessThan);
        if (includeProcessVariables != null && includeProcessVariables) {
            query.includeProcessVariables();
        }
        if (includeTaskVariables != null && includeTaskVariables) {
            query.includeTaskLocalVariables();
        }
        // use the limit set in alfresco-global.properties
        query.limitTaskVariables(taskVariablesLimit);
        List<QueryVariableHolder> variableProperties = propertyWalker.getVariableProperties();
        setQueryUsingVariables(query, variableProperties);
        // Add tenant-filtering
        if (tenantService.isEnabled()) {
            query.processVariableValueEquals(ActivitiConstants.VAR_TENANT_DOMAIN, TenantUtil.getCurrentDomain());
        }
        // Add involvement filtering if user is not admin
        if (processInstanceId == null && !authorityService.isAdminAuthority(AuthenticationUtil.getRunAsUser()) && candidateUser == null && candidateGroup == null) {
            query.taskInvolvedUser(AuthenticationUtil.getRunAsUser());
        }
        setSorting(query, sortColumn);
        List<org.activiti.engine.task.Task> tasks = query.listPage(paging.getSkipCount(), paging.getMaxItems());
        totalCount = (int) query.count();
        page = new ArrayList<Task>(tasks.size());
        Map<String, TypeDefinition> definitionTypeMap = new HashMap<String, TypeDefinition>();
        for (org.activiti.engine.task.Task taskInstance : tasks) {
            Task task = new Task(taskInstance);
            task.setFormResourceKey(getFormResourceKey(taskInstance));
            if ((includeProcessVariables != null && includeProcessVariables) || (includeTaskVariables != null && includeTaskVariables)) {
                addVariables(task, includeProcessVariables, includeTaskVariables, taskInstance.getProcessVariables(), taskInstance.getTaskLocalVariables(), definitionTypeMap);
            }
            page.add(task);
        }
    } else if (STATUS_COMPLETED.equals(status) || STATUS_ANY.equals(status)) {
        // Candidate user and group is only supported with STATUS_ACTIVE
        if (candidateUser != null) {
            throw new InvalidArgumentException("Filtering on candidateUser is only allowed in combination with status-parameter 'active'");
        }
        if (candidateGroup != null) {
            throw new InvalidArgumentException("Filtering on candidateGroup is only allowed in combination with status-parameter 'active'");
        }
        HistoricTaskInstanceQuery query = activitiProcessEngine.getHistoryService().createHistoricTaskInstanceQuery();
        if (STATUS_COMPLETED.equals(status))
            query.finished();
        if (assignee != null)
            query.taskAssignee(assignee);
        if (assigneeLike != null)
            query.taskAssigneeLike(assigneeLike);
        if (owner != null)
            query.taskOwner(owner);
        if (ownerLike != null)
            query.taskOwnerLike(ownerLike);
        if (name != null)
            query.taskName(name);
        if (nameLike != null)
            query.taskNameLike(nameLike);
        if (description != null)
            query.taskDescription(description);
        if (descriptionLike != null)
            query.taskDescriptionLike(descriptionLike);
        if (priority != null)
            query.taskPriority(priority);
        if (priorityGreaterThanOrEquals != null)
            query.taskMinPriority(priorityGreaterThanOrEquals);
        if (priorityLessThanOrEquals != null)
            query.taskMaxPriority(priorityLessThanOrEquals);
        if (processInstanceId != null)
            query.processInstanceId(processInstanceId);
        if (processInstanceBusinessKey != null)
            query.processInstanceBusinessKey(processInstanceBusinessKey);
        if (processInstanceBusinessKeyLike != null)
            query.processInstanceBusinessKeyLike(processInstanceBusinessKeyLike);
        if (activityDefinitionId != null)
            query.taskDefinitionKey(activityDefinitionId);
        if (activityDefinitionIdLike != null)
            query.taskDefinitionKey(activityDefinitionIdLike);
        if (processDefinitionId != null)
            query.processDefinitionId(processDefinitionId);
        if (processDefinitionKey != null)
            query.processDefinitionKey(processDefinitionKey);
        if (processDefinitionKeyLike != null)
            query.processDefinitionKeyLike(processDefinitionKeyLike);
        if (processDefinitionName != null)
            query.processDefinitionName(processDefinitionName);
        if (processDefinitionNameLike != null)
            query.processDefinitionNameLike(processDefinitionNameLike);
        if (dueAt != null)
            query.taskDueDate(dueAt);
        if (dueAtGreaterThan != null)
            query.taskDueAfter(dueAtGreaterThan);
        if (dueAtLessThan != null)
            query.taskDueBefore(dueAtLessThan);
        if (startedAt != null)
            query.taskCreatedOn(startedAt);
        if (startedAtGreaterThan != null)
            query.taskCreatedAfter(startedAtGreaterThan);
        if (startedAtLessThan != null)
            query.taskCreatedBefore(startedAtLessThan);
        if (endedAt != null)
            query.taskCompletedOn(endedAt);
        if (endedAtGreaterThan != null)
            query.taskCompletedAfter(endedAtGreaterThan);
        if (endedAtLessThan != null)
            query.taskCompletedBefore(endedAtLessThan);
        if (includeProcessVariables != null && includeProcessVariables) {
            query.includeProcessVariables();
        }
        if (includeTaskVariables != null && includeTaskVariables) {
            query.includeTaskLocalVariables();
        }
        List<QueryVariableHolder> variableProperties = propertyWalker.getVariableProperties();
        setQueryUsingVariables(query, variableProperties);
        // Add tenant filtering
        if (tenantService.isEnabled()) {
            query.processVariableValueEquals(ActivitiConstants.VAR_TENANT_DOMAIN, TenantUtil.getCurrentDomain());
        }
        // Add involvement filtering if user is not admin
        if (processInstanceId == null && !authorityService.isAdminAuthority(AuthenticationUtil.getRunAsUser())) {
            query.taskInvolvedUser(AuthenticationUtil.getRunAsUser());
        }
        setSorting(query, sortColumn);
        List<HistoricTaskInstance> tasks = query.listPage(paging.getSkipCount(), paging.getMaxItems());
        totalCount = (int) query.count();
        page = new ArrayList<Task>(tasks.size());
        Map<String, TypeDefinition> definitionTypeMap = new HashMap<String, TypeDefinition>();
        for (HistoricTaskInstance taskInstance : tasks) {
            Task task = new Task(taskInstance);
            if ((includeProcessVariables != null && includeProcessVariables) || (includeTaskVariables != null && includeTaskVariables)) {
                addVariables(task, includeProcessVariables, includeTaskVariables, taskInstance.getProcessVariables(), taskInstance.getTaskLocalVariables(), definitionTypeMap);
            }
            page.add(task);
        }
    } else {
        throw new InvalidArgumentException("Invalid status parameter: " + status);
    }
    return CollectionWithPagingInfo.asPaged(paging, page, (page.size() + paging.getSkipCount()) < totalCount, totalCount);
}
Also used : Task(org.alfresco.rest.workflow.api.model.Task) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryVariableHolder(org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker.QueryVariableHolder) SortColumn(org.alfresco.rest.framework.resource.parameters.SortColumn) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) TaskQuery(org.activiti.engine.task.TaskQuery) List(java.util.List) ArrayList(java.util.ArrayList) HistoricTaskInstance(org.activiti.engine.history.HistoricTaskInstance) Paging(org.alfresco.rest.framework.resource.parameters.Paging) HistoricTaskInstanceQuery(org.activiti.engine.history.HistoricTaskInstanceQuery) Date(java.util.Date) Map(java.util.Map) HashMap(java.util.HashMap)

Example 38 with TypeDefinition

use of org.alfresco.service.cmr.dictionary.TypeDefinition in project alfresco-remote-api by Alfresco.

the class TasksImpl method addVariables.

protected void addVariables(Task task, Boolean includeProcessVariables, Boolean includeTaskVariables, Map<String, Object> processVariables, Map<String, Object> taskVariables, Map<String, TypeDefinition> definitionTypeMap) {
    TypeDefinition startFormTypeDefinition = null;
    if (includeProcessVariables != null && includeProcessVariables) {
        if (definitionTypeMap.containsKey(task.getProcessDefinitionId()) == false) {
            StartFormData startFormData = activitiProcessEngine.getFormService().getStartFormData(task.getProcessDefinitionId());
            if (startFormData != null) {
                String formKey = startFormData.getFormKey();
                definitionTypeMap.put(task.getProcessDefinitionId(), getWorkflowFactory().getTaskFullTypeDefinition(formKey, true));
            }
        }
        if (definitionTypeMap.containsKey(task.getProcessDefinitionId())) {
            startFormTypeDefinition = definitionTypeMap.get(task.getProcessDefinitionId());
        }
    }
    TypeDefinition taskTypeDefinition = null;
    if (includeTaskVariables != null && includeTaskVariables) {
        taskTypeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(task.getFormResourceKey(), false);
    }
    List<TaskVariable> variables = restVariableHelper.getTaskVariables(taskVariables, processVariables, startFormTypeDefinition, taskTypeDefinition);
    task.setVariables(variables);
}
Also used : StartFormData(org.activiti.engine.form.StartFormData) TaskVariable(org.alfresco.rest.workflow.api.model.TaskVariable) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition)

Example 39 with TypeDefinition

use of org.alfresco.service.cmr.dictionary.TypeDefinition in project alfresco-remote-api by Alfresco.

the class TasksImpl method getTaskVariables.

@Override
public CollectionWithPagingInfo<TaskVariable> getTaskVariables(String taskId, Paging paging, VariableScope scope) {
    // Ensure the user is allowed to get variables for the task involved.
    HistoricTaskInstance taskInstance = getValidHistoricTask(taskId);
    String formKey = taskInstance.getFormKey();
    // Based on the scope, right variables are queried
    Map<String, Object> taskvariables = new HashMap<String, Object>();
    Map<String, Object> processVariables = new HashMap<String, Object>();
    if (scope == VariableScope.ANY || scope == VariableScope.LOCAL) {
        List<HistoricVariableInstance> variables = activitiProcessEngine.getHistoryService().createHistoricVariableInstanceQuery().taskId(taskId).list();
        if (variables != null) {
            for (HistoricVariableInstance variable : variables) {
                taskvariables.put(variable.getVariableName(), variable.getValue());
            }
        }
    }
    if ((scope == VariableScope.ANY || scope == VariableScope.GLOBAL) && taskInstance.getProcessInstanceId() != null) {
        List<HistoricVariableInstance> variables = activitiProcessEngine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(taskInstance.getProcessInstanceId()).excludeTaskVariables().list();
        if (variables != null) {
            for (HistoricVariableInstance variable : variables) {
                processVariables.put(variable.getVariableName(), variable.getValue());
            }
        }
    }
    // Convert raw variables to TaskVariables
    TypeDefinition taskTypeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(formKey, false);
    TypeDefinition startFormTypeDefinition = null;
    StartFormData startFormData = activitiProcessEngine.getFormService().getStartFormData(taskInstance.getProcessDefinitionId());
    if (startFormData != null) {
        startFormTypeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(startFormData.getFormKey(), true);
    } else {
        // fall back
        startFormTypeDefinition = taskTypeDefinition;
    }
    List<TaskVariable> page = restVariableHelper.getTaskVariables(taskvariables, processVariables, startFormTypeDefinition, taskTypeDefinition);
    return CollectionWithPagingInfo.asPaged(paging, page, false, page.size());
}
Also used : HistoricTaskInstance(org.activiti.engine.history.HistoricTaskInstance) HashMap(java.util.HashMap) StartFormData(org.activiti.engine.form.StartFormData) TaskVariable(org.alfresco.rest.workflow.api.model.TaskVariable) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition)

Example 40 with TypeDefinition

use of org.alfresco.service.cmr.dictionary.TypeDefinition in project alfresco-remote-api by Alfresco.

the class TasksImpl method convertToTypedVariable.

protected TaskVariable convertToTypedVariable(TaskVariable taskVariable, org.activiti.engine.task.Task taskInstance) {
    if (taskVariable.getName() == null) {
        throw new InvalidArgumentException("Variable name is required.");
    }
    if (taskVariable.getVariableScope() == null || (taskVariable.getVariableScope() != VariableScope.GLOBAL && taskVariable.getVariableScope() != VariableScope.LOCAL)) {
        throw new InvalidArgumentException("Variable scope is required and can only be 'local' or 'global'.");
    }
    DataTypeDefinition dataTypeDefinition = null;
    TypeDefinitionContext context = null;
    if (taskVariable.getVariableScope() == VariableScope.GLOBAL) {
        // Get start-task definition for explicit typing of variables submitted at the start
        String formKey = null;
        StartFormData startFormData = activitiProcessEngine.getFormService().getStartFormData(taskInstance.getProcessDefinitionId());
        if (startFormData != null) {
            formKey = startFormData.getFormKey();
        }
        TypeDefinition startTaskTypeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(formKey, true);
        context = new TypeDefinitionContext(startTaskTypeDefinition, getQNameConverter());
        if (context.getPropertyDefinition(taskVariable.getName()) != null) {
            dataTypeDefinition = context.getPropertyDefinition(taskVariable.getName()).getDataType();
            if (taskVariable.getType() != null && dataTypeDefinition.getName().toPrefixString(namespaceService).equals(taskVariable.getType()) == false) {
                throw new InvalidArgumentException("type of variable " + taskVariable.getName() + " should be " + dataTypeDefinition.getName().toPrefixString(namespaceService));
            }
        } else if (context.getAssociationDefinition(taskVariable.getName()) != null) {
            dataTypeDefinition = dictionaryService.getDataType(DataTypeDefinition.NODE_REF);
        }
    } else {
        // Revert to either the content-model type or the raw type provided by the request
        try {
            String formKey = activitiProcessEngine.getFormService().getTaskFormKey(taskInstance.getProcessDefinitionId(), taskInstance.getTaskDefinitionKey());
            TypeDefinition typeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(formKey, false);
            context = new TypeDefinitionContext(typeDefinition, getQNameConverter());
            if (context.getPropertyDefinition(taskVariable.getName()) != null) {
                dataTypeDefinition = context.getPropertyDefinition(taskVariable.getName()).getDataType();
                if (taskVariable.getType() != null && dataTypeDefinition.getName().toPrefixString(namespaceService).equals(taskVariable.getType()) == false) {
                    throw new InvalidArgumentException("type of variable " + taskVariable.getName() + " should be " + dataTypeDefinition.getName().toPrefixString(namespaceService));
                }
            } else if (context.getAssociationDefinition(taskVariable.getName()) != null) {
                dataTypeDefinition = dictionaryService.getDataType(DataTypeDefinition.NODE_REF);
            }
        } catch (InvalidQNameException ignore) {
        // In case the property is not part of the model, it's possible that the property-name is not a valid.
        // This can be ignored safeley as it falls back to the raw type
        }
    }
    if (dataTypeDefinition == null && taskVariable.getType() != null) {
        try {
            QName dataType = QName.createQName(taskVariable.getType(), namespaceService);
            dataTypeDefinition = dictionaryService.getDataType(dataType);
        } catch (InvalidQNameException iqne) {
            throw new InvalidArgumentException("Unsupported type of variable: '" + taskVariable.getType() + "'.");
        }
    } else if (dataTypeDefinition == null) {
        // Final fallback to raw value when no type has been passed and not present in model
        dataTypeDefinition = dictionaryService.getDataType(restVariableHelper.extractTypeFromValue(taskVariable.getValue()));
    }
    if (dataTypeDefinition == null) {
        throw new InvalidArgumentException("Unsupported type of variable: '" + taskVariable.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) taskVariable.getValue());
    } else {
        if (context != null && context.getAssociationDefinition(taskVariable.getName()) != null) {
            actualValue = convertAssociationDefinitionValue(context.getAssociationDefinition(taskVariable.getName()), taskVariable.getName(), taskVariable.getValue());
        } else {
            actualValue = DefaultTypeConverter.INSTANCE.convert(dataTypeDefinition, taskVariable.getValue());
        }
    }
    taskVariable.setValue(actualValue);
    // Set type so it's returned in case it was left empty
    taskVariable.setType(dataTypeDefinition.getName().toPrefixString(namespaceService));
    return taskVariable;
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) 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)

Aggregations

TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)43 QName (org.alfresco.service.namespace.QName)30 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)18 NodeRef (org.alfresco.service.cmr.repository.NodeRef)13 FacesContext (javax.faces.context.FacesContext)10 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 StartFormData (org.activiti.engine.form.StartFormData)8 IOException (java.io.IOException)7 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)7 Node (org.alfresco.web.bean.repository.Node)7 UserTransaction (javax.transaction.UserTransaction)6 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)6 DictionaryService (org.alfresco.service.cmr.dictionary.DictionaryService)6 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)6 Date (java.util.Date)5 SelectItem (javax.faces.model.SelectItem)5 MapNode (org.alfresco.web.bean.repository.MapNode)5 Serializable (java.io.Serializable)4 List (java.util.List)4