All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nitorcreations.nflow.engine.service.WorkflowDefinitionService Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
package com.nitorcreations.nflow.engine.service;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.AbstractResource;
import org.springframework.stereotype.Component;

import com.nitorcreations.nflow.engine.internal.config.NFlow;
import com.nitorcreations.nflow.engine.internal.dao.WorkflowDefinitionDao;
import com.nitorcreations.nflow.engine.internal.dao.WorkflowInstanceDao;
import com.nitorcreations.nflow.engine.workflow.definition.StateExecutionStatistics;
import com.nitorcreations.nflow.engine.workflow.definition.WorkflowDefinition;
import com.nitorcreations.nflow.engine.workflow.definition.WorkflowState;

/**
 * Service for managing workflow definitions.
 */
@Component
public class WorkflowDefinitionService {

  private static final Logger logger = getLogger(WorkflowDefinitionService.class);

  private final AbstractResource nonSpringWorkflowsListing;
  private final Map> workflowDefitions = new LinkedHashMap<>();
  private final WorkflowInstanceDao workflowInstanceDao;
  private final WorkflowDefinitionDao workflowDefinitionDao;

  @Inject
  public WorkflowDefinitionService(@NFlow AbstractResource nflowNonSpringWorkflowsListing,
      WorkflowInstanceDao workflowInstanceDao, WorkflowDefinitionDao workflowDefinitionDao) {
    this.nonSpringWorkflowsListing = nflowNonSpringWorkflowsListing;
    this.workflowInstanceDao = workflowInstanceDao;
    this.workflowDefinitionDao = workflowDefinitionDao;
  }

  /**
   * Add given workflow definitions to the managed definitions.
   * @param workflowDefinitions The workflow definitions to be added.
   */
  @Autowired(required=false)
  public void setWorkflowDefinitions(Collection> workflowDefinitions) {
    for (WorkflowDefinition wd : workflowDefinitions) {
      addWorkflowDefinition(wd);
    }
  }

  /**
   * Return the workflow definition that matches the give workflow type name.
   * @param type Workflow definition type.
   * @return The workflow definition or null if not found.
   */
  public WorkflowDefinition getWorkflowDefinition(String type) {
    return workflowDefitions.get(type);
  }

  /**
   * Return all managed workflow definitions.
   * @return List of workflow definitions.
   */
  public List> getWorkflowDefinitions() {
    return new ArrayList<>(workflowDefitions.values());
  }

  /**
   * Add workflow definitions from the nflowNonSpringWorkflowsListing resource and persist
   * all loaded workflow definitions.
   * @throws IOException when workflow definitions can not be read from the resource.
   * @throws ReflectiveOperationException when the workflow definition can not be instantiated.
   */
  @PostConstruct
  public void postProcessWorkflowDefinitions() throws IOException, ReflectiveOperationException {
    if (nonSpringWorkflowsListing == null) {
      logger.info("No non-Spring workflow definitions");
    } else {
      initNonSpringWorkflowDefinitions();
    }
    for (WorkflowDefinition definition : workflowDefitions.values()) {
      workflowDefinitionDao.storeWorkflowDefinition(definition);
    }
  }

  private void initNonSpringWorkflowDefinitions() throws IOException, ReflectiveOperationException {
    try (BufferedReader br = new BufferedReader(new InputStreamReader(nonSpringWorkflowsListing.getInputStream(), UTF_8))) {
      String row;
      while ((row = br.readLine()) != null) {
        logger.info("Preparing workflow {}", row);
        @SuppressWarnings("unchecked")
        Class> clazz = (Class>) Class.forName(row);
        addWorkflowDefinition(clazz.newInstance());
      }
    }
  }

  private void addWorkflowDefinition(WorkflowDefinition wd) {
    WorkflowDefinition conflict = workflowDefitions.put(wd.getType(), wd);
    if (conflict != null) {
      throw new IllegalStateException("Both " + wd.getClass().getName() + " and " + conflict.getClass().getName() +
          " define same workflow type: " + wd.getType());
    }
    logger.info("Added workflow type: {} ({})",  wd.getType(), wd.getClass().getName());
  }

  /**
   * Return workflow definition statistics for a given type.
   * @param type The workflow definition type.
   * @param createdAfter If given, count only workflow instances created after this time.
   * @param createdBefore If given, count only workflow instances created before this time.
   * @param modifiedAfter If given, count only workflow instances modified after this time.
   * @param modifiedBefore If given, count only workflow instances modified after this time.
   * @return The statistics per workflow state.
   */
  public Map getStatistics(String type, DateTime createdAfter, DateTime createdBefore,
      DateTime modifiedAfter, DateTime modifiedBefore) {
    return workflowInstanceDao.getStateExecutionStatistics(type, createdAfter, createdBefore, modifiedAfter, modifiedBefore);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy