Chcekpoint - needs docs cleaning and tests

This commit is contained in:
Filip Izydorczyk 2021-01-04 18:10:14 +01:00
parent a6e6618202
commit 2141f35e3f
7 changed files with 155 additions and 20 deletions

View File

@ -2,6 +2,8 @@ package com.plannaplan.entities;
import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.TimeZone;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@ -11,6 +13,8 @@ import javax.persistence.Id;
import com.plannaplan.models.TourData;
import com.plannaplan.types.AppState;
import org.springframework.scheduling.support.CronTrigger;
/**
* entity that keeps app configurations
*/
@ -115,4 +119,26 @@ public class AppConfig {
return AppState.NO_TOUR;
}
public CronTrigger getFirstTourEndCron() {
return getCron(this.firstTourEnd);
}
public CronTrigger getSecondTourEndCron() {
return getCron(this.secondTourEnd);
}
public CronTrigger getFirstTourStartCron() {
return getCron(this.firstTourStart);
}
public CronTrigger getSecondTourStartCron() {
return getCron(this.secondTourStart);
}
private CronTrigger getCron(Date date) {
final LocalDate tourEnd = date.toLocalDate();
return new CronTrigger("0 0 0 " + tourEnd.getDayOfMonth() + " " + tourEnd.getMonthValue() + " ?",
TimeZone.getTimeZone(TimeZone.getDefault().getID()));
}
}

View File

@ -4,6 +4,7 @@ import com.plannaplan.models.ConfigData;
import com.plannaplan.models.FileData;
import com.plannaplan.models.TourData;
import com.plannaplan.repositories.AppConfigRepository;
import com.plannaplan.tasks.PerformAcceptAlgorythmTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -26,29 +27,38 @@ public class ConfiguratorService {
private FileToDatabaseMigrator migrator;
@Autowired
private AppConfigRepository configRepo;
@Autowired
private EventService eventService;
public ConfiguratorService() {
}
/**
* methoid to config system
* methoid to config system. it shedules PerformAcceptAlgorythmTask as a side
* effect
*
* @param data ConfigData containng system configs
*/
public void config(ConfigData data) {
FileReader reader = new FileReader(data.getFilestream());
FileData coursesData = reader.read();
this.configRepo.save(new AppConfig(data.getFirstTour(), data.getSecondTour()));
final AppConfig config = new AppConfig(data.getFirstTour(), data.getSecondTour());
this.configRepo.save(config);
migrator.migrate(coursesData);
this.shceduleTaskAfterToursDateChange(config);
}
/**
* Save tours to DataBase
* @param firstTour First tour period.
* Save tours to DataBase and shedule PerformAcceptAlgorythmTask as a side
* effect
*
* @param firstTour First tour period.
* @param secondTour Second tour period.
*/
public void saveTours(TourData firstTour, TourData secondTour) {
this.configRepo.save(new AppConfig(firstTour, secondTour));
final AppConfig config = new AppConfig(firstTour, secondTour);
this.configRepo.save(config);
this.shceduleTaskAfterToursDateChange(config);
}
/**
@ -78,9 +88,17 @@ public class ConfiguratorService {
*
* @param inputStream This input stream contains new courses to import.
*/
public void importCoursesStream(InputStream inputStream) {
public void importCoursesStream(InputStream inputStream) {
FileReader reader = new FileReader(inputStream);
FileData coursesData = reader.read();
migrator.migrate(coursesData);
}
private void shceduleTaskAfterToursDateChange(AppConfig config) {
this.eventService.scheduleTask(EventService.FIRST_TOUR_SCHEDULE, new PerformAcceptAlgorythmTask(),
config.getFirstTourEndCron());
this.eventService.scheduleTask(EventService.SECOND_TOUR_SCHEDULE, new PerformAcceptAlgorythmTask(),
config.getSecondTourEndCron());
}
}

View File

@ -1,28 +1,82 @@
package com.plannaplan.services;
import java.sql.Date;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ScheduledFuture;
import javax.annotation.PostConstruct;
import com.plannaplan.entities.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
// import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
@Service
public class EventService {
public static final int FIRST_TOUR_SCHEDULE = 0;
public static final int SECOND_TOUR_SCHEDULE = 1;
// @Autowired
// private EmailService emailService;
@Scheduled(cron = "0 2 17 * * *")
private ThreadPoolTaskScheduler scheduler;
private Map<Integer, ScheduledFuture<?>> jobsMap = new HashMap<>();
// @Autowired
// private ConfiguratorService configService;
@Scheduled(cron = "0 24 17 * * *")
public void collectGroupLosses() {
System.out.println("Checking for groups");
}
@Scheduled(cron = "#{@getTourValue}")
public void performAcceptAction() {
System.out.println("Checking for hahaha");
// @Scheduled(cron = "#{eventService.getFirstTourValue()}")
// public void performAcceptAction() {
// System.out.println("Checking for hahaha");
// }
// new CronTrigger("0 0 0 * * ?",
// TimeZone.getTimeZone(TimeZone.getDefault().getID()))
public void scheduleTask(int taskId, Runnable task, CronTrigger cronTrigger) {
ScheduledFuture<?> scheduledTask = jobsMap.get(taskId);
if (scheduledTask != null) {
scheduledTask.cancel(true);
jobsMap.put(taskId, null);
}
scheduledTask = this.scheduler.schedule(task, cronTrigger);
jobsMap.put(taskId, scheduledTask);
}
@Bean
public String getTourValue() {
return "0 6 18 * * *";
// @Bean
// public String getFirstTourValue() {
// final AppConfig config = this.configService.getCurrentConfig();
// final LocalDate tourEnd = config.getFirstTourEnd().toLocalDate();
// final String cronExp = "0 0 0 " + tourEnd.getDayOfMonth() + " " +
// tourEnd.getMonthValue() + " "
// + tourEnd.getYear();
// return cronExp;
// }
// @Bean
// public String getSecondTourValue() {
// return "0 6 18 * * *";
// }
@PostConstruct
public void initialize() {
this.scheduler = new ThreadPoolTaskScheduler();
this.scheduler.initialize();
}
}

View File

@ -0,0 +1,11 @@
package com.plannaplan.tasks;
public class PerformAcceptAlgorythmTask implements Runnable {
@Override
public void run() {
System.out.println("XDDDDDDDd");
}
}

View File

@ -1,5 +1,6 @@
package com.plannaplan.entities;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.sql.Date;
@ -138,4 +139,24 @@ public class AppConfigTest {
assertTrue(config.getCurrentState() == AppState.SECOND_TOUR);
}
@Test
public void shouldReturnFirstStartDatesCorns() {
assertFalse(false);
}
@Test
public void shouldReturnSecondStartDatesCorns() {
assertFalse(false);
}
@Test
public void shouldReturnFirstEndDatesCorns() {
assertFalse(false);
}
@Test
public void shouldReturnSecondEndDatesCorns() {
assertFalse(false);
}
}

View File

@ -66,20 +66,20 @@ public class ConfiguratorServiceTest {
@Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldUpdatePreviousImport(){
public void shouldUpdatePreviousImport() {
final InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream(ConfiguratorServiceTest.BEFORE_UPDATE_FILE);
.getResourceAsStream(ConfiguratorServiceTest.BEFORE_UPDATE_FILE);
this.configuratorService.importCoursesStream(inputStream);
int groups_ammount = this.groupService.getGroupsAmmount();
assertTrue(groups_ammount == 2);
final InputStream inputStream2 = getClass().getClassLoader()
.getResourceAsStream(ConfiguratorServiceTest.AFTER_UPDATE_FILE);
.getResourceAsStream(ConfiguratorServiceTest.AFTER_UPDATE_FILE);
this.configuratorService.importCoursesStream(inputStream2);
int groups_ammount2 = this.groupService.getGroupsAmmount();
Optional<Groups> newGroup = this.groupService.find(456458, 3);
Optional<Groups> newGroup = this.groupService.find(456458, 3);
Optional<Groups> updateGroup = this.groupService.find(456457, 2);
assertTrue(groups_ammount2 == 3);
@ -101,4 +101,9 @@ public class ConfiguratorServiceTest {
}
@Test
public void shlouldScheduleTaskWhenSetTourDate() {
assertTrue(false);
}
}

View File

@ -56,8 +56,8 @@ public class App {
final Random generator = new Random();
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream("Zajecia.xlsx");
final ConfigData data = new ConfigData(
new TourData(new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis() + 86400000)),
new TourData(new Date(System.currentTimeMillis() - 86400000),
new Date(System.currentTimeMillis())),
new TourData(new Date(System.currentTimeMillis() + 86400000),
new Date(System.currentTimeMillis() + 2 * 86400000)),
inputStream);