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

com.elephantdrummer.validator.IValidatorDrummerJob Maven / Gradle / Ivy

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

import java.util.Arrays;

import com.elephantdrummer.annotation.DayOfWeek;
import com.elephantdrummer.annotation.DrummerJob;
import com.elephantdrummer.annotation.trigger.After;
import com.elephantdrummer.annotation.trigger.At;
import com.elephantdrummer.annotation.trigger.Before;
import com.elephantdrummer.annotation.trigger.Every;
import com.elephantdrummer.dictionary.DictTimeParameter;
import com.elephantdrummer.exception.ValidationException;

/**
 * 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. 
 */
public interface IValidatorDrummerJob {

	
	public static boolean isDrummerJobNameProvided(DrummerJob  from) {
		return from.name()==null==false && from.name().isEmpty()==false && from.name().equalsIgnoreCase("Unknown")==false;
	}
	
	public static boolean isDrummerJobNeedExternalConfiguration(DrummerJob from) {
		if (from==null) return true;
		
		boolean rv= isDrummerJobNameProvided(from) &&
				isDateMaskFilledWithNonDefaultContent(from.at())==false &&
				isPeriodFilledWithNonDefaultContent(from.every())==false&&
				(from.cron()==null||from.cron().length==0);
		return rv;
	}
	
	public static boolean isDrummerJobCorrect(DrummerJob from)  {
		if (from==null) return true;
		
		isDateMaskFilledWithNonDefaultContent(from.at());
//		isDateMaskFilledWithNonDefaultContent(from.after());
//		isDateMaskFilledWithNonDefaultContent(from.before());
		
		return true;
		
	}
	
	public static boolean isDateMaskFilledWithNonDefaultContentAndHasCorrectValues(At from)  {
		if (from==null) return true;
		if (from.year()<0) throw new ValidationException("Year value must be positive. There is: "+from.year());
		if (from.month()<-11||from.month()>12) throw new ValidationException("Month value out of scope (-11 - 12). There is: "+from.month());
		if (from.day()<-31||from.day()>31) throw new ValidationException("Day value out of scope (-31 - 31). There is: "+from.day());
		if (from.hour()>23) throw new ValidationException("Hour value can not be greater than 23. There is: "+from.hour());
		if (from.minute()>59) throw new ValidationException("Hour value can not be greater than 59. There is: "+from.minute());
		if (from.second()>59) throw new ValidationException("Hour value can not be greater than 59. There is: "+from.second());
		return true;
		
	}
	
	public static boolean isDateMaskFilledWithNonDefaultContentAndHasCorrectValues(After from)  {
		if (from==null) return true;
		if (from.year()<0) throw new ValidationException("Year value must be positive. There is: "+from.year());
		if (from.month()<-11||from.month()>12) throw new ValidationException("Month value out of scope (-11 - 12). There is: "+from.month());
		if (from.day()<-31||from.day()>31) throw new ValidationException("Day value out of scope (-31 - 31). There is: "+from.day());
		if (from.hour()>23) throw new ValidationException("Hour value can not be greater than 23. There is: "+from.hour());
		if (from.minute()>59) throw new ValidationException("Hour value can not be greater than 59. There is: "+from.minute());
		if (from.second()>59) throw new ValidationException("Hour value can not be greater than 59. There is: "+from.second());
		return true;
		
	}
	
	public static boolean isDateMaskFilledWithNonDefaultContentAndHasCorrectValues(Before from)  {
		if (from==null) return true;
		if (from.year()<0) throw new ValidationException("Year value must be positive. There is: "+from.year());
		if (from.month()<-11||from.month()>12) throw new ValidationException("Month value out of scope (-11 - 12). There is: "+from.month());
		if (from.day()<-31||from.day()>31) throw new ValidationException("Day value out of scope (-31 - 31). There is: "+from.day());
		if (from.hour()>23) throw new ValidationException("Hour value can not be greater than 23. There is: "+from.hour());
		if (from.minute()>59) throw new ValidationException("Hour value can not be greater than 59. There is: "+from.minute());
		if (from.second()>59) throw new ValidationException("Hour value can not be greater than 59. There is: "+from.second());
		return true;
		
	}
	
	public static boolean isDateMaskFilledWithNonDefaultContent(At[] in)  {
if (in==null||in.length==0) return false;
		
		for (At i:in) {
			
			if (isAtNonDefault(i)==false) return false;
		}
		return true;
	}
	
	

	
	public static int getFirstFilletParameterForAt(At at) {
		if (at==null) return 99;
		if (at.second()>=0) return DictTimeParameter.PARAMETER_SECOND;
		if (at.minute()>=0) return DictTimeParameter.PARAMETER_MINUTE;
		if (at.hour()>=0) return DictTimeParameter.PARAMETER_HOUR;
		if (at.day()==0==false|| (at.dayOfWeek().length==1&&at.dayOfWeek()[0].equals(DayOfWeek.ANY)==false)) return DictTimeParameter.PARAMETER_DAY;
		if (at.month()==0==false) return DictTimeParameter.PARAMETER_MONTH;
		if (at.hour()==0==false) return DictTimeParameter.PARAMETER_YEAR;
		return 99;
	}
	
