org.beigesoft.replicator.service.SrvEntityFieldPersistableBaseRepl 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.lang.reflect.Field;
import java.lang.reflect.Constructor;
import org.beigesoft.persistable.APersistableBase;
import org.beigesoft.exception.ExceptionWithCode;
import org.beigesoft.service.UtlReflection;
/**
* Service to fill a owned entity of replicable entity.
* Only for database replication!!! For restoring from file it must be a filler
* that will find out owned entity from list of already read entities.
*
* @author Yury Demidenko
*/
public class SrvEntityFieldPersistableBaseRepl
implements ISrvEntityFieldFiller {
/**
* Reflection service.
**/
private UtlReflection utlReflection;
/**
*
* Fill given field of given entity according value represented as
* string.
*
* @param pEntity Entity.
* @param pFieldName Field Name
* @param pFieldStrValue Field value
* @param pAddParam additional params
* @throws Exception - an exception
**/
@Override
public final void fill(final Object pEntity, final String pFieldName,
final String pFieldStrValue,
final Map pAddParam) throws Exception {
Field rField = getUtlReflection().retrieveField(pEntity.getClass(),
pFieldName);
rField.setAccessible(true);
if (!APersistableBase.class.isAssignableFrom(rField.getType())) {
throw new ExceptionWithCode(ExceptionWithCode
.CONFIGURATION_MISTAKE, "It's wrong service to fill that field: "
+ pEntity + "/" + pFieldName + "/" + pFieldStrValue);
}
if ("NULL".equals(pFieldStrValue)) {
rField.set(pEntity, null);
return;
}
try {
@SuppressWarnings("unchecked")
Constructor constructor = rField.getType().getDeclaredConstructor();
Object ownedEntity = constructor.newInstance();
((APersistableBase) ownedEntity).setItsId(Long.parseLong(pFieldStrValue));
rField.set(pEntity, ownedEntity);
} catch (Exception ex) {
throw new ExceptionWithCode(ExceptionWithCode
.WRONG_PARAMETER, "Can not fill field: " + pEntity + "/" + pFieldName
+ "/" + pFieldStrValue + ", " + ex.getMessage(), ex);
}
}
//Simple getters and setters:
/**
* 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;
}
}