2020-12-09 16:43:29 +01:00
|
|
|
package com.plannaplan.entities;
|
|
|
|
|
|
|
|
import java.sql.Date;
|
|
|
|
import java.sql.Timestamp;
|
2021-01-04 18:10:14 +01:00
|
|
|
import java.time.LocalDate;
|
|
|
|
import java.util.TimeZone;
|
2020-12-09 16:43:29 +01:00
|
|
|
|
|
|
|
import javax.persistence.Entity;
|
|
|
|
import javax.persistence.GeneratedValue;
|
|
|
|
import javax.persistence.GenerationType;
|
|
|
|
import javax.persistence.Id;
|
|
|
|
|
|
|
|
import com.plannaplan.models.TourData;
|
2020-12-12 15:33:00 +01:00
|
|
|
import com.plannaplan.types.AppState;
|
2020-12-09 16:43:29 +01:00
|
|
|
|
2021-01-04 18:10:14 +01:00
|
|
|
import org.springframework.scheduling.support.CronTrigger;
|
|
|
|
|
2020-12-09 16:43:29 +01:00
|
|
|
/**
|
|
|
|
* entity that keeps app configurations
|
|
|
|
*/
|
|
|
|
@Entity
|
|
|
|
public class AppConfig {
|
|
|
|
|
|
|
|
@Id
|
|
|
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
|
|
private Long id;
|
|
|
|
private Date firstTourStart;
|
|
|
|
private Date firstTourEnd;
|
|
|
|
private Date secondTourStart;
|
|
|
|
private Date secondTourEnd;
|
|
|
|
private Timestamp configDate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* no parameter construcotor
|
|
|
|
*/
|
|
|
|
public AppConfig() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* @param firstTour first TourData instacne
|
|
|
|
* @param scondTour second TourData instacne
|
|
|
|
*/
|
|
|
|
public AppConfig(TourData firstTour, TourData scondTour) {
|
|
|
|
this.firstTourStart = firstTour.getStart();
|
|
|
|
this.firstTourEnd = firstTour.getEnd();
|
|
|
|
this.secondTourStart = scondTour.getStart();
|
|
|
|
this.secondTourEnd = scondTour.getEnd();
|
|
|
|
this.configDate = new Timestamp(System.currentTimeMillis());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* second tour end getter
|
|
|
|
*
|
|
|
|
* @return Date inforamtion when second tour ends
|
|
|
|
*/
|
|
|
|
public Date getSecondTourEnd() {
|
|
|
|
return secondTourEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* first second start getter
|
|
|
|
*
|
|
|
|
* @return Date inforamtion when second tour start
|
|
|
|
*/
|
|
|
|
public Date getSecondTourStart() {
|
|
|
|
return secondTourStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* first tour end getter
|
|
|
|
*
|
|
|
|
* @return Date inforamtion when first tour end
|
|
|
|
*/
|
|
|
|
public Date getFirstTourEnd() {
|
|
|
|
return firstTourEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* first tour start getter
|
|
|
|
*
|
|
|
|
* @return Date inforamtion when first tour start
|
|
|
|
*/
|
|
|
|
public Date getFirstTourStart() {
|
|
|
|
return firstTourStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* config date getter
|
|
|
|
*
|
|
|
|
* @return Timestamp when configuration took place
|
|
|
|
*/
|
|
|
|
public Timestamp getConfigDate() {
|
|
|
|
return configDate;
|
|
|
|
}
|
|
|
|
|
2020-12-12 15:33:00 +01:00
|
|
|
/**
|
|
|
|
* current state getter
|
|
|
|
*
|
|
|
|
* @return AppState of app at the moment of calling method
|
|
|
|
*/
|
|
|
|
public AppState getCurrentState() {
|
|
|
|
final Date now = new Date(System.currentTimeMillis());
|
|
|
|
if (this.secondTourEnd.before(now)) {
|
|
|
|
return AppState.NO_TOUR;
|
|
|
|
}
|
|
|
|
if (this.secondTourStart.before(now)) {
|
|
|
|
return AppState.SECOND_TOUR;
|
|
|
|
}
|
|
|
|
if (this.firstTourEnd.before(now)) {
|
|
|
|
return AppState.NO_TOUR;
|
|
|
|
}
|
|
|
|
if (this.firstTourStart.before(now)) {
|
|
|
|
return AppState.FIRST_TOUR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AppState.NO_TOUR;
|
|
|
|
}
|
|
|
|
|
2021-01-05 11:14:10 +01:00
|
|
|
/**
|
|
|
|
* get cron expression of first tour end
|
|
|
|
*
|
|
|
|
* @return spring cron expression
|
|
|
|
*/
|
2021-01-04 18:10:14 +01:00
|
|
|
public CronTrigger getFirstTourEndCron() {
|
|
|
|
return getCron(this.firstTourEnd);
|
|
|
|
}
|
|
|
|
|
2021-01-05 11:14:10 +01:00
|
|
|
/**
|
|
|
|
* get cron expression of second tour end
|
|
|
|
*
|
|
|
|
* @return spring cron expression
|
|
|
|
*/
|
2021-01-04 18:10:14 +01:00
|
|
|
public CronTrigger getSecondTourEndCron() {
|
|
|
|
return getCron(this.secondTourEnd);
|
|
|
|
}
|
|
|
|
|
2021-01-05 11:14:10 +01:00
|
|
|
/**
|
|
|
|
* get cron expression of first tour start
|
|
|
|
*
|
|
|
|
* @return spring cron expression
|
|
|
|
*/
|
2021-01-04 18:10:14 +01:00
|
|
|
public CronTrigger getFirstTourStartCron() {
|
|
|
|
return getCron(this.firstTourStart);
|
|
|
|
}
|
|
|
|
|
2021-01-05 11:14:10 +01:00
|
|
|
/**
|
|
|
|
* get cron expression of second tour start
|
|
|
|
*
|
|
|
|
* @return spring cron expression
|
|
|
|
*/
|
2021-01-04 18:10:14 +01:00
|
|
|
public CronTrigger getSecondTourStartCron() {
|
|
|
|
return getCron(this.secondTourStart);
|
|
|
|
}
|
|
|
|
|
2021-01-05 11:14:10 +01:00
|
|
|
/**
|
|
|
|
* create spring cron expression
|
|
|
|
*
|
|
|
|
* @param date date to create cron exp from
|
|
|
|
* @return spring cron expression. Remember that spring's cron deosn't contain
|
|
|
|
* year in it.
|
|
|
|
*/
|
2021-01-04 18:10:14 +01:00
|
|
|
private CronTrigger getCron(Date date) {
|
|
|
|
final LocalDate tourEnd = date.toLocalDate();
|
2021-01-05 11:14:29 +01:00
|
|
|
return new CronTrigger("0 0 0 " + tourEnd.getDayOfMonth() + " " + tourEnd.getMonthValue() + " ?",
|
2021-01-04 18:10:14 +01:00
|
|
|
TimeZone.getTimeZone(TimeZone.getDefault().getID()));
|
|
|
|
}
|
|
|
|
|
2020-12-09 16:43:29 +01:00
|
|
|
}
|