	public static int getFirstFilletParameterForAfter(After at) {
		if (at==null) return 99;
		if (at.second()>=0) return DictTimeParameter.PARAMETER_SECOND;
		if (at.minute()>=0) return DictTimeParameter.PARAMETER_MINUTE;
		if (at.hour()>=0) return DictTimeParameter.PARAMETER_HOUR;
		if (at.day()==0==false|| (at.dayOfWeek().length==1&&at.dayOfWeek()[0].equals(DayOfWeek.ANY)==false)) return DictTimeParameter.PARAMETER_DAY;
		if (at.month()==0==false) return DictTimeParameter.PARAMETER_MONTH;
		if (at.hour()==0==false) return DictTimeParameter.PARAMETER_YEAR;
		return 99;
	}
	
	public static int getFirstFilletParameterForBefore(Before at) {
		if (at==null) return 99;
		if (at.second()>=0) return DictTimeParameter.PARAMETER_SECOND;
		if (at.minute()>=0) return DictTimeParameter.PARAMETER_MINUTE;
		if (at.hour()>=0) return DictTimeParameter.PARAMETER_HOUR;
		if (at.day()==0==false|| (at.dayOfWeek().length==1&&at.dayOfWeek()[0].equals(DayOfWeek.ANY)==false)) return DictTimeParameter.PARAMETER_DAY;
		if (at.month()==0==false) return DictTimeParameter.PARAMETER_MONTH;
		if (at.hour()==0==false) return DictTimeParameter.PARAMETER_YEAR;
		return 99;
	}
	
	public static boolean isAtNonDefault(At in)  {
		if (in==null) return false;
		if (in.day()==0==false && isDayOfWeekArrayCorrect(in.dayOfWeek())){
			//bład walidacja tygodnia
		}
		return in.year()>0
				||in.month()==0==false
				||(in.day()==0==false|| (in.dayOfWeek().length==1&&in.dayOfWeek()[0].equals(DayOfWeek.ANY)==false))
				||in.hour()>=0
				||in.minute()>=0
				||in.second()>=0;
		
	}
	
	
	public static boolean isDateMaskFilledWithNonDefaultContent(Before in)  {
		if (in==null) return false;
		if (in.day()==0==false && isDayOfWeekArrayCorrect(in.dayOfWeek())){
			//bład walidacja tygodnia
		}
		

		return in.year()>0
				||in.month()==0==false
				||(in.day()==0==false|| (in.dayOfWeek().length==1&&in.dayOfWeek()[0].equals(DayOfWeek.ANY)==false))
				||in.hour()>=0
				||in.minute()>=0
				||in.second()>=0;
		
	}
	
	
	public static boolean isAfterNotDefault(After in)  {
		if (in==null) return false;
		if (in.day()==0==false && isDayOfWeekArrayCorrect(in.dayOfWeek())){
			//bład walidacja tygodnia
		}
		

		return in.year()>0
				||in.month()==0==false
				||(in.day()==0==false|| (in.dayOfWeek().length==1&&in.dayOfWeek()[0].equals(DayOfWeek.ANY)==false))
				||in.hour()>=0
				||in.minute()>=0
				||in.second()>=0;
		
	}
	
	public static boolean isPeriodFilledWithNonDefaultContent(Every[] in)  {
		if (in==null||in.length==0) return false;
		
		for (Every i:in) {
			
			if (isEveryNotDefault(i)==false) return false;
		}
		return true;
	}
	
	public static boolean isEveryNotDefault(Every in)  {
		if (in==null) return false;
		return 
				(in.day()==0==false
				||in.hour()>0
				||in.minute()>0
				||in.second()>0 );
		
	}
	

	
	
	
	public static boolean isDayOfWeekArrayCorrect(DayOfWeek[] days){
		if (days==null) return true;
		
		boolean mon=false;
		boolean thu=false;
		boolean wen=false;
		boolean tue=false;
		boolean fri=false;
		boolean sat=false;
		boolean sun=false;
		
		for (DayOfWeek dw:days){
			if (DayOfWeek.ANY.equals(dw)) return true;
			if (DayOfWeek.MONDAY.equals(dw))  mon=true;
			if (DayOfWeek.THURSDAY.equals(dw))  thu=true;
			if (DayOfWeek.WEDNESDAY.equals(dw))  wen=true;
			if (DayOfWeek.TUESDAY.equals(dw))  tue=true;
			if (DayOfWeek.FRIDAY.equals(dw))  fri=true;
			if (DayOfWeek.SATURDAY.equals(dw))  sat=true;
			if (DayOfWeek.SUNDAY.equals(dw))  sun=true;
		}
		
		boolean val= mon&&thu&&wen&&tue&&fri&&sat&&sun;
		return val;
	}
	
	public static boolean isThisDayOfWeekOK(DayOfWeek day,DayOfWeek[] days){
		if (day==null) return true;
		return isDayOfWeekArrayCorrect(days)||Arrays.asList(days).contains(day);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy