Search in sources :

Example 1 with ScriptTaskListenerBuilder

use of org.activiti.workflow.simple.alfresco.conversion.script.ScriptTaskListenerBuilder in project Activiti by Activiti.

the class AlfrescoReviewStepConverter method createProcessArtifact.

@Override
protected FlowElement createProcessArtifact(AlfrescoReviewStepDefinition stepDefinition, WorkflowDefinitionConversion conversion) {
    FlowElement lastElement;
    M2Model model = AlfrescoConversionUtil.getContentModel(conversion);
    String namespacePrefix = model.getNamespaces().get(0).getPrefix();
    String id = stepDefinition.getId();
    if (id == null) {
        id = AlfrescoConversionUtil.getValidIdString(stepDefinition.getName());
    }
    // Break down the review into separate steps and convert those instead
    HumanStepDefinition reviewTask = new HumanStepDefinition();
    reviewTask.setName(stepDefinition.getName());
    reviewTask.setDescription("Review task");
    // Clone the review-form and add custom transitions property
    FormDefinition finalForm = null;
    if (stepDefinition.getForm() != null) {
        finalForm = stepDefinition.getForm().clone();
    } else {
        finalForm = new FormDefinition();
    }
    finalForm.addFormProperty(createTransitionsProperty());
    reviewTask.setForm(finalForm);
    // Assignment
    if (stepDefinition.getAssignmentType() == HumanStepAssignmentType.USER) {
        reviewTask.setAssignee(new PropertyReference(stepDefinition.getAssignmentPropertyName()).getPlaceholder());
    }
    // Add a script-task that initializes the correct variables for the review
    ScriptServiceTaskBuilder builder = new ScriptServiceTaskBuilder();
    builder.setExecutionVariable(getCountVariableName(id, namespacePrefix), "0");
    String requiredCount = null;
    if (stepDefinition.getRequiredApprovalCount() != null) {
        if (PropertyReference.isPropertyReference(stepDefinition.getRequiredApprovalCount())) {
            PropertyReference reference = PropertyReference.createReference(stepDefinition.getRequiredApprovalCount());
            requiredCount = reference.getVariableReference(namespacePrefix);
            AlfrescoConversionUtil.getPropertyReferences(conversion).add(reference);
        } else {
            // No reference, use explicit value
            requiredCount = stepDefinition.getRequiredApprovalCount();
        }
    } else {
        requiredCount = "1";
    }
    builder.setExecutionVariable(getRequiredCountVariableName(id, namespacePrefix), requiredCount);
    ServiceTask serviceTask = builder.build();
    serviceTask.setName("Review initialization");
    serviceTask.setId(conversion.getUniqueNumberedId(ConversionConstants.SERVICE_TASK_ID_PREFIX));
    addFlowElement(conversion, serviceTask, true);
    // Create the actual element
    UserTask userTask = (UserTask) conversion.getConversionFactory().getStepConverterFor(reviewTask).convertStepDefinition(reviewTask, conversion);
    lastElement = userTask;
    M2Type userTaskType = model.getType(userTask.getFormKey());
    // Update parent, since we use an "outcome" for this task
    userTaskType.setParentName(AlfrescoConversionConstants.OUTCOME_BASE_FORM_TYPE);
    // Add script to the complete-task listener to update approval count (if needed)
    ScriptTaskListenerBuilder listenerBuilder = AlfrescoConversionUtil.getScriptTaskListenerBuilder(conversion, userTask.getId(), AlfrescoConversionConstants.TASK_LISTENER_EVENT_COMPLETE);
    String approverCount = getCountVariableName(id, namespacePrefix);
    listenerBuilder.addLine("if(task.getVariableLocal('" + getTransitionProperty(userTaskType, namespacePrefix) + "') == '" + AlfrescoConversionConstants.TRANSITION_APPROVE + "') {");
    listenerBuilder.addLine("execution.setVariable('" + approverCount + "', " + approverCount + " + 1);");
    listenerBuilder.addLine("}");
    if (stepDefinition.getAssignmentType() == HumanStepAssignmentType.USERS) {
        String assignmentVariableName = id + "Assignee";
        // Add the assignee-property to the content-model
        M2ClassAssociation reviewAssignee = new M2ClassAssociation();
        M2AssociationTarget target = new M2AssociationTarget();
        target.setClassName(AlfrescoConversionConstants.CONTENT_TYPE_PEOPLE);
        target.setMandatory(true);
        target.setMany(false);
        M2AssociationSource source = new M2AssociationSource();
        source.setMany(false);
        source.setMandatory(true);
        reviewAssignee.setName(AlfrescoConversionUtil.getQualifiedName(namespacePrefix, assignmentVariableName));
        reviewAssignee.setTarget(target);
        reviewAssignee.setSource(source);
        userTaskType.getAssociations().add(reviewAssignee);
        userTask.setAssignee(new PropertyReference(assignmentVariableName).getUsernameReferenceExpression(namespacePrefix));
        // Finally, add the multi-instance characteristics to the userTask
        MultiInstanceLoopCharacteristics mi = new MultiInstanceLoopCharacteristics();
        mi.setCompletionCondition(getCompletionCondition(id, namespacePrefix));
        mi.setElementVariable(new PropertyReference(assignmentVariableName).getVariableReference(namespacePrefix));
        PropertyReference reference = null;
        if (PropertyReference.isPropertyReference(stepDefinition.getAssignmentPropertyName())) {
            reference = PropertyReference.createReference(stepDefinition.getAssignmentPropertyName());
        } else {
            reference = new PropertyReference(stepDefinition.getAssignmentPropertyName());
        }
        mi.setInputDataItem(reference.getVariableReference(namespacePrefix));
        AlfrescoConversionUtil.getPropertyReferences(conversion).add(reference);
        mi.setSequential(false);
        userTask.setLoopCharacteristics(mi);
    }
    if (stepDefinition.getRejectionSteps() != null) {
        // Create choice-step
        ChoiceStepsDefinition choice = new ChoiceStepsDefinition();
        choice.setId(id + "choice");
        // Add rejection steps to the choice
        ListConditionStepDefinition<ChoiceStepsDefinition> rejectStepList = new ListConditionStepDefinition<ChoiceStepsDefinition>();
        rejectStepList.setName("Rejected");
        for (StepDefinition child : stepDefinition.getRejectionSteps()) {
            rejectStepList.addStep(child);
        }
        // Add end-process step to reject path, if needed
        if (stepDefinition.isEndProcessOnReject()) {
            rejectStepList.addStep(new AlfrescoEndProcessStepDefinition());
        }
        // Make choice condition based on review outcome
        ConditionDefinition condition = new ConditionDefinition();
        condition.setLeftOperand(getCountVariableName(id, namespacePrefix));
        condition.setOperator("<");
        condition.setRightOperand(getRequiredCountVariableName(id, namespacePrefix));
        rejectStepList.setConditions(Arrays.asList(condition));
        choice.addStepList(rejectStepList);
        // Add default (empty) choice for approval AFTER the review-one
        ListConditionStepDefinition<ChoiceStepsDefinition> defaultStepList = new ListConditionStepDefinition<ChoiceStepsDefinition>();
        defaultStepList.setName("Approved");
        choice.addStepList(defaultStepList);
        // Convert the choice-step
        lastElement = (FlowElement) conversion.getConversionFactory().getStepConverterFor(choice).convertStepDefinition(choice, conversion);
    }
    return lastElement;
}
Also used : ServiceTask(org.activiti.bpmn.model.ServiceTask) ListConditionStepDefinition(org.activiti.workflow.simple.definition.ListConditionStepDefinition) M2Model(org.activiti.workflow.simple.alfresco.model.M2Model) ChoiceStepsDefinition(org.activiti.workflow.simple.definition.ChoiceStepsDefinition) UserTask(org.activiti.bpmn.model.UserTask) PropertyReference(org.activiti.workflow.simple.alfresco.conversion.script.PropertyReference) M2AssociationTarget(org.activiti.workflow.simple.alfresco.model.M2AssociationTarget) HumanStepDefinition(org.activiti.workflow.simple.definition.HumanStepDefinition) M2Type(org.activiti.workflow.simple.alfresco.model.M2Type) AlfrescoEndProcessStepDefinition(org.activiti.workflow.simple.alfresco.step.AlfrescoEndProcessStepDefinition) MultiInstanceLoopCharacteristics(org.activiti.bpmn.model.MultiInstanceLoopCharacteristics) FlowElement(org.activiti.bpmn.model.FlowElement) ScriptServiceTaskBuilder(org.activiti.workflow.simple.alfresco.conversion.script.ScriptServiceTaskBuilder) M2AssociationSource(org.activiti.workflow.simple.alfresco.model.M2AssociationSource) StepDefinition(org.activiti.workflow.simple.definition.StepDefinition) AlfrescoEndProcessStepDefinition(org.activiti.workflow.simple.alfresco.step.AlfrescoEndProcessStepDefinition) ListConditionStepDefinition(org.activiti.workflow.simple.definition.ListConditionStepDefinition) AlfrescoReviewStepDefinition(org.activiti.workflow.simple.alfresco.step.AlfrescoReviewStepDefinition) HumanStepDefinition(org.activiti.workflow.simple.definition.HumanStepDefinition) FormDefinition(org.activiti.workflow.simple.definition.form.FormDefinition) ConditionDefinition(org.activiti.workflow.simple.definition.ConditionDefinition) ScriptTaskListenerBuilder(org.activiti.workflow.simple.alfresco.conversion.script.ScriptTaskListenerBuilder) M2ClassAssociation(org.activiti.workflow.simple.alfresco.model.M2ClassAssociation)

