/*
 * Copyright 2018 Alfresco, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.activiti.examples;

import java.util.Map;

import org.activiti.runtime.api.connector.Connector;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.messaging.MessageChannel;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

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


    }


    @Override
    public void run(String... args) {
    }

    @Bean
    public MessageChannel fileChannel() {
        return new DirectChannel();
    }


    @Bean
    public Connector processTextConnector() {
        return integrationContext -> {
            Map<String, Object> inBoundVariables = integrationContext.getInBoundVariables();
            String contentToProcess = (String) inBoundVariables.get("fileContent");
            // Logic Here to decide if content is approved or not
            if (contentToProcess.contains("activiti")) {
                integrationContext.addOutBoundVariable("approved",
                        true);
            } else {
                integrationContext.addOutBoundVariable("approved",
                        false);
            }
            return integrationContext;
        };
    }

    @Bean
    public Connector tagTextConnector() {
        return integrationContext -> {
            String contentToTag = (String) integrationContext.getInBoundVariables().get("fileContent");
            contentToTag += " :) ";
            integrationContext.addOutBoundVariable("fileContent",
                    contentToTag);
            System.out.println("Final Content: " + contentToTag);
            return integrationContext;
        };
    }

    @Bean
    public Connector discardTextConnector() {
        return integrationContext -> {
            String contentToDiscard = (String) integrationContext.getInBoundVariables().get("fileContent");
            contentToDiscard += " :( ";
            integrationContext.addOutBoundVariable("fileContent",
                    contentToDiscard);
            System.out.println("Final Content: " + contentToDiscard);
            return integrationContext;
        };
    }

}

@@ -649,16 +649,39 @@
 mples;%0A%0A
+import java.util.List;%0A
 import j
@@ -731,27 +731,305 @@
 api.
-connector.Connector
+ProcessRuntime;%0Aimport org.activiti.runtime.api.connector.Connector;%0Aimport org.activiti.runtime.api.model.ProcessDefinition;%0Aimport org.activiti.runtime.api.model.ProcessInstance;%0Aimport org.activiti.runtime.api.model.builders.ProcessPayloadBuilder;%0Aimport org.activiti.runtime.api.query.Pageable
 ;%0Aim
@@ -1228,68 +1228,30 @@
 ork.
-boot.autoconfigure.security.servlet.SecurityAutoConfiguratio
+context.annotation.Bea
 n;%0Ai
@@ -1268,39 +1268,40 @@
 springframework.
-context
+web.bind
 .annotation.Bean
@@ -1288,36 +1288,42 @@
 bind.annotation.
-Bean
+GetMapping
 ;%0Aimport org.spr
@@ -1339,94 +1339,99 @@
 ork.
-integration.channel.DirectChannel;%0Aimport org.springframework.messaging.MessageChannel
+web.bind.annotation.PostMapping;%0Aimport org.springframework.web.bind.annotation.RequestBody
 ;%0Aim
@@ -1515,16 +1515,32 @@
 ication%0A
+@RestController%0A
 public c
@@ -1592,16 +1592,173 @@
 nner %7B%0A%0A
+    private ProcessRuntime processRuntime;%0A%0A    public DemoApplication(ProcessRuntime processRuntime) %7B%0A        this.processRuntime = processRuntime;%0A    %7D%0A%0A
     publ
@@ -1854,18 +1854,16 @@
  args);%0A
-%0A%0A
     %7D%0A%0A%0A
@@ -1871,112 +1871,885 @@
    @
-Override%0A    public void run(String... args) %7B%0A    %7D%0A%0A    @Bean%0A    public MessageChannel fileChannel
+PostMapping(%22/documents%22)%0A    public String processFile(@RequestBody String content) %7B%0A%0A        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder%0A                                                                       .start()%0A                                                                       .withProcessDefinitionKey(%22categorizeProcess%22)%0A                                                                       .withVariable(%22content%22,%0A                                                                                     content)%0A                                                                       .build());%0A        String message = %22%3E%3E%3E Created Process Instance: %22 + processInstance;%0A        System.out.println(message);%0A        return message;%0A    %7D%0A%0A    @GetMapping(%22/process-definitions%22)%0A    public List%3CProcessDefinition%3E getSomething
 ()
- 
 %7B%0A  
@@ -2765,36 +2765,143 @@
 urn 
-new DirectChannel();
+processRuntime.processDefinitions(Pageable.of(0, 100)).getContent();%0A    %7D%0A%0A    @Override%0A    public void run(String... args) %7B
 %0A    %7D%0A%0A
 %0A   
@@ -2892,25 +2892,24 @@
 s) %7B%0A    %7D%0A%0A
-%0A
     @Bean%0A  

Add get process definition