<p>Make other entries include reversing if it's need when save.</p>
@param pAddParam additional param
@param pEntity entity
@param pRequestData Request Data
@param pIsNew if entity was new
@throws Exception - an exception
11867
@Override
  public final void makeOtherEntries(final Map<String, Object> pAddParam,
    final BeginningInventory pEntity, final IRequestData pRequestData,
      final boolean pIsNew) throws Exception {
    String actionAdd = pRequestData.getParameter("actionAdd");
    if ("makeAccEntries".equals(actionAdd)
      && pEntity.getReversedId() != null) {
      //reverse none-reversed lines:
      BeginningInventoryLine bil = new BeginningInventoryLine();
      BeginningInventory reversed = new BeginningInventory();
      reversed.setItsId(pEntity.getReversedId());
      bil.setItsOwner(reversed);
      List<BeginningInventoryLine> reversedLines = getSrvOrm().
        retrieveListForField(pAddParam, bil, "itsOwner");
      String langDef = (String) pAddParam.get("langDef");
      for (BeginningInventoryLine reversedLine : reversedLines) {
        if (reversedLine.getReversedId() == null) {
          if (!reversedLine.getItsQuantity()
            .equals(reversedLine.getTheRest())) {
            throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
              "where_is_withdrawals_from_this_source");
          }
          BeginningInventoryLine reversingLine = new BeginningInventoryLine();
          reversingLine.setIdDatabaseBirth(getSrvOrm().getIdDatabase());
          reversingLine.setReversedId(reversedLine.getItsId());
          reversingLine.setWarehouseSite(reversedLine.getWarehouseSite());
          reversingLine.setInvItem(reversedLine.getInvItem());
          reversingLine.setUnitOfMeasure(reversedLine.getUnitOfMeasure());
          reversingLine.setItsCost(reversedLine.getItsCost());
          reversingLine.setItsQuantity(reversedLine.getItsQuantity()
            .negate());
          reversingLine.setItsTotal(reversedLine.getItsTotal().negate());
          reversingLine.setIsNew(true);
          reversingLine.setItsOwner(pEntity);
          reversingLine.setDescription(getSrvI18n()
            .getMsg("reversed_n", langDef) + reversedLine.getIdDatabaseBirth()
              + "-" + reversedLine.getItsId()); //local
          getSrvOrm().insertEntity(pAddParam, reversingLine);
          reversingLine.setIsNew(false);
          getSrvWarehouseEntry().load(pAddParam, reversingLine,
            reversingLine.getWarehouseSite());
          String descr;
          if (reversedLine.getDescription() == null) {
            descr = "";
          } else {
            descr = reversedLine.getDescription();
          }
          reversedLine.setDescription(descr + " " + getSrvI18n()
            .getMsg("reversing_n", langDef) + reversingLine.getIdDatabaseBirth()
              + "-" + reversingLine.getItsId());
          reversedLine.setReversedId(reversingLine.getItsId());
          reversedLine.setTheRest(BigDecimal.ZERO);
          getSrvOrm().updateEntity(pAddParam, reversedLine);
        }
      }
    }
  }
231605
@Override
  public final void makeFirstPrepareForSave(final Map<String, Object> pAddParam,
    final PaymentFrom pEntity,
      final IRequestData pRequestData) throws Exception {
    //BeigeORM refresh:
    pEntity.setAccCash(getSrvOrm()
      .retrieveEntity(pAddParam, pEntity.getAccCash()));
    if (pEntity.getAccCash().getSubaccType() != null
      && pEntity.getSubaccCashId() == null) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "select_subaccount");
    }
    if (pEntity.getSalesInvoice() != null) {
      SalesInvoice salesInvoice = getSrvOrm()
        .retrieveEntity(pAddParam, pEntity.getSalesInvoice());
      if (!salesInvoice.getHasMadeAccEntries()
        || salesInvoice.getReversedId() != null) {
        throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
          "sales_invoice_must_be_accounted");
      }
      pEntity.setSalesInvoice(salesInvoice);
    }
  }
