package com.megatim.fdxconvert.service.util; import com.megatim.fdxconvert.model.Tache; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.TriggerBuilder; /** * * @author MGT_DEV3 */ public class SchedulerUtil { private SchedulerUtil() { } public static CronTrigger buildTriggerForTache(Tache tache) { return TriggerBuilder.newTrigger() .startNow() .withSchedule(CronScheduleBuilder.cronSchedule(getPlanification(tache))) //"0/20 * * * * ?" .build(); } //Prend une tâche en entrée et renvoie l'expression cron de sa planifaction public static String getPlanification(Tache tache) { LocalDate localDate = LocalDate.now(); boolean isActual = (tache.isMonday() && localDate.getDayOfWeek() == DayOfWeek.MONDAY) || (tache.isTuesday() && localDate.getDayOfWeek() == DayOfWeek.TUESDAY) || (tache.isWednesday() && localDate.getDayOfWeek() == DayOfWeek.WEDNESDAY) || (tache.isThursday() && localDate.getDayOfWeek() == DayOfWeek.THURSDAY) || (tache.isFriday() && localDate.getDayOfWeek() == DayOfWeek.FRIDAY) || (tache.isSaturday() && localDate.getDayOfWeek() == DayOfWeek.SATURDAY) || (tache.isSunday() && localDate.getDayOfWeek() == DayOfWeek.SUNDAY); int heureTache = tache.getHeureTache(); int minuteTache = tache.getMinuteTache(); LocalTime tacheTime = LocalTime.parse(String.format("%02d", heureTache) + ":" + String.format("%02d", minuteTache) + ":00"); LocalTime actualTime = LocalTime.now(); if (isActual && !tache.isWithoutInterval() && actualTime.isAfter(tacheTime)) { heureTache = actualTime.getHour(); minuteTache = actualTime.getMinute(); } StringBuilder strBuilder = new StringBuilder(); strBuilder.append("0 "); if (tache.isWithoutInterval()) { strBuilder.append(minuteTache).append(" "); strBuilder.append(heureTache).append(" "); } else { strBuilder.append(minuteTache).append("/").append(tache.getIntervalleTache()).append(" "); if (heureTache == 0) { strBuilder.append("*").append(" "); } else { strBuilder.append(heureTache).append("-").append("0").append(" "); } } strBuilder.append("? * "); StringBuilder strBuilderJour = new StringBuilder(); if (tache.isMonday()) { strBuilderJour.append("2"); } if (tache.isTuesday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("3"); } else { strBuilderJour.append(",3"); } } if (tache.isWednesday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("4"); } else { strBuilderJour.append(",4"); } } if (tache.isThursday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("5"); } else { strBuilderJour.append(",5"); } } if (tache.isFriday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("6"); } else { strBuilderJour.append(",6"); } } if (tache.isSaturday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("7"); } else { strBuilderJour.append(",7"); } } if (tache.isSunday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("1"); } else { strBuilderJour.append(",1"); } } //Ajout de l'expression énumérant les jours construite strBuilder.append(strBuilderJour.toString()); return strBuilder.toString(); } //Prend une tâche du protocol fdx en paramêtre et contruit en fonction des information de celle-ci le déclencheur approprié /*public static CronTrigger buildTriggerForTache(Tache tache) { System.out.println("LA PLANIFICATION SPECIFIEE EST : " + getPlanification(tache)); return TriggerBuilder.newTrigger() .startNow() .withSchedule(CronScheduleBuilder.cronSchedule(getPlanification(tache))) //"0/20 * * * * ?" .withIdentity(tache.getLibelle()) .build(); } //Prend une tâche en entrée et renvoie l'expression cron de sa planifaction public static String getPlanification(Tache tache) { StringBuilder strBuilder = new StringBuilder(); strBuilder.append("0 "); strBuilder.append(tache.getMinuteTache()).append(" "); strBuilder.append(tache.getHeureTache()).append(" "); strBuilder.append("? * "); StringBuilder strBuilderJour = new StringBuilder(); if (tache.isMonday()) { strBuilderJour.append("2"); } if (tache.isTuesday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("3"); } else { strBuilderJour.append(",3"); } } if (tache.isWednesday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("4"); } else { strBuilderJour.append(",4"); } } if (tache.isThursday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("5"); } else { strBuilderJour.append(",5"); } } if (tache.isFriday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("6"); } else { strBuilderJour.append(",6"); } } if (tache.isSaturday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("7"); } else { strBuilderJour.append(",7"); } } if (tache.isSunday()) { if (strBuilderJour.toString().isEmpty()) { strBuilderJour.append("1"); } else { strBuilderJour.append(",1"); } } //Ajout de l'expression énumérant les jours construite strBuilder.append(strBuilderJour.toString()); return strBuilder.toString(); }*/ }