/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package salidasautomaticas.schedule;

import java.util.Timer;
import java.util.TimerTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import salidasautomaticas.main.Main;
import salidasautomaticas.services.SalidasServices;

/**
 *
 * @author sistem19user
 */
public class ActualizarSalidaTask extends TimerTask {
    private static final Logger log = LogManager.getLogger(Main.class);

    SalidasServices services = new SalidasServices();
    private Timer timer;
    private String horaSalida;

    public ActualizarSalidaTask(Timer timer, String horaSalida) {
        this.timer = timer;
        this.horaSalida = horaSalida;
    }

    @Override
    public void run() {
        log.trace("Ejecutando salida " + horaSalida);
        services.actualizarSalidas(horaSalida);
        log.info("Se generaron salidas");
        
        JSONObject objSalidaAsignada = services.asignarHorarioSalida();

        if (objSalidaAsignada.getInt("msg") > 0) {
            log.info("Se asignaron horarios de salida correctamente, filas afectadas=" + objSalidaAsignada.getInt("msg"));
        } else {
            log.info("No hubo horarios de salida que asignar");
        }

        log.trace("Matando timer");
        timer.cancel();
    }

}