1014201
protected final PrcBankStatementSave<RS> lazyGetPrcBankStatementSave(
    final Map<String, Object> pAddParam) throws Exception {
    @SuppressWarnings("unchecked")
    PrcBankStatementSave<RS> proc = (PrcBankStatementSave<RS>)
      this.processorsMap.get(PrcBankStatementSave.class.getSimpleName());
    if (proc == null) {
      proc = new PrcBankStatementSave<RS>();
      proc.setSrvOrm(getSrvOrm());
      proc.setCsvReader(getCsvReader());
      //assigning fully initialized object:
      this.processorsMap.put(PrcBankStatementSave.class.getSimpleName(), proc);
    }
    return proc;
  }
482603
public final <T> T retrieveEntity(final Map<String, Object> pAddParam,
    final Class<T> pEntityClass,
      final IRecordSet<RS> pRecordSet) throws Exception {
    @SuppressWarnings("unchecked")
    IFactorySimple<T> facEn = (IFactorySimple<T>) this.entitiesFactoriesFatory
      .lazyGet(pAddParam, pEntityClass);
    T entity = facEn.create(pAddParam);
    String foreignFieldNmsNm = "foreignFieldNms";
    List<String> foreignFieldNms = new ArrayList<String>();
    pAddParam.put(foreignFieldNmsNm, foreignFieldNms);
    String currentLevelNm = "currentLevel";
    List<Integer> currentLevel = new ArrayList<Integer>();
    currentLevel.add(1);
    pAddParam.put(currentLevelNm, currentLevel);
    String deepLevelNm = "deepLevel";
    List<Integer> deepLevel = new ArrayList<Integer>();
    deepLevel.add(2); //default top level
    pAddParam.put(deepLevelNm, deepLevel);
    this.fillerEntitiesFromRs.fill(pAddParam, entity, pRecordSet);
    //(TODO itself) exit from all recursions:
    pAddParam.remove(currentLevelNm);
    pAddParam.remove(deepLevelNm);
    pAddParam.remove(foreignFieldNmsNm);
    return entity;
  }
632671
@Override
  public final <T> T retrieveEntity(final Map<String, Object> pAddParam,
    final Class<T> pEntityClass, final String pQuery) throws Exception {
    T entity = null;
    IRecordSet<RS> recordSet = null;
    try {
      recordSet = getSrvDatabase().retrieveRecords(pQuery);
      if (recordSet.moveToFirst()) {
        entity = retrieveEntity(pAddParam, pEntityClass, recordSet);
      }
    } finally {
      if (recordSet != null) {
        recordSet.close();
      }
    }
    return entity;
  }
