Search in sources :

Example 11 with StartFormData

use of org.activiti.engine.form.StartFormData in project carbon-business-process by wso2.

the class ProcessDefinitionFormPropertyService method getStartFormProperties.

@GET
@Path("/{processDefinitionId}/properties")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getStartFormProperties(@PathParam("processDefinitionId") String processDefinitionId) {
    FormService formService = BPMNOSGIService.getFormService();
    StartFormData startFormData = formService.getStartFormData(processDefinitionId);
    FormPropertyResponseCollection formPropertyResponseCollection = new FormPropertyResponseCollection();
    if (startFormData != null) {
        List<FormProperty> properties = startFormData.getFormProperties();
        List<FormPropertyResponse> formPropertyResponseList = new ArrayList<>();
        for (FormProperty property : properties) {
            // ObjectNode propertyJSON = objectMapper.createObjectNode();
            FormPropertyResponse formPropertyResponse = new FormPropertyResponse();
            formPropertyResponse.setId(property.getId());
            formPropertyResponse.setName(property.getName());
            if (property.getValue() != null) {
                formPropertyResponse.setValue(property.getValue());
            } else {
                formPropertyResponse.setValue(null);
            }
            if (property.getType() != null) {
                formPropertyResponse.setType(property.getType().getName());
                if (property.getType() instanceof EnumFormType) {
                    @SuppressWarnings("unchecked") Map<String, String> valuesMap = (Map<String, String>) property.getType().getInformation("values");
                    if (valuesMap != null) {
                        List<FormPropertyEnumDataHolder> formPropertyEnumDataHoldersList = new ArrayList<>();
                        for (String key : valuesMap.keySet()) {
                            FormPropertyEnumDataHolder formPropertyEnumDataHolder = new FormPropertyEnumDataHolder();
                            formPropertyEnumDataHolder.setId(key);
                            formPropertyEnumDataHolder.setName(valuesMap.get(key));
                            formPropertyEnumDataHoldersList.add(formPropertyEnumDataHolder);
                        }
                        formPropertyResponse.setEnumValues(formPropertyEnumDataHoldersList);
                    }
                }
            } else {
                formPropertyResponse.setType("String");
            }
            formPropertyResponse.setRequired(property.isRequired());
            formPropertyResponse.setReadable(property.isReadable());
            formPropertyResponse.setWritable(property.isWritable());
            formPropertyResponseList.add(formPropertyResponse);
        }
        formPropertyResponseCollection.setData(formPropertyResponseList);
    }
    return Response.ok().entity(formPropertyResponseCollection).build();
}
Also used : FormPropertyEnumDataHolder(org.wso2.carbon.bpmn.rest.model.form.FormPropertyEnumDataHolder) FormProperty(org.activiti.engine.form.FormProperty) FormService(org.activiti.engine.FormService) FormPropertyResponse(org.wso2.carbon.bpmn.rest.model.form.FormPropertyResponse) ArrayList(java.util.ArrayList) EnumFormType(org.activiti.engine.impl.form.EnumFormType) FormPropertyResponseCollection(org.wso2.carbon.bpmn.rest.model.form.FormPropertyResponseCollection) StartFormData(org.activiti.engine.form.StartFormData) Map(java.util.Map) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 12 with StartFormData

use of org.activiti.engine.form.StartFormData in project carbon-business-process by wso2.

the class RestResponseFactory method createFormDataResponse.

