content: /*
   * Copyright 2010-2025 Hyland Software, Inc. and 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.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Map;
  import java.util.Random;
  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.process.runtime.events.ProcessCompletedEvent;
  import org.activiti.api.process.runtime.events.listener.ProcessRuntimeEventListener;
  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;
  import org.springframework.scheduling.annotation.Scheduled;
  
  @SpringBootApplication
  @EnableScheduling
  public class DemoApplication implements CommandLineRunner {
  
      private Logger logger = LoggerFactory.getLogger(DemoApplication.class);
  
      @Autowired
      private ProcessRuntime processRuntime;
  
      @Autowired
      private SecurityUtil securityUtil;
  
      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);
          }
      }
  
      @Scheduled(initialDelay = 1000, fixedDelay = 1000)
      public void processText() {
          securityUtil.logInAs("system");
  
          String content = pickRandomString();
  
          SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
  
          logger.info("> Processing content: " + content + " at " + formatter.format(new Date()));
  
          ProcessInstance processInstance = processRuntime.start(
              ProcessPayloadBuilder.start()
                  .withProcessDefinitionKey("categorizeProcess")
                  .withName("Processing Content: " + content)
                  .withVariable("content", content)
                  .build()
          );
          logger.info(">>> Created Process Instance: " + processInstance);
      }
  
      @Bean
      public Connector processTextConnector() {
          return integrationContext -> {
              Map<String, Object> inBoundVariables = integrationContext.getInBoundVariables();
              String contentToProcess = (String) inBoundVariables.get("content");
              // Logic Here to decide if content is approved or not
              if (contentToProcess.contains("activiti")) {
                  logger.info("> Approving content: " + contentToProcess);
                  integrationContext.addOutBoundVariable("approved", true);
              } else {
                  logger.info("> Discarding content: " + contentToProcess);
                  integrationContext.addOutBoundVariable("approved", false);
              }
              return integrationContext;
          };
      }
  
      @Bean
      public Connector tagTextConnector() {
          return integrationContext -> {
              String contentToTag = (String) integrationContext.getInBoundVariables().get("content");
              contentToTag += " :) ";
              integrationContext.addOutBoundVariable("content", contentToTag);
              logger.info("Final Content: " + contentToTag);
              return integrationContext;
          };
      }
  
      @Bean
      public Connector discardTextConnector() {
          return integrationContext -> {
              String contentToDiscard = (String) integrationContext.getInBoundVariables().get("content");
              contentToDiscard += " :( ";
              integrationContext.addOutBoundVariable("content", contentToDiscard);
              logger.info("Final Content: " + contentToDiscard);
              return integrationContext;
          };
      }
  
      @Bean
      public ProcessRuntimeEventListener<ProcessCompletedEvent> processCompletedListener() {
          return processCompleted ->
              logger.info(
                  ">>> Process Completed: '" +
                  processCompleted.getEntity().getName() +
                  "' We can send a notification to the initiator: " +
                  processCompleted.getEntity().getInitiator()
              );
      }
  
      private String pickRandomString() {
          String[] texts = {
              "hello from london",
              "Hi there from activiti!",
              "all good news over here.",
              "I've tweeted about activiti today.",
              "other boring projects.",
              "activiti cloud - Cloud Native Java BPM",
          };
          return texts[new Random().nextInt(texts.length)];
      }
  }
  