package org.activiti.examples;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.activiti.api.model.shared.event.VariableCreatedEvent;
import org.activiti.api.process.model.ProcessDefinition;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.process.runtime.connector.Connector;
import org.activiti.api.runtime.shared.events.VariableEventListener;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class DemoApplication implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    @Autowired
    private ProcessRuntime processRuntime;

    @Autowired
    private SecurityUtil securityUtil;

    private List<VariableCreatedEvent> variableCreatedEvents = new ArrayList<>();

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

    }

    @Override
    public void run(String... args) {
        securityUtil.logInAs("system");

        Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
        logger.info("> Available Process definitions: " + processDefinitionPage.getTotalItems());
        for (ProcessDefinition pd : processDefinitionPage.getContent()) {
            logger.info("\t > Process definition: " + pd);
        }

        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey("RankMovie")
                .withName("myProcess")
                .withVariable("movieToRank", "Lord of the rings")
                .build());
        logger.info(">>> Created Process Instance: " + processInstance);

    }

    @Bean("Movies.getMovieDesc")
    public Connector getMovieDesc() {
        return integrationContext -> {
            Map<String, Object> inBoundVariables = integrationContext.getInBoundVariables();
            System.out.println(">>inbound: " + inBoundVariables);
            return integrationContext;
        };
    }

    @Bean
    public VariableEventListener<VariableCreatedEvent> variableCreatedEventListener() {
        return variableCreatedEvent -> variableCreatedEvents.add(variableCreatedEvent);
    }


}

@@ -463,16 +463,171 @@
 nector;%0A
+import org.activiti.api.process.runtime.events.ProcessCompletedEvent;%0Aimport org.activiti.api.process.runtime.events.listener.ProcessRuntimeEventListener;%0A
 import o
@@ -679,32 +679,32 @@
 eEventListener;%0A
-
 import org.activ
@@ -1576,24 +1576,109 @@
 yList%3C%3E();%0A%0A
+    private List%3CProcessCompletedEvent%3E processCompletedEvents = new ArrayList%3C%3E();%0A%0A
     public s
@@ -2591,24 +2591,484 @@
 Instance);%0A%0A
+        logger.info(%22%3E%3E%3E Created variables:%22);%0A        variableCreatedEvents.forEach(variableCreatedEvent -%3E logger.info(%22%3E Variable: %7Bname=%60%22 + variableCreatedEvent.getEntity().getName()%0A        + %22%60, value=%60%22 + variableCreatedEvent.getEntity().getValue()));%0A%0A        logger.info(%22%3E%3E%3E Completed process Instances: %22 );%0A        processCompletedEvents.forEach(processCompletedEvent -%3E logger.info(%22%3E Process instance : %22 + processCompletedEvent.getEntity()));%0A%0A
     %7D%0A%0A    @
@@ -3281,26 +3281,19 @@
     
-System.out.println
+logger.info
 (%22%3E%3E
@@ -3475,24 +3475,24 @@
 istener() %7B%0A
-
         retu
@@ -3574,11 +3574,215 @@
 %0A    %7D%0A%0A
+    @Bean%0A    public ProcessRuntimeEventListener%3CProcessCompletedEvent%3E processCompletedEventListener() %7B%0A        return processCompletedEvent -%3E processCompletedEvents.add(processCompletedEvent);%0A    %7D%0A%0A
 %0A%7D%0A

Add logs