org.beigesoft.replicator.service.SrvEntityReaderXml 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 java.io.Reader;
import java.lang.reflect.Constructor;
import org.beigesoft.service.IUtilXml;
import org.beigesoft.exception.ExceptionWithCode;
import org.beigesoft.settings.IMngSettings;
/**
* Service to read a replicable/persistable entity.
* According Beige Replicator specification #1.
*
* @author Yury Demidenko
*/
public class SrvEntityReaderXml implements ISrvEntityReader {
/**
* Manager Settings.
**/
private IMngSettings mngSettings;
/**
* Fields fillers map.
**/
private Map fieldsFillersMap;
/**
* XML service.
**/
private IUtilXml utilXml;
/**
*
* Read entity(fill fields) from a stream (reader - file or through network).
* It is invoked when it's start of <entity
*
* @param pReader reader.
* @param pAddParam additional params
* @return entity filled/refreshed.
* @throws Exception - an exception
**/
@Override
public final Object read(final Reader pReader,
final Map pAddParam) throws Exception {
Map attributesMap = readAttributes(pReader, pAddParam);
if (attributesMap.get("class") == null) {
throw new ExceptionWithCode(ExceptionWithCode
.CONFIGURATION_MISTAKE, "There is no class attribute for entity!");
}
Class entityClass = Class.forName(attributesMap.get("class"));
@SuppressWarnings("unchecked")
Constructor constructor = entityClass.getDeclaredConstructor();
Object entity = constructor.newInstance();
Map> fieldsSettingsMap =
getMngSettings().getFieldsSettings()
.get(entityClass.getCanonicalName());
for (Map.Entry> entry
: fieldsSettingsMap.entrySet()) {
if ("true".equals(entry.getValue().get("isEnabled"))
&& attributesMap.get(entry.getKey()) != null) {
ISrvEntityFieldFiller srvEntityFieldFiller = getFieldsFillersMap()
.get(entry.getValue().get("ISrvEntityFieldFiller"));
if (srvEntityFieldFiller == null) {
throw new ExceptionWithCode(ExceptionWithCode
.CONFIGURATION_MISTAKE, "There is no ISrvEntityFieldFiller "
+ entry.getValue().get("ISrvEntityFieldFiller") + " for "
+ entityClass + " / " + entry.getKey());
}
srvEntityFieldFiller.fill(entity, entry.getKey(), attributesMap
.get(entry.getKey()), pAddParam);
}
}
return entity;
}
/**
*
* Read entity attributes from stream.
*
* @param pReader reader.
* @param pAddParam additional params
* @return attributes map
* @throws Exception - an exception
**/
@Override
public final Map readAttributes(final Reader pReader,
final Map pAddParam) throws Exception {
return this.utilXml.readAttributes(pReader, pAddParam);
}
//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 fieldsFillersMap.
* @return Map
**/
public final Map getFieldsFillersMap() {
return this.fieldsFillersMap;
}
/**
* Setter for fieldsFillersMap.
* @param pFieldsFillersMap reference
**/
public final void setFieldsFillersMap(
final Map pFieldsFillersMap) {
this.fieldsFillersMap = pFieldsFillersMap;
}
/**
* 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;
}
}