package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cedarsoftware.util.io.JsonWriter;
import dao.DaoUser;
import externaldata.gareSelection.GareSelection;
import externaldata.theoricaltimefile.TheoricalTrainData;
import servlet.utilities.Paris_S;
import user.User;
public class S_SearchTrain extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
//@SuppressWarnings("unchecked")
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("json");
PrintWriter out = response.getWriter();
Map<String, List<TheoricalTrainData>> trains = (Map<String, List<TheoricalTrainData>>) request.getAttribute("paris_disponibles");
List<GareSelection> selection = (List<GareSelection>) request.getAttribute("gares_selection");
//List<TheoricalTimeBundleData> data = new ArrayList<TheoricalTimeBundleData>();
List<Paris_S> json = new ArrayList<Paris_S>();
if(trains != null && !trains.isEmpty()) {
String station_uic = (String) request.getAttribute("station_uic");
if(station_uic == null || station_uic.isEmpty()) {
for(List<TheoricalTrainData> l : trains.values()) {
//data.addAll(l);
for(TheoricalTrainData ttd : l) {
Paris_S p_s = new Paris_S();
p_s.update(selection, ttd);
json.add(p_s);
}
}
}
else {
//data = trains.get(station_uic);
for(TheoricalTrainData ttd : trains.get(station_uic)) {
Paris_S p_s = new Paris_S();
p_s.update(selection, ttd);
json.add(p_s);
}
}
//Collections.sort(data);
Collections.sort(json);
//out.print(JsonWriter.objectToJson(data));
System.out.println("\t\t\t\tS_SearchTrain\n" + json);
out.print(JsonWriter.objectToJson(json));
}
else {
out.print("{}");
}
out.close();
}
}