org.beigesoft.replicator.service.SrvEntityWriterXml 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.Writer;
import java.lang.reflect.Field;
import org.beigesoft.exception.ExceptionWithCode;
import org.beigesoft.service.UtlReflection;
import org.beigesoft.settings.IMngSettings;
/**
* Service to write a replicable/persistable entity as XML.
*
* @author Yury Demidenko
*/
public class SrvEntityWriterXml implements ISrvEntityWriter {
/**
* Manager Settings.
**/
private IMngSettings mngSettings;
/**
* Fields writers map.
**/
private Map fieldsWritersMap;
/**
* Reflection service.
**/
private UtlReflection utlReflection;
/**
*
* Write entity into a stream (writer - file or pass it through network).
*
* @param pEntity object
* @param pWriter writer
* @param pAddParam additional params (e.g. exclude fields set)
* @throws Exception - an exception
**/
@Override
public final void write(final Object pEntity, final Writer pWriter,
final Map pAddParam) throws Exception {
Map> fieldsSettingsMap =
getMngSettings().getFieldsSettings()
.get(pEntity.getClass().getCanonicalName());
pWriter.write("> entry
: fieldsSettingsMap.entrySet()) {
if ("true".equals(entry.getValue().get("isEnabled"))) {
Field field = getUtlReflection()
.retrieveField(pEntity.getClass(), entry.getKey());
field.setAccessible(true);
Object fieldValue = field.get(pEntity);
ISrvFieldWriter srvFieldWriter = getFieldsWritersMap()
.get(entry.getValue().get("ISrvFieldWriter"));
if (srvFieldWriter == null) {
throw new ExceptionWithCode(ExceptionWithCode
.CONFIGURATION_MISTAKE, "There is no ISrvFieldWriter " + entry
.getValue().get("ISrvFieldWriter") + " for "
+ pEntity.getClass() + " / " + field.getName());
}
srvFieldWriter.write(fieldValue, field.getName(), pWriter, pAddParam);
}
}
pWriter.write("/>\n");
}
//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 fieldsWritersMap.
* @return Map
**/
public final Map getFieldsWritersMap() {
return this.fieldsWritersMap;
}
/**
* Setter for fieldsWritersMap.
* @param pFieldsWritersMap reference
**/
public final void setFieldsWritersMap(
final Map pFieldsWritersMap) {
this.fieldsWritersMap = pFieldsWritersMap;
}
/**
* Getter for utlReflection.
* @return UtlReflection
**/
public final UtlReflection getUtlReflection() {
return this.utlReflection;
}
/**
* Setter for utlReflection.
* @param pUtlReflection reference
**/
public final void setUtlReflection(final UtlReflection pUtlReflection) {
this.utlReflection = pUtlReflection;
}
}