<p>Insert entity into DB.
For autogenerated ID fill it in the entity</p>
@param <T> entity type
@param pAddParam additional param
@param pEntity entity
@throws Exception - an exception
343323
@Override
  public final <T> void insertEntity(final Map<String, Object> pAddParam,
    final T pEntity) throws Exception {
    ColumnsValues columnsValues = evalColumnsValues(pAddParam, pEntity);
    long result = getSrvDatabase().executeInsert(pEntity.getClass()
      .getSimpleName().toUpperCase(), columnsValues);
    if (result != 1) {
      String query = getHlpInsertUpdate().evalSqlInsert(pEntity.getClass()
        .getSimpleName().toUpperCase(), columnsValues);
      throw new ExceptionWithCode(ISrvDatabase.ERROR_INSERT_UPDATE,
        "It should be 1 row inserted but it is "
          + result + ", query:\n" + query + ";\n");
    }
    String[] idName = columnsValues.getIdColumnsNames();
    if (idName.length == 1) { // if non-composite PK
      Field fieldId = getUtlReflection()
        .retrieveField(pEntity.getClass(), idName[0]);
      fieldId.setAccessible(true);
      Object idValue = fieldId.get(pEntity);
      if (idValue == null) {
        //It must be an autogenerated Integer or Long ID
        IRecordSet<RS> recordSet = null;
        try {
          String insertedIdName = "INSERTEDID";
          recordSet = getSrvDatabase().retrieveRecords(
            "select SCOPE_IDENTITY() as " + insertedIdName + ";");
          recordSet.moveToFirst();
          if (fieldId.getType() == Long.class) {
            fieldId.set(pEntity, recordSet.getLong(insertedIdName));
          } else if (fieldId.getType() == Integer.class) {
            fieldId.set(pEntity, recordSet.getInteger(insertedIdName));
          } else {
            String msg = "There is no rule to fill ID "
              + fieldId.getName().toUpperCase()
                + " of " + fieldId.getType() + " in "
                  + pEntity;
            throw new ExceptionWithCode(ExceptionWithCode.NOT_YET_IMPLEMENTED,
              msg);
          }
        } finally {
          if (recordSet != null) {
            recordSet.close();
          }
        }
      }
    }
  }
467462
@Override
  public final SeGoodsSpecifics process(final Map<String, Object> pReqVars,
    final SeGoodsSpecifics pEntity,
      final IRequestData pRequestData) throws Exception {
    SeSeller ses = findSeSeller.find(pReqVars, pRequestData.getUserName());
    pEntity.setSeller(ses);
    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()) {
      this.srvOrm.insertEntity(pReqVars, pEntity);
      pEntity.setIsNew(false);
    } else {
      SeGoodsSpecifics entOld = this.srvOrm.retrieveEntity(pReqVars, pEntity);
      if (!entOld.getSeller().getItsId().getItsId()
        .equals(pEntity.getSeller().getItsId().getItsId())) {
        throw new ExceptionWithCode(ExceptionWithCode.FORBIDDEN,
        "Attempt to update smb. else's entity: user/entity/EID/SEOLDID/SEID - "
          + pRequestData.getUserName() + "/" + pEntity.getClass()
           .getSimpleName() + "/" + pEntity.getItsId() + "/" + entOld
            .getSeller().getItsId().getItsId() + "/" + pEntity.getSeller()
              .getItsId().getItsId());
      }
      this.srvOrm.updateEntity(pReqVars, pEntity);
    }
    return pEntity;
  }
1241795
@Override
  public final synchronized void saveAccSettings(
    final Map<String, Object> pAddParam,
      final AccSettings pEntity) throws Exception {
    if (pEntity.getIsNew()) {
      throw new ExceptionWithCode(ExceptionWithCode.FORBIDDEN,
        "Attempt to insert accounting settings by " + pAddParam.get("user"));
    } else {
      if (pEntity.getCostPrecision() < 0 || pEntity.getCostPrecision() > 4) {
        throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
          "precision_must_be_from_0_to_4");
      }
      if (pEntity.getPricePrecision() < 0 || pEntity.getPricePrecision() > 4) {
        throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
          "precision_must_be_from_0_to_4");
      }
      if (pEntity.getQuantityPrecision() < 0
        || pEntity.getQuantityPrecision() > 4) {
        throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
          "precision_must_be_from_0_to_4");
      }
      if (pEntity.getBalancePrecision() < 0
        || pEntity.getBalancePrecision() > 4) {
        throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
          "precision_must_be_from_0_to_4");
      }
      getSrvOrm().updateEntity(pAddParam, pEntity);
      retrieveAccSettings(pAddParam);
    }
  }
656418
protected final PrcAccEntityPbWithSubaccEditDelete<RS, IPersistableBase>
    createPutPrcAccEntityPbWithSubaccEditDelete(
      final Map<String, Object> pAddParam) throws Exception {
    PrcAccEntityPbWithSubaccEditDelete<RS, IPersistableBase> proc =
      new PrcAccEntityPbWithSubaccEditDelete<RS, IPersistableBase>();
    @SuppressWarnings("unchecked")
    PrcEntityPbEditDelete<RS, IPersistableBase> procDlg =
      (PrcEntityPbEditDelete<RS, IPersistableBase>) this.fctBnEntitiesProcessors
        .lazyGet(pAddParam, PrcEntityPbEditDelete.class.getSimpleName());
    proc.setPrcAccEntityPbEditDelete(procDlg);
    proc.setSrvTypeCode(getSrvTypeCode());
    //assigning fully initialized object:
    this.processorsMap
      .put(PrcAccEntityPbWithSubaccEditDelete.class.getSimpleName(), proc);
    return proc;
  }
181964
protected final PrcAccEntityWithSubaccCopy<RS, IHasId<Object>, Object>
    createPutPrcAccEntityWithSubaccCopy(
      final Map<String, Object> pAddParam) throws Exception {
    PrcAccEntityWithSubaccCopy<RS, IHasId<Object>, Object> proc =
      new PrcAccEntityWithSubaccCopy<RS, IHasId<Object>, Object>();
    @SuppressWarnings("unchecked")
    PrcEntityCopy<RS, IHasId<Object>, Object> procDlg =
      (PrcEntityCopy<RS, IHasId<Object>, Object>) this.fctBnEntitiesProcessors
        .lazyGet(pAddParam, PrcEntityCopy.class.getSimpleName());
    proc.setPrcAccEntityCopy(procDlg);
    proc.setSrvTypeCode(getSrvTypeCode());
    //assigning fully initialized object:
    this.processorsMap
      .put(PrcAccEntityWithSubaccCopy.class.getSimpleName(), proc);
    return proc;
  }
828773
@Override
  public final synchronized void saveTradingSettings(
    final Map<String, Object> pAddParam,
      final TradingSettings pEntity) throws Exception {
    if (pEntity.getIsNew()) {
      throw new ExceptionWithCode(ExceptionWithCode.FORBIDDEN,
        "insert_not_allowed::" + pAddParam.get("user"));
    } else if (pEntity.getItsId() != 1L) {
      throw new ExceptionWithCode(ExceptionWithCode.FORBIDDEN,
        "id_not_allowed::" + pAddParam.get("user"));
    } else {
      getSrvOrm().updateEntity(pAddParam, pEntity);
      this.tradingSettings = null;
      lazyGetTradingSettings(pAddParam);
    }
  }
630714
@Override
  public final boolean sync(final Map<String, Object> pAddParam,
    final Object pEntity) throws Exception {
    AccountingEntry entityPb = (AccountingEntry) pEntity;
    int currDbId = getSrvOrm().getIdDatabase();
    if (currDbId == entityPb.getIdDatabaseBirth()) {
      throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
        "Foreign entity born in this database! {ID, ID BIRTH, DB BIRTH}:"
          + " {" + entityPb.getItsId() + ", " + entityPb.getIdBirth()
            + "," + entityPb.getIdDatabaseBirth());
    }
    String tblNm = pEntity.getClass().getSimpleName().toUpperCase();
    String whereStr = " where " + tblNm + ".IDBIRTH=" + entityPb.getItsId()
      + " and " + tblNm + ".IDDATABASEBIRTH=" + entityPb.getIdDatabaseBirth();
    AccountingEntry entityPbDb = getSrvOrm()
      .retrieveEntityWithConditions(pAddParam, entityPb.getClass(), whereStr);
    if (entityPb.getSourceType().equals(this.accountingEntriesCode)) {
      tblNm = AccountingEntries.class.getSimpleName().toUpperCase();
      whereStr = " where " + tblNm + ".IDBIRTH=" + entityPb.getSourceId()
      + " and " + tblNm + ".IDDATABASEBIRTH=" + entityPb.getIdDatabaseBirth();
      AccountingEntries accountingEntries = getSrvOrm()
      .retrieveEntityWithConditions(pAddParam, AccountingEntries.class,
        whereStr);
      if (accountingEntries == null) {
        throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
          "Can't find foreign AccountingEntries {ID BIRTH, DB BIRTH}:"
            + " {" + entityPb.getSourceId()
              + "," + entityPb.getIdDatabaseBirth());
      }
      entityPb.setSourceId(accountingEntries.getItsId());
    }
    entityPb.setIdBirth(entityPb.getItsId());
    entityPb.setItsId(null);
    boolean isNew = true;
    if (entityPbDb != null) {
      entityPb.setItsId(entityPbDb.getItsId());
      isNew = false;
    }
    getSrvBalance().handleNewAccountEntry(pAddParam, null, null,
      entityPb.getItsDate()); //This is for SrvBalanceStd only!!!
    return isNew;
  }
587222
protected final PrcPageWithSubaccTypes<RS>
    createPutPrcPageWithSubaccTypes(
      final Map<String, Object> pAddParam) throws Exception {
    PrcPageWithSubaccTypes<RS> proc = new PrcPageWithSubaccTypes<RS>();
    PrcEntitiesPage procDlg = (PrcEntitiesPage) this.fctBnProcessors
      .lazyGet(pAddParam, PrcEntitiesPage.class.getSimpleName());
    proc.setPrcAccEntitiesPage(procDlg);
    proc.setSrvTypeCode(getSrvTypeCode());
    //assigning fully initialized object:
    this.processorsMap
      .put(PrcPageWithSubaccTypes.class.getSimpleName(), proc);
    return proc;
  }
356083
@Override
  protected void insertEntity(DbEntityOperation operation) {

    final DbEntity dbEntity = operation.getEntity();

    // get statement
    String insertStatement = dbSqlSessionFactory.getInsertStatement(dbEntity);
    insertStatement = dbSqlSessionFactory.mapStatement(insertStatement);
    ensureNotNull("no insert statement for " + dbEntity.getClass() + " in the ibatis mapping files", "insertStatement", insertStatement);

    // execute the insert
    executeInsertEntity(insertStatement, dbEntity);

    // perform post insert actions on entity
    entityInserted(dbEntity);
  }
839398
protected final PrcEntityFfolDelete<RS, IHasId<Object>, Object>
    createPutPrcEntityFfolDelete(
      final Map<String, Object> pAddParam) throws Exception {
    @SuppressWarnings("unchecked")
    PrcEntityFfolDelete<RS, IHasId<Object>, Object> proc =
      (PrcEntityFfolDelete<RS, IHasId<Object>, Object>)
        this.fctBnEntitiesProcessors
          .lazyGet(pAddParam, PrcEntityFfolDelete.class.getSimpleName());
    //assigning fully initialized object:
    this.processorsMap.put(PrcEntityFfolDelete.class.getSimpleName(), proc);
    return proc;
  }
405128
@Override
  public final void addCheckIsReadyToAccount(
    final Map<String, Object> pAddParam,
      final ManufacturingProcess pEntity,
        final IRequestData pRequestData) throws Exception {
    if (!pEntity.getIsComplete()) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "manufacturing_must_be_completed");
    }
  }
727359
public List<Object> addForGeneratedKeys(Entity entity) throws SQLException {
		return db.insertForGeneratedKeys(fixEntity(entity));
	}
877381
public void insertAttributeValueEntity(String attributeName, CmsEntityWrapper value, int index) {

        m_entity.insertAttributeValue(attributeName, value.getEntity(), index);
    }
461179
@Override
  public final void makeFirstPrepareForSave(final Map<String, Object> pAddParam,
    final Manufacture pEntity,
      final IRequestData pRequestData) throws Exception {
    // Beige-ORM refresh:
    pEntity.setManufacturingProcess(getSrvOrm()
      .retrieveEntity(pAddParam, pEntity.getManufacturingProcess()));
    pEntity.setInvItem(getSrvOrm()
      .retrieveEntity(pAddParam, pEntity.getInvItem()));
    if (!(InvItem.FINISHED_PRODUCT_ID.equals(pEntity.getInvItem()
      .getItsType().getItsId()) || InvItem.MATERIAL_ID
        .equals(pEntity.getInvItem().getItsType().getItsId()))) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "type_must_be_product_material");
    }
    if (pEntity.getItsQuantity().compareTo(pEntity.getManufacturingProcess()
      .getItsQuantity()) > 0) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "source_has_no_enough_item");
    }
    if (!pEntity.getUnitOfMeasure().getItsId().equals(pEntity
      .getManufacturingProcess().getUnitOfMeasure().getItsId())) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "UnitOfMeasure_fiffer_with_source");
    }
    if (pEntity.getItsQuantity().doubleValue() < 0
      && pEntity.getReversedId() == null) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "reversed_manufacture_is_null");
    }
    pEntity.setItsQuantity(pEntity.getItsQuantity().setScale(
      getSrvAccSettings().lazyGetAccSettings(pAddParam).getQuantityPrecision(),
        getSrvAccSettings().lazyGetAccSettings(pAddParam).getRoundingMode()));
    if (pEntity.getReversedId() != null) {
      Manufacture reversed = getSrvOrm().retrieveEntityById(pAddParam,
        Manufacture.class, pEntity.getReversedId());
      if (reversed.getReversedId() != null) {
        throw new ExceptionWithCode(ExceptionWithCode.FORBIDDEN,
          "Attempt to double reverse" + pAddParam.get("user"));
      }
      if (!reversed.getItsQuantity().equals(reversed.getTheRest())) {
        throw new ExceptionWithCode(ExceptionWithCode
          .WRONG_PARAMETER, "where_is_withdrawals_from_this_source");
      }
      reversed.setTheRest(BigDecimal.ZERO);
      getSrvOrm().updateEntity(pAddParam, reversed);
      pEntity.setTheRest(BigDecimal.ZERO);
    } else {
      pEntity.setTheRest(pEntity.getItsQuantity());
    }
    pEntity.setItsCost(pEntity.getManufacturingProcess().getItsCost());
    if (pEntity.getItsQuantity()
      .compareTo(pEntity.getManufacturingProcess().getItsQuantity()) == 0) {
      //to reduce rounding error:
      pEntity.setItsTotal(pEntity.getManufacturingProcess().getItsTotal());
    } else {
      pEntity.setItsTotal(pEntity.getItsCost()
        .multiply(pEntity.getItsQuantity()).setScale(getSrvAccSettings()
          .lazyGetAccSettings(pAddParam).getCostPrecision(), getSrvAccSettings()
            .lazyGetAccSettings(pAddParam).getRoundingMode()));
    }
  }
380375
public final void updateOwner(final Map<String, Object> pAddParam,
    final WageLine pEntity) throws Exception {
    String query =
    "select sum(GROSSWAGE) as GROSSWAGE from " + WageLine.class
      .getSimpleName().toUpperCase() + " where ITSOWNER="
        + pEntity.getItsOwner().getItsId();
    Double total = getSrvDatabase().evalDoubleResult(query, "GROSSWAGE");
    if (total == null) {
      total = 0d;
    }
    pEntity.getItsOwner().setItsTotal(BigDecimal.valueOf(total).setScale(
      getSrvAccSettings().lazyGetAccSettings(pAddParam).getPricePrecision(),
        getSrvAccSettings().lazyGetAccSettings(pAddParam).getRoundingMode()));
    getSrvOrm().updateEntity(pAddParam, pEntity.getItsOwner());
  }
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");
    }
  }
1051673
@Override
  public final synchronized void saveSettingsAdd(
    final Map<String, Object> pAddParam,
      final SettingsAdd pEntity) throws Exception {
    if (pEntity.getIsNew()) {
      throw new ExceptionWithCode(ExceptionWithCode.FORBIDDEN,
        "insert_not_allowed::" + pAddParam.get("user"));
    } else if (pEntity.getItsId() != 1L) {
      throw new ExceptionWithCode(ExceptionWithCode.FORBIDDEN,
        "id_not_allowed::" + pAddParam.get("user"));
    } else {
      getSrvOrm().updateEntity(pAddParam, pEntity);
      this.settingsAdd = null;
      lazyGetSettingsAdd(pAddParam);
    }
  }