2020-09-23 19:12:25 +02:00
|
|
|
package com.plannaplan.services;
|
2020-07-23 17:27:20 +02:00
|
|
|
|
|
|
|
import com.plannaplan.models.ConfigData;
|
2020-08-04 17:30:03 +02:00
|
|
|
import com.plannaplan.models.FileData;
|
2020-12-26 15:33:17 +01:00
|
|
|
import com.plannaplan.models.TourData;
|
2020-12-11 18:08:23 +01:00
|
|
|
import com.plannaplan.repositories.AppConfigRepository;
|
2020-07-23 17:27:20 +02:00
|
|
|
|
2020-08-04 17:30:03 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
2020-12-26 12:09:55 +01:00
|
|
|
import java.io.InputStream;
|
2021-01-05 11:14:10 +01:00
|
|
|
import java.sql.Date;
|
|
|
|
import java.time.LocalDate;
|
2020-12-11 18:08:23 +01:00
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2020-08-04 17:30:03 +02:00
|
|
|
import com.plannaplan.configutils.*;
|
2020-12-09 16:43:29 +01:00
|
|
|
import com.plannaplan.entities.AppConfig;
|
2020-08-04 17:30:03 +02:00
|
|
|
|
2020-11-20 13:20:44 +01:00
|
|
|
/**
|
2020-12-09 16:43:29 +01:00
|
|
|
* FileReader is used for reading xls file from input stream.
|
2020-11-20 13:20:44 +01:00
|
|
|
*/
|
2020-08-04 17:30:03 +02:00
|
|
|
@Component
|
2020-09-23 19:12:25 +02:00
|
|
|
public class ConfiguratorService {
|
2020-07-23 17:27:20 +02:00
|
|
|
|
2020-08-04 17:30:03 +02:00
|
|
|
@Autowired
|
2020-09-25 16:43:24 +02:00
|
|
|
private FileToDatabaseMigrator migrator;
|
2020-12-09 16:43:29 +01:00
|
|
|
@Autowired
|
2020-12-11 18:08:23 +01:00
|
|
|
private AppConfigRepository configRepo;
|
2021-01-04 18:10:14 +01:00
|
|
|
@Autowired
|
|
|
|
private EventService eventService;
|
2021-01-05 11:14:10 +01:00
|
|
|
@Autowired
|
|
|
|
private AssignmentService assignmentService;
|
2020-08-06 14:32:41 +02:00
|
|
|
|
2020-09-23 19:12:25 +02:00
|
|
|
public ConfiguratorService() {
|
2020-07-23 17:27:20 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 16:43:29 +01:00
|
|
|
/**
|
2021-01-04 18:10:14 +01:00
|
|
|
* methoid to config system. it shedules PerformAcceptAlgorythmTask as a side
|
|
|
|
* effect
|
2020-12-09 16:43:29 +01:00
|
|
|
*
|
2020-12-12 13:33:11 +01:00
|
|
|
* @param data ConfigData containng system configs
|
2020-12-09 16:43:29 +01:00
|
|
|
*/
|
2020-07-23 17:27:20 +02:00
|
|
|
public void config(ConfigData data) {
|
2020-08-04 17:30:03 +02:00
|
|
|
FileReader reader = new FileReader(data.getFilestream());
|
|
|
|
FileData coursesData = reader.read();
|
2021-01-04 18:10:14 +01:00
|
|
|
final AppConfig config = new AppConfig(data.getFirstTour(), data.getSecondTour());
|
|
|
|
this.configRepo.save(config);
|
2020-09-21 17:45:52 +02:00
|
|
|
migrator.migrate(coursesData);
|
2021-01-04 18:10:14 +01:00
|
|
|
this.shceduleTaskAfterToursDateChange(config);
|
2020-07-23 17:27:20 +02:00
|
|
|
}
|
|
|
|
|
2020-12-26 15:33:17 +01:00
|
|
|
/**
|
2021-01-04 18:10:14 +01:00
|
|
|
* Save tours to DataBase and shedule PerformAcceptAlgorythmTask as a side
|
|
|
|
* effect
|
|
|
|
*
|
|
|
|
* @param firstTour First tour period.
|
2020-12-26 15:33:17 +01:00
|
|
|
* @param secondTour Second tour period.
|
|
|
|
*/
|
|
|
|
public void saveTours(TourData firstTour, TourData secondTour) {
|
2021-01-04 18:10:14 +01:00
|
|
|
final AppConfig config = new AppConfig(firstTour, secondTour);
|
|
|
|
this.configRepo.save(config);
|
|
|
|
this.shceduleTaskAfterToursDateChange(config);
|
2020-12-26 15:33:17 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 18:08:23 +01:00
|
|
|
/**
|
|
|
|
* current config getter
|
|
|
|
*
|
|
|
|
* @return AppConfig with newest config_date
|
|
|
|
*/
|
|
|
|
public AppConfig getCurrentConfig() {
|
|
|
|
final List<AppConfig> repsonse = this.configRepo.findAll().stream().sorted(new Comparator<AppConfig>() {
|
|
|
|
@Override
|
|
|
|
public int compare(AppConfig i1, AppConfig i2) {
|
|
|
|
if (i1.getConfigDate().after(i2.getConfigDate())) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (i1.getConfigDate().before(i2.getConfigDate())) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
|
|
return repsonse.get(0);
|
|
|
|
}
|
2020-12-26 12:09:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param inputStream This input stream contains new courses to import.
|
|
|
|
*/
|
2021-01-04 18:10:14 +01:00
|
|
|
public void importCoursesStream(InputStream inputStream) {
|
2020-12-26 12:09:55 +01:00
|
|
|
FileReader reader = new FileReader(inputStream);
|
|
|
|
FileData coursesData = reader.read();
|
|
|
|
migrator.migrate(coursesData);
|
|
|
|
}
|
2021-01-04 18:10:14 +01:00
|
|
|
|
|
|
|
private void shceduleTaskAfterToursDateChange(AppConfig config) {
|
|
|
|
|
2021-01-05 11:14:10 +01:00
|
|
|
this.eventService.scheduleTask(EventService.FIRST_TOUR_SCHEDULE,
|
|
|
|
new AlgorythmAcceptTask(config.getFirstTourEnd()), config.getFirstTourEndCron());
|
|
|
|
|
|
|
|
this.eventService.scheduleTask(EventService.SECOND_TOUR_SCHEDULE,
|
|
|
|
new AlgorythmAcceptTask(config.getSecondTourEnd()), config.getSecondTourEndCron());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* It's local class to perform accept Task. It's needed to be here due to have
|
|
|
|
* acces to AssignmentService.
|
|
|
|
*/
|
|
|
|
private class AlgorythmAcceptTask implements Runnable {
|
|
|
|
|
|
|
|
private LocalDate localDate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create instance of AlgorythmAcceptTask
|
|
|
|
*
|
|
|
|
* @param date date of algorythm perform. Needed to check if ti's being called
|
|
|
|
* in correct year
|
|
|
|
*/
|
|
|
|
public AlgorythmAcceptTask(Date date) {
|
|
|
|
this.localDate = date.toLocalDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2021-01-06 11:47:18 +01:00
|
|
|
System.out.println("Performing event");
|
2021-01-05 11:14:10 +01:00
|
|
|
if (localDate.getYear() == LocalDate.now().getYear()) {
|
|
|
|
assignmentService.callAcceptAlgorythm();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-04 18:10:14 +01:00
|
|
|
}
|
2020-07-23 17:27:20 +02:00
|
|
|
}
|