From ebeba722784c9f57b776b6f8d6d05fc482a92d17 Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 10 Jun 2020 17:03:59 +0200 Subject: [PATCH] impl tracking ID --- .../ecommerce/api/RestServiceForDelivery.java | 19 +++------- .../org/hso/ecommerce/app/Application.java | 4 ++ .../customers/CustomerOrderController.java | 38 ++++++++++++------- .../entities/shop/CustomerOrder.java | 5 --- .../repos/shop/CustomerOderRepository.java | 2 + .../templates/intern/customerOrders/id.html | 7 +++- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java b/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java index 2a591c7..7cfdfc5 100644 --- a/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java +++ b/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java @@ -1,4 +1,4 @@ -package org.hso.ecommerce.action.shop; +package org.hso.ecommerce.api; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -19,8 +19,8 @@ import java.util.*; public class RestServiceForDelivery { @Autowired - private RestTemplateBuilder restTemplateBuilder; - + private final RestTemplateBuilder restTemplateBuilder = null; + public String getDeliveryID(CustomerOrder customerOrder) { @@ -61,20 +61,11 @@ public class RestServiceForDelivery { HttpEntity entity = new HttpEntity<>(headers); - ResponseEntity response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class); - - ObjectMapper mapper = new ObjectMapper(); - JsonNode node = null; - - try { - node = mapper.readTree(response.getBody()); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } + ResponseEntity response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, CustomerOrderController.DeliveryData.class); if (response.getStatusCode() == HttpStatus.OK) { - return new CustomerOrderController.DeliveryData(node.get("status").asText(),node.get("estimatedArrival").asText()); + return response.getBody(); } else { return null; } diff --git a/prototype/src/main/java/org/hso/ecommerce/app/Application.java b/prototype/src/main/java/org/hso/ecommerce/app/Application.java index 8b45a22..ef719c7 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/Application.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/Application.java @@ -3,9 +3,12 @@ package org.hso.ecommerce.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import java.util.Arrays; + @SpringBootApplication @ComponentScan(basePackages = {"org.hso.ecommerce"}) @EntityScan("org.hso.ecommerce") @@ -13,6 +16,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; public class Application { public static void main(String[] args) { + SpringApplication.run(Application.class, args); } diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/intern/customers/CustomerOrderController.java b/prototype/src/main/java/org/hso/ecommerce/controller/intern/customers/CustomerOrderController.java index da2e1f5..dc44655 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/intern/customers/CustomerOrderController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/intern/customers/CustomerOrderController.java @@ -2,21 +2,21 @@ package org.hso.ecommerce.controller.intern.customers; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import org.hibernate.Session; -import org.hso.ecommerce.action.shop.RestServiceForDelivery; +import org.hso.ecommerce.api.RestServiceForDelivery; import org.hso.ecommerce.entities.shop.CustomerOrder; import org.hso.ecommerce.repos.shop.CustomerOderRepository; -import org.hso.ecommerce.repos.user.UserRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.data.jpa.repository.Query; +import org.springframework.format.datetime.DateFormatterRegistrar; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import javax.transaction.Transaction; import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.function.Function; @@ -28,6 +28,8 @@ public class CustomerOrderController { @Autowired private final CustomerOderRepository customerOrderRepository = null; + @Autowired + private final RestServiceForDelivery restServiceForDelivery = null; @GetMapping("") public String internCustomerOrder(Model model) { @@ -61,12 +63,21 @@ public class CustomerOrderController { if(customerOrder.deliveredAt == null) { - DeliveryData deliveryData = new RestServiceForDelivery(new RestTemplateBuilder()).getDeliveryData(UUID.fromString(customerOrder.trackingId)); + DeliveryData deliveryData = restServiceForDelivery.getDeliveryData(UUID.fromString(customerOrder.trackingId)); if(deliveryData.isDelivered()) { - customerOrderRepository.updateUserDeliveredAt(customerOrder.id, Timestamp.valueOf(deliveryData.estimatedArrival)); - System.out.println(Timestamp.valueOf(deliveryData.estimatedArrival)); + Calendar calendar = Calendar.getInstance(); + + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy"); + + try { + calendar.setTime(simpleDateFormat.parse(deliveryData.estimatedArrival)); + } catch (ParseException e) { + e.printStackTrace(); + } + + customerOrderRepository.updateUserDeliveredAt(customerOrder.id, new Timestamp(calendar.getTimeInMillis())); } return deliveryData; @@ -78,17 +89,16 @@ public class CustomerOrderController { public static class DeliveryData { - @JsonProperty("status") - private String status; - @JsonProperty("estimatedArrival") - private String estimatedArrival; + + private final String status; + + private final String estimatedArrival; private boolean isDelivered; - @JsonCreator(mode = JsonCreator.Mode.DELEGATING) + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) public DeliveryData(String status, String estimatedArrival) { this.status = status; - System.out.println(status); this.estimatedArrival = estimatedArrival; isDelivered = status.equals("Lieferung erfolgreich"); } diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/shop/CustomerOrder.java b/prototype/src/main/java/org/hso/ecommerce/entities/shop/CustomerOrder.java index 0272599..8c80c58 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/shop/CustomerOrder.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/shop/CustomerOrder.java @@ -43,9 +43,4 @@ public class CustomerOrder { public int totalNetCent; public int totalGrossCent; public int totalVatCent; - - public String getEstimatedArrival() { - //TODO: get estimated arrival from api - return "TODO TODO TODO"; - } } diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/shop/CustomerOderRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/shop/CustomerOderRepository.java index 88f715b..6f673d4 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/shop/CustomerOderRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/CustomerOderRepository.java @@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -19,6 +20,7 @@ public interface CustomerOderRepository extends JpaRepository getAllOrders(); + @Transactional @Modifying(clearAutomatically = true) @Query("UPDATE CustomerOrder co SET co.deliveredAt = :newDeliveredAt WHERE co.id = :customerOrderID") int updateUserDeliveredAt(long customerOrderID, java.sql.Timestamp newDeliveredAt); diff --git a/prototype/src/main/resources/templates/intern/customerOrders/id.html b/prototype/src/main/resources/templates/intern/customerOrders/id.html index ea180b6..2ea505f 100644 --- a/prototype/src/main/resources/templates/intern/customerOrders/id.html +++ b/prototype/src/main/resources/templates/intern/customerOrders/id.html @@ -35,8 +35,11 @@ Lieferstatus - Vorraussichtliche Ankunft: - Angekommen Ankunft: + + + + Vorraussichtliche Ankunft: + Angekommen Ankunft: Sendeverfolgungsnummer