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

fi.evolver.basics.spring.clean.GenericCleaner Maven / Gradle / Ivy

There is a newer version: 6.5.1
Show newest version
package fi.evolver.basics.spring.clean;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import fi.evolver.basics.spring.job.ResultState;
import fi.evolver.basics.spring.job.TaskStatusService;
import fi.evolver.basics.spring.triggerable.AbstractTriggerable;
import fi.evolver.basics.spring.triggerable.TriggerableException;
import fi.evolver.utils.arg.Arg;
import fi.evolver.utils.arg.IntArg;
import fi.evolver.utils.arg.StringArg;
import fi.evolver.utils.collection.JoinedList;


public abstract class GenericCleaner extends AbstractTriggerable {
	private static final StringArg ARG_ENTITY_NAME = new StringArg("EntityName");
	protected static final IntArg ARG_MAX_BATCH_SIZE = new IntArg("MaxBatchSize", 1, null, 1000);
	protected static final IntArg ARG_MAX_RUN_TIME_MS = new IntArg("MaxRunTimeMs", 1, null, 30_000);


	protected final DataCleaner dataCleaner;
	protected final TaskStatusService taskStatusService;


	@Autowired
	public GenericCleaner(DataCleaner dataCleaner, TaskStatusService taskStatusService) {
		this.dataCleaner = dataCleaner;
		this.taskStatusService = taskStatusService;
	}


	@Override
	protected ResultState run(Map args) throws TriggerableException {
		Class table = getEntityClass(args);
		return clean(table, args);
	}


	private static Class getEntityClass(Map args) throws TriggerableException {
		String className = ARG_ENTITY_NAME.get(args);
		try {
			return Class.forName(className);
		}
		catch (ClassNotFoundException e) {
			throw new TriggerableException(e, "Could not get class %s", className);
		}
	}


	protected abstract ResultState clean(Class table, Map args) throws TriggerableException;

	protected abstract List> getAdditionalArgs();


	@Override
	public List> getArgs() {
		return new JoinedList<>(
				Arrays.asList(ARG_MAX_BATCH_SIZE, ARG_MAX_RUN_TIME_MS, ARG_ENTITY_NAME),
				getAdditionalArgs());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy