src/main/java/org/beigesoft/replicator/service/DatabaseWriterXml.java
@Override
  public final <T> int retrieveAndWriteEntities(
    final Map<String, Object> pAddParam,
      final Class<T> pEntityClass, final Writer pWriter) throws Exception {
    //e.g. "limit 20 offset 19":
    //e.g. "where (ITSID>0 and IDDATABASEBIRTH=2135) limit 20 offset 19":
    String conditions = (String) pAddParam.get("conditions");
    int requestingDatabaseVersion = Integer
      .parseInt((String) pAddParam.get("requestingDatabaseVersion"));
    int databaseVersion = this.srvDatabase.getVersionDatabase();
    List<T> entities = null;
    int entitiesCount = 0;
    DatabaseInfo di;
    if (requestingDatabaseVersion == databaseVersion) {
      try {
        this.srvDatabase.setIsAutocommit(false);
        this.srvDatabase.
          setTransactionIsolation(ISrvDatabase.TRANSACTION_READ_UNCOMMITTED);
        this.srvDatabase.beginTransaction();
        di = getSrvOrm()
          .retrieveEntityWithConditions(pAddParam, DatabaseInfo.class, "");
        String requestedDatabaseIdStr = (String) pAddParam
          .get("requestedDatabaseId");
        if (requestedDatabaseIdStr != null) {
          int requestedDatabaseId = Integer.parseInt(requestedDatabaseIdStr);
          if (requestedDatabaseId != di.getDatabaseId()) {
            String error = "Different requested database ID! required/is: "
                + requestedDatabaseId + "/" + di.getDatabaseId();
            this.logger.error(null, DatabaseWriterXml.class, error);
            pWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            pWriter.write("<message error=\"" + error + "\">\n");
            pWriter.write("</message>\n");
            return entitiesCount;
          }
        }
        if (conditions == null) {
          entities = getSrvOrm().retrieveList(pAddParam, pEntityClass);
        } else {
          entities = getSrvOrm()
            .retrieveListWithConditions(pAddParam, pEntityClass, conditions);
        }
        entitiesCount = entities.size();
        this.srvDatabase.commitTransaction();
      } catch (Exception ex) {
        this.srvDatabase.rollBackTransaction();
        throw ex;
      } finally {
        this.srvDatabase.releaseResources();
      }
      this.logger.info(null, DatabaseWriterXml.class, "Start write entities of "
        + pEntityClass.getCanonicalName() + " count=" + entitiesCount);
      pWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
      pWriter.write("<message databaseId=\"" + di.getDatabaseId()
        + "\" databaseVersion=\"" + di.getDatabaseVersion()
          + "\" description=\"" + di.getDescription() + "\" entitiesCount=\""
            + entitiesCount + "\">\n");
      for (T entity : entities) {
        this.srvEntityWriter.write(pAddParam, entity, pWriter);
      }
      pWriter.write("</message>\n");
      this.logger.info(null, DatabaseWriterXml.class,
        "Entities has been wrote");
    } else {
      this.logger.error(null, DatabaseWriterXml.class,
        "Send error message - Different database version!");
      pWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
      pWriter.write("<message error=\"Different database version!\">\n");
      pWriter.write("</message>\n");
    }
    return entitiesCount;
  }
@Override
  public final <T> int retrieveAndWriteEntities(
    final Map<String, Object> pAddParam,
      final Class<T> pEntityClass, final Writer pWriter) throws Exception {
    //e.g. "limit 20 offset 19":
    //e.g. "where (ITSID>0 and IDDATABASEBIRTH=2135) limit 20 offset 19":
    String conditions = (String) pAddParam.get("conditions");
    int requestingDatabaseVersion = Integer
      .parseInt((String) pAddParam.get("requestingDatabaseVersion"));
    int databaseVersion = this.srvDatabase.getVersionDatabase();
    List<T> entities = null;
    int entitiesCount = 0;
    DatabaseInfo di;
    if (requestingDatabaseVersion == databaseVersion) {
      try {
        this.srvDatabase.setIsAutocommit(false);
        this.srvDatabase.
          setTransactionIsolation(ISrvDatabase.TRANSACTION_READ_UNCOMMITTED);
        this.srvDatabase.beginTransaction();
        di = getSrvOrm()
          .retrieveEntityWithConditions(pAddParam, DatabaseInfo.class, "");
        String requestedDatabaseIdStr = (String) pAddParam
          .get("requestedDatabaseId");
        if (requestedDatabaseIdStr != null) {
          int requestedDatabaseId = Integer.parseInt(requestedDatabaseIdStr);
          if (requestedDatabaseId != di.getDatabaseId()) {
            String error = "Different requested database ID! required/is: "
                + requestedDatabaseId + "/" + di.getDatabaseId();
            this.logger.error(null, DatabaseWriterXml.class, error);
            pWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            pWriter.write("<message error=\"" + error + "\">\n");
            pWriter.write("</message>\n");
            return entitiesCount;
          }
        }
        if (conditions == null) {
          entities = getSrvOrm().retrieveList(pAddParam, pEntityClass);
        } else {
          entities = getSrvOrm()
            .retrieveListWithConditions(pAddParam, pEntityClass, conditions);
        }
        entitiesCount = entities.size();
        this.srvDatabase.commitTransaction();
      } catch (Exception ex) {
        this.srvDatabase.rollBackTransaction();
        throw ex;
      } finally {
        this.srvDatabase.releaseResources();
      }
      this.logger.info(null, DatabaseWriterXml.class, "Start write entities of "
        + pEntityClass.getCanonicalName() + " count=" + entitiesCount);
      pWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
      pWriter.write("<message databaseId=\"" + di.getDatabaseId()
        + "\" databaseVersion=\"" + di.getDatabaseVersion()
          + "\" description=\"" + di.getDescription() + "\" entitiesCount=\""
            + entitiesCount + "\">\n");
      for (T entity : entities) {
        this.srvEntityWriter.write(pAddParam, entity, pWriter);
      }
      pWriter.write("</message>\n");
      this.logger.info(null, DatabaseWriterXml.class,
        "Entities has been wrote");
    } else {
      this.logger.error(null, DatabaseWriterXml.class,
        "Send error message - Different database version!");
      pWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
      pWriter.write("<message error=\"Different database version!\">\n");
      pWriter.write("</message>\n");
    }
    return entitiesCount;
  }
<p>
Retrieve requested entities from DB then write them into a stream
by given writer.
</p>
@param T Entity Class
@param pAddParam additional params (e.g. where and order clause)
@param pEntityClass Entity Class
@param pWriter writer
@return entities count
@throws Exception - an exception