From d237b78ba1358607ffc5ba50241d491e46fe0a8b Mon Sep 17 00:00:00 2001 From: joethei Date: Wed, 15 May 2019 15:14:54 +0200 Subject: [PATCH] + working version #SPM-28: add work 2h development --- .../spm/baudas/web/ShoppingTimesDiagram.java | 20 +++---- .../spm/baudas/web/WeekOverviewDiagram.java | 19 ++++--- src/main/webapp/footer.jsp | 6 ++- src/main/webapp/header.jsp | 10 +--- src/main/webapp/js/cache.js | 53 +++++++++++++++++++ src/main/webapp/js/materialize_components.js | 14 ----- src/main/webapp/js/shopping_times.js | 19 ++++--- src/main/webapp/js/week_overview.js | 27 +++++++--- 8 files changed, 107 insertions(+), 61 deletions(-) create mode 100644 src/main/webapp/js/cache.js delete mode 100644 src/main/webapp/js/materialize_components.js diff --git a/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java b/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java index 41a491e..db4f44a 100644 --- a/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java @@ -9,18 +9,11 @@ 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.*; /** * changes data from shopping times diagram into a readable format for chart.js @@ -48,15 +41,16 @@ public class ShoppingTimesDiagram extends HttpServlet { Gson gson = new Gson(); - SavedFile file = Data.getFiles().peek(); - if (file == null) { + + 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); - assert file.getUuid() != null; - ShoppingTimes shoppingTimes = new ShoppingTimes(Data.get(file.getUuid())); + ShoppingTimes shoppingTimes = new ShoppingTimes(file); Map map = shoppingTimes.getResult(); Set days = new TreeSet<>(); diff --git a/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java b/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java index 905c8a5..a9321a7 100644 --- a/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java @@ -8,15 +8,11 @@ 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.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * changes data from week overview into readable format for chart.js @@ -43,16 +39,19 @@ public class WeekOverviewDiagram extends HttpServlet { Gson gson = new Gson(); - SavedFile file = Data.getFiles().peek(); - if (file == null) { + + if (req.getParameter("id") == null) { result.put("labels", definedOrder); result.put("count", new ArrayList<>()); result.put("revenue", new ArrayList<>()); out.print(gson.toJson(result)); return; } - assert file.getUuid() != null; - WeekOverview overview = new WeekOverview(Data.get(file.getUuid())); + + UUID uuid = UUID.fromString(req.getParameter("id")); + File file = Data.get(uuid); + + WeekOverview overview = new WeekOverview(file); List count = new ArrayList<>(); List revenue = new ArrayList<>(); diff --git a/src/main/webapp/footer.jsp b/src/main/webapp/footer.jsp index e74138a..e6dd7bc 100644 --- a/src/main/webapp/footer.jsp +++ b/src/main/webapp/footer.jsp @@ -7,7 +7,11 @@ - + + + diff --git a/src/main/webapp/header.jsp b/src/main/webapp/header.jsp index c7bd797..3594b19 100644 --- a/src/main/webapp/header.jsp +++ b/src/main/webapp/header.jsp @@ -79,15 +79,9 @@
  • - +
    -
    diff --git a/src/main/webapp/js/cache.js b/src/main/webapp/js/cache.js new file mode 100644 index 0000000..23d1187 --- /dev/null +++ b/src/main/webapp/js/cache.js @@ -0,0 +1,53 @@ +$(document).ready(function () { + let dataset = $('#dataset'); + $.ajax({ + url: 'files', + dataType: 'json' + }).done(function (results) { + dataset.empty(); + for (const result of results) { + dataset.append($("") + .attr("value", result.uuid) + .text(result.name)); + } + dataset.formSelect(); + }); + updateWeekOverviewChart(); + updateShoppingTimesChart(); +}); + +function updateCache() { + let selected = $('#dataset :selected'); + + if (selected.index() !== -1) { + updateWeekOverviewChart(selected.val()); + updateShoppingTimesChart(selected.val()); + } +} + +function request(url, id) { + return new Promise(((resolve, reject) => { + if (typeof id !== 'undefined') { + requestInternal(url + '?id=' + id).then(function (data) { + resolve(data); + }); + } else { + requestInternal(url).then(function (data) { + resolve(data); + }); + } + + })); +} + +function requestInternal(url) { + return new Promise(((resolve, reject) => { + $.ajax({ + url: url, + dataType: 'json' + }).done(function (results) { + resolve(results); + }); + })); + +} \ No newline at end of file diff --git a/src/main/webapp/js/materialize_components.js b/src/main/webapp/js/materialize_components.js deleted file mode 100644 index c788aff..0000000 --- a/src/main/webapp/js/materialize_components.js +++ /dev/null @@ -1,14 +0,0 @@ -$(".dropdown-trigger").dropdown(); -$('.tabs').tabs(); -$(document).ready(function(){ - $('.sidenav').sidenav(); -}); -$(document).ready(function(){ - $('select').formSelect(); -}); -$(document).ready(function(){ - $('.collapsible').collapsible(); -}); -$(document).ready(function() { - M.updateTextFields(); -}); \ No newline at end of file diff --git a/src/main/webapp/js/shopping_times.js b/src/main/webapp/js/shopping_times.js index 6a745b1..02f8047 100644 --- a/src/main/webapp/js/shopping_times.js +++ b/src/main/webapp/js/shopping_times.js @@ -52,13 +52,18 @@ let shopping_chart = new Chart(document.getElementById("shopping_times_chart"), } }); -$.ajax({ - url: 'shopping_times', - dataType: 'json' -}).done(function (results) { - shopping_result = results; - updateDays(); -}); +function updateShoppingTimesChart(id) { + if(typeof id !== 'undefined') { + request('shopping_times', id).then(function (data) { + shopping_result = data; + updateDays(); + }); + }else request('shopping_times').then(function(data) { + shopping_result = data; + updateDays(); + }); +} + function updateDays() { if(typeof shopping_result["Montag"] !== 'undefined') { diff --git a/src/main/webapp/js/week_overview.js b/src/main/webapp/js/week_overview.js index 9d44521..dc5bc92 100644 --- a/src/main/webapp/js/week_overview.js +++ b/src/main/webapp/js/week_overview.js @@ -1,3 +1,5 @@ +let week_result; + let week_overview = new Chart(document.getElementById("overview_chart"), { type: 'line', data: { @@ -52,14 +54,23 @@ let week_overview = new Chart(document.getElementById("overview_chart"), { } }); -$.ajax({ - url: 'week_overview', - dataType: 'json' -}).done(function (results) { - week_overview.data.labels = results.labels; - week_overview.data.datasets[0].data = results.count; - week_overview.data.datasets[1].data = results.revenue; +function updateWeekOverviewChart(id) { + if(typeof id !== 'undefined') { + request('week_overview', id).then(function (data) { + week_result = data; + updateWeekOverview(); + }); + }else request('week_overview').then(function(data) { + week_result = data; + updateWeekOverview(); + }); +} +function updateWeekOverview() { + + week_overview.data.labels = week_result.labels; + week_overview.data.datasets[0].data = week_result.count; + week_overview.data.datasets[1].data = week_result.revenue; week_overview.update(); -}); \ No newline at end of file +} \ No newline at end of file