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

fi.evolver.basics.spring.triggerable.TriggerService Maven / Gradle / Ivy

package fi.evolver.basics.spring.triggerable;

import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.ProxyUtils;
import org.springframework.stereotype.Service;

import fi.evolver.basics.spring.common.ConfigurationRepository;
import fi.evolver.basics.spring.job.ResultState;
import fi.evolver.basics.spring.job.TaskStatusService;
import fi.evolver.basics.spring.job.TaskStatusService.Task;
import fi.evolver.basics.spring.job.entity.TaskStatus;
import fi.evolver.basics.spring.triggerable.AsyncTriggerService.AsyncTriggerCallback;
import fi.evolver.utils.ContextUtils;
import fi.evolver.utils.arg.Arg;


@Service
public class TriggerService {
	private static final Logger LOG = LoggerFactory.getLogger(TriggerService.class);

	private final AsyncTriggerService asyncTriggerService;
	private final ConfigurationRepository configurationRepository;
	private final TaskStatusService taskStatusService;

	private final Map triggerables;


	@Autowired
	public TriggerService(AsyncTriggerService asyncTriggerService, ConfigurationRepository configurationRepository, TaskStatusService taskStatusService, List triggerables) {
		this.asyncTriggerService = asyncTriggerService;
		this.configurationRepository = configurationRepository;
		this.taskStatusService = taskStatusService;

		Map map = new TreeMap<>();
		for (Triggerable triggerable: triggerables)
			map.put(ProxyUtils.getUserClass(triggerable).getSimpleName(), triggerable);
		this.triggerables = Collections.unmodifiableMap(map);
	}


	public ResultState trigger(String triggerName, Map arguments) throws TriggerableException {
		if (!isEnabled(triggerName)) {
			LOG.info("Trigger {} is disabled: NOT RUN", triggerName);
			return ResultState.nothingToDo("Trigger disabled: %s", triggerName);
		}

		Triggerable triggerable = getTriggerable(triggerName);
		if (triggerable == null)
			throw new IllegalArgumentException("Unknown trigger: " + triggerName);

		Class type = ProxyUtils.getUserClass(triggerable);

		try (Task task = taskStatusService.start(type, "Triggered " + triggerName, arguments)) {
			try {
				task.setResultState(triggerable.trigger(arguments));
				LOG.info("{} - {}", triggerName, task.getResultState().getMessage());
			}
			catch (RuntimeException e) {
				task.setResultState(ResultState.failed("Failed: %s", e.getMessage()));
				throw new TriggerableException(e, "Triggering %s failed", triggerName);
			}
			return task.getResultState();
		}
	}


	public Optional triggerAsync(String triggerName, Map arguments) {
		return triggerAsync(triggerName, arguments, null);
	}

	public Optional triggerAsync(String triggerName, Map arguments, AsyncTriggerCallback callBack) {
		if (!isEnabled(triggerName)) {
			LOG.info("Trigger {} is disabled: NOT RUN", triggerName);
			return Optional.empty();
		}

		Triggerable triggerable = getTriggerable(triggerName);
		if (triggerable == null)
			throw new IllegalArgumentException("Unknown trigger: " + triggerName);

		Class type = ProxyUtils.getUserClass(triggerable);
		TaskStatus taskStatus = taskStatusService.initialize(type, "Triggered " + type.getSimpleName(), arguments);

		asyncTriggerService.runAsync(triggerable, type.getSimpleName(), arguments, taskStatus, ContextUtils.getContext(), Optional.ofNullable(callBack));
		return Optional.of(taskStatus.getId());
	}


	private boolean isEnabled(String triggerName) {
		List list = configurationRepository.getList("TriggersDisabled");

		for (String criteria: list) {
			if (triggerName.matches(criteria))
				return false;
		}

		return true;
	}


	public Set listTriggerNames() {
		return triggerables.keySet();
	}

	public Triggerable getTriggerable(String name) {
		return triggerables.get(name);
	}

	public List> getArgs(String name) {
		return getTriggerable(name).getArgs();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy