Search in sources :

Example 1 with Persistent

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);
}
Also used : Field(java.lang.reflect.Field) Persistent(org.jaffa.persistence.Persistent) Validator(org.jaffa.rules.fieldvalidators.Validator) Test(org.junit.Test)

Example 2 with Persistent

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);
            }
        }
    }
}
Also used : Persistent(org.jaffa.persistence.Persistent) Criteria(org.jaffa.persistence.Criteria) RuleMetaDataCriteria(org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria) UOW(org.jaffa.persistence.UOW) RulesEngineException(org.jaffa.rules.RulesEngineException)

Example 3 with Persistent

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);
}
Also used : Persistent(org.jaffa.persistence.Persistent) IPersistent(org.jaffa.persistence.IPersistent) Method(java.lang.reflect.Method)

Example 4 with Persistent

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);
}
Also used : Persistent(org.jaffa.persistence.Persistent) IPersistent(org.jaffa.persistence.IPersistent) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) Method(java.lang.reflect.Method)

Aggregations

Persistent (org.jaffa.persistence.Persistent)4 Method (java.lang.reflect.Method)2 IPersistent (org.jaffa.persistence.IPersistent)2 Field (java.lang.reflect.Field)1 Criteria (org.jaffa.persistence.Criteria)1 UOW (org.jaffa.persistence.UOW)1 RulesEngineException (org.jaffa.rules.RulesEngineException)1 Validator (org.jaffa.rules.fieldvalidators.Validator)1 RuleMetaDataCriteria (org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria)1 GraphDataObject (org.jaffa.soa.graph.GraphDataObject)1 Test (org.junit.Test)1