Search in sources :

Example 46 with ActivitiException

use of org.activiti.engine.ActivitiException in project Activiti by Activiti.

the class ExecutionVariableDataResource method getVariableData.

@RequestMapping(value = "/runtime/executions/{executionId}/variables/{variableName}/data", method = RequestMethod.GET)
@ResponseBody
public byte[] getVariableData(@PathVariable("executionId") String executionId, @PathVariable("variableName") String variableName, @RequestParam(value = "scope", required = false) String scope, HttpServletRequest request, HttpServletResponse response) {
    try {
        byte[] result = null;
        Execution execution = getExecutionFromRequest(executionId);
        RestVariable variable = getVariableFromRequest(execution, variableName, scope, true);
        if (RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variable.getType())) {
            result = (byte[]) variable.getValue();
            response.setContentType("application/octet-stream");
        } else if (RestResponseFactory.SERIALIZABLE_VARIABLE_TYPE.equals(variable.getType())) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(buffer);
            outputStream.writeObject(variable.getValue());
            outputStream.close();
            result = buffer.toByteArray();
            response.setContentType("application/x-java-serialized-object");
        } else {
            throw new ActivitiObjectNotFoundException("The variable does not have a binary data stream.", null);
        }
        return result;
    } catch (IOException ioe) {
        throw new ActivitiException("Error getting variable " + variableName, ioe);
    }
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiException(org.activiti.engine.ActivitiException) Execution(org.activiti.engine.runtime.Execution) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 47 with ActivitiException

use of org.activiti.engine.ActivitiException in project Activiti by Activiti.

the class TaskAttachmentCollectionResource method createBinaryAttachment.

