Search in sources :

Example 76 with ObjectNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode in project Activiti by Activiti.

the class TaskVariablesCollectionResourceTest method testCreateMultipleTaskVariables.

/**
   * Test creating a multipe task variable in a single call.
   * POST runtime/tasks/{taskId}/variables
   */
public void testCreateMultipleTaskVariables() throws Exception {
    try {
        Task task = taskService.newTask();
        taskService.saveTask(task);
        ArrayNode requestNode = objectMapper.createArrayNode();
        // String variable
        ObjectNode stringVarNode = requestNode.addObject();
        stringVarNode.put("name", "stringVariable");
        stringVarNode.put("value", "simple string value");
        stringVarNode.put("scope", "local");
        stringVarNode.put("type", "string");
        // Integer
        ObjectNode integerVarNode = requestNode.addObject();
        integerVarNode.put("name", "integerVariable");
        integerVarNode.put("value", 1234);
        integerVarNode.put("scope", "local");
        integerVarNode.put("type", "integer");
        // Short
        ObjectNode shortVarNode = requestNode.addObject();
        shortVarNode.put("name", "shortVariable");
        shortVarNode.put("value", 123);
        shortVarNode.put("scope", "local");
        shortVarNode.put("type", "short");
        // Long
        ObjectNode longVarNode = requestNode.addObject();
        longVarNode.put("name", "longVariable");
        longVarNode.put("value", 4567890L);
        longVarNode.put("scope", "local");
        longVarNode.put("type", "long");
        // Double
        ObjectNode doubleVarNode = requestNode.addObject();
        doubleVarNode.put("name", "doubleVariable");
        doubleVarNode.put("value", 123.456);
        doubleVarNode.put("scope", "local");
        doubleVarNode.put("type", "double");
        // Boolean
        ObjectNode booleanVarNode = requestNode.addObject();
        booleanVarNode.put("name", "booleanVariable");
        booleanVarNode.put("value", Boolean.TRUE);
        booleanVarNode.put("scope", "local");
        booleanVarNode.put("type", "boolean");
        // Date
        Calendar varCal = Calendar.getInstance();
        String isoString = getISODateString(varCal.getTime());
        ObjectNode dateVarNode = requestNode.addObject();
        dateVarNode.put("name", "dateVariable");
        dateVarNode.put("value", isoString);
        dateVarNode.put("scope", "local");
        dateVarNode.put("type", "date");
        // Create local variables with a single request
        HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        CloseableHttpResponse response = executeBinaryRequest(httpPost, HttpStatus.SC_CREATED);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertTrue(responseNode.isArray());
        assertEquals(7, responseNode.size());
        // Check if engine has correct variables set
        Map<String, Object> taskVariables = taskService.getVariablesLocal(task.getId());
        assertEquals(7, taskVariables.size());
        assertEquals("simple string value", taskVariables.get("stringVariable"));
        assertEquals(1234, taskVariables.get("integerVariable"));
        assertEquals((short) 123, taskVariables.get("shortVariable"));
        assertEquals(4567890L, taskVariables.get("longVariable"));
        assertEquals(123.456, taskVariables.get("doubleVariable"));
        assertEquals(Boolean.TRUE, taskVariables.get("booleanVariable"));
        assertEquals(dateFormat.parse(isoString), taskVariables.get("dateVariable"));
        // repeat the process with additional variables, testing PUT of a mixed set of variables
        // where some exist and others do not
        requestNode = objectMapper.createArrayNode();
        // new String variable
        ObjectNode stringVarNode2 = requestNode.addObject();
        stringVarNode2.put("name", "new stringVariable");
        stringVarNode2.put("value", "simple string value 2");
        stringVarNode2.put("scope", "local");
        stringVarNode2.put("type", "string");
        // changed Integer variable
        ObjectNode integerVarNode2 = requestNode.addObject();
        integerVarNode2.put("name", "integerVariable");
        integerVarNode2.put("value", 4321);
        integerVarNode2.put("scope", "local");
        integerVarNode2.put("type", "integer");
        // Create or update local variables with a single request
        HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPut.setEntity(new StringEntity(requestNode.toString()));
        response = executeBinaryRequest(httpPut, HttpStatus.SC_CREATED);
        responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertTrue(responseNode.isArray());
        assertEquals(2, responseNode.size());
        // Check if engine has correct variables set
        taskVariables = taskService.getVariablesLocal(task.getId());
        assertEquals(8, taskVariables.size());
        assertEquals("simple string value", taskVariables.get("stringVariable"));
        assertEquals("simple string value 2", taskVariables.get("new stringVariable"));
        assertEquals(4321, taskVariables.get("integerVariable"));
        assertEquals((short) 123, taskVariables.get("shortVariable"));
        assertEquals(4567890L, taskVariables.get("longVariable"));
        assertEquals(123.456, taskVariables.get("doubleVariable"));
        assertEquals(Boolean.TRUE, taskVariables.get("booleanVariable"));
        assertEquals(dateFormat.parse(isoString), taskVariables.get("dateVariable"));
    } finally {
        // Clean adhoc-tasks even if test fails
        List<Task> tasks = taskService.createTaskQuery().list();
        for (Task task : tasks) {
            taskService.deleteTask(task.getId(), true);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Task(org.activiti.engine.task.Task) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Calendar(java.util.Calendar) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 77 with ObjectNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode in project Activiti by Activiti.

the class SecureScriptingTest method testDynamicScript.

@Test
public void testDynamicScript() {
    addWhiteListedClass("org.activiti.engine.impl.persistence.entity.ExecutionEntity");
    deployProcessDefinition("test-dynamic-secure-script.bpmn20.xml");
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testDynamicScript", CollectionUtil.map("a", 20, "b", 22));
    Assert.assertEquals(42.0, runtimeService.getVariable(processInstance.getId(), "test"));
    taskService.complete(taskService.createTaskQuery().singleResult().getId());
    assertProcessEnded(processInstance.getId());
    String processDefinitionId = processInstance.getProcessDefinitionId();
    ObjectNode infoNode = dynamicBpmnService.changeScriptTaskScript("script1", "var sum = c + d;\nexecution.setVariable('test2', sum);");
    dynamicBpmnService.saveProcessDefinitionInfo(processDefinitionId, infoNode);
    processInstance = runtimeService.startProcessInstanceByKey("testDynamicScript", CollectionUtil.map("c", 10, "d", 12));
    Assert.assertEquals(22.0, runtimeService.getVariable(processInstance.getId(), "test2"));
    taskService.complete(taskService.createTaskQuery().singleResult().getId());
    assertProcessEnded(processInstance.getId());
    removeWhiteListedClass("org.activiti.engine.impl.persistence.entity.ExecutionEntity");
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Test(org.junit.Test)

Example 78 with ObjectNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode in project Activiti by Activiti.

the class JsonNodeELResolver method setValue.

/**
   * If the base object is a map, attempts to set the value associated with the given key, as
   * specified by the property argument. If the base is a Map, the propertyResolved property of
   * the ELContext object must be set to true by this resolver, before returning. If this property
   * is not true after this method is called, the caller can safely assume no value was set. If
   * this resolver was constructed in read-only mode, this method will always throw
   * PropertyNotWritableException. If a Map was created using
   * java.util.Collections.unmodifiableMap(Map), this method must throw
   * PropertyNotWritableException. Unfortunately, there is no Collections API method to detect
   * this. However, an implementation can create a prototype unmodifiable Map and query its
   * runtime type to see if it matches the runtime type of the base object as a workaround.
   * 
   * @param context
   *            The context of this evaluation.
   * @param base
   *            The map to analyze. Only bases of type Map are handled by this resolver.
   * @param property
   *            The key to return the acceptable type for. Ignored by this resolver.
   * @param value
   *            The value to be associated with the specified key.
   * @throws ClassCastException
   *             if the class of the specified key or value prevents it from being stored in this
   *             map.
   * @throws NullPointerException
   *             if context is null, or if this map does not permit null keys or values, and the
   *             specified key or value is null.
   * @throws IllegalArgumentException
   *             if some aspect of this key or value prevents it from being stored in this map.
   * @throws PropertyNotWritableException
   *             if this resolver was constructed in read-only mode, or if the put operation is
   *             not supported by the underlying map.
   * @throws ELException
   *             if an exception was thrown while performing the property or variable resolution.
   *             The thrown exception must be included as the cause property of this exception, if
   *             available.
   */
@Override
public void setValue(ELContext context, Object base, Object property, Object value) {
    if (context == null) {
        throw new NullPointerException("context is null");
    }
    if (base instanceof ObjectNode) {
        if (readOnly) {
            throw new PropertyNotWritableException("resolver is read-only");
        }
        ObjectNode node = (ObjectNode) base;
        if (value instanceof BigDecimal) {
            node.put(property.toString(), (BigDecimal) value);
        } else if (value instanceof Boolean) {
            node.put(property.toString(), (Boolean) value);
        } else if (value instanceof Long) {
            node.put(property.toString(), (Long) value);
        } else if (value instanceof Double) {
            node.put(property.toString(), (Double) value);
        } else if (value != null) {
            node.put(property.toString(), value.toString());
        } else {
            node.putNull(property.toString());
        }
        context.setPropertyResolved(true);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BigDecimal(java.math.BigDecimal)

Example 79 with ObjectNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode in project Activiti by Activiti.

the class ProcessDefinitionInfoCache method get.

public ProcessDefinitionInfoCacheObject get(final String processDefinitionId) {
    ProcessDefinitionInfoCacheObject infoCacheObject = null;
    if (cache.containsKey(processDefinitionId)) {
        infoCacheObject = commandExecutor.execute(new Command<ProcessDefinitionInfoCacheObject>() {

            @Override
            public ProcessDefinitionInfoCacheObject execute(CommandContext commandContext) {
                ProcessDefinitionInfoEntityManager infoEntityManager = commandContext.getProcessDefinitionInfoEntityManager();
                ObjectMapper objectMapper = commandContext.getProcessEngineConfiguration().getObjectMapper();
                ProcessDefinitionInfoCacheObject cacheObject = cache.get(processDefinitionId);
                ProcessDefinitionInfoEntity infoEntity = infoEntityManager.findProcessDefinitionInfoByProcessDefinitionId(processDefinitionId);
                if (infoEntity != null && infoEntity.getRevision() != cacheObject.getRevision()) {
                    cacheObject.setRevision(infoEntity.getRevision());
                    if (infoEntity.getInfoJsonId() != null) {
                        byte[] infoBytes = infoEntityManager.findInfoJsonById(infoEntity.getInfoJsonId());
                        try {
                            ObjectNode infoNode = (ObjectNode) objectMapper.readTree(infoBytes);
                            cacheObject.setInfoNode(infoNode);
                        } catch (Exception e) {
                            throw new ActivitiException("Error reading json info node for process definition " + processDefinitionId, e);
                        }
                    }
                } else if (infoEntity == null) {
                    cacheObject.setRevision(0);
                    cacheObject.setInfoNode(objectMapper.createObjectNode());
                }
                return cacheObject;
            }
        });
    }
    return infoCacheObject;
}
Also used : ProcessDefinitionInfoEntityManager(org.activiti.engine.impl.persistence.entity.ProcessDefinitionInfoEntityManager) ActivitiException(org.activiti.engine.ActivitiException) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Command(org.activiti.engine.impl.interceptor.Command) ProcessDefinitionInfoEntity(org.activiti.engine.impl.persistence.entity.ProcessDefinitionInfoEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ActivitiException(org.activiti.engine.ActivitiException)

Example 80 with ObjectNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode in project Activiti by Activiti.

the class UserTaskActivityBehavior method execute.

public void execute(ActivityExecution execution) throws Exception {
    TaskEntity task = TaskEntity.createAndInsert(execution);
    task.setExecution(execution);
    Expression activeNameExpression = null;
    Expression activeDescriptionExpression = null;
    Expression activeDueDateExpression = null;
    Expression activePriorityExpression = null;
    Expression activeCategoryExpression = null;
    Expression activeFormKeyExpression = null;
    Expression activeSkipExpression = null;
    Expression activeAssigneeExpression = null;
    Expression activeOwnerExpression = null;
    Set<Expression> activeCandidateUserExpressions = null;
    Set<Expression> activeCandidateGroupExpressions = null;
    if (Context.getProcessEngineConfiguration().isEnableProcessDefinitionInfoCache()) {
        ObjectNode taskElementProperties = Context.getBpmnOverrideElementProperties(userTaskId, execution.getProcessDefinitionId());
        activeNameExpression = getActiveValue(taskDefinition.getNameExpression(), DynamicBpmnConstants.USER_TASK_NAME, taskElementProperties);
        taskDefinition.setNameExpression(activeNameExpression);
        activeDescriptionExpression = getActiveValue(taskDefinition.getDescriptionExpression(), DynamicBpmnConstants.USER_TASK_DESCRIPTION, taskElementProperties);
        taskDefinition.setDescriptionExpression(activeDescriptionExpression);
        activeDueDateExpression = getActiveValue(taskDefinition.getDueDateExpression(), DynamicBpmnConstants.USER_TASK_DUEDATE, taskElementProperties);
        taskDefinition.setDueDateExpression(activeDueDateExpression);
        activePriorityExpression = getActiveValue(taskDefinition.getPriorityExpression(), DynamicBpmnConstants.USER_TASK_PRIORITY, taskElementProperties);
        taskDefinition.setPriorityExpression(activePriorityExpression);
        activeCategoryExpression = getActiveValue(taskDefinition.getCategoryExpression(), DynamicBpmnConstants.USER_TASK_CATEGORY, taskElementProperties);
        taskDefinition.setCategoryExpression(activeCategoryExpression);
        activeFormKeyExpression = getActiveValue(taskDefinition.getFormKeyExpression(), DynamicBpmnConstants.USER_TASK_FORM_KEY, taskElementProperties);
        taskDefinition.setFormKeyExpression(activeFormKeyExpression);
        activeSkipExpression = getActiveValue(taskDefinition.getSkipExpression(), DynamicBpmnConstants.TASK_SKIP_EXPRESSION, taskElementProperties);
        taskDefinition.setSkipExpression(activeSkipExpression);
        activeAssigneeExpression = getActiveValue(taskDefinition.getAssigneeExpression(), DynamicBpmnConstants.USER_TASK_ASSIGNEE, taskElementProperties);
        taskDefinition.setAssigneeExpression(activeAssigneeExpression);
        activeOwnerExpression = getActiveValue(taskDefinition.getOwnerExpression(), DynamicBpmnConstants.USER_TASK_OWNER, taskElementProperties);
        taskDefinition.setOwnerExpression(activeOwnerExpression);
        activeCandidateUserExpressions = getActiveValueSet(taskDefinition.getCandidateUserIdExpressions(), DynamicBpmnConstants.USER_TASK_CANDIDATE_USERS, taskElementProperties);
        taskDefinition.setCandidateUserIdExpressions(activeCandidateUserExpressions);
        activeCandidateGroupExpressions = getActiveValueSet(taskDefinition.getCandidateGroupIdExpressions(), DynamicBpmnConstants.USER_TASK_CANDIDATE_GROUPS, taskElementProperties);
        taskDefinition.setCandidateGroupIdExpressions(activeCandidateGroupExpressions);
    } else {
        activeNameExpression = taskDefinition.getNameExpression();
        activeDescriptionExpression = taskDefinition.getDescriptionExpression();
        activeDueDateExpression = taskDefinition.getDueDateExpression();
        activePriorityExpression = taskDefinition.getPriorityExpression();
        activeCategoryExpression = taskDefinition.getCategoryExpression();
        activeFormKeyExpression = taskDefinition.getFormKeyExpression();
        activeSkipExpression = taskDefinition.getSkipExpression();
        activeAssigneeExpression = taskDefinition.getAssigneeExpression();
        activeOwnerExpression = taskDefinition.getOwnerExpression();
        activeCandidateUserExpressions = taskDefinition.getCandidateUserIdExpressions();
        activeCandidateGroupExpressions = taskDefinition.getCandidateGroupIdExpressions();
    }
    task.setTaskDefinition(taskDefinition);
    if (activeNameExpression != null) {
        String name = null;
        try {
            name = (String) activeNameExpression.getValue(execution);
        } catch (ActivitiException e) {
            name = activeNameExpression.getExpressionText();
            LOGGER.warn("property not found in task name expression " + e.getMessage());
        }
        task.setName(name);
    }
    if (activeDescriptionExpression != null) {
        String description = null;
        try {
            description = (String) activeDescriptionExpression.getValue(execution);
        } catch (ActivitiException e) {
            description = activeDescriptionExpression.getExpressionText();
            LOGGER.warn("property not found in task description expression " + e.getMessage());
        }
        task.setDescription(description);
    }
    if (activeDueDateExpression != null) {
        Object dueDate = activeDueDateExpression.getValue(execution);
        if (dueDate != null) {
            if (dueDate instanceof Date) {
                task.setDueDate((Date) dueDate);
            } else if (dueDate instanceof String) {
                BusinessCalendar businessCalendar = Context.getProcessEngineConfiguration().getBusinessCalendarManager().getBusinessCalendar(taskDefinition.getBusinessCalendarNameExpression().getValue(execution).toString());
                task.setDueDate(businessCalendar.resolveDuedate((String) dueDate));
            } else {
                throw new ActivitiIllegalArgumentException("Due date expression does not resolve to a Date or Date string: " + activeDueDateExpression.getExpressionText());
            }
        }
    }
    if (activePriorityExpression != null) {
        final Object priority = activePriorityExpression.getValue(execution);
        if (priority != null) {
            if (priority instanceof String) {
                try {
                    task.setPriority(Integer.valueOf((String) priority));
                } catch (NumberFormatException e) {
                    throw new ActivitiIllegalArgumentException("Priority does not resolve to a number: " + priority, e);
                }
            } else if (priority instanceof Number) {
                task.setPriority(((Number) priority).intValue());
            } else {
                throw new ActivitiIllegalArgumentException("Priority expression does not resolve to a number: " + activePriorityExpression.getExpressionText());
            }
        }
    }
    if (activeCategoryExpression != null) {
        final Object category = activeCategoryExpression.getValue(execution);
        if (category != null) {
            if (category instanceof String) {
                task.setCategory((String) category);
            } else {
                throw new ActivitiIllegalArgumentException("Category expression does not resolve to a string: " + activeCategoryExpression.getExpressionText());
            }
        }
    }
    if (activeFormKeyExpression != null) {
        final Object formKey = activeFormKeyExpression.getValue(execution);
        if (formKey != null) {
            if (formKey instanceof String) {
                task.setFormKey((String) formKey);
            } else {
                throw new ActivitiIllegalArgumentException("FormKey expression does not resolve to a string: " + activeFormKeyExpression.getExpressionText());
            }
        }
    }
    boolean skipUserTask = SkipExpressionUtil.isSkipExpressionEnabled(execution, activeSkipExpression) && SkipExpressionUtil.shouldSkipFlowElement(execution, activeSkipExpression);
    if (!skipUserTask) {
        handleAssignments(activeAssigneeExpression, activeOwnerExpression, activeCandidateUserExpressions, activeCandidateGroupExpressions, task, execution);
    }
    task.fireEvent(TaskListener.EVENTNAME_CREATE);
    // All properties set, now firing 'create' events
    if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_CREATED, task));
    }
    if (skipUserTask) {
        task.complete(null, false);
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) TaskEntity(org.activiti.engine.impl.persistence.entity.TaskEntity) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Date(java.util.Date) Expression(org.activiti.engine.delegate.Expression) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) DueDateBusinessCalendar(org.activiti.engine.impl.calendar.DueDateBusinessCalendar) BusinessCalendar(org.activiti.engine.impl.calendar.BusinessCalendar)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2446 JsonNode (com.fasterxml.jackson.databind.JsonNode)556 Test (org.junit.Test)556 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)509 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)370 IOException (java.io.IOException)214 ArrayList (java.util.ArrayList)119 HashMap (java.util.HashMap)107 Map (java.util.Map)106 List (java.util.List)96 StringEntity (org.apache.http.entity.StringEntity)94 Deployment (org.activiti.engine.test.Deployment)85 Test (org.testng.annotations.Test)81 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)69 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)65 HttpPost (org.apache.http.client.methods.HttpPost)57 AbstractRpcLwM2MIntegrationTest (org.thingsboard.server.transport.lwm2m.rpc.AbstractRpcLwM2MIntegrationTest)54 TextNode (com.fasterxml.jackson.databind.node.TextNode)52 File (java.io.File)51 Task (org.activiti.engine.task.Task)51