945625
@Override
  public final void withdrawal(final Map<String, Object> pAddParam,
    final IMakingWarehouseEntry pEntity, final Date pDateAccount,
      final Long pDrawingOwnerId) throws Exception {
    if (!pEntity.getIdDatabaseBirth()
      .equals(getSrvOrm().getIdDatabase())) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "can_not_make_di_entry_for_foreign_src");
    }
    String queryMain = lazyGetQuery(srvAccSettings
      .lazyGetAccSettings(pAddParam).getCogsMethod().getFileName());
    StringBuffer sb = new StringBuffer();
    int i = 0;
    for (ADrawItemSourcesLine drawItemSourceLine
      : getDrawItemSources(pAddParam)) {
      if (drawItemSourceLine.getIsUsed()) {
        String query = lazyGetQuery(drawItemSourceLine.getFileName());
        query = query.replace(":IDDATABASEBIRTH", String.valueOf(getSrvOrm()
          .getIdDatabase()));
        query = query.replace(":INVITEM", pEntity.getInvItem()
          .getItsId().toString());
        query = query.replace(":UNITOFMEASURE", pEntity.getUnitOfMeasure()
          .getItsId().toString());
        if (i++ > 0) {
          sb.append("\nunion all\n\n");
        }
        sb.append(query);
      }
    }
    String sourceAll = sb.toString();
    if (sourceAll.trim().length() == 0) {
      throw new ExceptionWithCode(ExceptionWithCode.CONFIGURATION_MISTAKE,
        "there_is_no_draw_material_rules");
    }
    //ADrawItemEntry just holder source id and type
    List<T> sources =
      new ArrayList<T>();
    queryMain = queryMain.replace(":SOURCEALL",
      sourceAll);
    BigDecimal quantityInSources = BigDecimal.ZERO;
    IRecordSet<RS> recordSet = null;
    try {
      recordSet = getSrvDatabase().retrieveRecords(queryMain);
      if (recordSet.moveToFirst()) {
        do {
          Long sourceId = recordSet.getLong("SOURCEID");
          Integer sourceType = recordSet.getInteger("SOURCETYPE");
          Long sourceOwnerId = recordSet.getLong("SOURCEOWNERID");
          Integer sourceOwnerType = recordSet.getInteger("SOURCEOWNERTYPE");
          BigDecimal theRest = BigDecimal
              .valueOf(recordSet.getDouble("THEREST"));
          T source = createDrawItemEntry(pAddParam);
          source.setSourceId(sourceId);
          source.setSourceType(sourceType);
          source.setSourceOwnerId(sourceOwnerId);
          source.setSourceOwnerType(sourceOwnerType);
          sources.add(source);
          quantityInSources = quantityInSources.add(theRest);
          if (quantityInSources.compareTo(pEntity.getItsQuantity()) >= 0) {
            break;
          }
        } while (recordSet.moveToNext());
      }
    } finally {
      if (recordSet != null) {
        recordSet.close();
      }
    }
    if (quantityInSources.compareTo(pEntity.getItsQuantity()) < 0) {
      throw new ExceptionWithCode(PurchaseInvoice.THERE_IS_NO_GOODS,
        "there_is_no_goods_in_stock");
    }
    BigDecimal quantityToDrawRest = pEntity.getItsQuantity();
    for (T source : sources) {
      @SuppressWarnings("unchecked")
      IFactorySimple<IDrawItemSource> fctDis =
        (IFactorySimple<IDrawItemSource>) this.entitiesFactoriesFatory
         .lazyGet(pAddParam, srvTypeCode.getTypeCodeMap()
          .get(source.getSourceType()));
      IDrawItemSource drawed = fctDis.create(pAddParam);
      String fldIdName = this.srvOrm.getTablesMap()
        .get(drawed.getClass().getSimpleName()).getIdFieldName();
      Method setterId = this.settersRapiHolder
        .getFor(drawed.getClass(), fldIdName);
      setterId.invoke(drawed, source.getSourceId());
      drawed = srvOrm.retrieveEntity(pAddParam, drawed);
      BigDecimal quantityToDraw;
      if (quantityToDrawRest.compareTo(drawed.getTheRest()) < 0) {
        quantityToDraw = quantityToDrawRest;
      } else {
        quantityToDraw = drawed.getTheRest();
      }
      withdrawalFrom(pAddParam, pEntity, drawed, quantityToDraw);
      quantityToDrawRest = quantityToDrawRest.subtract(quantityToDraw);
      if (quantityToDrawRest.doubleValue() == 0) {
        break;
      }
    }
  }
496750
@Override
  public final void makeOtherEntries(final Map<String, Object> pAddParam,
    final PrepaymentFrom pEntity, final IRequestData pRequestData,
      final boolean pIsNew) throws Exception {
    // nothing
  }
17154
@Override
  public final void makeFirstPrepareForSave(final Map<String, Object> pReqVars,
    final PurchaseInvoice pEntity,
      final IRequestData pRequestData) throws Exception {
    if (pEntity.getPrepaymentTo() != null) {
      pEntity.setPrepaymentTo(getSrvOrm()
        .retrieveEntity(pReqVars, pEntity.getPrepaymentTo()));
      if (pEntity.getReversedId() == null && pEntity.getPrepaymentTo()
        .getPurchaseInvoiceId() != null && !pEntity.getHasMadeAccEntries()) {
        throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
          "prepayment_already_in_use");
      }
      if (pEntity.getReversedId() == null && !pEntity.getPrepaymentTo()
        .getVendor().getItsId().equals(pEntity.getVendor().getItsId())) {
        throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
          "prepayment_for_different_vendor");
      }
    }
    if (pEntity.getReversedId() != null && pEntity.getPrepaymentTo() != null
      && pEntity.getPaymentTotal().compareTo(pEntity.getPrepaymentTo()
        .getItsTotal()) != 0) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "reverse_payments_first");
    }
    if (pEntity.getReversedId() != null && pEntity.getPrepaymentTo() == null
      && pEntity.getPaymentTotal().compareTo(BigDecimal.ZERO) != 0) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "reverse_payments_first");
    }
    if (pEntity.getReversedId() == null) {
      calculateTotalPayment(pReqVars, pEntity);
    }
    if (!pEntity.getIsNew()) {
      pReqVars.put("DebtorCreditortaxDestinationdeepLevel", 2);
      Set<String> ndFlDc = new HashSet<String>();
      ndFlDc.add("itsId");
      ndFlDc.add("isForeigner");
      ndFlDc.add("taxDestination");
      pReqVars.put("DebtorCreditorneededFields", ndFlDc);
    }
  }
