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

com.elephantdrummer.parser.ParserDateMaskToStep Maven / Gradle / Ivy

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

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.elephantdrummer.annotation.DayOfWeek;
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.annotation.trigger.Range;
import com.elephantdrummer.dictionary.DictPeriod;
import com.elephantdrummer.validator.IValidatorDrummerJob;

/**
 * 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 ParserDateMaskToStep {

	public static Long toMiliseconds(Every period) {
		if (period==null) return null;
		long retVal=0;
		if (period.milisecond()>0) retVal=retVal+period.milisecond()*DictPeriod.PERIOD_MILISECOND;
		if (period.second()>0) retVal=retVal+period.second()*DictPeriod.PERIOD_SECOND;
		if (period.minute()>0) retVal=retVal+period.minute()*DictPeriod.PERIOD_MINUTE;
		if (period.hour()>0) retVal=retVal+period.hour()*DictPeriod.PERIOD_HOUR;
		if (period.day()>0) retVal=retVal+period.day()*DictPeriod.PERIOD_DAY;
		return retVal;
	}
	
	public static Long toMiliseconds(Range period) {
		if (period==null) return null;
		long retVal=0;
		if (period.milisecond()>0) retVal=retVal+period.milisecond()*DictPeriod.PERIOD_MILISECOND;
		if (period.second()>0) retVal=retVal+period.second()*DictPeriod.PERIOD_SECOND;
		if (period.minute()>0) retVal=retVal+period.minute()*DictPeriod.PERIOD_MINUTE;
		if (period.hour()>0) retVal=retVal+period.hour()*DictPeriod.PERIOD_HOUR;
		if (period.day()>0) retVal=retVal+period.day()*DictPeriod.PERIOD_DAY;
		return retVal;
	}
	
	
	public static Date addDateStepToDate(Date before,Date after,At datemask){
		long diff = after.getTime() -before.getTime();
		long daysbetween=TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
		Calendar cal=Calendar.getInstance();
		cal.setTime(before);
		cal.add(Calendar.DAY_OF_YEAR, (int)daysbetween);
		
		//TODO: czy nie powinno byc wczesniej?
		if (IValidatorDrummerJob.isAtNonDefault(datemask)==false) return before;
		
		if (datemask.hour()>0||datemask.minute()>0||datemask.second()>0){
	
			if (datemask.hour()>0)cal.add(Calendar.DAY_OF_MONTH,1);
			else if (datemask.minute()>0)cal.add(Calendar.HOUR,1);
			else if (datemask.second()>0)cal.add(Calendar.MINUTE,1);
		}
		
		if (datemask.day()!=0||datemask.month()>0||datemask.year()>0||IValidatorDrummerJob.isDayOfWeekArrayCorrect(datemask.dayOfWeek())==false){
			if (datemask.day()==0){
				if (IValidatorDrummerJob.isDayOfWeekArrayCorrect(datemask.dayOfWeek())){
					//Jesli nie bawimy sie w dni tygodnia, dodajmy i zapominamy
					cal.add(Calendar.DAY_OF_YEAR,1);
				}else{
		    		Integer dow=cal.get(Calendar.DAY_OF_WEEK);
		    		
		    		DayOfWeek drummerDoW=ParserDayOfWeekToCalendarDay.toDrummerDayOfWeek(dow);
//		    		if (isAnyDayOfWeek.isThisDayOfWeekOK(drummerDoW, datemask.dayOfWeek())==false){
		    			int shiftindays=getNearestDayOfWeek(drummerDoW,datemask.dayOfWeek());
		    			cal.add(Calendar.DAY_OF_YEAR,shiftindays);
//		    		}
				}
			}
			else if (datemask.month()==0)cal.add(Calendar.MONTH,1);
			else if (datemask.year()==0)cal.add(Calendar.YEAR,1);
		}
		return cal.getTime();
	}
	
	
	public static Date addDateStepToDate(Date before,Date after,Before datemask){
		long diff = after.getTime() -before.getTime();
		long daysbetween=TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
		Calendar cal=Calendar.getInstance();
		cal.setTime(before);
		cal.add(Calendar.DAY_OF_YEAR, (int)daysbetween);
		
		//TODO: czy nie powinno byc wczesniej?
		if (IValidatorDrummerJob.isDateMaskFilledWithNonDefaultContent(datemask)==false) return before;
		
		if (datemask.hour()>0||datemask.minute()>0||datemask.second()>0){
	
			if (datemask.hour()>0)cal.add(Calendar.DAY_OF_MONTH,1);
			else if (datemask.minute()>0)cal.add(Calendar.HOUR,1);
			else if (datemask.second()>0)cal.add(Calendar.MINUTE,1);
		}
		
		if (datemask.day()!=0||datemask.month()>0||datemask.year()>0||IValidatorDrummerJob.isDayOfWeekArrayCorrect(datemask.dayOfWeek())==false){
			if (datemask.day()==0){
				if (IValidatorDrummerJob.isDayOfWeekArrayCorrect(datemask.dayOfWeek())){
					//Jesli nie bawimy sie w dni tygodnia, dodajmy i zapominamy
					cal.add(Calendar.DAY_OF_YEAR,1);
				}else{
		    		Integer dow=cal.get(Calendar.DAY_OF_WEEK);
		    		
		    		DayOfWeek drummerDoW=ParserDayOfWeekToCalendarDay.toDrummerDayOfWeek(dow);
//		    		if (isAnyDayOfWeek.isThisDayOfWeekOK(drummerDoW, datemask.dayOfWeek())==false){
		    			int shiftindays=getNearestDayOfWeek(drummerDoW,datemask.dayOfWeek());
		    			cal.add(Calendar.DAY_OF_YEAR,shiftindays);
//		    		}
				}
			}
			else if (datemask.month()==0)cal.add(Calendar.MONTH,1);
			else if (datemask.year()==0)cal.add(Calendar.YEAR,1);
		}
		return cal.getTime();
	}
	
	
	public static Date addDateStepToDate(Date before,Date after,After datemask){
		long diff = after.getTime() -before.getTime();
		long daysbetween=TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
		Calendar cal=Calendar.getInstance();
		cal.setTime(before);
		cal.add(Calendar.DAY_OF_YEAR, (int)daysbetween);
		
		//TODO: czy nie powinno byc wczesniej?
		if (IValidatorDrummerJob.isAfterNotDefault(datemask)==false) return before;
		
		if (datemask.hour()>0||datemask.minute()>0||datemask.second()>0){
	
			if (datemask.hour()>0)cal.add(Calendar.DAY_OF_MONTH,1);
			else if (datemask.minute()>0)cal.add(Calendar.HOUR,1);
			else if (datemask.second()>0)cal.add(Calendar.MINUTE,1);
		}
		
		if (datemask.day()!=0||datemask.month()>0||datemask.year()>0||IValidatorDrummerJob.isDayOfWeekArrayCorrect(datemask.dayOfWeek())==false){
			if (datemask.day()==0){
				if (IValidatorDrummerJob.isDayOfWeekArrayCorrect(datemask.dayOfWeek())){
					//Jesli nie bawimy sie w dni tygodnia, dodajmy i zapominamy
					cal.add(Calendar.DAY_OF_YEAR,1);
				}else{
		    		Integer dow=cal.get(Calendar.DAY_OF_WEEK);
		    		
		    		DayOfWeek drummerDoW=ParserDayOfWeekToCalendarDay.toDrummerDayOfWeek(dow);
//		    		if (isAnyDayOfWeek.isThisDayOfWeekOK(drummerDoW, datemask.dayOfWeek())==false){
		    			int shiftindays=getNearestDayOfWeek(drummerDoW,datemask.dayOfWeek());
		    			cal.add(Calendar.DAY_OF_YEAR,shiftindays);
//		    		}
				}
			}
			else if (datemask.month()==0)cal.add(Calendar.MONTH,1);
			else if (datemask.year()==0)cal.add(Calendar.YEAR,1);
		}
		return cal.getTime();
	}
	
	
	public static Date addDateStepToDate(Date before,At job){
		if (IValidatorDrummerJob.isAtNonDefault(job)==false) return before;
		Calendar cal=Calendar.getInstance();
		
		if (job.hour()>0||job.minute()>0||job.second()>0){
	
			if (job.hour()>0)cal.add(Calendar.DAY_OF_MONTH,1);
			else if (job.minute()>0)cal.add(Calendar.HOUR,1);
			else if (job.second()>0)cal.add(Calendar.MINUTE,1);
		}
		
		if (job.day()!=0||IValidatorDrummerJob.isDayOfWeekArrayCorrect(job.dayOfWeek())==false||job.month()>0||job.year()>0){
			if (job.day()==0&&IValidatorDrummerJob.isDayOfWeekArrayCorrect(job.dayOfWeek())==false)cal.add(Calendar.DAY_OF_YEAR,1);
			else if (job.day()==0)cal.add(Calendar.DAY_OF_YEAR,1);
			else if (job.month()==0)cal.add(Calendar.MONTH,1);
			else if (job.year()==0)cal.add(Calendar.YEAR,1);
		}

		
		return cal.getTime();
	}

	
	public static int getNearestDayOfWeek(DayOfWeek day,DayOfWeek[] days){
		if (day==null||days==null||days.length==0) return 0;
		List list=Arrays.asList(days);
		for (int i=0;i<7;i++){
			if (list.contains(day)){
				return i;
			}
			day=ParserDayOfWeekToCalendarDay.getNextDayOfWeek(day);
		}
		return 0;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy