package engine;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import calculatorTools.TimeTester;
import dao.DaoTheoricalTrainData;
import dao.DaoUser;
import externaldata.realtimeapi.RealTimeBundleData;
import externaldata.realtimeapi.RealTimeTrainHelper;
import externaldata.realtimeapi.RealTrainData;
import externaldata.theoricaltimefile.TheoricalTrainData;
/**
* Thread charg� de r�cup�rer les horaires r�els des trains d'une station
* et de les comparer avec ceux th�oriques stock�s en BDD.
* Si besoin, ce thread devra mettre � jour les paris faits par les utilisateurs
* (valid� ou invalid�)
*/
public class StationTask implements Runnable {
private String stationUIC;
private String stationDUA;
private RealTimeBundleData trainsInStation;
private ServletContext context;
public StationTask(String stationUIC, String stationDUA, ServletContext context) {
this.stationUIC = stationUIC;
this.stationDUA = stationDUA;
this.context = context;
}
@Override
public void run() {
trainsInStation = null;
try {
trainsInStation = RealTimeTrainHelper.getDataFromAPI(stationUIC);
} catch (Exception e) {
e.printStackTrace();
}
if (trainsInStation != null) {
List<TheoricalTrainData> new_theoricalDataList = new ArrayList<TheoricalTrainData>();
DaoTheoricalTrainData daoTheoricalTrainData = new DaoTheoricalTrainData();
DaoUser daoUser = new DaoUser();
for (RealTrainData tmp_real_data : trainsInStation.datas) {
// TODO: faire le -1 sur le id_RealTrainData
TheoricalTrainData tmp_theorical_data = daoTheoricalTrainData.findWithNumGare(tmp_real_data.id_RealTrainData, stationDUA);
if (tmp_theorical_data != null) {
// Pari possible ? (au moins 10 minutes avant l'arriv�e th�orique du train)
if (TimeTester.compareTimes(trainsInStation.time, tmp_theorical_data.arrivalTime) >= 10) {
tmp_theorical_data.destinationUIC = tmp_real_data.destinationId;
tmp_theorical_data.trainName = tmp_real_data.name;
daoTheoricalTrainData.update(tmp_theorical_data);
new_theoricalDataList.add(tmp_theorical_data);
}
// Train en gare ?
if (tmp_real_data.datePassage.equals(trainsInStation.time)) {
// TODO: Donner aussi le retard du train (sinon �a sert � rien ...)
daoUser.validBetOnTheorical(tmp_theorical_data);
}
}
}
// Mise � jour dans le contexte des paris possibles pour cette gare
@SuppressWarnings("unchecked")
List<TheoricalTrainData> theoricalDataList = ((Map<String, List<TheoricalTrainData>>) context.getAttribute("paris_disponibles")).get(stationUIC);
theoricalDataList.clear();
theoricalDataList.addAll(new_theoricalDataList);
/*
for (TheoricalTrainData data : new_theoricalDataList) {
System.out.println("Train sur lequel on peut faire un pari : ");
System.out.println("\tName : " + data.trainName);
System.out.println("\tDestination (UIC) : " + data.destinationUIC);
System.out.println("\tHeure th�orique : " + data.arrivalTime);
}
*/
}
}
}