Search in sources :

Example 21 with Model

use of org.activiti.engine.repository.Model in project Activiti by Activiti.

the class ModelResourceTest method testDeleteModel.

public void testDeleteModel() throws Exception {
    Model model = null;
    try {
        Calendar now = Calendar.getInstance();
        now.set(Calendar.MILLISECOND, 0);
        processEngineConfiguration.getClock().setCurrentTime(now.getTime());
        model = repositoryService.newModel();
        model.setCategory("Model category");
        model.setKey("Model key");
        model.setMetaInfo("Model metainfo");
        model.setName("Model name");
        model.setVersion(2);
        repositoryService.saveModel(model);
        HttpDelete httpDelete = new HttpDelete(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_MODEL, model.getId()));
        closeResponse(executeRequest(httpDelete, HttpStatus.SC_NO_CONTENT));
        // Check if the model is really gone
        assertNull(repositoryService.createModelQuery().modelId(model.getId()).singleResult());
        model = null;
    } finally {
        if (model != null) {
            try {
                repositoryService.deleteModel(model.getId());
            } catch (Throwable ignore) {
            // Ignore, model might not be created
            }
        }
    }
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) Calendar(java.util.Calendar) Model(org.activiti.engine.repository.Model)

Example 22 with Model

use of org.activiti.engine.repository.Model in project Activiti by Activiti.

the class DeploymentEntityManager method deleteDeployment.

public void deleteDeployment(String deploymentId, boolean cascade) {
    List<ProcessDefinition> processDefinitions = getDbSqlSession().createProcessDefinitionQuery().deploymentId(deploymentId).list();
    // Remove the deployment link from any model. 
    // The model will still exists, as a model is a source for a deployment model and has a different lifecycle
    List<Model> models = getDbSqlSession().createModelQueryImpl().deploymentId(deploymentId).list();
    for (Model model : models) {
        ModelEntity modelEntity = (ModelEntity) model;
        modelEntity.setDeploymentId(null);
        getModelManager().updateModel(modelEntity);
    }
    if (cascade) {
        // delete process instances
        for (ProcessDefinition processDefinition : processDefinitions) {
            String processDefinitionId = processDefinition.getId();
            getProcessInstanceManager().deleteProcessInstancesByProcessDefinition(processDefinitionId, "deleted deployment", cascade);
        }
    }
    for (ProcessDefinition processDefinition : processDefinitions) {
        String processDefinitionId = processDefinition.getId();
        // remove related authorization parameters in IdentityLink table
        getIdentityLinkManager().deleteIdentityLinksByProcDef(processDefinitionId);
        // event subscriptions
        List<EventSubscriptionEntity> eventSubscriptionEntities = getEventSubscriptionManager().findEventSubscriptionsByTypeAndProcessDefinitionId(null, processDefinitionId, // null type ==> all types
        processDefinition.getTenantId());
        for (EventSubscriptionEntity eventSubscriptionEntity : eventSubscriptionEntities) {
            eventSubscriptionEntity.delete();
        }
        getProcessDefinitionInfoManager().deleteProcessDefinitionInfo(processDefinitionId);
    }
    // delete process definitions from db
    getProcessDefinitionManager().deleteProcessDefinitionsByDeploymentId(deploymentId);
    for (ProcessDefinition processDefinition : processDefinitions) {
        // remove timer start events for current process definition:
        List<Job> timerStartJobs = Context.getCommandContext().getJobEntityManager().findJobsByTypeAndProcessDefinitionId(TimerStartEventJobHandler.TYPE, processDefinition.getId());
        if (timerStartJobs != null && timerStartJobs.size() > 0) {
            for (Job timerStartJob : timerStartJobs) {
                if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
                    Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.JOB_CANCELED, timerStartJob, null, null, processDefinition.getId()));
                }
                ((JobEntity) timerStartJob).delete();
            }
        }
        // If previous process definition version has a timer/message/signal start event, it must be added
        ProcessDefinitionEntity latestProcessDefinition = findLatestProcessDefinition(processDefinition);
        // Only if the currently deleted process definition is the latest version, we fall back to the previous start event type
        if (processDefinition.getId().equals(latestProcessDefinition.getId())) {
            // Try to find a previous version (it could be some versions are missing due to deletions)
            ProcessDefinition previousProcessDefinition = findNewLatestProcessDefinitionAfterRemovalOf(processDefinition);
            if (previousProcessDefinition != null) {
                // Need to resolve process definition to make sure it's parsed
                ProcessDefinitionEntity resolvedProcessDefinition = Context.getProcessEngineConfiguration().getDeploymentManager().resolveProcessDefinition((ProcessDefinitionEntity) previousProcessDefinition);
                // Timer start
                List<TimerDeclarationImpl> timerDeclarations = (List<TimerDeclarationImpl>) resolvedProcessDefinition.getProperty(BpmnParse.PROPERTYNAME_START_TIMER);
                if (timerDeclarations != null) {
                    for (TimerDeclarationImpl timerDeclaration : timerDeclarations) {
                        TimerEntity timer = timerDeclaration.prepareTimerEntity(null);
                        timer.setProcessDefinitionId(previousProcessDefinition.getId());
                        if (previousProcessDefinition.getTenantId() != null) {
                            timer.setTenantId(previousProcessDefinition.getTenantId());
                        }
                        Context.getCommandContext().getJobEntityManager().schedule(timer);
                    }
                }
                // Signal / Message start
                List<EventSubscriptionDeclaration> signalEventDefinitions = (List<EventSubscriptionDeclaration>) resolvedProcessDefinition.getProperty(BpmnParse.PROPERTYNAME_EVENT_SUBSCRIPTION_DECLARATION);
                if (signalEventDefinitions != null) {
                    for (EventSubscriptionDeclaration eventDefinition : signalEventDefinitions) {
                        if (eventDefinition.getEventType().equals("signal") && eventDefinition.isStartEvent()) {
                            SignalEventSubscriptionEntity subscriptionEntity = new SignalEventSubscriptionEntity();
                            subscriptionEntity.setEventName(eventDefinition.getEventName());
                            subscriptionEntity.setActivityId(eventDefinition.getActivityId());
                            subscriptionEntity.setProcessDefinitionId(previousProcessDefinition.getId());
                            subscriptionEntity.setTenantId(previousProcessDefinition.getTenantId());
                            subscriptionEntity.insert();
                        } else if (eventDefinition.getEventType().equals("message") && eventDefinition.isStartEvent()) {
                            MessageEventSubscriptionEntity newSubscription = new MessageEventSubscriptionEntity();
                            newSubscription.setEventName(eventDefinition.getEventName());
                            newSubscription.setActivityId(eventDefinition.getActivityId());
                            newSubscription.setConfiguration(previousProcessDefinition.getId());
                            newSubscription.setProcessDefinitionId(previousProcessDefinition.getId());
                            newSubscription.setTenantId(previousProcessDefinition.getTenantId());
                            newSubscription.insert();
                        }
                    }
                }
            }
        }
    }
    getResourceManager().deleteResourcesByDeploymentId(deploymentId);
    getDbSqlSession().delete("deleteDeployment", deploymentId);
}
Also used : TimerDeclarationImpl(org.activiti.engine.impl.jobexecutor.TimerDeclarationImpl) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) Model(org.activiti.engine.repository.Model) List(java.util.List) Job(org.activiti.engine.runtime.Job) EventSubscriptionDeclaration(org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration)

