package mapper; import api.v1.*; import exceptions.PoseidonException; import models.OrderModel; import models.ProductModel; import models.RecipientModel; import play.mvc.Http; import service.PoseidonPropertyService; import service.PoseidonService; import java.util.ArrayList; import java.util.Date; import java.util.List; public class OrderMapper { public static Termin[] mapTerminsToApi(String terminString) { Termin[] termins = new Termin[8]; for (int i = 0; i < 8; i++) { termins[i] = new Termin(i * 3, false); } if (terminString != null && !terminString.isEmpty()) { String[] tArray = terminString.split(","); if (tArray != null) { for (int i = 0; i < tArray.length; i++) { int index = (int)Math.floor((double)(Integer.parseInt(tArray[i]))/ 3.0); termins[index].selected = true; } } } return termins; } private static String mapTerminsToModel(Termin[] termins) { String retval = ""; for(Termin t : termins){ if ( t != null && t.selected ) retval = retval + t.hour + ","; } if ( retval.endsWith(","))retval = retval.replaceAll(",$",""); return retval; } public List<Order> mapToApi(List<OrderModel> mOrders) { List<Order> OrderList = new ArrayList<>(); for (OrderModel mOrder : mOrders) { Order Order = mapToApi(mOrder); OrderList.add(Order); } return OrderList; } public Order mapToApi(OrderModel model) { Order order = new Order(); order.id = model.id; order.customer_email = model.customer_email; order.customer_name = model.customer_name; order.customer_phone = model.customer_phone; order.custref_contractnum = model.custref_contractnum; order.custref_email = model.custref_email; order.custref_po_calloff = model.custref_po_calloff; order.start_date1= model.start_date1; order.start_termin1= model.start_termin1; order.end_date1= model.end_date1; order.end_termin1= model.end_termin1; order.start_date2= model.start_date2; order.start_termin2= model.start_termin2; order.end_date2= model.end_date2; order.end_termin2= model.end_termin2; order.start_date3= model.start_date3; order.start_termin3= model.start_termin3; order.end_date3= model.end_date3; order.end_termin3= model.end_termin3; order.termins1 = mapTerminsToApi(model.termins1); order.termins2 = mapTerminsToApi(model.termins2); order.termins3 = mapTerminsToApi(model.termins3); order.info_for_meteorologist = model.info_for_meteorologist; order.met_ref = model.met_ref; order.other_info = model.other_info; order.customer = new CustomerMapper().mapToApi(model.customer); order.position_name = model.position_name; order.position = new PositionMapper().mapToApi(model.position); order.recipients = new ArrayList<>(); RecipientMapper recipientMapper = new RecipientMapper(); for (RecipientModel recipientModel : model.recipients) { order.recipients.add(recipientMapper.mapToApi(recipientModel)); } order.status= new OrderStatus(model.orderStatus.ordinal(),model.orderStatus.getStatusString()); order.product = model.product != null ? new ProductMapper().mapToApi(model.product):null; order.startup_fee = model.startup_fee; order.taskDescription = model.taskDescription; order.audit = new Audit(model); return order; } public OrderModel mapToModel(Order aOrder, OrderModel mOrder, String user) { if (aOrder == null) { throw new PoseidonException(Http.Status.BAD_REQUEST, "Ingen ordre-data angitt"); } if (aOrder.id != null) { mOrder.id = aOrder.id; } mOrder.customer_email = aOrder.customer_email; mOrder.customer_name = aOrder.customer_name; mOrder.customer_phone = aOrder.customer_phone; mOrder.custref_contractnum = aOrder.custref_contractnum; mOrder.custref_email = aOrder.custref_email; mOrder.custref_po_calloff = aOrder.custref_po_calloff; mOrder.start_date1 = aOrder.start_date1; mOrder.start_termin1 = aOrder.start_termin1; mOrder.end_date1 = aOrder.end_date1; mOrder.end_termin1 = aOrder.end_termin1; mOrder.start_date2 = aOrder.start_date2; mOrder.start_termin2 = aOrder.start_termin2; mOrder.end_date2 = aOrder.end_date2; mOrder.end_termin2 = aOrder.end_termin2; mOrder.start_date3 = aOrder.start_date3; mOrder.start_termin3 = aOrder.start_termin3; mOrder.end_date3 = aOrder.end_date3; mOrder.end_termin3 = aOrder.end_termin3; mOrder.termins1 = mapTerminsToModel(aOrder.termins1); mOrder.termins2 = mapTerminsToModel(aOrder.termins2); mOrder.termins3 = mapTerminsToModel(aOrder.termins3); mOrder.info_for_meteorologist = aOrder.info_for_meteorologist; mOrder.met_ref = aOrder.met_ref; mOrder.other_info = aOrder.other_info; mOrder.customer = new CustomerMapper().mapToModel(aOrder.customer, mOrder.customer); mOrder.position_name = aOrder.position_name; mOrder.position = new PositionMapper().mapToModel(aOrder.position); mOrder.recipients = new ArrayList<>(); RecipientMapper recipientMapper = new RecipientMapper(); for (Recipient r : aOrder.recipients) { RecipientModel recipientModel = recipientMapper.mapToModel(r,user); if ( recipientModel.createdBy == null || recipientModel.createdBy.isEmpty()){ recipientModel.createdBy = user; recipientModel.created = PoseidonService.getNow().toDate(); } mOrder.recipients.add(recipientModel); if ( !recipientModel.orders.contains(mOrder)){ recipientModel.orders.add(mOrder); } } if ( aOrder.status != null ) mOrder.orderStatus = models.OrderStatus.values()[aOrder.status.code]; ProductModel defaultProduct = ProductModel.findByName(PoseidonPropertyService.getProperty("order.default_productname")); mOrder.product= aOrder.product != null ? new ProductMapper().mapToModel(aOrder.product):defaultProduct; mOrder.startup_fee = aOrder.startup_fee; mOrder.taskDescription = aOrder.taskDescription; return mOrder; } }