public void removeOrder(Integer itemId) {
      List<OrderLineDTO> list =
          new OrderLineDAS().findByUserItem(order.getBaseUserByUserId().getId(), itemId);

      for (OrderLineDTO line : list) {
        LOG.debug("Deleting order %s", line.getPurchaseOrder().getId());

        // who is the executor? we'll use the owner.. she is cancelling
        new OrderBL(line.getPurchaseOrder()).delete(order.getBaseUserByUserId().getId());
      }
    }
  private boolean addOrderToInvoice(
      Integer entityId, OrderDTO order, NewInvoiceDTO newInvoice, Date processDate, int maxPeriods)
      throws SessionInternalError, TaskException, PluggableTaskException {
    // require the calculation of the period dates
    PluggableTaskManager taskManager =
        new PluggableTaskManager(entityId, Constants.PLUGGABLE_TASK_ORDER_PERIODS);
    OrderPeriodTask optask = (OrderPeriodTask) taskManager.getNextClass();

    if (optask == null) {
      throw new SessionInternalError(
          "There has to be " + "one order period pluggable task configured");
    }
    Date start = optask.calculateStart(order);
    Date end = optask.calculateEnd(order, processDate, maxPeriods, start);
    List<PeriodOfTime> periods = optask.getPeriods();
    // there isn't anything billable from this order
    if (periods.size() == 0) {
      return false;
    }

    if (start != null && end != null && start.after(end)) {
      // how come it starts after it ends ???
      throw new SessionInternalError(
          "Calculated for "
              + "order "
              + order.getId()
              + " a period that"
              + " starts after it ends:"
              + start
              + " "
              + end);
    }

    // add this order to the invoice being created
    newInvoice.addOrder(order, start, end, periods);

    // prepaid orders shouldn't have to be included
    // past time.
    if (order.getBillingTypeId().compareTo(Constants.ORDER_BILLING_PRE_PAID) == 0
        && start != null
        && // it has to be recursive too
        processDate.after(start)) {

      eLogger.warning(
          entityId,
          order.getBaseUserByUserId().getId(),
          order.getId(),
          EventLogger.MODULE_BILLING_PROCESS,
          EventLogger.BILLING_PROCESS_UNBILLED_PERIOD,
          Constants.TABLE_PUCHASE_ORDER);

      LOG.warn("Order " + order.getId() + " is prepaid " + "but has past time not billed.");
    }

    // initialize the currency of the new invoice
    if (newInvoice.getCurrency() == null) {
      newInvoice.setCurrency(order.getCurrency());
    } else {
      // now we are not supporting orders with different
      // currencies in the same invoice. Later this could be done
      if (newInvoice.getCurrency().getId() != order.getCurrency().getId()) {
        throw new SessionInternalError(
            "Orders with different "
                + "currencies not supported in one invoice. "
                + "Currency = "
                + newInvoice.getCurrency().getId()
                + "order = "
                + order.getId());
      }
    }
    return true;
  }
  protected void processRules(OrderDTO newOrder) throws TaskException {
    // now we have the line with good defaults, the order and the item
    // These have to be visible to the rules
    KnowledgeBase knowledgeBase;
    try {
      knowledgeBase = readKnowledgeBase();
    } catch (Exception e) {
      throw new TaskException(e);
    }
    session = knowledgeBase.newStatefulKnowledgeSession();
    List<Object> rulesMemoryContext = new ArrayList<Object>();
    rulesMemoryContext.add(helperOrder);

    // add OrderDTO to rules memory context
    newOrder.setCurrency(new CurrencyDAS().find(newOrder.getCurrency().getId()));
    if (newOrder.getCreateDate() == null) {
      newOrder.setCreateDate(new Date());
    }
    rulesMemoryContext.add(newOrder);

    for (OrderLineDTO line : newOrder.getLines()) {
      if (line.getItem() != null) {
        ItemBL item = new ItemBL(line.getItemId());
        rulesMemoryContext.add(
            item.getDTO(
                helperOrder.getLanguage(),
                helperOrder.getUserId(),
                helperOrder.getEntityId(),
                helperOrder.getCurrencyId()));
      }
      rulesMemoryContext.add(line);
    }

    if (newOrder.getPricingFields() != null && newOrder.getPricingFields().size() > 0) {
      for (PricingField pf : newOrder.getPricingFields()) {
        rulesMemoryContext.add(pf);
      }
    }
    try {
      Integer userId = newOrder.getBaseUserByUserId().getId();
      UserDTOEx user = DTOFactory.getUserDTOEx(userId);
      rulesMemoryContext.add(user);
      ContactBL contact = new ContactBL();
      contact.set(userId);
      ContactDTOEx contactDTO = contact.getDTO();
      rulesMemoryContext.add(contactDTO);

      // Add the subscriptions
      OrderBL order = new OrderBL();
      for (OrderDTO myOrder : order.getActiveRecurringByUser(userId)) {
        for (OrderLineDTO myLine : myOrder.getLines()) {
          rulesMemoryContext.add(new Subscription(myLine));
        }
      }
    } catch (Exception e) {
      throw new TaskException(e);
    }
    session.setGlobal("order", helperOrder);
    // then execute the rules

    executeStatefulRules(session, rulesMemoryContext);
  }