Search in sources :

Example 1 with FlowableFormService

use of org.dshubs.odc.workflow.service.FlowableFormService in project ox-data-cloud by ox-data.

the class DeployModelCmd method execute.

@Override
public Deployment execute(CommandContext commandContext) {
    RepositoryService repositoryService = CommandContextUtil.getProcessEngineConfiguration(commandContext).getRepositoryService();
    Model model = repositoryService.getModel(modelId);
    if (model == null) {
        throw new FlowableObjectNotFoundException("Could not find a model with id '" + modelId + "'.", Model.class);
    }
    if (model.getDeploymentId() != null && model.getDeploymentId().length() > 0) {
        throw new FlowableException("The model is already deployed");
    }
    if (!model.hasEditorSource()) {
        throw new FlowableObjectNotFoundException("Model with id '" + modelId + "' does not have source " + "available" + ".", String.class);
    }
    byte[] bpmnBytes = CommandContextUtil.getProcessEngineConfiguration(commandContext).getModelEntityManager().findEditorSourceByModelId(modelId);
    if (bpmnBytes == null) {
        throw new FlowableObjectNotFoundException("Model with id '" + modelId + "' does not have source " + "available" + ".", String.class);
    }
    try {
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        String fileName = model.getId() + ".bpmn20.xml";
        ByteArrayInputStream bis = new ByteArrayInputStream(bpmnBytes);
        deploymentBuilder.addInputStream(fileName, bis);
        deploymentBuilder.name(fileName);
        deploymentBuilder.category(model.getCategory());
        XMLInputFactory xif = XMLInputFactory.newInstance();
        InputStreamReader xmlIn = new InputStreamReader(new ByteArrayInputStream(bpmnBytes), "UTF-8");
        XMLStreamReader xtr = xif.createXMLStreamReader(xmlIn);
        BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
        org.flowable.bpmn.model.Process process = bpmnModel.getMainProcess();
        Collection<FlowElement> flowElements = process.getFlowElements();
        Map<String, String> formKeyMap = new HashMap<String, String>(16);
        for (FlowElement flowElement : flowElements) {
            String formKey = null;
            if (flowElement instanceof StartEvent) {
                StartEvent startEvent = (StartEvent) flowElement;
                if (startEvent.getFormKey() != null && startEvent.getFormKey().length() > 0) {
                    formKey = startEvent.getFormKey();
                }
            } else if (flowElement instanceof UserTask) {
                UserTask userTask = (UserTask) flowElement;
                if (userTask.getFormKey() != null && userTask.getFormKey().length() > 0) {
                    formKey = userTask.getFormKey();
                }
            }
            if (formKey != null && formKey.length() > 0) {
                if (formKeyMap.containsKey(formKey)) {
                    continue;
                } else {
                    String formKeyDefinition = formKey.replace(".form", "");
                    FlowableFormService flowableFormService = SpringContextUtils.getBean(FlowableFormService.class);
                    FlowableForm form = flowableFormService.getById(formKeyDefinition);
                    if (form != null && form.getFormJson() != null && form.getFormJson().length() > 0) {
                        byte[] formJson = form.getFormJson().getBytes("UTF-8");
                        ByteArrayInputStream bi = new ByteArrayInputStream(formJson);
                        deploymentBuilder.addInputStream(formKey, bi);
                        formKeyMap.put(formKey, formKey);
                    } else {
                        throw new FlowableObjectNotFoundException("Cannot find formJson with formKey " + formKeyDefinition);
                    }
                }
            }
        }
        if (model.getTenantId() != null) {
            deploymentBuilder.tenantId(model.getTenantId());
        }
        Deployment deployment = deploymentBuilder.deploy();
        if (deployment != null) {
            model.setDeploymentId(deployment.getId());
        }
        return deployment;
    } catch (Exception e) {
        if (e instanceof FlowableException) {
            throw (FlowableException) e;
        }
        throw new FlowableException(e.getMessage(), e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) Deployment(org.flowable.engine.repository.Deployment) FlowableException(org.flowable.common.engine.api.FlowableException) org.flowable.bpmn.model(org.flowable.bpmn.model) RepositoryService(org.flowable.engine.RepositoryService) InputStreamReader(java.io.InputStreamReader) FlowableForm(org.dshubs.odc.workflow.entity.FlowableForm) FlowableException(org.flowable.common.engine.api.FlowableException) FlowableObjectNotFoundException(org.flowable.common.engine.api.FlowableObjectNotFoundException) BpmnXMLConverter(org.flowable.bpmn.converter.BpmnXMLConverter) FlowableObjectNotFoundException(org.flowable.common.engine.api.FlowableObjectNotFoundException) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.flowable.engine.repository.Model) FlowableFormService(org.dshubs.odc.workflow.service.FlowableFormService) DeploymentBuilder(org.flowable.engine.repository.DeploymentBuilder) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 FlowableForm (org.dshubs.odc.workflow.entity.FlowableForm)1 FlowableFormService (org.dshubs.odc.workflow.service.FlowableFormService)1 BpmnXMLConverter (org.flowable.bpmn.converter.BpmnXMLConverter)1 org.flowable.bpmn.model (org.flowable.bpmn.model)1 FlowableException (org.flowable.common.engine.api.FlowableException)1 FlowableObjectNotFoundException (org.flowable.common.engine.api.FlowableObjectNotFoundException)1 RepositoryService (org.flowable.engine.RepositoryService)1 Deployment (org.flowable.engine.repository.Deployment)1 DeploymentBuilder (org.flowable.engine.repository.DeploymentBuilder)1 Model (org.flowable.engine.repository.Model)1