Example 23 with Model

use of org.activiti.engine.repository.Model in project Activiti by Activiti.

the class RepositoryServiceTest method testUpdateModelPersistence.

public void testUpdateModelPersistence() throws Exception {
    Model model = repositoryService.newModel();
    assertNotNull(model);
    model.setName("Test model");
    model.setCategory("test");
    model.setMetaInfo("meta");
    repositoryService.saveModel(model);
    assertNotNull(model.getId());
    model = repositoryService.getModel(model.getId());
    assertNotNull(model);
    model.setName("New name");
    model.setCategory("New category");
    model.setMetaInfo("test");
    model.setVersion(2);
    repositoryService.saveModel(model);
    assertNotNull(model.getId());
    repositoryService.addModelEditorSource(model.getId(), "new".getBytes("utf-8"));
    repositoryService.addModelEditorSourceExtra(model.getId(), "new".getBytes("utf-8"));
    model = repositoryService.getModel(model.getId());
    assertEquals("New name", model.getName());
    assertEquals("New category", model.getCategory());
    assertEquals("test", model.getMetaInfo());
    assertNotNull(model.getCreateTime());
    assertEquals(Integer.valueOf(2), model.getVersion());
    assertEquals("new", new String(repositoryService.getModelEditorSource(model.getId()), "utf-8"));
    assertEquals("new", new String(repositoryService.getModelEditorSourceExtra(model.getId()), "utf-8"));
    repositoryService.deleteModel(model.getId());
}
Also used : Model(org.activiti.engine.repository.Model) BpmnModel(org.activiti.bpmn.model.BpmnModel)

