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

com.elephantdrummer.trigger.resolver.TriggerResolver Maven / Gradle / Ivy

There is a newer version: 1.2.5
Show newest version
package com.elephantdrummer.trigger.resolver;

/**
 * Copyright 2018 Elephant Software Klaudiusz Wojtkowiak e-mail: [email protected]
 * 
 * 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. 
 */
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import com.elephantdrummer.annotation.DrummerJob;
import com.elephantdrummer.annotation.trigger.At;
import com.elephantdrummer.annotation.trigger.Every;
import com.elephantdrummer.container.Container;
import com.elephantdrummer.container.ContainerElement;
import com.elephantdrummer.tool.DetailedLogs;
import com.elephantdrummer.tool.ProducerCDIObjectFromClass;
import com.elephantdrummer.trigger.AtTrigger;
import com.elephantdrummer.trigger.CronTrigger;
import com.elephantdrummer.trigger.DummyTrigger;
import com.elephantdrummer.trigger.EveryTrigger;
import com.elephantdrummer.trigger.MultiTrigger;
import com.elephantdrummer.trigger.base.DrumTrigger;
import com.elephantdrummer.validator.IValidatorDrummerJob;

@DetailedLogs
public class TriggerResolver implements ContainerElement{

	private ProducerCDIObjectFromClass cdiproducer=Container.getElement(ProducerCDIObjectFromClass.class);
	private Logger log=Logger.getLogger(TriggerResolver.class.getSimpleName());
	
	
	
//	public DrumTrigger getTrigger(JobClassConfig classConfig){
//		return null;
//	}
	
//	public DrumTrigger getTrigger(DrummerJob annotation){
//		if (annotation==null) return null;
//		DrumTrigger trigger=null;
//			
//		List triggersFromDrummerJob=getTriggerFromDrummerJobAnnotation(annotation);
//		if (triggersFromDrummerJob.size()==1) {
//			trigger=triggersFromDrummerJob.get(0);
//		}else {
//			MultiTrigger mt=new MultiTrigger();
//			triggersFromDrummerJob.forEach(t->mt.addTrigger(t));
//			trigger=mt;
//		}
//		
//		return trigger;
//	}
	
	
	public DrumTrigger getTrigger(DrummerJob annotation){
		
		if (annotation==null) return null;
		DrumTrigger rv=null;
		List triglist=new LinkedList<>();
		
		Class triggerClassFromAnnotation=annotation.trigger();
		
		if (DummyTrigger.class.getName().equals(triggerClassFromAnnotation.getName())==false) {
			
			//Trigger dostarczony w adnnotacji
			return cdiproducer.get(triggerClassFromAnnotation);
		}
		
		//domyslny trigger - tworzymy go na podstawie sub-adnotacjii lub crona
			
		if (annotation.every()==null==false) {
			for (Every p:annotation.every()) {
				if (IValidatorDrummerJob.isEveryNotDefault(p)){
					EveryTrigger pt=cdiproducer.get(EveryTrigger.class);
					pt.setStep(p);
					pt.setAfter(p.after());
					pt.setBefore(p.before());
					triglist.add(pt); 
				}
			}
		}
		
		
		if (annotation.at()==null==false) {
			for (At dm:annotation.at()) {
				if (IValidatorDrummerJob.isAtNonDefault(dm)) {
					AtTrigger at=cdiproducer.get(AtTrigger.class);
					at.setAfter(dm.after());
					at.setBefore(dm.before());
					at.setAt(dm);
					triglist.add(at);
				}
			}
		}

		boolean cronprovided=annotation==null==false&&annotation.cron()==null==false;
		
		if (cronprovided) {
			for (String cr:annotation.cron()) {
				if (cr==null||cr.trim().isEmpty()) continue;
				CronTrigger crontrig=cdiproducer.get(CronTrigger.class);
				crontrig.setCron(cr);
				triglist.add(crontrig);
			}
		}
		
		triglist.forEach(t->{
			t.setShift(annotation.shift());
		});
		
		
		
		
		
		
		
		if (triglist.size()==1) {
			rv=triglist.get(0);
		}else {
			MultiTrigger mt=new MultiTrigger();
			triglist.forEach(t->mt.addTrigger(t));
			rv=mt;
		}
			
		
		
		return rv;
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy