org.beigesoft.replicator.service.DatabaseReaderSyncStdXml Maven / Gradle / Ivy
Show all versions of beige-replicator Show documentation
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 java.io.Reader;
import org.beigesoft.settings.IMngSettings;
import org.beigesoft.exception.ExceptionWithCode;
import org.beigesoft.service.IUtilXml;
import org.beigesoft.orm.service.ISrvOrm;
import org.beigesoft.orm.service.ISrvDatabase;
import org.beigesoft.log.ILogger;
/**
* Service that reads entities from given stream XML
* (file or network connection) and insert them into database.
* It makes database synchronized copy i.e. if entity is APersistableBase
* (itsId, databaseBirth, idBirth) then it be synchronized by
* {idBirth and idDatabaseBirth}. Other by explicit ID (Long or String type).
* This is transactional service!
* This is only for entities of types:
* APersistableBase, AHasIdLong and AHasIdString
*
*
* @param platform dependent record set type
* @author Yury Demidenko
*/
public class DatabaseReaderSyncStdXml implements IDatabaseReader {
/**
* Entity Reader.
**/
private ISrvEntityReader srvEntityReader;
/**
* Logger.
**/
private ILogger logger;
/**
* ORM service.
**/
private ISrvOrm srvOrm;
/**
* Database service.
**/
private ISrvDatabase srvDatabase;
/**
* XML service.
**/
private IUtilXml utilXml;
/**
* Manager Settings.
**/
private IMngSettings mngSettings;
/**
* Services Entity Sync map.
**/
private Map srvEntitySyncMap;
/**
*
* Read entities from stream (by given reader) and synchronize them
* into DB.
*
* @param pReader Reader
* @param pAddParam additional params
* @throws Exception - an exception
**/
@Override
public final void readAndStoreEntities(final Reader pReader,
final Map pAddParam) throws Exception {
try {
this.srvDatabase.setIsAutocommit(false);
this.srvDatabase.
setTransactionIsolation(ISrvDatabase.TRANSACTION_READ_UNCOMMITTED);
this.srvDatabase.beginTransaction();
while (this.utilXml.readUntilStart(pReader, "entity")) {
Object entity = this.srvEntityReader.read(pReader, null);
String nameEntitySync = this.mngSettings.getClassesSettings()
.get(entity.getClass().getCanonicalName()).get("ISrvEntitySync");
ISrvEntitySync srvEntitySync = this.srvEntitySyncMap
.get(nameEntitySync);
if (srvEntitySync == null) {
throw new ExceptionWithCode(ExceptionWithCode
.CONFIGURATION_MISTAKE, "There is no ISrvEntitySync "
+ nameEntitySync + " for " + entity.getClass());
}
boolean isNew = srvEntitySync.sync(entity, null);
if (isNew) {
this.srvOrm.insertEntity(entity);
} else {
this.srvOrm.updateEntity(entity);
}
}
this.srvDatabase.commitTransaction();
} catch (Exception ex) {
this.srvDatabase.rollBackTransaction();
throw ex;
} finally {
this.srvDatabase.releaseResources();
}
}
//Simple getters and setters:
/**
* Getter for mngSettings.
* @return IMngSettings
**/
public final IMngSettings getMngSettings() {
return this.mngSettings;
}
/**
* Setter for mngSettings.
* @param pMngSettings reference
**/
public final void setMngSettings(final IMngSettings pMngSettings) {
this.mngSettings = pMngSettings;
}
/**
* Getter for srvEntityReader.
* @return ISrvEntityReader
**/
public final ISrvEntityReader getSrvEntityReader() {
return this.srvEntityReader;
}
/**
* Setter for srvEntityReader.
* @param pSrvEntityReader reference
**/
public final void setSrvEntityReader(
final ISrvEntityReader pSrvEntityReader) {
this.srvEntityReader = pSrvEntityReader;
}
/**
* Getter for logger.
* @return ILogger
**/
public final ILogger getLogger() {
return this.logger;
}
/**
* Setter for logger.
* @param pLogger reference
**/
public final void setLogger(final ILogger pLogger) {
this.logger = pLogger;
}
/**
* 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;
}
/**
* Getter for srvDatabase.
* @return ISrvDatabase
**/
public final ISrvDatabase getSrvDatabase() {
return this.srvDatabase;
}
/**
* Setter for srvDatabase.
* @param pSrvDatabase reference
**/
public final void setSrvDatabase(final ISrvDatabase pSrvDatabase) {
this.srvDatabase = pSrvDatabase;
}
/**
* Getter for utilXml.
* @return IUtilXml
**/
public final IUtilXml getUtilXml() {
return this.utilXml;
}
/**
* Setter for utilXml.
* @param pUtilXml reference
**/
public final void setUtilXml(final IUtilXml pUtilXml) {
this.utilXml = pUtilXml;
}
/**
* Getter for srvEntitySyncMap.
* @return Map
**/
public final Map getSrvEntitySyncMap() {
return this.srvEntitySyncMap;
}
/**
* Setter for srvEntitySyncMap.
* @param pSrvEntitySyncMap reference
**/
public final void setSrvEntitySyncMap(
final Map pSrvEntitySyncMap) {
this.srvEntitySyncMap = pSrvEntitySyncMap;
}
}