diff --git a/src/main/java/com/application/DB/DB.java b/src/main/java/com/application/DB/DB.java index 5c3d6d9ec1c456315235eb3d782fe175c6499244..fd0b15ab4c2889a1e1d95d4acdfd581762395473 100644 --- a/src/main/java/com/application/DB/DB.java +++ b/src/main/java/com/application/DB/DB.java @@ -60,7 +60,7 @@ public class DB { //getKwh(); //getName(); //getZeroPointDate(); - System.out.println(setInputParameters(false,false,false,false)); + System.out.println(setInputParameters()); } @@ -127,14 +127,19 @@ public class DB { } + public static void getData(){ - public static Map<Integer, Map<String, Number>> setInputParameters(boolean sawsetRemoved, - boolean treespeciesRemoved, - boolean dimensionsRemoved, - boolean moistureRemoved) throws Exception { - //public static Map<String, String> setInputParameters() throws Exception { - Map<Integer, Map<String, Number>> allDryingPeriods = null; + } + + + public static Map<Integer, Map<String, Number>> setInputParameters() throws Exception { + + Map<Integer, Map<String, Number>> allDryingPeriods = new HashMap<>(); + boolean sawsetRemoved = false; + boolean treespeciesRemoved = false; + boolean dimensionsRemoved = false; + boolean moistureRemoved = false; String extraInputParameter = ""; int locationID; @@ -161,116 +166,140 @@ public class DB { String valueIDName = ""; int valueID = 0; - for (HashMap.Entry<Integer, HashMap<String, HashMap<String, String>>> location : Constants.getSawmills().entrySet()) { - System.out.printf("\n\nLocation ID: \t%s\t\t\tRest of map: \t%s\n", location.getKey(), location.getValue()); - allDryingPeriods = new TreeMap<>(); + while(true){ - locationID = location.getKey(); + Map<Integer, Map<String, Number>> intermediateHashMap = new HashMap<>(); - //System.out.println(location.getValue()); + for (HashMap.Entry<Integer, HashMap<String, HashMap<String, String>>> location : Constants.getSawmills().entrySet()) { - if (location.getKey() == 124) { - // Valmetics - valmeticsTableName = getValasenTableNames().get("Valmetics"); - nameParameter = location.getValue().get(getValasenTableNames().get("Valmetics")).get("Name"); - startDryingTime = location.getValue().get(getValasenTableNames().get("Valmetics")).get("DryingStarted"); - stopDryingTime = location.getValue().get(getValasenTableNames().get("Valmetics")).get("DryingStopped"); - kilinName = location.getValue().get(getValasenTableNames().get("Valmetics")).get("KilnName"); - kilinID = Integer.parseInt(location.getValue().get(getValasenTableNames().get("Valmetics")).get("KilinID")); - limit = Integer.parseInt(location.getValue().get(getValasenTableNames().get("Valmetics")).get("Limit")); + System.out.printf("\nLocation ID: \t%s\t\t\tRest of map: \t%s\n", location.getKey(), location.getValue()); + System.out.println("Number of entries: "+Constants.getSawmills().size()); - // Kwh - kwhTableName = getValasenTableNames().get("Kwh"); - KwhName = location.getValue().get(getValasenTableNames().get("Kwh")).get("KwhName"); - timestamp = location.getValue().get(getValasenTableNames().get("Kwh")).get("Timestamp"); - valueIDName = location.getValue().get(getValasenTableNames().get("Kwh")).get("ValueIDName"); - valueID = Integer.parseInt(location.getValue().get(getValasenTableNames().get("Kwh")).get("ValueID")); - } - if (location.getKey() == 174) { - // Valmetics - valmeticsTableName = getArjangTableNames().get("Valmetics"); - nameParameter = location.getValue().get(getArjangTableNames().get("Valmetics")).get("Name"); - startDryingTime = location.getValue().get(getArjangTableNames().get("Valmetics")).get("DryingStarted"); - stopDryingTime = location.getValue().get(getArjangTableNames().get("Valmetics")).get("DryingStopped"); - kilinName = location.getValue().get(getArjangTableNames().get("Valmetics")).get("KilnName"); - kilinID = Integer.parseInt(location.getValue().get(getArjangTableNames().get("Valmetics")).get("KilinID")); - limit = Integer.parseInt(location.getValue().get(getArjangTableNames().get("Valmetics")).get("Limit")); - - // Kwh - kwhTableName = getArjangTableNames().get("Kwh"); - KwhName = location.getValue().get(getArjangTableNames().get("Kwh")).get("KwhName"); - timestamp = location.getValue().get(getArjangTableNames().get("Kwh")).get("Timestamp"); - valueIDName = location.getValue().get(getArjangTableNames().get("Kwh")).get("ValueIDName"); - valueID = Integer.parseInt(location.getValue().get(getArjangTableNames().get("Kwh")).get("ValueID")); - } + //allDryingPeriods = new TreeMap<>(); - // If location is Valasen, then the database stores furu as fura, swedish. - if (location.getKey() == 124 && TREE_SPECIES.equalsIgnoreCase("Furu")) { - TREE_SPECIES = "Fura"; - } + locationID = location.getKey(); + //System.out.println(location.getValue()); - System.out.printf("Tree species: \t%s\n", TREE_SPECIES); - System.out.printf("Width: \t\t\t%s\n", DIMENSIONS); - System.out.printf("Sawset: \t\t%s\n", SAWSET); - System.out.printf("Moisture: \t\t%s\n", MOISTURE_GOAL); + if (location.getKey() == 124) { + // Valmetics + valmeticsTableName = getValasenTableNames().get("Valmetics"); + nameParameter = location.getValue().get(getValasenTableNames().get("Valmetics")).get("Name"); + startDryingTime = location.getValue().get(getValasenTableNames().get("Valmetics")).get("DryingStarted"); + stopDryingTime = location.getValue().get(getValasenTableNames().get("Valmetics")).get("DryingStopped"); + kilinName = location.getValue().get(getValasenTableNames().get("Valmetics")).get("KilnName"); + kilinID = Integer.parseInt(location.getValue().get(getValasenTableNames().get("Valmetics")).get("KilinID")); + limit = Integer.parseInt(location.getValue().get(getValasenTableNames().get("Valmetics")).get("Limit")); + // Kwh + kwhTableName = getValasenTableNames().get("Kwh"); + KwhName = location.getValue().get(getValasenTableNames().get("Kwh")).get("KwhName"); + timestamp = location.getValue().get(getValasenTableNames().get("Kwh")).get("Timestamp"); + valueIDName = location.getValue().get(getValasenTableNames().get("Kwh")).get("ValueIDName"); + valueID = Integer.parseInt(location.getValue().get(getValasenTableNames().get("Kwh")).get("ValueID")); + } + if (location.getKey() == 174) { + // Valmetics + valmeticsTableName = getArjangTableNames().get("Valmetics"); + nameParameter = location.getValue().get(getArjangTableNames().get("Valmetics")).get("Name"); + startDryingTime = location.getValue().get(getArjangTableNames().get("Valmetics")).get("DryingStarted"); + stopDryingTime = location.getValue().get(getArjangTableNames().get("Valmetics")).get("DryingStopped"); + kilinName = location.getValue().get(getArjangTableNames().get("Valmetics")).get("KilnName"); + kilinID = Integer.parseInt(location.getValue().get(getArjangTableNames().get("Valmetics")).get("KilinID")); + limit = Integer.parseInt(location.getValue().get(getArjangTableNames().get("Valmetics")).get("Limit")); + + // Kwh + kwhTableName = getArjangTableNames().get("Kwh"); + KwhName = location.getValue().get(getArjangTableNames().get("Kwh")).get("KwhName"); + timestamp = location.getValue().get(getArjangTableNames().get("Kwh")).get("Timestamp"); + valueIDName = location.getValue().get(getArjangTableNames().get("Kwh")).get("ValueIDName"); + valueID = Integer.parseInt(location.getValue().get(getArjangTableNames().get("Kwh")).get("ValueID")); + } + // If location is Valasen, then the database stores furu as fura, swedish. + if (location.getKey() == 124 && TREE_SPECIES.equalsIgnoreCase("Furu")) { + TREE_SPECIES = "Fura"; + } - if (!treespeciesRemoved) treeSpecies = "AND LOWER(" + nameParameter + ") LIKE LOWER(" + '"' + "%" + TREE_SPECIES + "%" + '"' + ") "; - if (!dimensionsRemoved) dimensions = "AND LOWER(" + nameParameter + ") LIKE LOWER(" + '"' + "%" + DIMENSIONS + "%" + '"' + ") "; - if (!sawsetRemoved) sawset = "AND LOWER(" + nameParameter + ") LIKE LOWER(" + '"' + "%" + SAWSET + "%" + '"' + ") "; - if (!moistureRemoved) moistureGoal = "AND LOWER(" + nameParameter + ") LIKE LOWER(" + '"' + "%" + MOISTURE_GOAL + "%" + '"' + ") "; + System.out.printf("Tree species: \t%s\n", TREE_SPECIES); + System.out.printf("Width: \t\t\t%s\n", DIMENSIONS); + System.out.printf("Sawset: \t\t%s\n", SAWSET); + System.out.printf("Moisture: \t\t%s\n", MOISTURE_GOAL); - // Input parameters - if (!TREE_SPECIES.isEmpty()) { - extraInputParameter += treeSpecies; - } - if (!DIMENSIONS.isEmpty()) { - extraInputParameter += dimensions; - } - if (!SAWSET.isEmpty()) { - extraInputParameter += sawset; - } - if (!MOISTURE_GOAL.isEmpty()) { - extraInputParameter += moistureGoal; - } - Map<Integer, Map<String, Number>> results = new TreeMap<>(); - results = getKwh(getZeroPointDate(locationID, extraInputParameter, nameParameter, kilinName, kilinID, startDryingTime, stopDryingTime, valmeticsTableName, limit), - locationID, kwhTableName, KwhName, timestamp,valueIDName, valueID, index); + // Defining the extra sql statements for extra parameters + treeSpecies = "AND LOWER(" + nameParameter + ") LIKE LOWER(" + '"' + "%" + TREE_SPECIES + "%" + '"' + ") "; + dimensions = "AND LOWER(" + nameParameter + ") LIKE LOWER(" + '"' + "%" + DIMENSIONS + "%" + '"' + ") "; + sawset = "AND LOWER(" + nameParameter + ") LIKE LOWER(" + '"' + "%" + SAWSET + "%" + '"' + ") "; + moistureGoal = "AND LOWER(" + nameParameter + ") LIKE LOWER(" + '"' + "%" + MOISTURE_GOAL + "%" + '"' + ") "; + + + // 1. Checking if user input is empty. + // 2. Checking if the extraInputParameter variable already contains the extra parameters, + // if so not adding a second time. + // 3. Checking if the while loop have previously removed the parameter, if so, not adding again. + if (!TREE_SPECIES.isEmpty() && !extraInputParameter.contains(treeSpecies) && !treespeciesRemoved) { + extraInputParameter += treeSpecies; + } + if (!DIMENSIONS.isEmpty() && !extraInputParameter.contains(dimensions) && !dimensionsRemoved) { + extraInputParameter += dimensions; + } + if (!SAWSET.isEmpty() && !extraInputParameter.contains(sawset) && !sawsetRemoved) { + extraInputParameter += sawset; + } + if (!MOISTURE_GOAL.isEmpty() && !extraInputParameter.contains(moistureGoal) && !moistureRemoved) { + extraInputParameter += moistureGoal; + } + + System.out.printf("\nExtra parameters:\n %s\n\n", extraInputParameter); + + //System.out.println("Før results"); + Map<Integer, Map<String, Number>> results = new TreeMap<>(getKwh(getZeroPointDate(locationID, extraInputParameter, nameParameter, kilinName, kilinID, startDryingTime, stopDryingTime, valmeticsTableName, limit), + locationID, kwhTableName, KwhName, timestamp, valueIDName, valueID, index)); + + //System.out.println("\n\nResults size: "+results.size()+"\n\n"); + intermediateHashMap.putAll(results); + index += results.size(); + //System.out.println("Etter results"); + } - allDryingPeriods.putAll(results); - index += results.size(); - } - while (true) { System.out.printf("\nExtra parameters:\n %s\n\n", extraInputParameter); + System.out.println("\n\nAll drying periods size: "+intermediateHashMap.size()+"\n\n"); // Retrieves the dates //results = getKwh(getZeroPointDate(locationID, extraInputParameter, nameParameter, kilinName, kilinID, startDryingTime, stopDryingTime, valmeticsTableName, limit)); // Checks if any dates where found, if not parameters are removed until dates are found - if (allDryingPeriods.size() < NUMBER_OF_PERIODS) { + if (intermediateHashMap.size() <= NUMBER_OF_PERIODS) { + //System.out.println("\nInni if, sletter parameter:\n"); if (extraInputParameter.contains(sawset) && !sawsetRemoved) { extraInputParameter = extraInputParameter.replace(sawset, ""); System.out.println("Sawset is removed"); - setInputParameters(true,false,false,false); + sawsetRemoved = true; + //setInputParameters(true,false,false,false); } else if (extraInputParameter.contains(treeSpecies) && !treespeciesRemoved) { extraInputParameter = extraInputParameter.replace(treeSpecies, ""); System.out.println("Tree species is removed"); - setInputParameters(true,true,false,false); + treespeciesRemoved = true; + //setInputParameters(true,true,false,false); } else if (extraInputParameter.contains(dimensions) && !dimensionsRemoved) { extraInputParameter = extraInputParameter.replace(dimensions, ""); System.out.println("Dimensions is removed"); - setInputParameters(true,true,true,false); + dimensionsRemoved = true; + //setInputParameters(true,true,true,false); } else if (extraInputParameter.contains(moistureGoal) && !moistureRemoved) { extraInputParameter = extraInputParameter.replace(moistureGoal, ""); System.out.println("Moisture goal is removed"); - setInputParameters(true,true,true,true); + moistureRemoved = true; + //setInputParameters(true,true,true,true); } else break; - } else break; + } else { + // Adding all periods to the hashmap to return + allDryingPeriods.putAll(intermediateHashMap); + break; + } } System.out.println(allDryingPeriods.size()); diff --git a/src/main/java/com/application/GUI/InputPopUpWindow.java b/src/main/java/com/application/GUI/InputPopUpWindow.java index b304471ee7e6919c6390eb92cf44b26b479c550a..6a7fdec3aab01a2fdf6c28cafb0bf99f7ba14b4b 100644 --- a/src/main/java/com/application/GUI/InputPopUpWindow.java +++ b/src/main/java/com/application/GUI/InputPopUpWindow.java @@ -30,8 +30,7 @@ public class InputPopUpWindow { Executors.newCachedThreadPool().submit(() -> { Thread.sleep(500); - completableFuture.complete(loadSingleSeries(setInputParameters(false, - false,false,false))); + completableFuture.complete(loadSingleSeries(setInputParameters())); return null; }); diff --git a/target/classes/com/application/DB/DB.class b/target/classes/com/application/DB/DB.class index 6238fce9aa2d95d0e06960ebaa0ba4fbb8944821..00b5e6cf1bded86e7d423fbd6fc8086e98136a1c 100644 Binary files a/target/classes/com/application/DB/DB.class and b/target/classes/com/application/DB/DB.class differ diff --git a/target/classes/com/application/GUI/InputPopUpWindow.class b/target/classes/com/application/GUI/InputPopUpWindow.class index 67be2b11dadcc835d48052b6830ed4398ec413d8..ac025e16a74ffbc38b252cffa522a108424edd17 100644 Binary files a/target/classes/com/application/GUI/InputPopUpWindow.class and b/target/classes/com/application/GUI/InputPopUpWindow.class differ