Example 24 with Model

use of org.activiti.engine.repository.Model in project Activiti by Activiti.

the class SimpleTableEditor method save.

protected void save() {
    WorkflowDefinition workflowDefinition = createWorkflow();
    final ProcessEngineImpl defaultProcessEngine = (ProcessEngineImpl) ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
    ProcessEngineConfiguration processEngineConfiguration = defaultProcessEngine.getProcessEngineConfiguration();
    ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
    Model model = null;
    if (modelId == null) {
        // new process
        model = repositoryService.newModel();
    } else {
        // update existing process
        model = repositoryService.getModel(modelId);
    }
    model.setName(workflowDefinition.getName());
    model.setCategory(SimpleTableEditorConstants.TABLE_EDITOR_CATEGORY);
    repositoryService.saveModel(model);
    // Store model entity
    WorkflowDefinitionConversion conversion = ExplorerApp.get().getWorkflowDefinitionConversionFactory().createWorkflowDefinitionConversion(workflowDefinition);
    conversion.convert();
    try {
        // Write JSON to byte-array and set as editor-source
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ExplorerApp.get().getSimpleWorkflowJsonConverter().writeWorkflowDefinition(workflowDefinition, new OutputStreamWriter(baos));
        repositoryService.addModelEditorSource(model.getId(), baos.toByteArray());
        // Store process image
        // TODO: we should really allow the service to take an inputstream as input. Now we load it into memory ...
        repositoryService.addModelEditorSourceExtra(model.getId(), IOUtils.toByteArray(diagramGenerator.generateDiagram(conversion.getBpmnModel(), "png", processEngineConfiguration.getActivityFontName(), processEngineConfiguration.getLabelFontName(), processEngineConfiguration.getAnnotationFontName(), processEngineConfiguration.getClassLoader())));
    } catch (IOException e) {
        logger.warn("Could not generate process image. Image is not stored and will not be shown.", e);
    }
    ExplorerApp.get().getViewManager().showEditorProcessDefinitionPage(model.getId());
}
Also used : WorkflowDefinitionConversion(org.activiti.workflow.simple.converter.WorkflowDefinitionConversion) ProcessEngineConfiguration(org.activiti.engine.ProcessEngineConfiguration) ProcessDiagramGenerator(org.activiti.image.ProcessDiagramGenerator) Model(org.activiti.engine.repository.Model) WorkflowDefinition(org.activiti.workflow.simple.definition.WorkflowDefinition) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProcessEngineImpl(org.activiti.engine.impl.ProcessEngineImpl) RepositoryService(org.activiti.engine.RepositoryService)

Example 25 with Model

use of org.activiti.engine.repository.Model in project Activiti by Activiti.

the class ModelResourceSourceTest method testSetModelEditorSourceExtra.

public void testSetModelEditorSourceExtra() throws Exception {
    Model model = null;
    try {
        model = repositoryService.newModel();
        model.setName("Model name");
        repositoryService.saveModel(model);
        HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_MODEL_SOURCE_EXTRA, model.getId()));
        httpPut.setEntity(HttpMultipartHelper.getMultiPartEntity("sourcefile", "application/octet-stream", new ByteArrayInputStream("This is the new extra editor source".getBytes()), null));
        closeResponse(executeBinaryRequest(httpPut, HttpStatus.SC_NO_CONTENT));
        assertEquals("This is the new extra editor source", new String(repositoryService.getModelEditorSourceExtra(model.getId())));
    } finally {
        try {
            repositoryService.deleteModel(model.getId());
        } catch (Throwable ignore) {
        // Ignore, model might not be created
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.activiti.engine.repository.Model) HttpPut(org.apache.http.client.methods.HttpPut)

Aggregations

Model (org.activiti.engine.repository.Model)49 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)12 ModelQuery (org.activiti.engine.repository.ModelQuery)9 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Calendar (java.util.Calendar)6 Deployment (org.activiti.engine.test.Deployment)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 InputStream (java.io.InputStream)5 HttpGet (org.apache.http.client.methods.HttpGet)5 HttpPut (org.apache.http.client.methods.HttpPut)5 BpmnModel (org.activiti.bpmn.model.BpmnModel)4 ActivitiException (org.activiti.engine.ActivitiException)4 StringEntity (org.apache.http.entity.StringEntity)4 Button (com.vaadin.ui.Button)3 ClickEvent (com.vaadin.ui.Button.ClickEvent)3 ClickListener (com.vaadin.ui.Button.ClickListener)3 HorizontalLayout (com.vaadin.ui.HorizontalLayout)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3