958817
@Override
  public final <T> void fill(final Map<String, Object> pAddParam,
    final T pEntity, final IRecordSet<RS> pSource) throws Exception {
    @SuppressWarnings("unchecked")
    IFillerObjectFields<T> filler = (IFillerObjectFields<T>)
      fillersFieldsFactory.lazyGet(pAddParam, pEntity.getClass());
    TableSql tableSql = this.tablesMap.get(pEntity.getClass().getSimpleName());
    @SuppressWarnings("unchecked")
    Collection<String> fieldsNames = (Collection<String>) pAddParam.
      get(pEntity.getClass().getSimpleName() + "neededFields");
    if (fieldsNames == null) {
      fieldsNames = tableSql.getFieldsMap().keySet();
    }
    boolean isDbgSh = this.logger.getDbgSh(this.getClass())
      && this.logger.getDbgFl() < 7031 && this.logger.getDbgCl() > 7033;
    String currentLevelNm = "currentLevel";
    @SuppressWarnings("unchecked")
    List<Integer> currentLevel = (List<Integer>) pAddParam.get(currentLevelNm);
    String deepLevelNm = "deepLevel";
    @SuppressWarnings("unchecked")
    List<Integer> deepLevel = (List<Integer>) pAddParam.get(deepLevelNm);
    @SuppressWarnings("unchecked")
    List<String> foreignFieldNms = (List<String>)
      pAddParam.get("foreignFieldNms");
    for (String fieldName : fieldsNames) {
      if (!tableSql.getFieldsMap().get(fieldName).getTypeField()
            .equals(ETypeField.DERIVED_FROM_COMPOSITE)) {
        String convName = this.fieldConverterNamesHolder.getFor(pEntity
         .getClass(), fieldName);
        IConverterByName<IRecordSet<RS>, ?> conv = this.convertersFieldsFatory
          .lazyGet(pAddParam, convName);
        if (conv != null) { // e.g. transient or composite (non-foreign) itsId
          try {
            Field field = this.fieldsRapiHolder
              .getFor(pEntity.getClass(), fieldName);
            String parName;
            String tableAlias;
            if (currentLevel.get(0) > 1) { // in TOP level
              String currForeignFieldNm = foreignFieldNms
                .get(foreignFieldNms.size() - 1);
              tableAlias = currForeignFieldNm.toUpperCase();
            } else {
              tableAlias = "";
            }
            if (Enum.class.isAssignableFrom(field.getType())) {
              parName = tableAlias + fieldName.toUpperCase();
              pAddParam.put("fieldClass", field.getType());
            } else {
              if (tableSql.getFieldsMap().get(fieldName)
                .getForeignEntity() != null) {
                Integer deepLevelReq = (Integer) pAddParam.get(pEntity
                  .getClass().getSimpleName() + fieldName + deepLevelNm);
                if (deepLevelReq != null) {
                  //sub-entities with overridden deep:
                  currentLevel.add(1);
                  deepLevel.add(deepLevelReq);
                } //nothing for sub-entities
                if (deepLevel.size() != currentLevel.size()) {
                  throw new Exception(
                "Algorithm error: deepLevel.size/currentLevel.size/Entity/Field"
                  + deepLevel.size() + "/" + currentLevel.size() + "/"
                    + pEntity.getClass().getSimpleName() + "/" + fieldName);
                }
                parName = fieldName;
                 //table current alias for name
                foreignFieldNms.add(fieldName);
                // foreign entity class for factory:
                pAddParam.put("fieldClass", field.getType());
                pAddParam.put("entityClass", pEntity.getClass());
                if (isDbgSh) {
                  StringBuffer sb = new StringBuffer();
                  sb.append("deepLevel size/value1/value...: " + deepLevel
                    .size() + "/");
                  for (Integer dl : deepLevel) {
                    sb.append(dl + "/");
                  }
                  sb.append("\ncurrentLevel size/value1/value...: "
                    + currentLevel.size() + "/");
                  for (Integer cl : currentLevel) {
                    sb.append(cl + "/");
                  }
                  sb.append("\nforeignFieldNms size/value1/value...: "
                    + foreignFieldNms.size() + "/");
                  for (String fln : foreignFieldNms) {
                    sb.append(fln + "/");
                  }
                  sb.append("\nForeign class/fieldNm/type: "
                    + pEntity.getClass().getSimpleName() + "/" + fieldName + "/"
                     + field.getType().getSimpleName());
                  this.logger.debug(null, FillerEntitiesFromRs.class,
                    sb.toString());
                }
              } else {
                parName = tableAlias + fieldName.toUpperCase();
              }
            }
            Object fieldVal = conv.convert(pAddParam, pSource, parName);
            if (isDbgSh) {
              this.logger.debug(null, FillerEntitiesFromRs.class,
                "Converted from RS class/field/type/converter/value: "
                  + pEntity.getClass().getSimpleName() + "/" + fieldName + "/"
                    + field.getType().getSimpleName() + "/"
                      + conv.getClass().getSimpleName() + "/" + fieldVal);
            }
            filler.fill(pAddParam, pEntity, fieldVal, fieldName);
          } catch (Exception ex) {
            String msg = "Can't fill field/class: " + fieldName + "/"
              + pEntity.getClass().getCanonicalName();
            throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
              msg, ex);
          }
        }
      }
    }
    pAddParam.remove("fieldClass");
    pAddParam.remove("entityClass");
  }
