org.beigesoft.replicator.service.SrvEntitySyncPersistableBaseVersion Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of beige-replicator Show documentation
Show all versions of beige-replicator Show documentation
It replicate/persist any entity according XML settings and user's requirements with a file or network (HTTP).
Right now it has implemented XML format of stored/transferred data.
The newest version!
package org.beigesoft.replicator.service;
/*
* Beigesoft ™
*
* Licensed under the Apache License, Version 2.0
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
import java.util.Map;
import org.beigesoft.exception.ExceptionWithCode;
import org.beigesoft.persistable.APersistableBaseVersion;
import org.beigesoft.orm.service.ISrvOrm;
/**
* Service to synchronize APersistableBaseVersion (itsVersion changed time).
* It's untransactional service. Transaction must be started.
*
* @param platform dependent record set type
* @author Yury Demidenko
*/
public class SrvEntitySyncPersistableBaseVersion implements ISrvEntitySync {
/**
* ORM service.
**/
private ISrvOrm srvOrm;
/**
*
* Synchronize APersistableBaseVersion (itsVersion changed time).
*
* @param pEntity object
* @param pAddParam additional params
* @return isNew if entity exist in database (need update)
* @throws Exception - an exception
**/
@Override
public final boolean sync(final Object pEntity,
final Map pAddParam) throws Exception {
APersistableBaseVersion entityPb = (APersistableBaseVersion) 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();
APersistableBaseVersion entityPbDb = getSrvOrm()
.retrieveEntityWithConditions(entityPb.getClass(), whereStr);
entityPb.setIdBirth(entityPb.getItsId());
entityPb.setItsId(null);
boolean isNew = true;
if (entityPbDb != null) {
entityPb.setItsVersion(entityPbDb.getItsVersion());
entityPb.setItsId(entityPbDb.getItsId());
isNew = false;
}
return isNew;
}
//Simple getters and setters:
/**
* Getter for srvOrm.
* @return ISrvOrm
**/
public final ISrvOrm getSrvOrm() {
return this.srvOrm;
}
/**
* Setter for srvOrm.
* @param pSrvOrm reference
**/
public final void setSrvOrm(final ISrvOrm pSrvOrm) {
this.srvOrm = pSrvOrm;
}
}