protected AttachmentResponse createBinaryAttachment(MultipartHttpServletRequest request, Task task, HttpServletResponse response) {
    String name = null;
    String description = null;
    String type = null;
    Map<String, String[]> paramMap = request.getParameterMap();
    for (String parameterName : paramMap.keySet()) {
        if (paramMap.get(parameterName).length > 0) {
            if (parameterName.equalsIgnoreCase("name")) {
                name = paramMap.get(parameterName)[0];
            } else if (parameterName.equalsIgnoreCase("description")) {
                description = paramMap.get(parameterName)[0];
            } else if (parameterName.equalsIgnoreCase("type")) {
                type = paramMap.get(parameterName)[0];
            }
        }
    }
    if (name == null) {
        throw new ActivitiIllegalArgumentException("Attachment name is required.");
    }
    if (request.getFileMap().size() == 0) {
        throw new ActivitiIllegalArgumentException("Attachment content is required.");
    }
    MultipartFile file = request.getFileMap().values().iterator().next();
    if (file == null) {
        throw new ActivitiIllegalArgumentException("Attachment content is required.");
    }
    try {
        Attachment createdAttachment = taskService.createAttachment(type, task.getId(), task.getProcessInstanceId(), name, description, file.getInputStream());
        response.setStatus(HttpStatus.CREATED.value());
        return restResponseFactory.createAttachmentResponse(createdAttachment);
    } catch (Exception e) {
        throw new ActivitiException("Error creating attachment response", e);
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ActivitiException(org.activiti.engine.ActivitiException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) Attachment(org.activiti.engine.task.Attachment) ActivitiException(org.activiti.engine.ActivitiException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException)

Example 48 with ActivitiException

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

Example 49 with ActivitiException

use of org.activiti.engine.ActivitiException in project Activiti by Activiti.

the class BpmnDeployer method addMessageEventSubscriptions.

@SuppressWarnings("unchecked")
protected void addMessageEventSubscriptions(ProcessDefinitionEntity processDefinition) {
    CommandContext commandContext = Context.getCommandContext();
    List<EventSubscriptionDeclaration> eventDefinitions = (List<EventSubscriptionDeclaration>) processDefinition.getProperty(BpmnParse.PROPERTYNAME_EVENT_SUBSCRIPTION_DECLARATION);
    if (eventDefinitions != null) {
        Set<String> messageNames = new HashSet<String>();
        for (EventSubscriptionDeclaration eventDefinition : eventDefinitions) {
            if (eventDefinition.getEventType().equals("message") && eventDefinition.isStartEvent()) {
                if (!messageNames.contains(eventDefinition.getEventName())) {
                    messageNames.add(eventDefinition.getEventName());
                } else {
                    throw new ActivitiException("Cannot deploy process definition '" + processDefinition.getResourceName() + "': there are multiple message event subscriptions for the message with name '" + eventDefinition.getEventName() + "'.");
                }
                // look for subscriptions for the same name in db:
                List<EventSubscriptionEntity> subscriptionsForSameMessageName = commandContext.getEventSubscriptionEntityManager().findEventSubscriptionsByName(MessageEventHandler.EVENT_HANDLER_TYPE, eventDefinition.getEventName(), processDefinition.getTenantId());
                // also look for subscriptions created in the session:
                List<MessageEventSubscriptionEntity> cachedSubscriptions = commandContext.getDbSqlSession().findInCache(MessageEventSubscriptionEntity.class);
                for (MessageEventSubscriptionEntity cachedSubscription : cachedSubscriptions) {
                    if (eventDefinition.getEventName().equals(cachedSubscription.getEventName()) && !subscriptionsForSameMessageName.contains(cachedSubscription)) {
                        subscriptionsForSameMessageName.add(cachedSubscription);
                    }
                }
                // remove subscriptions deleted in the same command
                subscriptionsForSameMessageName = commandContext.getDbSqlSession().pruneDeletedEntities(subscriptionsForSameMessageName);
                for (EventSubscriptionEntity eventSubscriptionEntity : subscriptionsForSameMessageName) {
                    // no process instance-id = it's a message start event
                    if (StringUtils.isEmpty(eventSubscriptionEntity.getProcessInstanceId())) {
                        throw new ActivitiException("Cannot deploy process definition '" + processDefinition.getResourceName() + "': there already is a message event subscription for the message with name '" + eventDefinition.getEventName() + "'.");
                    }
                }
                MessageEventSubscriptionEntity newSubscription = new MessageEventSubscriptionEntity();
                newSubscription.setEventName(eventDefinition.getEventName());
                newSubscription.setActivityId(eventDefinition.getActivityId());
                newSubscription.setConfiguration(processDefinition.getId());
                newSubscription.setProcessDefinitionId(processDefinition.getId());
                if (processDefinition.getTenantId() != null) {
                    newSubscription.setTenantId(processDefinition.getTenantId());
                }
                newSubscription.insert();
            }
        }
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) List(java.util.List) ArrayList(java.util.ArrayList) EventSubscriptionDeclaration(org.activiti.engine.impl.bpmn.parser.EventSubscriptionDeclaration) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) SignalEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity) HashSet(java.util.HashSet)

Example 50 with ActivitiException

use of org.activiti.engine.ActivitiException in project Activiti by Activiti.

the class ExclusiveGatewayActivityBehavior method leave.

/**
   * The default behaviour of BPMN, taking every outgoing sequence flow
   * (where the condition evaluates to true), is not valid for an exclusive
   * gateway. 
   * 
   * Hence, this behaviour is overriden and replaced by the correct behavior:
   * selecting the first sequence flow which condition evaluates to true
   * (or which hasn't got a condition) and leaving the activity through that
   * sequence flow. 
   * 
   * If no sequence flow is selected (ie all conditions evaluate to false),
   * then the default sequence flow is taken (if defined).
   */
@Override
protected void leave(ActivityExecution execution) {
    if (log.isDebugEnabled()) {
        log.debug("Leaving activity '{}'", execution.getActivity().getId());
    }
    PvmTransition outgoingSeqFlow = null;
    String defaultSequenceFlow = (String) execution.getActivity().getProperty("default");
    Iterator<PvmTransition> transitionIterator = execution.getActivity().getOutgoingTransitions().iterator();
    while (outgoingSeqFlow == null && transitionIterator.hasNext()) {
        PvmTransition seqFlow = transitionIterator.next();
        Expression skipExpression = seqFlow.getSkipExpression();
        if (!SkipExpressionUtil.isSkipExpressionEnabled(execution, skipExpression)) {
            Condition condition = (Condition) seqFlow.getProperty(BpmnParse.PROPERTYNAME_CONDITION);
            if ((condition == null && (defaultSequenceFlow == null || !defaultSequenceFlow.equals(seqFlow.getId()))) || (condition != null && condition.evaluate(seqFlow.getId(), execution))) {
                if (log.isDebugEnabled()) {
                    log.debug("Sequence flow '{}'selected as outgoing sequence flow.", seqFlow.getId());
                }
                outgoingSeqFlow = seqFlow;
            }
        } else if (SkipExpressionUtil.shouldSkipFlowElement(execution, skipExpression)) {
            outgoingSeqFlow = seqFlow;
        }
    }
    if (outgoingSeqFlow != null) {
        execution.take(outgoingSeqFlow);
    } else {
        if (defaultSequenceFlow != null) {
            PvmTransition defaultTransition = execution.getActivity().findOutgoingTransition(defaultSequenceFlow);
            if (defaultTransition != null) {
                execution.take(defaultTransition);
            } else {
                throw new ActivitiException("Default sequence flow '" + defaultSequenceFlow + "' not found");
            }
        } else {
            //No sequence flow could be found, not even a default one
            throw new ActivitiException("No outgoing sequence flow of the exclusive gateway '" + execution.getActivity().getId() + "' could be selected for continuing the process");
        }
    }
}
Also used : Condition(org.activiti.engine.impl.Condition) ActivitiException(org.activiti.engine.ActivitiException) Expression(org.activiti.engine.delegate.Expression) PvmTransition(org.activiti.engine.impl.pvm.PvmTransition)

Aggregations

ActivitiException (org.activiti.engine.ActivitiException)289 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)45 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)44 ExecutionEntity (org.activiti.engine.impl.persistence.entity.ExecutionEntity)38 IOException (java.io.IOException)35 ArrayList (java.util.ArrayList)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 FlowElement (org.activiti.bpmn.model.FlowElement)19 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)18 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)17 Expression (org.activiti.engine.delegate.Expression)17 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)17 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)17 ObjectOutputStream (java.io.ObjectOutputStream)15 WorkflowException (org.alfresco.service.cmr.workflow.WorkflowException)15 HashMap (java.util.HashMap)12 ExecutionEntityManager (org.activiti.engine.impl.persistence.entity.ExecutionEntityManager)11 ProcessDefinitionEntity (org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)11 InputStream (java.io.InputStream)10