88 lines
2.7 KiB
Java
88 lines
2.7 KiB
Java
package de.hsel.spm.baudas.web;
|
|
|
|
import com.google.gson.Gson;
|
|
import de.hsel.spm.baudas.analysis.DayHour;
|
|
import de.hsel.spm.baudas.analysis.ShoppingTimes;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
import javax.servlet.http.HttpServlet;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.PrintWriter;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Collection;
|
|
import java.util.Comparator;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.TreeSet;
|
|
import java.util.UUID;
|
|
|
|
/**
|
|
* changes data from shopping times diagram into a readable format for chart.js
|
|
*
|
|
* @author Johannes Theiner
|
|
* @version 0.1
|
|
* @since 0.1
|
|
**/
|
|
@WebServlet("/shopping_times")
|
|
public class ShoppingTimesDiagram extends HttpServlet {
|
|
|
|
private static final long serialVersionUID = 6567531484L;
|
|
|
|
@Override
|
|
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
|
|
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
|
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
|
resp.setContentType("application/json");
|
|
PrintWriter out = resp.getWriter();
|
|
|
|
List<String> definedOrder = Arrays.asList("<10 Uhr", "10-12 Uhr", "12-14 Uhr", "14-17 Uhr", ">17 Uhr");
|
|
Comparator<String> comparator = Comparator.comparingInt(definedOrder::indexOf);
|
|
|
|
Map<String, Collection<String>> result = new HashMap<>();
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
if (req.getParameter("id") == null) {
|
|
result.put("labels", definedOrder);
|
|
out.print(gson.toJson(result));
|
|
return;
|
|
}
|
|
UUID uuid = UUID.fromString(req.getParameter("id"));
|
|
File file = Data.get(uuid);
|
|
|
|
ShoppingTimes shoppingTimes = new ShoppingTimes(file);
|
|
Map<DayHour, Integer> map = shoppingTimes.getResult();
|
|
|
|
Set<String> days = new TreeSet<>();
|
|
Set<String> hours = new TreeSet<>(comparator);
|
|
for (DayHour dayHour : map.keySet()) {
|
|
days.add(dayHour.getDay());
|
|
hours.add(dayHour.getHour());
|
|
}
|
|
result.put("labels", hours);
|
|
|
|
|
|
for (String day : days) {
|
|
if (!result.containsKey(day))
|
|
result.put(day, new ArrayList<>());
|
|
for (String hour : hours) {
|
|
if (map.containsKey(new DayHour(day, hour))) {
|
|
result.get(day).add(map.get(new DayHour(day, hour)).toString());
|
|
} else result.get(day).add("0");
|
|
}
|
|
}
|
|
|
|
|
|
out.print(gson.toJson(result));
|
|
|
|
}
|
|
} |