use of org.jaffa.persistence.Persistent in project jaffa-framework by jaffa-projects.
the class PersistentConfigValidatorTest method testPersistentConfig.
/**
* Test Persistent has a validator pushed into it during construction
*/
@Test
public void testPersistentConfig() throws NoSuchFieldException, IllegalAccessException {
// creating a new instance of a persistent object should have its validator pushed into it
// automatically by persistent config
Persistent persistent = new TestModelPersistent();
Field f = persistent.getClass().getSuperclass().getDeclaredField("validator");
f.setAccessible(true);
Validator validator = (Validator) f.get(persistent);
assertNotNull(validator);
}
use of org.jaffa.persistence.Persistent in project jaffa-framework by jaffa-projects.
the class ApplicationRulesUtilities method incorporateValuesFromPersistentStore.
private void incorporateValuesFromPersistentStore(String source, Map<String, Collection<String>> domainRulesCache, Collection<String> expandedRules, String[] domainInfo, String preRule, String postRule) {
Criteria myCriteria = new Criteria();
UOW myUOW = null;
try {
myCriteria.setTable(domainInfo[1]);
/*
* Added group by to avoid fetching everything. Performance optimization.
* It will find the unique field value based on the provided field on rule.
*/
myCriteria.addGroupBy(domainInfo[2], domainInfo[2]);
myUOW = new UOW();
Collection<String> domainFieldValues = new ArrayList<>();
Iterator<Persistent> myCollection = myUOW.query(myCriteria).iterator();
while (myCollection.hasNext()) {
Map row = (Map) myCollection.next();
String domainFieldValue = (String) row.get(domainInfo[2]);
domainFieldValues.add(domainFieldValue);
String expandedRule = preRule + domainFieldValue + postRule;
// Recursively call this method to handle rules with
// multiple internal properties.
Collection<String> furtherExpandedRules = elaborateDomainRules(expandedRule, domainRulesCache);
expandedRules.addAll(furtherExpandedRules);
}
// Mapping the domain rule name to its content in the cache
domainRulesCache.put(domainInfo[1], domainFieldValues);
} catch (Exception e) {
log.error("Unable to elaborate " + source, e);
} finally {
if (myUOW != null) {
try {
myUOW.rollback();
} catch (Exception e) {
log.error("Failed to rollback UOW:" + e);
}
}
}
}
use of org.jaffa.persistence.Persistent in project jaffa-framework by jaffa-projects.
the class BeanMoulder method updateProperty.
/**
* Set a value on a Bean, if its a persistent bean, try to use an update method first
* (for v1.0 domain objects), otherwise use the setter (for v1.1 and above).
*/
private static void updateProperty(PropertyDescriptor pd, Object value, Object source) throws IllegalAccessException, InvocationTargetException, MouldException {
if (source instanceof Persistent) {
if (pd != null && pd.getWriteMethod() != null) {
try {
Method m = source.getClass().getMethod("update" + StringHelper.getUpper1(pd.getName()), pd.getWriteMethod().getParameterTypes());
if (!m.isAccessible())
m.setAccessible(true);
Class tClass = m.getParameterTypes()[0];
if (value == null || tClass.isAssignableFrom(value.getClass())) {
m.invoke(source, new Object[] { value });
log.debug("Update property '" + pd.getName() + "=" + value + "' on object '" + source.getClass().getName() + "'");
// See if there is a datatype mapper for these classes
} else if (DataTypeMapper.instance().isMappable(value.getClass(), tClass)) {
value = DataTypeMapper.instance().map(value, tClass);
m.invoke(source, new Object[] { value });
log.debug("Translate+Update property '" + pd.getName() + "=" + value + "' on object '" + source.getClass().getName() + "'");
} else {
// Data type mismatch
throw new MouldException(MouldException.DATATYPE_MISMATCH, source.getClass().getName() + "." + m.getName(), tClass.getName(), value.getClass().getName());
}
} catch (NoSuchMethodException e) {
log.debug("No Updator, try Setter for DO property '" + pd.getName() + "' on object '" + source.getClass().getName() + "'");
// try to use the setter if there is no update method
setProperty(pd, value, source);
}
} else {
MouldException me = new MouldException(MouldException.NO_SETTER, null, pd == null ? "???" : pd.getName(), source.getClass().getName());
log.error(me.getLocalizedMessage());
throw me;
}
} else
setProperty(pd, value, source);
}
use of org.jaffa.persistence.Persistent in project jaffa-framework by jaffa-projects.
the class TransformerUtils method updateProperty.
/**
* Set a value on a Bean, if its a persistent bean, try to use an update method first
* (for v1.0 domain objects), otherwise use the setter (for v1.1 and above).
*/
static void updateProperty(PropertyDescriptor pd, Object value, Object source) throws IllegalAccessException, InvocationTargetException, TransformException {
if (source instanceof Persistent) {
if (pd != null && pd.getWriteMethod() != null) {
try {
Method m = source.getClass().getMethod("update" + StringHelper.getUpper1(pd.getName()), pd.getWriteMethod().getParameterTypes());
if (!m.isAccessible())
m.setAccessible(true);
Class tClass = m.getParameterTypes()[0];
if (value == null || tClass.isAssignableFrom(value.getClass())) {
m.invoke(source, new Object[] { value });
if (log.isDebugEnabled())
log.debug("Update property '" + pd.getName() + '=' + value + "' on object '" + source.getClass().getName() + '\'');
// See if there is a datatype mapper for these classes
} else if (DataTypeMapper.instance().isMappable(value.getClass(), tClass)) {
value = DataTypeMapper.instance().map(value, tClass);
m.invoke(source, new Object[] { value });
if (log.isDebugEnabled())
log.debug("Translate+Update property '" + pd.getName() + '=' + value + "' on object '" + source.getClass().getName() + '\'');
} else {
// Data type mismatch
throw new TransformException(TransformException.DATATYPE_MISMATCH, source.getClass().getName() + '.' + m.getName(), tClass.getName(), value.getClass().getName());
}
} catch (NoSuchMethodException e) {
if (log.isDebugEnabled())
log.debug("No Updator, try Setter for DO property '" + pd.getName() + "' on object '" + source.getClass().getName() + '\'');
// try to use the setter if there is no update method
setProperty(pd, value, source);
}
} else {
TransformException me = new TransformException(TransformException.NO_SETTER, null, pd == null ? "???" : pd.getName(), source.getClass().getName());
log.error(me.getLocalizedMessage());
throw me;
}
} else
setProperty(pd, value, source);
}
Aggregations