1196461
protected final PrcRevealTaxCat<RS>
    createPutPrcRevealTaxCat(
      final Map<String, Object> pAddParam) throws Exception {
    PrcRevealTaxCat<RS> proc = new PrcRevealTaxCat<RS>();
    proc.setLogger(getLogger());
    proc.setSrvDatabase(getSrvDatabase());
    //assigning fully initialized object:
    this.processorsMap
      .put(PrcRevealTaxCat.class.getSimpleName(), proc);
    return proc;
  }
1118597
@Override
  public final void makeFirstPrepareForSave(final Map<String, Object> pAddParam,
    final PrepaymentFrom pEntity,
      final IRequestData pRequestData) throws Exception {
    //BeigeORM refresh:
    pEntity.setAccCash(getSrvOrm()
      .retrieveEntity(pAddParam, pEntity.getAccCash()));
    if (pEntity.getAccCash().getSubaccType() != null
      && pEntity.getSubaccCashId() == null) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "select_subaccount");
    }
  }
751451
@Override
  public final AItemSpecifics<T, ID> process(
    final Map<String, Object> pReqVars,
      final AItemSpecifics<T, ID> pEntity,
        final IRequestData pRequestData) throws Exception {
    //Beige-ORM refresh:
    pEntity.setSpecifics(getSrvOrm().retrieveEntity(pReqVars,
      pEntity.getSpecifics()));
    if (pEntity.getSpecifics().getChooseableSpecificsType() != null) {
      pEntity.setLongValue2(pEntity.getSpecifics().getChooseableSpecificsType()
        .getItsId());
      pEntity.setStringValue2(pEntity.getSpecifics()
        .getChooseableSpecificsType().getItsName());
    }
    if (pEntity.getIsNew()) {
      //only complex ID
      getSrvOrm().insertEntity(pReqVars, pEntity);
    } else {
      getSrvOrm().updateEntity(pReqVars, pEntity);
    }
    return pEntity;
  }
