/**
   * Save Selection
   *
   * @return asi which was saved or null
   * @return true if saved
   */
  private MAttributeSetInstance saveSelection() {
    final IMutable<MAttributeSetInstance> asiRef = new Mutable<>();
    trxManager.run(
        new TrxRunnableAdapter() {
          @Override
          public void run(String localTrxName) throws Exception {
            final MAttributeSetInstance asi = saveSelection0();
            asiRef.setValue(asi);
          }
        });

    return asiRef.getValue();
  }
  public I_M_HU createHU(final I_M_HU_Item parentHUItem) {
    final IMutable<I_M_HU> huRef = new Mutable<>();
    Services.get(ITrxManager.class)
        .run(
            getContext().getTrxName(),
            new TrxRunnableAdapter() {

              @Override
              public void run(final String localTrxName) {
                final I_M_HU hu = createHU_InTrx(parentHUItem);
                huRef.setValue(hu);
              }
            });

    return huRef.getValue();
  }
  @Override
  public boolean applies(final IMRPContext mrpContext, final IMutable<String> notAppliesReason) {
    if (!mrpContext.isRequireDRP()) {
      notAppliesReason.setValue("DRP not enabled");
      return false;
    }

    // Check distribution network
    final I_PP_Product_Planning productPlanning = mrpContext.getProductPlanning();
    if (productPlanning.getDD_NetworkDistribution_ID() <= 0) {
      notAppliesReason.setValue(
          "No distribution network configured in product data planning: " + productPlanning);
      return false;
    }

    // If nothing else is preventing as, consider that we can do DRP
    return true;
  }
  private final I_M_HU createHU_InTrx(final I_M_HU_Item parentHUItem) {
    //
    // Get HUStatus to set
    final String huStatus;
    if (isHUStatusSet()) {
      huStatus = getHUStatus();
    } else if (parentHUItem != null) {
      huStatus = parentHUItem.getM_HU().getHUStatus();
    } else {
      huStatus = null;
    }

    //
    // Get M_Locator to set
    final I_M_Locator locator;
    if (_locatorSet) {
      locator = _locator;
    } else if (parentHUItem != null) {
      locator = parentHUItem.getM_HU().getM_Locator();
    } else {
      locator = null;
    }

    //
    // Create and save HU
    final I_M_HU hu = InterfaceWrapperHelper.newInstance(I_M_HU.class, getContext());
    POJOWrapper.setInstanceName(hu, _instanceName);
    hu.setM_HU_PI_Version(getM_HU_PI_Version());
    hu.setM_HU_Item_Parent(parentHUItem);
    hu.setC_BPartner(_bpartner);
    hu.setC_BPartner_Location(_bpartnerLocation);
    hu.setHUStatus(huStatus);
    hu.setM_Locator(locator);
    InterfaceWrapperHelper.save(hu);

    //
    // Create HU Attributes
    if (attributesExpectations != null) {
      attributesExpectations.createHUAttributes(hu);
    }

    //
    // Create HU Items
    if (huItemExpectations != null) {
      for (final HUItemExpectation<HUExpectation<ParentExpectationType>> huItemExpectation :
          huItemExpectations) {
        huItemExpectation.createHUItem(hu);
      }
    }

    // Capture the HU if required
    if (_huToSetRef != null) {
      _huToSetRef.setValue(hu);
    }

    return hu;
  }
  public HUExpectation<ParentExpectationType> assertExpected(
      final String message, final I_M_HU hu) {
    Assert.assertNotNull("hu not null", hu);

    final String prefix = (message == null ? "" : message) + "\nHU: " + hu + "\n" + "\nInvalid ";

    if (_huStatusSet) {
      Assert.assertEquals(prefix + "HUStatus", _huStatus, hu.getHUStatus());
    }

    if (_locatorSet) {
      assertModelEquals(prefix + "M_Locator", _locator, hu.getM_Locator());
    }

    if (_huPI != null) {
      final I_M_HU_PI_Version actual_huPIVersion = hu.getM_HU_PI_Version();
      final I_M_HU_PI actual_huPI =
          actual_huPIVersion == null ? null : actual_huPIVersion.getM_HU_PI();
      assertModelEquals(prefix + "HU PI", _huPI, actual_huPI);
    }

    if (huItemExpectations != null) {
      final List<I_M_HU_Item> huItems = handlingUnitsDAO.retrieveItems(hu);
      assertExpected(prefix + " HU Items", huItems);
    }

    if (_bpartner != null) {
      Assert.assertEquals(prefix + " C_BPartner", _bpartner, hu.getC_BPartner());
    }
    if (_bpartnerLocation != null) {
      Assert.assertEquals(
          prefix + " C_BPartner_Location", _bpartnerLocation, hu.getC_BPartner_Location());
    }

    //
    // Set capture
    if (_huToSetRef != null) {
      _huToSetRef.setValue(hu);
    }

    if (attributesExpectations != null) {
      attributesExpectations.assertExpected(prefix + " Attributes", hu);
    }

    return this;
  }
 public I_M_HU getCapturedHU() {
   Check.assumeNotNull(_huToSetRef, "Expectation {} was not configured to capture HU", this);
   return _huToSetRef.getValue();
 }