Advertisement
Hellboyandso

domain_App.java

Dec 12th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. package domain;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.json.JSONObject;
  8.  
  9. import com.fasterxml.jackson.core.JsonParseException;
  10. import com.fasterxml.jackson.core.type.TypeReference;
  11. import com.fasterxml.jackson.databind.JsonMappingException;
  12. import com.fasterxml.jackson.databind.ObjectMapper;
  13. import com.google.gson.Gson;
  14. import com.sun.jersey.api.client.Client;
  15. import com.sun.jersey.api.client.ClientResponse;
  16. import com.sun.jersey.api.client.WebResource;
  17.  
  18. import exceptions.DomainException;
  19. import ui.Menu;
  20.  
  21. public class App {
  22.     //private String baseURL = "http://localhost:9099/";
  23.     private String orderID = "";
  24.    
  25.     private Menu menu = new Menu();
  26.    
  27.     protected void showWelcomeScreen() throws DomainException, JsonParseException, JsonMappingException, IOException{
  28.         String choice = menu.showWelcomeScreen();
  29.         createOrder();
  30.         requestHandler(choice);
  31.     }
  32.    
  33.     private void requestHandler(String choice) throws DomainException, JsonParseException, JsonMappingException, IOException{
  34.         if(choice.equals(PizzaEnum.addPizza))
  35.             addPizza();
  36.         if(choice.equals(PizzaEnum.cancelOrder))
  37.             cancelOrder();
  38.         if(choice.equals(PizzaEnum.cheaperPizzas))
  39.             cheaperPizzas();
  40.         if(choice.equals(PizzaEnum.checkMenu))
  41.             checkMenu();
  42.         //if(choice.equals(PizzaEnum.createOrder))
  43.             //createOrder();
  44.         if(choice.equals(PizzaEnum.getPizzas))
  45.             getPizzas();
  46.         if(choice.equals(PizzaEnum.orderState))
  47.             orderState();
  48.         if(choice.equals(PizzaEnum.pizzaIngredients))
  49.             pizzaIngredients();
  50.         if(choice.equals(PizzaEnum.price))
  51.             price();
  52.         if(choice.equals(PizzaEnum.submitOrder))
  53.             submitOrder();
  54.         if(choice.equals(PizzaEnum.vegetarian))
  55.             vegetarian();
  56.         else
  57.             throw new DomainException("A wrong choice was made.");
  58.     }
  59.  
  60.     private void addPizza() {
  61.         List<String> ingredients = menu.addPizza();
  62.         String json = new Gson().toJson(ingredients);
  63.         String response = executeManageOrderPost(generateUrlPartManageOrder(), "addPizza", json);
  64.         menu.showResponse(response);
  65.     }
  66.  
  67.     private void cancelOrder() {
  68.         String response = executeManageOrderPost(generateUrlPartManageOrder(), "cancelOrder", null);
  69.         menu.showResponse(response);
  70.     }
  71.  
  72.     private void cheaperPizzas() throws JsonParseException, JsonMappingException, IOException {
  73.         ObjectMapper mapper = new ObjectMapper();
  74.         String response = executeManageOrderGet(generateUrlPartCheckMenu(), "cheaperPizzas");
  75.         List<String> cheaperPizzas = mapper.readValue(response, new TypeReference<List<String>>(){});
  76.         menu.showResponseList(cheaperPizzas);
  77.     }
  78.  
  79.     private void checkMenu() {
  80.         //TODO
  81.         //Onvoldoende gedocumenteerd in de blueprint.
  82.         menu.showWelcomeScreen();
  83.     }
  84.  
  85.     private void createOrder() {
  86.         this.orderID = executeManageOrderGet(generateUrlPartManageOrder(), "createorder");
  87.     }
  88.  
  89.     private void getPizzas() {
  90.         String response = executeManageOrderGet(generateUrlPartCheckMenu(), "getPizzas");
  91.         JSONObject jsonObject = new JSONObject(response);
  92.         List<Pizza> pizzas = new ArrayList<Pizza>();
  93.         for(int i = 0; i < jsonObject.length(); i++){
  94.             JSONObject jO = jsonObject.getJSONObject("Pizza");
  95.             Pizza pizza = new Pizza(jO.getString("Name"), Double.parseDouble(jO.getString("Price")));
  96.             JSONObject ingredients = jO.getJSONObject("Ingredients");
  97.             for(int j = 0; i < ingredients.length(); i++){
  98.                 pizza.addIngredient(new Ingredient(ingredients.getString("Name"), ingredients.getString("Description"), Double.parseDouble(ingredients.getString("Price")), Integer.parseInt(ingredients.getString("maxAmountInStock"))));
  99.             }
  100.             pizzas.add(pizza);
  101.         }
  102.         menu.showResponseListAllPizzas(pizzas);
  103.     }
  104.  
  105.     private void orderState() {
  106.         String response = executeManageOrderGetOrderID(generateUrlPartManageOrder(), "orderState");
  107.         menu.showResponse(response);
  108.     }
  109.  
  110.     private void pizzaIngredients() {
  111.         String input = menu.askPizzaName();
  112.         //TODO
  113.         //Blueprint was hier verkeerd over. je kan beter een naam vragen en dan de
  114.         //lijst geven van ingredienten.
  115.         menu.showWelcomeScreen();
  116.     }
  117.  
  118.     private void price() {
  119.         String response = executeManageOrderGetOrderID(generateUrlPartManageOrder(),"price");
  120.         menu.showResponse(response);
  121.     }
  122.  
  123.     private void submitOrder() {
  124.         String response = executeManageOrderPost(generateUrlPartManageOrder(), "submitOrder", null);
  125.         menu.showResponse(response);
  126.     }
  127.  
  128.     private void vegetarian() {
  129.         String response = executeManageOrderGet(generateUrlPartCheckMenu(), "vegetarian");
  130.         JSONObject jsonObject = new JSONObject(response);
  131.         List<Pizza> pizzas = new ArrayList<Pizza>();
  132.         for(int i = 0; i < jsonObject.length(); i++){
  133.             JSONObject jO = jsonObject.getJSONObject("Pizza");
  134.             Pizza pizza = new Pizza(jO.getString("Name"), Double.parseDouble(jO.getString("Price")));
  135.             JSONObject ingredients = jO.getJSONObject("Ingredients");
  136.             for(int j = 0; i < ingredients.length(); i++){
  137.                 pizza.addIngredient(new Ingredient(ingredients.getString("Name"), ingredients.getString("Description"), Double.parseDouble(ingredients.getString("Price")), Integer.parseInt(ingredients.getString("maxAmountInStock"))));
  138.             }
  139.             pizzas.add(pizza);
  140.         }
  141.         menu.showResponseListAllPizzas(pizzas);
  142.     }
  143.    
  144.     private String generateUrlPartManageOrder(){
  145.         return "manageOrder";
  146.     }
  147.    
  148.     private String generateUrlPartCheckMenu(){
  149.         return "checkMenu";
  150.     }
  151.    
  152.     private String executeManageOrderGet(String urlPathPiece, String urlPart){
  153.         Client client = Client.create();
  154.         WebResource webResource = client.resource("http://localhost:9099/" + urlPathPiece + "/" + urlPart);
  155.         ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
  156.         if(response.getStatus() != 200)
  157.             throw new RuntimeException("Failed: HTTP error code: " + response.getStatus());
  158.         return response.getEntity(String.class);
  159.     }
  160.    
  161.     private String executeManageOrderGetOrderID(String urlPathPiece, String urlPart){
  162.         Client client = Client.create();
  163.         WebResource webResource = client.resource("http://localhost:9099/" + urlPathPiece + "/" + urlPart + "/" + orderID);
  164.         ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
  165.         if(response.getStatus() != 200)
  166.             throw new RuntimeException("Failed: HTTP error code: " + response.getStatus());
  167.         return response.getEntity(String.class);
  168.     }
  169.    
  170.     private String executeManageOrderPost(String urlPathPiece, String urlPart, String json){
  171.         Client client = Client.create();
  172.         WebResource webResource = client.resource("http://localhost:9099/" + urlPathPiece + "/" + urlPart + "/" +orderID);
  173.         ClientResponse response = webResource.type("application/json").post(ClientResponse.class, json);
  174.         return response.getEntity(String.class);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement