String Error -> Enum ErrorHandling Logic removed from model class
This commit is contained in:
parent
ebb3c8c235
commit
187656814e
|
@ -0,0 +1,49 @@
|
|||
package org.hso.ecommerce.action.user;
|
||||
|
||||
import org.hso.ecommerce.api.RestServiceForDelivery;
|
||||
import org.hso.ecommerce.entities.shop.CustomerOrder;
|
||||
import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
|
||||
import org.hso.ecommerce.uimodel.DeliveryData;
|
||||
import org.hso.ecommerce.uimodel.DeliveryDataEnum;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CreateDeliveryData {
|
||||
|
||||
public static DeliveryData getDeliveryDataFromCustomerOrder(CustomerOrder customerOrder, CustomerOrderRepository customerOrderRepository, RestServiceForDelivery restServiceForDelivery)
|
||||
{
|
||||
if(customerOrder.trackingId == null)
|
||||
return new DeliveryData("", "", DeliveryDataEnum.NO_TRACKING_ID);
|
||||
|
||||
if(customerOrder.deliveredAt == null)
|
||||
{
|
||||
DeliveryData deliveryData = restServiceForDelivery.getDeliveryData(UUID.fromString(customerOrder.trackingId));
|
||||
|
||||
|
||||
if(deliveryData.isDelivered())
|
||||
{
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
|
||||
|
||||
try {
|
||||
calendar.setTime(simpleDateFormat.parse(deliveryData.getEstimatedArrival()));
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
customerOrder.deliveredAt = new Timestamp(calendar.getTimeInMillis());
|
||||
customerOrderRepository.save(customerOrder);
|
||||
}
|
||||
|
||||
return deliveryData;
|
||||
}
|
||||
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
|
||||
return new DeliveryData("Lieferung erfolgreich", formatter.format(customerOrder.deliveredAt), DeliveryDataEnum.OK);
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package org.hso.ecommerce.api;
|
|||
|
||||
import org.hso.ecommerce.entities.shop.CustomerOrder;
|
||||
import org.hso.ecommerce.uimodel.DeliveryData;
|
||||
import org.hso.ecommerce.uimodel.DeliveryDataEnum;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.http.*;
|
||||
|
@ -19,10 +20,12 @@ public class RestServiceForDelivery {
|
|||
@Autowired
|
||||
private final RestTemplateBuilder restTemplateBuilder = null;
|
||||
|
||||
private final String DELIVERY_IP_ADDRESS = "http://[::1]:8082";
|
||||
|
||||
|
||||
public String getDeliveryID(CustomerOrder customerOrder) throws ResourceAccessException
|
||||
{
|
||||
String url = "http://[::1]:8082/newDelivery";
|
||||
String url = DELIVERY_IP_ADDRESS + "/newDelivery";
|
||||
|
||||
RestTemplate restTemplate = restTemplateBuilder.build();
|
||||
|
||||
|
@ -48,7 +51,7 @@ public class RestServiceForDelivery {
|
|||
|
||||
public DeliveryData getDeliveryData(UUID trackingID) {
|
||||
|
||||
String url = "http://[::1]:8082/status";
|
||||
String url = DELIVERY_IP_ADDRESS + "/status";
|
||||
|
||||
RestTemplate restTemplate = restTemplateBuilder.build();
|
||||
|
||||
|
@ -68,13 +71,13 @@ public class RestServiceForDelivery {
|
|||
{
|
||||
return response.getBody();
|
||||
} else {
|
||||
return new DeliveryData("DHL-Server ist gerade nicht erreichbar","--:--:----");
|
||||
|
||||
return new DeliveryData("","",DeliveryDataEnum.NO_DATA);
|
||||
}
|
||||
}
|
||||
catch (ResourceAccessException e)
|
||||
{
|
||||
return new DeliveryData("DHL-Server gerade nicht erreichbar","--:--:----");
|
||||
return new DeliveryData("","",DeliveryDataEnum.NO_DATA);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.hso.ecommerce.controller;
|
||||
|
||||
import org.hso.ecommerce.action.user.CreateDeliveryData;
|
||||
import org.hso.ecommerce.action.user.UpdateUserSettingsAction;
|
||||
import org.hso.ecommerce.api.RestServiceForDelivery;
|
||||
import org.hso.ecommerce.entities.shop.CustomerOrder;
|
||||
|
@ -58,7 +59,7 @@ public class UserController {
|
|||
List<CustomerOrder> orders = customerOrderRepository.getAllOrders();
|
||||
|
||||
Map<CustomerOrder, DeliveryData> customerOrderDeliveryDataMap = orders.stream().
|
||||
collect(Collectors.toMap(Function.identity(), c -> DeliveryData.getDeliveryDataFromCustomerOrder(c, customerOrderRepository, restServiceForDelivery)));
|
||||
collect(Collectors.toMap(Function.identity(), c -> CreateDeliveryData.getDeliveryDataFromCustomerOrder(c, customerOrderRepository, restServiceForDelivery)));
|
||||
|
||||
model.addAttribute("orderDeliveryDataMap", customerOrderDeliveryDataMap);
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.hso.ecommerce.controller.intern.customers;
|
||||
|
||||
import org.hso.ecommerce.action.user.CreateDeliveryData;
|
||||
import org.hso.ecommerce.api.RestServiceForDelivery;
|
||||
import org.hso.ecommerce.entities.shop.CustomerOrder;
|
||||
import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
|
||||
|
@ -30,7 +31,7 @@ public class CustomerOrderController {
|
|||
|
||||
Map<CustomerOrder, DeliveryData> customerOrderDeliveryDataMap = orders.stream().collect
|
||||
(Collectors.toMap
|
||||
(Function.identity(), c-> DeliveryData.getDeliveryDataFromCustomerOrder(c,customerOrderRepository,restServiceForDelivery)));
|
||||
(Function.identity(), c-> CreateDeliveryData.getDeliveryDataFromCustomerOrder(c,customerOrderRepository,restServiceForDelivery)));
|
||||
|
||||
model.addAttribute("orderDeliveryDataMap", customerOrderDeliveryDataMap);
|
||||
return "intern/customerOrders/index";
|
||||
|
@ -41,7 +42,7 @@ public class CustomerOrderController {
|
|||
@PathVariable("id") String id
|
||||
) {
|
||||
CustomerOrder order = customerOrderRepository.findById(Long.parseLong(id)).get();
|
||||
DeliveryData deliveryData = DeliveryData.getDeliveryDataFromCustomerOrder(order, customerOrderRepository,restServiceForDelivery);
|
||||
DeliveryData deliveryData = CreateDeliveryData.getDeliveryDataFromCustomerOrder(order, customerOrderRepository,restServiceForDelivery);
|
||||
|
||||
model.addAttribute("order", order);
|
||||
model.addAttribute("deliveryData", deliveryData);
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
package org.hso.ecommerce.uimodel;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import org.hso.ecommerce.api.RestServiceForDelivery;
|
||||
import org.hso.ecommerce.entities.shop.CustomerOrder;
|
||||
import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
public class DeliveryData
|
||||
|
@ -17,6 +8,8 @@ public class DeliveryData
|
|||
|
||||
private final String status;
|
||||
|
||||
private final DeliveryDataEnum deliveryDataEnum;
|
||||
|
||||
private final String estimatedArrival;
|
||||
|
||||
private boolean isDelivered;
|
||||
|
@ -25,39 +18,15 @@ public class DeliveryData
|
|||
public DeliveryData(String status, String estimatedArrival) {
|
||||
this.status = status;
|
||||
this.estimatedArrival = estimatedArrival;
|
||||
this.deliveryDataEnum = DeliveryDataEnum.OK;
|
||||
isDelivered = status.equals("Lieferung erfolgreich");
|
||||
}
|
||||
|
||||
public static DeliveryData getDeliveryDataFromCustomerOrder(CustomerOrder customerOrder, CustomerOrderRepository customerOrderRepository, RestServiceForDelivery restServiceForDelivery)
|
||||
{
|
||||
if(customerOrder.trackingId == null)
|
||||
return new DeliveryData("Bestellung wurde elektronisch angekündigt","");
|
||||
|
||||
if(customerOrder.deliveredAt == null)
|
||||
{
|
||||
DeliveryData deliveryData = restServiceForDelivery.getDeliveryData(UUID.fromString(customerOrder.trackingId));
|
||||
|
||||
if(deliveryData.isDelivered())
|
||||
{
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
|
||||
|
||||
try {
|
||||
calendar.setTime(simpleDateFormat.parse(deliveryData.getEstimatedArrival()));
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
customerOrder.deliveredAt = new Timestamp(calendar.getTimeInMillis());
|
||||
customerOrderRepository.save(customerOrder);
|
||||
}
|
||||
|
||||
return deliveryData;
|
||||
}
|
||||
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
|
||||
return new DeliveryData("Lieferung erfolgreich",formatter.format(customerOrder.deliveredAt));
|
||||
public DeliveryData(String status, String estimatedArrival, DeliveryDataEnum deliveryDataEnum) {
|
||||
this.status = status;
|
||||
this.estimatedArrival = estimatedArrival;
|
||||
this.deliveryDataEnum = deliveryDataEnum;
|
||||
isDelivered = status.equals("Lieferung erfolgreich");
|
||||
}
|
||||
|
||||
public boolean isDelivered() {
|
||||
|
@ -71,5 +40,18 @@ public class DeliveryData
|
|||
public String getEstimatedArrival() {
|
||||
return estimatedArrival;
|
||||
}
|
||||
|
||||
public boolean allOk() {
|
||||
return deliveryDataEnum == DeliveryDataEnum.OK;
|
||||
}
|
||||
|
||||
public boolean noTrackingID() {
|
||||
return deliveryDataEnum == DeliveryDataEnum.NO_TRACKING_ID;
|
||||
}
|
||||
|
||||
public boolean noData() {
|
||||
return deliveryDataEnum == DeliveryDataEnum.NO_DATA;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package org.hso.ecommerce.uimodel;
|
||||
|
||||
public enum DeliveryDataEnum {
|
||||
OK,
|
||||
NO_TRACKING_ID,
|
||||
NO_DATA
|
||||
}
|
|
@ -25,7 +25,7 @@
|
|||
<nav th:replace="fragments/intern :: sidebar"></nav>
|
||||
<div class="content-width">
|
||||
<div>
|
||||
<h2 th:text="| Bestellung vom ${order.created.toString().substring(0,10)}"></h2>
|
||||
<h2 th:text="| Bestellung vom ${order.created.toString().substring(0,10)}|"></h2>
|
||||
<div>
|
||||
<table class="key-value">
|
||||
<tr>
|
||||
|
@ -34,11 +34,14 @@
|
|||
</tr>
|
||||
<tr>
|
||||
<th>Lieferstatus</th>
|
||||
<td th:text="${deliveryData.getStatus()}"></td>
|
||||
<td th:if="${deliveryData.allOk()}"><span th:text="${deliveryData.getStatus()}" /></td>
|
||||
<td th:if="${deliveryData.noTrackingID()}">Bestellung wurde elektronisch angekündigt</td>
|
||||
<td th:if="${deliveryData.noData()}">DHL-Server ist gerade nicht erreichbar</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td th:if="${order.deliveredAt == null && order.trackingId!=null}">Vorraussichtliche Ankunft: <span th:text="${deliveryData.getEstimatedArrival()}" /></td>
|
||||
<td th:if="${order.deliveredAt == null && order.trackingId!=null && !deliveryData.noData()}">Vorraussichtliche Ankunft: <span th:text="${deliveryData.getEstimatedArrival()}" /></td>
|
||||
<td th:if="${order.deliveredAt == null && order.trackingId!=null && deliveryData.noData()}">Vorraussichtliche Ankunft: --:--:---- </td>
|
||||
<td th:if="${order.deliveredAt != null && order.trackingId!=null}"><b>Angekommen</b> Ankunft: <span th:text="${deliveryData.getEstimatedArrival()}" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
|
|
@ -45,7 +45,9 @@
|
|||
<td th:text="${order.getKey().id}"></td>
|
||||
<td th:text="${order.getKey().created.toString().substring(0, 10)}"></td>
|
||||
<td th:text="${#numbers.formatDecimal(order.getKey().totalGrossCent * 0.01, 1, 'POINT', 2, 'COMMA')}"></td>
|
||||
<td th:text="${order.getValue().getStatus()}"></td>
|
||||
<td th:if="${order.getValue().allOk()}"><span th:text="${order.getValue().getStatus()}" /></td>
|
||||
<td th:if="${order.getValue().noTrackingID()}">Bestellung wurde elektronisch angekündigt</td>
|
||||
<td th:if="${order.getValue().noData()}">DHL-Server ist gerade nicht erreichbar</td>
|
||||
<td><a th:href="@{/intern/customerOrders/{id}(id=${order.getKey().id})}" class="button smaller">Details</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
|
|
@ -22,16 +22,19 @@
|
|||
<nav th:replace="fragments/customer :: sidebar"></nav>
|
||||
<div class="content-width detailflex">
|
||||
<div th:each="order: ${orderDeliveryDataMap}">
|
||||
<h2 id="20202701" th:text="|Bestellung vom ${order.getKey().formatCreated()}" />
|
||||
<h2 id="20202701" th:text="|Bestellung vom ${order.getKey().formatCreated()}|" />
|
||||
<div>
|
||||
<table class="key-value">
|
||||
<tr>
|
||||
<th>Lieferstatus</th>
|
||||
<td th:text="${order.getValue().getStatus()}"></td>
|
||||
<td th:if="${order.getValue().allOk()}"><span th:text="${order.getValue().getStatus()}" /></td>
|
||||
<td th:if="${order.getValue().noTrackingID()}">Bestellung wurde elektronisch angekündigt</td>
|
||||
<td th:if="${order.getValue().noData()}">DHL-Server ist gerade nicht erreichbar</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td th:if="${order.getKey().deliveredAt == null && order.getKey().trackingId!=null}">Vorraussichtliche Ankunft: <span th:text="${order.getValue().getEstimatedArrival()}" /></td>
|
||||
<td th:if="${order.getKey().deliveredAt == null && order.getKey().trackingId!=null && !order.getValue().noData()}">Vorraussichtliche Ankunft: <span th:text="${order.getValue().getEstimatedArrival()}" /></td>
|
||||
<td th:if="${order.getKey().deliveredAt == null && order.getKey().trackingId!=null && order.getValue().noData()}">Vorraussichtliche Ankunft: --:--:----</td>
|
||||
<td th:if="${order.getKey().deliveredAt != null && order.getKey().trackingId!=null}"><b>Angekommen</b> Ankunft: <span th:text="${order.getValue().getEstimatedArrival()}" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
|
Reference in New Issue