From 8e58118ac728bf42b69d13513fc97df4ded440e4 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Mon, 25 Mar 2019 13:32:01 +0100 Subject: [PATCH] Add new classes for calculator --- .../main/java/greenify/server/InputItem.java | 59 +++++++++++++ .../java/greenify/server/InputValidator.java | 87 +++++++++++++++++++ .../server/service/CalculatorService.java | 67 ++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 src/Server/src/main/java/greenify/server/InputItem.java create mode 100644 src/Server/src/main/java/greenify/server/InputValidator.java create mode 100644 src/Server/src/main/java/greenify/server/service/CalculatorService.java diff --git a/src/Server/src/main/java/greenify/server/InputItem.java b/src/Server/src/main/java/greenify/server/InputItem.java new file mode 100644 index 0000000..985ec2e --- /dev/null +++ b/src/Server/src/main/java/greenify/server/InputItem.java @@ -0,0 +1,59 @@ +package greenify.server; + +public class InputItem { + private String name; + private Boolean isFloat; + private String defaultValue; + private Boolean isPresentByDefault = true; + + /** + * Constructor for input items. + * @param name of the input + * @param isFloat whether it is float or not + * @param defaultValue states the value + */ + public InputItem(String name, boolean isFloat, String defaultValue) { + this.name = name; + this.isFloat = isFloat; + this.defaultValue = defaultValue; + } + + /** + * Constructor for input items. + * @param name of the input + * @param isFloat whether it is float or not + * @param defaultValue states the value + * @param isPresentByDefault for different number of cars + */ + public InputItem(String name, Boolean isFloat, String defaultValue, + Boolean isPresentByDefault) { + this.name = name; + this.isFloat = isFloat; + this.defaultValue = defaultValue; + this.isPresentByDefault = isPresentByDefault; + } + + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Boolean getFloat() { + return isFloat; + } + + public void setFloat(Boolean isFloat) { + this.isFloat = isFloat; + } +} diff --git a/src/Server/src/main/java/greenify/server/InputValidator.java b/src/Server/src/main/java/greenify/server/InputValidator.java new file mode 100644 index 0000000..a17b575 --- /dev/null +++ b/src/Server/src/main/java/greenify/server/InputValidator.java @@ -0,0 +1,87 @@ +package greenify.server; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class InputValidator { + + private static final List inputItems = Arrays.asList( + new InputItem("input_size", false, "1"), + new InputItem("input_income", false, "40000"), + new InputItem("transportation_num_vehicles", false, "1" ), + new InputItem("transportation_miles1", false, "16100", false), + new InputItem("transportation_fuels1", false, "2" , false), + new InputItem("transportation_mpg1", false, null, false ), + new InputItem("transportation_miles2", false, "13200", false ), + new InputItem("transportation_fuels2", false, "0" , false), + new InputItem("transportation_mpg2", false, "22" ,false), + new InputItem("transportation_publicTrans", false, "436" ), + new InputItem("transportation_air", false, "3900" ), + new InputItem("housing_electricity_kwh_year", false, "12632" ), + new InputItem("housing_cleanPercent", false, "0" ), + new InputItem("housing_naturalGas_therms_year", false, "472" ), + new InputItem("housing_heatingOil_gallons_year", false, "73" ), + new InputItem("housing_square_feet", false, "1850" ), + new InputItem("housing_water_sewage", false, "100" ), + new InputItem("food_meat_fish_eggs", true, "2.4" ), + new InputItem("food_grains", true, "4.1" ), + new InputItem("food_dairy", true, "2.2" ), + new InputItem("food_fruit_vegetables", true, "3.5" ), + new InputItem("food_snacks_drinks", true, "3.4" ), + new InputItem("shopping_goods", false, "1310" ), + new InputItem("shopping_services", false, "2413" ) + ); + + /** + * The method checks that the id is valid or not. + * @param inputName the name of input + * @return true or false + */ + public static Boolean isValidItem(String inputName) { + return inputItems.stream().filter(i -> i.getName() == inputName).findAny().isPresent(); + } + + /** + * The method checks that the item value is valid or not. + * @param inputName the name of input + * @param value the value of item + * @return true or false + */ + public static boolean isValidItemValue(String inputName, String value) { + InputItem item = null; + for (int i = 0; i < inputItems.size(); i++) { + if (inputItems.get(i).getName() == inputName) { + item = inputItems.get(i); + } + } + if (item.getFloat()) { + try { + Float number = Float.parseFloat(value); + } catch (NumberFormatException | NullPointerException nfe) { + return false; + } + return true; + } else { + try { + Integer number = Integer.parseInt(value); + } catch (NumberFormatException | NullPointerException nfe) { + return false; + } + return true; + } + } + + /** + * getter for default values. + * @return the map of default values + */ + public static Map getDefaultValues() { + Map map = new HashMap(){}; + for (int i = 0; i < inputItems.size(); i++) { + map.put(inputItems.get(i).getName(), inputItems.get(i).getDefaultValue()); + } + return map; + } +} diff --git a/src/Server/src/main/java/greenify/server/service/CalculatorService.java b/src/Server/src/main/java/greenify/server/service/CalculatorService.java new file mode 100644 index 0000000..7ad64b4 --- /dev/null +++ b/src/Server/src/main/java/greenify/server/service/CalculatorService.java @@ -0,0 +1,67 @@ +package greenify.server.service; + +import greenify.server.data.model.User; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import java.util.Map; + +@Service +public class CalculatorService { + Logger logger = LoggerFactory.getLogger(UserService.class); + + @Autowired + RestTemplate restTemplate; + + @Bean + RestTemplate restTemplate(RestTemplateBuilder builder) { + return builder.build(); + } + + protected Float invokeExternalService(Map map) { + /** + * curl -X GET "https://apis.berkeley.edu/coolclimate/footprint-sandbox?input_location_mode=1 + * &input_location=48001&input_income=1&input_size=0&input_footprint_transportation_miles1=3 + * &input_footprint_transportation_mpg1=5&input_footprint_transportation_fuel1=0" + * -H "accept: application/json" -H "app_id: a98272e3" + * -H "app_key: b9167c4918cb2b3143614b595065d83b" + */ + HttpHeaders headers = new HttpHeaders(); + headers.set("accept", MediaType.APPLICATION_JSON_VALUE); + headers.set("app_id", "a98272e3"); + headers.set("app_key", "b9167c4918cb2b3143614b595065d83b"); + HttpEntity entity = new HttpEntity<>("parameters", headers); + UriComponentsBuilder builder = + UriComponentsBuilder.fromHttpUrl("https://apis.berkeley.edu/coolclimate/footprint-sandbox"); + for (String inputId : map.keySet()) { + builder = builder.queryParam(inputId, map.get(inputId)); + } + ResponseEntity response = restTemplate + .exchange(builder.build().encode().toUri(), HttpMethod.GET, + entity, String.class); + logger.info(response.getStatusCode().toString()); + logger.info(response.getBody()); + String result = response.getBody().substring(response.getBody() + .indexOf("") + + 20, response.getBody().indexOf("")); + // to do: in not HTTP 200 or exception case throws exception + System.out.println(Float.parseFloat(result)); + return Float.parseFloat(result); + } + + public Float calculateFootprint(User user) { + return invokeExternalService(user.getFootPrintInputs()); + } +} +