public FormDataResponse createFormDataResponse(FormData formData, String baseUri) {
    FormDataResponse result = new FormDataResponse();
    result.setDeploymentId(formData.getDeploymentId());
    result.setFormKey(formData.getFormKey());
    if (formData.getFormProperties() != null) {
        for (FormProperty formProp : formData.getFormProperties()) {
            RestFormProperty restFormProp = new RestFormProperty();
            restFormProp.setId(formProp.getId());
            restFormProp.setName(formProp.getName());
            if (formProp.getType() != null) {
                restFormProp.setType(formProp.getType().getName());
            }
            restFormProp.setValue(formProp.getValue());
            restFormProp.setReadable(formProp.isReadable());
            restFormProp.setRequired(formProp.isRequired());
            restFormProp.setWritable(formProp.isWritable());
            if ("enum".equals(restFormProp.getType())) {
                Object values = formProp.getType().getInformation("values");
                if (values != null) {
                    @SuppressWarnings("unchecked") Map<String, String> enumValues = (Map<String, String>) values;
                    for (String enumId : enumValues.keySet()) {
                        RestEnumFormProperty enumProperty = new RestEnumFormProperty();
                        enumProperty.setId(enumId);
                        enumProperty.setName(enumValues.get(enumId));
                        restFormProp.addEnumValue(enumProperty);
                    }
                }
            } else if ("date".equals(restFormProp.getType())) {
                restFormProp.setDatePattern((String) formProp.getType().getInformation("datePattern"));
            }
            result.addFormProperty(restFormProp);
        }
    }
    RestUrlBuilder urlBuilder = createUrlBuilder(baseUri);
    if (formData instanceof StartFormData) {
        StartFormData startFormData = (StartFormData) formData;
        if (startFormData.getProcessDefinition() != null) {
            result.setProcessDefinitionId(startFormData.getProcessDefinition().getId());
            result.setProcessDefinitionUrl(urlBuilder.buildUrl(RestUrls.URL_PROCESS_DEFINITION, startFormData.getProcessDefinition().getId()));
        }
    } else if (formData instanceof TaskFormData) {
        TaskFormData taskFormData = (TaskFormData) formData;
        if (taskFormData.getTask() != null) {
            result.setTaskId(taskFormData.getTask().getId());
            result.setTaskUrl(urlBuilder.buildUrl(RestUrls.URL_TASK, taskFormData.getTask().getId()));
        }
    }
    return result;
}
Also used : RestEnumFormProperty(org.wso2.carbon.bpmn.rest.model.form.RestEnumFormProperty) RestFormProperty(org.wso2.carbon.bpmn.rest.model.form.RestFormProperty) RestEnumFormProperty(org.wso2.carbon.bpmn.rest.model.form.RestEnumFormProperty) FormProperty(org.activiti.engine.form.FormProperty) StartFormData(org.activiti.engine.form.StartFormData) RestFormProperty(org.wso2.carbon.bpmn.rest.model.form.RestFormProperty) TaskFormData(org.activiti.engine.form.TaskFormData) Map(java.util.Map) FormDataResponse(org.wso2.carbon.bpmn.rest.model.form.FormDataResponse)

Example 13 with StartFormData

use of org.activiti.engine.form.StartFormData in project alfresco-remote-api by Alfresco.

the class ProcessesImpl method create.

@Override
public ProcessInfo create(ProcessInfo process) {
    if (process == null) {
        throw new InvalidArgumentException("post body expected when starting a new process instance");
    }
    boolean definitionExistingChecked = false;
    RuntimeService runtimeService = activitiProcessEngine.getRuntimeService();
    String processDefinitionId = null;
    if (process.getProcessDefinitionId() != null) {
        processDefinitionId = process.getProcessDefinitionId();
    } else if (process.getProcessDefinitionKey() != null) {
        ProcessDefinition definition = activitiProcessEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey(getProcessDefinitionKey(process.getProcessDefinitionKey())).latestVersion().singleResult();
        if (definition == null) {
            throw new InvalidArgumentException("No workflow definition could be found with key '" + process.getProcessDefinitionKey() + "'.");
        }
        processDefinitionId = definition.getId();
        definitionExistingChecked = true;
    } else {
        throw new InvalidArgumentException("Either processDefinitionId or processDefinitionKey is required");
    }
    if (definitionExistingChecked == false) {
        // Check if the required definition actually exists
        ProcessDefinitionQuery query = activitiProcessEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionId(processDefinitionId);
        if (tenantService.isEnabled() && deployWorkflowsInTenant) {
            query.processDefinitionKeyLike("@" + TenantUtil.getCurrentDomain() + "@%");
        }
        if (query.count() == 0) {
            throw new InvalidArgumentException("No workflow definition could be found with id '" + processDefinitionId + "'.");
        }
    }
    Map<QName, Serializable> startParams = new HashMap<QName, Serializable>();
    StartFormData startFormData = activitiProcessEngine.getFormService().getStartFormData(processDefinitionId);
    if (startFormData != null) {
        if (CollectionUtils.isEmpty(process.getVariables()) == false) {
            TypeDefinition startTaskType = getWorkflowFactory().getTaskFullTypeDefinition(startFormData.getFormKey(), true);
            // Lookup type definition for the startTask
            Map<QName, PropertyDefinition> taskProperties = startTaskType.getProperties();
            Map<String, QName> propNameMap = new HashMap<String, QName>();
            for (QName key : taskProperties.keySet()) {
                propNameMap.put(key.getPrefixString().replace(':', '_'), key);
            }
            Map<QName, AssociationDefinition> taskAssociations = startTaskType.getAssociations();
            for (QName key : taskAssociations.keySet()) {
                propNameMap.put(key.getPrefixString().replace(':', '_'), key);
            }
            for (String variableName : process.getVariables().keySet()) {
                if (propNameMap.containsKey(variableName)) {
                    Object variableValue = process.getVariables().get(variableName);
                    if (taskAssociations.containsKey(propNameMap.get(variableName))) {
                        AssociationDefinition associationDef = taskAssociations.get(propNameMap.get(variableName));
                        variableValue = convertAssociationDefinitionValue(associationDef, variableName, variableValue);
                    } else if (taskProperties.containsKey(propNameMap.get(variableName))) {
                        PropertyDefinition propDef = taskProperties.get(propNameMap.get(variableName));
                        DataTypeDefinition propDataType = propDef.getDataType();
                        if ("java.util.Date".equalsIgnoreCase(propDataType.getJavaClassName())) {
                            // fix for different ISO 8601 Date format classes in Alfresco (org.alfresco.util and Spring Surf)
                            variableValue = ISO8601DateFormat.parse((String) variableValue);
                        }
                    }
                    if (variableValue instanceof Serializable) {
                        startParams.put(propNameMap.get(variableName), (Serializable) variableValue);
                    }
                }
            }
        }
    }
    String currentUserName = AuthenticationUtil.getFullyAuthenticatedUser();
    Authentication.setAuthenticatedUserId(currentUserName);
    NodeRef workflowPackageNodeRef = null;
    try {
        workflowPackageNodeRef = workflowPackageComponent.createPackage(null);
        startParams.put(WorkflowModel.ASSOC_PACKAGE, workflowPackageNodeRef);
    } catch (Exception e) {
        throw new ApiException("couldn't create workflow package: " + e.getMessage(), e);
    }
    if (org.apache.commons.collections.CollectionUtils.isNotEmpty(process.getItems())) {
        try {
            for (String item : process.getItems()) {
                NodeRef itemNodeRef = getNodeRef(item);
                QName workflowPackageItemId = QName.createQName("wpi", itemNodeRef.toString());
                nodeService.addChild(workflowPackageNodeRef, itemNodeRef, WorkflowModel.ASSOC_PACKAGE_CONTAINS, workflowPackageItemId);
            }
        } catch (Exception e) {
            throw new ApiException("Error while adding items to package: " + e.getMessage(), e);
        }
    }
    // Set start task properties. This should be done before instance is started, since it's id will be used
    Map<String, Object> variables = getPropertyConverter().getStartVariables(processDefinitionId, startParams);
    variables.put(WorkflowConstants.PROP_CANCELLED, Boolean.FALSE);
    // Add company home
    Object companyHome = getNodeConverter().convertNode(repositoryHelper.getCompanyHome());
    variables.put(WorkflowConstants.PROP_COMPANY_HOME, companyHome);
    // Add the initiator
    NodeRef initiator = getPersonNodeRef(currentUserName);
    if (initiator != null) {
        variables.put(WorkflowConstants.PROP_INITIATOR, nodeConverter.convertNode(initiator));
        // Also add the initiator home reference, if one exists
        NodeRef initiatorHome = (NodeRef) nodeService.getProperty(initiator, ContentModel.PROP_HOMEFOLDER);
        if (initiatorHome != null) {
            variables.put(WorkflowConstants.PROP_INITIATOR_HOME, nodeConverter.convertNode(initiatorHome));
        }
    }
    if (tenantService.isEnabled()) {
        // Specify which tenant domain the workflow was started in.
        variables.put(ActivitiConstants.VAR_TENANT_DOMAIN, TenantUtil.getCurrentDomain());
    }
    // Start the process-instance
    ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, process.getBusinessKey(), variables);
    if (processInstance.isEnded() == false) {
        runtimeService.setVariable(processInstance.getProcessInstanceId(), ActivitiConstants.PROP_START_TASK_END_DATE, new Date());
    }
    HistoricProcessInstance historicProcessInstance = activitiProcessEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
    return createProcessInfo(historicProcessInstance);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ProcessDefinitionQuery(org.activiti.engine.repository.ProcessDefinitionQuery) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) StartFormData(org.activiti.engine.form.StartFormData) RuntimeService(org.activiti.engine.RuntimeService) QName(org.alfresco.service.namespace.QName) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) IOException(java.io.IOException) InvalidQNameException(org.alfresco.service.namespace.InvalidQNameException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Date(java.util.Date) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Example 14 with StartFormData

use of org.activiti.engine.form.StartFormData 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)

Example 15 with StartFormData

use of org.activiti.engine.form.StartFormData 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)

Aggregations

StartFormData (org.activiti.engine.form.StartFormData)23 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)8 HashMap (java.util.HashMap)7 FormProperty (org.activiti.engine.form.FormProperty)7 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)7 Map (java.util.Map)5 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)5 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)5 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)4 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)4 QName (org.alfresco.service.namespace.QName)4 ArrayList (java.util.ArrayList)3 TaskFormData (org.activiti.engine.form.TaskFormData)3 ReadOnlyProcessDefinition (org.activiti.engine.impl.pvm.ReadOnlyProcessDefinition)3 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)3 Deployment (org.activiti.engine.test.Deployment)3 WorkflowTaskDefinition (org.alfresco.service.cmr.workflow.WorkflowTaskDefinition)3 InvalidQNameException (org.alfresco.service.namespace.InvalidQNameException)3 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2