365339
@Override
  public final void process(final Map<String, Object> pAddParam,
    final IRequestData pRequestData) throws Exception {
    for (ILstnCatalogChanged lstn : this.listeners) {
      lstn.onCatalogChanged();
    }
  }
642413
@Override
  public final void fill(final Map<String, Object> pAddParam,
    final Object pEntity, final String pFieldName,
      final String pFieldStrValue) throws Exception {
    if (SeService.class != pEntity.getClass()) {
      throw new ExceptionWithCode(ExceptionWithCode
        .CONFIGURATION_MISTAKE, "It's wrong service to fill that field: "
          + pEntity + "/" + pFieldName + "/" + pFieldStrValue);
    }
    SeService seService = (SeService) pEntity;
    if ("NULL".equals(pFieldStrValue)) {
      seService.setSeller(null);
      return;
    }
    try {
      SeSeller ownedEntity = new SeSeller();
      DebtorCreditor dk = new DebtorCreditor();
      dk.setItsId(Long.parseLong(pFieldStrValue));
      ownedEntity.setSeller(dk);
      seService.setSeller(ownedEntity);
    } catch (Exception ex) {
      throw new ExceptionWithCode(ExceptionWithCode
        .WRONG_PARAMETER, "Can not fill field: " + pEntity + "/" + pFieldName
          + "/" + pFieldStrValue + ", " + ex.getMessage(), ex);
    }
  }
1189334
@Override
  public final T fromString(final Map<String, Object> pAddParam,
    final String pStrVal) throws Exception {
    if (pStrVal == null || "".equals(pStrVal)) {
      return null;
    }
    T object = this.objectClass.newInstance();
    for (String fldValStr : pStrVal.split(",")) {
      int eqIdx = fldValStr.indexOf("=");
      String fldName = fldValStr.substring(0, eqIdx);
      String cnvrtName = this.fieldConverterNamesHolder.getFor(this.objectClass,
        fldName);
      if (cnvrtName != null) { // e.g. transient field
        IConverterToFromString cnvrt = this.fieldsConvertersFatory
          .lazyGet(null, cnvrtName);
        Method fldSetter = this.settersRapiHolder
          .getFor(this.objectClass, fldName);
        String strVal = fldValStr.substring(eqIdx + 1);
        Object fldVal = cnvrt.fromString(pAddParam, strVal);
        fldSetter.invoke(object, fldVal);
      }
    }
    return object;
  }
164847
@Override
  public final M create(final Map<String, Object> pAddParam) throws Exception {
    M object = this.objectClass.newInstance();
    return object;
  }
779159
public final void updateOwner(final Map<String, Object> pAddParam,
    final AdditionCostLine pEntity) throws Exception {
    String query =
    "select sum(ITSTOTAL) as ITSTOTAL from"
      + " ADDITIONCOSTLINE where ITSOWNER="
        + pEntity.getItsOwner().getItsId();
    Double total = getSrvDatabase().evalDoubleResult(query, "ITSTOTAL");
    pEntity.getItsOwner().setTotalAdditionCost(BigDecimal.valueOf(total)
.setScale(getSrvAccSettings().lazyGetAccSettings(pAddParam).getCostPrecision(),
  getSrvAccSettings().lazyGetAccSettings(pAddParam).getRoundingMode()));
    pEntity.getItsOwner().setItsTotal(pEntity.getItsOwner()
      .getTotalMaterialsCost().add(pEntity.getItsOwner()
        .getTotalAdditionCost()).setScale(getSrvAccSettings()
      .lazyGetAccSettings(pAddParam).getCostPrecision(),
    getSrvAccSettings().lazyGetAccSettings(pAddParam).getRoundingMode()));
    pEntity.getItsOwner().setItsCost(pEntity.getItsOwner().getItsTotal()
      .divide(pEntity.getItsOwner().getItsQuantity(),
        getSrvAccSettings().lazyGetAccSettings(pAddParam).getCostPrecision(),
          getSrvAccSettings().lazyGetAccSettings(pAddParam).getRoundingMode()));
    getSrvOrm().updateEntity(pAddParam, pEntity.getItsOwner());
  }