public static LineItemDomain createDefaultLineItem(OrderDomain order) throws Exception { final String name = "Line item #" + order.getLineItems().size() + 1; final String sapProduct = new String[] {"DECOMM", "DLINKP", "DPUBLI"}[random.nextInt(3)]; final String costType = new String[] {"CPC", "CPM", "CPD"}[random.nextInt(3)]; final CreativePlaceholder creativePlaceholder = new CreativePlaceholder(); creativePlaceholder.setSize(new Size(320, 480, false)); final BaseCustomFieldValue sapProductField = createCustomFieldSapProduct(sapProduct); final Calendar tomorrow = Calendar.getInstance(); final Calendar dayAfterTomorrow = Calendar.getInstance(); tomorrow.add(Calendar.DAY_OF_MONTH, 1); dayAfterTomorrow.add(Calendar.DAY_OF_MONTH, 2); LineItem dfpLineItem = new LineItem(); dfpLineItem.setName(name); dfpLineItem.setOrderId(order.getId()); dfpLineItem.setAllowOverbook(true); dfpLineItem.setCreativeRotationType(CreativeRotationType.EVEN); dfpLineItem.setCreativePlaceholders(new CreativePlaceholder[] {creativePlaceholder}); dfpLineItem.setCustomFieldValues(new BaseCustomFieldValue[] {sapProductField}); dfpLineItem.setTargeting(createDefaultTargeting()); dfpLineItem.setStartDateTime(toDateTime(tomorrow)); dfpLineItem.setEndDateTime(toDateTime(dayAfterTomorrow)); setLineItemsCostValues(dfpLineItem, order.getCurrency(), costType); final LineItemServiceInterface itemService = dfpServiceFactory.newDfpService(LineItemServiceInterface.class); try { dfpLineItem = itemService.createLineItem(dfpLineItem); } catch (ApiException e) { if (e.getFaultString().contains("RangeError.TOO_HIGH @ discount")) { dfpLineItem.setDiscount(0D); dfpLineItem = itemService.createLineItem(dfpLineItem); } } return createLineItemDomain(dfpLineItem, order, sapProduct); }
public static void updateCostPerUnit(LineItemDomain item, OrderDomain order, Long rate) throws Exception { final LineItemServiceInterface itemService = dfpServiceFactory.newDfpService(LineItemServiceInterface.class); LineItem dfpItem = itemService.getLineItem(item.getId()); dfpItem.setCostPerUnit(new Money(dfpItem.getCostPerUnit().getCurrencyCode(), rate)); if (Objects.equals(dfpItem.getDiscountType(), LineItemDiscountType.ABSOLUTE_VALUE) && dfpItem.getDiscount() > rate) { dfpItem.setDiscount(Math.max(0, dfpItem.getDiscount() - rate - 1)); } try { dfpItem = itemService.updateLineItem(dfpItem); } catch (ApiException e) { if (e.getFaultString().contains("RangeError.TOO_HIGH @ discount")) { dfpItem.setDiscount(0D); dfpItem = itemService.updateLineItem(dfpItem); } } setLineItemDomainCostValues(dfpItem, item, order); }