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.io.File;
  import java.io.IOException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Map;
  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.query.Page;
  import org.activiti.api.runtime.shared.query.Pageable;
  import org.apache.commons.io.FileUtils;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  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.integration.annotation.InboundChannelAdapter;
  import org.springframework.integration.annotation.Poller;
  import org.springframework.integration.annotation.ServiceActivator;
  import org.springframework.integration.channel.DirectChannel;
  import org.springframework.integration.config.EnableIntegration;
  import org.springframework.integration.core.MessageSource;
  import org.springframework.integration.file.FileReadingMessageSource;
  import org.springframework.integration.file.filters.SimplePatternFileListFilter;
  import org.springframework.messaging.Message;
  import org.springframework.messaging.MessageChannel;
  
  @SpringBootApplication
  @EnableIntegration
  public class DemoApplication implements CommandLineRunner {
  
      private String INPUT_DIR = "/tmp/";
      private String FILE_PATTERN = "*.txt";
      private Logger logger = LoggerFactory.getLogger(DemoApplication.class);
  
      private final ProcessRuntime processRuntime;
  
      private final SecurityUtil securityUtil;
  
      public DemoApplication(ProcessRuntime processRuntime, SecurityUtil securityUtil) {
          this.processRuntime = processRuntime;
          this.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);
          }
      }
  
      @Bean
      public MessageChannel fileChannel() {
          return new DirectChannel();
      }
  
      @Bean
      @InboundChannelAdapter(value = "fileChannel", poller = @Poller(fixedDelay = "1000"))
      public MessageSource<File> fileReadingMessageSource() {
          FileReadingMessageSource sourceReader = new FileReadingMessageSource();
          sourceReader.setDirectory(new File(INPUT_DIR));
          sourceReader.setFilter(new SimplePatternFileListFilter(FILE_PATTERN));
          return sourceReader;
      }
  
      @ServiceActivator(inputChannel = "fileChannel")
      public void processFile(Message<File> message) throws IOException {
          securityUtil.logInAs("system");
  
          File payload = message.getPayload();
          logger.info(">>> Processing file: " + payload.getName());
  
          String content = FileUtils.readFileToString(payload, "UTF-8");
  
          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);
  
          logger.info(">>> Deleting processed file: " + payload.getName());
          payload.delete();
      }
  
      @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;
          };
      }
  }
  