Example 2 with ScriptTaskListenerBuilder

use of org.activiti.workflow.simple.alfresco.conversion.script.ScriptTaskListenerBuilder in project Activiti by Activiti.

the class AlfrescoHumanStepDefinitionConverter method addPropertySharing.

protected void addPropertySharing(HumanStepDefinition humanStep, WorkflowDefinitionConversion conversion, UserTask userTask) {
    PropertySharing sharing = AlfrescoConversionUtil.getPropertySharing(conversion, userTask.getId());
    // Add default incoming properties (due-date and priority)
    // TODO: make optional?
    ScriptTaskListenerBuilder createEventBuilder = AlfrescoConversionUtil.getScriptTaskListenerBuilder(conversion, userTask.getId(), AlfrescoConversionConstants.TASK_LISTENER_EVENT_CREATE);
    createEventBuilder.addDueDateInheritance();
    createEventBuilder.addPriorityInheritance();
    // Add create-listener in case incoming properties are present
    if (sharing.hasIncomingProperties()) {
        createEventBuilder = AlfrescoConversionUtil.getScriptTaskListenerBuilder(conversion, userTask.getId(), AlfrescoConversionConstants.TASK_LISTENER_EVENT_CREATE);
        for (Entry<String, String> entry : sharing.getIncomingProperties().entrySet()) {
            createEventBuilder.addIncomingProperty(entry.getKey(), entry.getValue());
        }
    }
    // Add complete-listener in case incoming properties are present
    if (sharing.hasOutgoingProperties()) {
        ScriptTaskListenerBuilder completeEventBuilder = AlfrescoConversionUtil.getScriptTaskListenerBuilder(conversion, userTask.getId(), AlfrescoConversionConstants.TASK_LISTENER_EVENT_COMPLETE);
        for (Entry<String, String> entry : sharing.getOutgoingProperties().entrySet()) {
            completeEventBuilder.addOutgoingProperty(entry.getKey(), entry.getValue());
        }
    }
}
Also used : ScriptTaskListenerBuilder(org.activiti.workflow.simple.alfresco.conversion.script.ScriptTaskListenerBuilder)

Example 3 with ScriptTaskListenerBuilder

use of org.activiti.workflow.simple.alfresco.conversion.script.ScriptTaskListenerBuilder in project Activiti by Activiti.

the class AlfrescoConversionUtil method getScriptTaskListenerBuilder.

/**
	 * @return the {@link ScriptTaskListenerBuilder} object for the given usertask-id. Creates and registers a new
	 * object if there is no {@link ScriptTaskListenerBuilder} object registered for the given usertask.
	 */
@SuppressWarnings("unchecked")
public static ScriptTaskListenerBuilder getScriptTaskListenerBuilder(WorkflowDefinitionConversion conversion, String userTaskId, String eventName) {
    String key = userTaskId + "-" + eventName;
    Map<String, ScriptTaskListenerBuilder> builderMap = (Map<String, ScriptTaskListenerBuilder>) conversion.getArtifact(ARTIFACT_PROPERTY_TASK_SCRIPT_BUILDER);
    if (builderMap == null) {
        builderMap = new HashMap<String, ScriptTaskListenerBuilder>();
        conversion.setArtifact(ARTIFACT_PROPERTY_TASK_SCRIPT_BUILDER, builderMap);
    }
    ScriptTaskListenerBuilder result = builderMap.get(key);
    if (result == null) {
        result = new ScriptTaskListenerBuilder();
        result.setEvent(eventName);
        builderMap.put(key, result);
    }
    return result;
}
Also used : Map(java.util.Map) HashMap(java.util.HashMap) ScriptTaskListenerBuilder(org.activiti.workflow.simple.alfresco.conversion.script.ScriptTaskListenerBuilder)

Aggregations

ScriptTaskListenerBuilder (org.activiti.workflow.simple.alfresco.conversion.script.ScriptTaskListenerBuilder)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 FlowElement (org.activiti.bpmn.model.FlowElement)1 MultiInstanceLoopCharacteristics (org.activiti.bpmn.model.MultiInstanceLoopCharacteristics)1 ServiceTask (org.activiti.bpmn.model.ServiceTask)1 UserTask (org.activiti.bpmn.model.UserTask)1 PropertyReference (org.activiti.workflow.simple.alfresco.conversion.script.PropertyReference)1 ScriptServiceTaskBuilder (org.activiti.workflow.simple.alfresco.conversion.script.ScriptServiceTaskBuilder)1 M2AssociationSource (org.activiti.workflow.simple.alfresco.model.M2AssociationSource)1 M2AssociationTarget (org.activiti.workflow.simple.alfresco.model.M2AssociationTarget)1 M2ClassAssociation (org.activiti.workflow.simple.alfresco.model.M2ClassAssociation)1 M2Model (org.activiti.workflow.simple.alfresco.model.M2Model)1 M2Type (org.activiti.workflow.simple.alfresco.model.M2Type)1 AlfrescoEndProcessStepDefinition (org.activiti.workflow.simple.alfresco.step.AlfrescoEndProcessStepDefinition)1 AlfrescoReviewStepDefinition (org.activiti.workflow.simple.alfresco.step.AlfrescoReviewStepDefinition)1 ChoiceStepsDefinition (org.activiti.workflow.simple.definition.ChoiceStepsDefinition)1 ConditionDefinition (org.activiti.workflow.simple.definition.ConditionDefinition)1 HumanStepDefinition (org.activiti.workflow.simple.definition.HumanStepDefinition)1 ListConditionStepDefinition (org.activiti.workflow.simple.definition.ListConditionStepDefinition)1