/**
   * Loads the ASI template to be used.
   *
   * @param fromAttributeSetInstanceId
   * @return
   *     <ul>
   *       <li>ASI template
   *       <li><code>null</code> if this is not a valid settings and we need to dispose the dialog
   *     </ul>
   *
   * @throws AdempiereException if something failed
   */
  private final MAttributeSetInstance loadASITemplate(final int fromAttributeSetInstanceId) {
    final Properties ctx = getCtx();
    final int productId = getM_Product_ID();
    final boolean isPureProductASI = isPureProductASI();

    //
    // If there is not product specified
    // and we need a pure product ASI (i.e. not the ASI that we configure on product level)
    // => this dialog does not make sense and we need to dispose it ASAP
    // TODO: in future we shall do this checking BEFORE we reach this point
    if (productId <= 0 && isPureProductASI) {
      return null;
    }

    final MAttributeSetInstance asiTemplate;

    //
    // Load/Create the ASI
    // Get the M_AttributeSet.
    MAttributeSet as = null;
    if (productId > 0) {
      // Get/Create the ASI
      asiTemplate = MAttributeSetInstance.get(ctx, fromAttributeSetInstanceId, productId);
      if (asiTemplate == null) {
        throw new AdempiereException(
            "@NotFound@ @M_AttributeSetInstance_ID@ (@M_Product_ID@=" + productId + ")");
      }
      Env.setContext(ctx, m_WindowNo, "M_AttributeSet_ID", asiTemplate.getM_AttributeSet_ID());

      // Get Attribute Set
      as = asiTemplate.getMAttributeSet();
    } else {
      final int M_AttributeSet_ID = attributeContext.getM_AttributeSet_ID();
      asiTemplate =
          new MAttributeSetInstance(ctx, 0, M_AttributeSet_ID, ITrx.TRXNAME_None); // new ASI
      as = asiTemplate.getMAttributeSet();
      if (as == null && M_AttributeSet_ID == 0) {
        // FIXME: workaround to deal with M_AttributeSet_ID=0 which is an existing record
        as =
            queryBL
                .createQueryBuilder(I_M_AttributeSet.class)
                .setContext(ctx, ITrx.TRXNAME_None)
                .addEqualsFilter(I_M_AttributeSet.COLUMNNAME_M_AttributeSet_ID, 0)
                .create()
                .firstOnlyNotNull(MAttributeSet.class);
        asiTemplate.setMAttributeSet(as);
      }
    }
    // Product has no Attribute Set
    if (as == null) {
      throw new AdempiereException("@PAttributeNoAttributeSet@");
    }
    // Product has no Instance Attributes
    if (isPureProductASI && !as.isInstanceAttribute()) {
      throw new AdempiereException("@PAttributeNoInstanceAttribute@");
    }

    return asiTemplate;
  }