Search in sources :

Example 6 with Variable

use of org.alfresco.rest.workflow.api.model.Variable 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 7 with Variable

use of org.alfresco.rest.workflow.api.model.Variable in project alfresco-remote-api by Alfresco.

the class RestVariableHelper method getVariables.

/**
 * @param variables raw variables
 * @param typeDefinition the typê definition for the start-task of the process, used to extract types.
 * @return list of {@link Variable}, representing the given raw variables
 */
public List<Variable> getVariables(Map<String, Object> variables, TypeDefinition typeDefinition) {
    List<Variable> result = new ArrayList<Variable>();
    TypeDefinitionContext context = new TypeDefinitionContext(typeDefinition, getQNameConverter());
    Variable variable = null;
    for (Entry<String, Object> entry : variables.entrySet()) {
        if (!INTERNAL_PROPERTIES.contains(entry.getKey())) {
            variable = new Variable();
            variable.setName(entry.getKey());
            // Set value and type
            setVariableValueAndType(variable, entry.getValue(), context);
            result.add(variable);
        }
    }
    return result;
}
Also used : TaskVariable(org.alfresco.rest.workflow.api.model.TaskVariable) Variable(org.alfresco.rest.workflow.api.model.Variable) ArrayList(java.util.ArrayList)

Aggregations

Variable (org.alfresco.rest.workflow.api.model.Variable)7 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 StartFormData (org.activiti.engine.form.StartFormData)3 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)3 ProcessInfo (org.alfresco.rest.workflow.api.model.ProcessInfo)3 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)3 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)3 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 JSONObject (org.json.simple.JSONObject)2 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HistoricProcessInstanceQuery (org.activiti.engine.history.HistoricProcessInstanceQuery)1 HistoricVariableInstance (org.activiti.engine.history.HistoricVariableInstance)1 ActivitiScriptNode (org.alfresco.repo.workflow.activiti.ActivitiScriptNode)1 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)1 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)1 Paging (org.alfresco.rest.framework.resource.parameters.Paging)1