From bb0fda948c74ae452c38044500e04501b8ca1afa Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Wed, 5 Jul 2017 15:03:19 +0200 Subject: [PATCH] Extended cross table with time and product as grouping elements Signed-off-by: Jim Martens --- 07/src/de/dis2017/MainAnalysis.java | 78 ++++++++++++++++++++--------- 07/src/de/dis2017/data/db/ORM.java | 2 + 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/07/src/de/dis2017/MainAnalysis.java b/07/src/de/dis2017/MainAnalysis.java index cfde89d..de578b1 100644 --- a/07/src/de/dis2017/MainAnalysis.java +++ b/07/src/de/dis2017/MainAnalysis.java @@ -81,6 +81,41 @@ public class MainAnalysis { return false; } + private static String getMonth(int monthNr) { + Map months = new HashMap<>(); + months.put(1, "January"); + months.put(2, "February"); + months.put(3, "March"); + months.put(4, "April"); + months.put(5, "May"); + months.put(6, "June"); + months.put(7, "July"); + months.put(8, "August"); + months.put(9, "September"); + months.put(10, "October"); + months.put(11, "November"); + months.put(12, "December"); + + return months.get(monthNr); + } + + private static String getTimeCell(String timeDimension, String time) { + String returnValue = ""; + switch (timeDimension) { + case "QUARTER": + returnValue = "quarter " + time + ", 2017"; + break; + case "MONTH": + returnValue = getMonth(Integer.valueOf(time)) + ", 2017"; + break; + case "YEAR": + returnValue = time; + break; + } + + return returnValue; + } + private static void printTable(Map>> crossTable, String timeDimension) { List articleNames = new ArrayList<>(crossTable.get("total").get("total").keySet()); Collections.sort(articleNames); @@ -124,36 +159,13 @@ public class MainAnalysis { Map> timeMap = crossTable.get(city); List times = new ArrayList<>(timeMap.keySet()); Collections.sort(times); - - Map months = new HashMap<>(); - months.put(1, "January"); - months.put(2, "February"); - months.put(3, "March"); - months.put(4, "April"); - months.put(5, "May"); - months.put(6, "June"); - months.put(7, "July"); - months.put(8, "August"); - months.put(9, "September"); - months.put(10, "October"); - months.put(11, "November"); - months.put(12, "December"); + for (String time : times) { if (time.equals("total")) continue; Map productMap = timeMap.get(time); List values = new ArrayList<>(); values.add(city); - switch (timeDimension) { - case "QUARTER": - values.add("quarter " + time + ", 2017"); - break; - case "MONTH": - values.add(months.get(Integer.valueOf(time)) + ", 2017"); - break; - case "YEAR": - values.add(time); - break; - } + values.add(getTimeCell(timeDimension, time)); for (String article : articleNames) { if (article.equals("total")) continue; values.add(productMap.get(article)); @@ -173,6 +185,22 @@ public class MainAnalysis { System.out.format(leftAlignFormat.toString(), unpack(totalValues.toArray())); } Map> timeMap = crossTable.get("total"); + List times = new ArrayList<>(timeMap.keySet()); + Collections.sort(times); + for (String time : times) { + if (time.equals("total")) continue; + Map productMap = timeMap.get(time); + List values = new ArrayList<>(); + values.add("total"); + values.add(getTimeCell(timeDimension, time)); + for (String article : articleNames) { + if (article.equals("total")) continue; + values.add(productMap.get(article)); + } + values.add(productMap.get("total")); + System.out.format(leftAlignFormat.toString(), unpack(values.toArray())); + } + Map productMap = timeMap.get("total"); List globalTotalValues = new ArrayList<>(); globalTotalValues.add("total"); diff --git a/07/src/de/dis2017/data/db/ORM.java b/07/src/de/dis2017/data/db/ORM.java index eace4a8..6db165d 100644 --- a/07/src/de/dis2017/data/db/ORM.java +++ b/07/src/de/dis2017/data/db/ORM.java @@ -222,8 +222,10 @@ public class ORM { "AND s.ARTICLEID = a.ID " + "AND d.YEAR = " + year + " " + "GROUP BY GROUPING SETS ( (), (sh." + locationDimension + "), (a." + productDimension + "), " + + "(d." + timeDimension + "), " + "(sh." + locationDimension + ", a." + productDimension + "), " + "(d." + timeDimension + ", sh." + locationDimension + "), " + + "(d." + timeDimension + ", a." + productDimension + "), " + "(sh." + locationDimension + ", a." + productDimension + ", d." + timeDimension + ") )"; Map>> sales = new HashMap<>();