backend/buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java

172 lines
4.1 KiB
Java
Raw Normal View History

2020-12-09 16:43:29 +01:00
package com.plannaplan.entities;
import java.sql.Date;
import java.sql.Timestamp;
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
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
*/
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
*/
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
*/
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
*/
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.
*/
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() + " ?",
TimeZone.getTimeZone(TimeZone.getDefault().getID()));
}
2020-12-09 16:43:29 +01:00
}