private static void removeLineItemFromOrder(LineItemDomain lineItem, OrderDomain order) {
   if (order.getLineItems().remove(lineItem)) {
     order.setGrossValue(order.getGrossValue() - lineItem.getGrossValue());
     order.setNetValue(order.getNetValue() - lineItem.getNetValue());
     order.setGeneralDiscount(order.getGeneralDiscount() - lineItem.getGeneralDiscount());
   }
 }
 private static void addLineItemIntoOrder(LineItemDomain lineItem, OrderDomain order) {
   if (order.getLineItems().add(lineItem)) {
     order.setGrossValue(order.getGrossValue() + lineItem.getGrossValue());
     order.setNetValue(order.getNetValue() + lineItem.getNetValue());
     order.setGeneralDiscount(order.getGeneralDiscount() + lineItem.getGeneralDiscount());
   }
 }
  public static void removeLineItems(OrderDomain order) throws Exception {
    if (CollectionUtils.isEmpty(order.getLineItems())) {
      return;
    }

    final List<Long> itemsIds = new ArrayList<>(order.getLineItems().size());
    for (LineItemDomain item : order.getLineItems()) {
      itemsIds.add(item.getId());
    }

    final String itemsIdsString = StringUtils.join(itemsIds, ',');

    final Statement stmtFilterByIds =
        new StatementBuilder().where(String.format("id IN (%s)", itemsIdsString)).toStatement();

    final LineItemServiceInterface itemService =
        dfpServiceFactory.newDfpService(LineItemServiceInterface.class);
    itemService.performLineItemAction(new DeleteLineItems(), stmtFilterByIds);

    order.getLineItems().clear();
  }
  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);
  }
  private static LineItemDomain createLineItemDomain(
      LineItem dfpLineItem, OrderDomain order, String sapProduct) {
    final LineItemDomain lineItem = new LineItemDomain();
    lineItem.setId(dfpLineItem.getId());
    lineItem.setStatus(dfpLineItem.getStatus().toString());
    lineItem.setStartDate(dfpLineItem.getStartDateTime().toString());
    lineItem.setEndDate(dfpLineItem.getEndDateTime().toString());
    lineItem.setEnterprise(order.getEnterprise());
    lineItem.setSapProduct(sapProduct);
    lineItem.setArchived(dfpLineItem.getIsArchived());

    setLineItemDomainCostValues(dfpLineItem, lineItem, order);

    return lineItem;
  }
  public static void archiveLineItem(OrderDomain order, LineItemDomain item) throws Exception {
    final LineItem dfpItem = performLineItemAction(item, new ArchiveLineItems());
    item.setArchived(dfpItem.getIsArchived());

    if (item.isArchived()) {
      order.setGrossValue(order.getGrossValue() - item.getGrossValue());
      order.setNetValue(order.getNetValue() - item.getNetValue());
      order.setGeneralDiscount(order.getGeneralDiscount() - item.getGeneralDiscount());
    }
  }