Search in sources :

Example 1 with GraphCriteria

use of org.jaffa.soa.graph.GraphCriteria in project jaffa-framework by jaffa-projects.

the class DataTransformer method findRelatedObjects.

/**
 * Retrieves the related object, as specified by the relatedObjectClass, from the input domain object.
 * It'll utilize the findXyzCriteria() method, if available, to build the query, as well as to fire the handler.
 * Else it'll utilize the getXyzArray() method, which may cache a huge number of objects.
 *
 * @param domain              The domain object against which the query is to be invoked.
 * @param relatedObjectClass  The Class of the related object.
 * @param relatedObjectGetter The getter for obtaining the array of related objects.
 * @param handler             The handler, if passed, will be invoked prior to retrieving the related object.
 * @param originalCriteria    The original Criteria used for the query. This is passed to the handler.
 * @param path                This is the source path of this graph, used when processing a more complex tree, where this is the path to get to this root object being processed.
 * @return an array of related objects.
 * @throws IllegalAccessException    if the relatedObjectGetter is not accessible.
 * @throws InvocationTargetException if any error occurs during invocation of the relatedObjectGetter.
 */
private static Object[] findRelatedObjects(Object domain, Class relatedObjectClass, Method relatedObjectGetter, ITransformationHandler handler, GraphCriteria originalCriteria, String path) throws IllegalAccessException, InvocationTargetException {
    // The Getter typically caches the related objects. Hence try to locate the corresponding findXyzCriteria() method
    try {
        Matcher m = RELATED_OBJECT_GETTER.matcher(relatedObjectGetter.getName());
        if (m.matches()) {
            Method criteriaMethod = domain.getClass().getMethod("find" + m.group(1) + "Criteria");
            if (Criteria.class.isAssignableFrom(criteriaMethod.getReturnType())) {
                Criteria criteria = (Criteria) criteriaMethod.invoke(domain);
                // Invoke the handler
                if (handler != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Invoking the preQuery on the handler");
                    }
                    for (ITransformationHandler transformationHandler : handler.getTransformationHandlers()) {
                        Criteria handlerCriteria = transformationHandler.preQuery(path, criteria, originalCriteria, relatedObjectClass);
                        if (handlerCriteria != null) {
                            criteria = handlerCriteria;
                        }
                    }
                }
                if (criteria == null) {
                    // The handler will return a null if this query is not to be performed
                    return null;
                } else {
                    UOW uow = domain instanceof IPersistent ? ((IPersistent) domain).getUOW() : null;
                    boolean localUow = uow == null;
                    try {
                        if (localUow)
                            uow = new UOW();
                        Collection col = uow.query(criteria);
                        // Iterate through the Collection to retrieve all the objects
                        for (Object o : col) {
                        }
                        return col.toArray();
                    } finally {
                        if (localUow && uow != null)
                            uow.rollback();
                    }
                }
            } else {
                if (log.isDebugEnabled())
                    log.debug("Method '" + criteriaMethod.getName() + "' does not does not return a Criteria. Will invoke the method '" + relatedObjectGetter.getName() + "' directly to obtain the related object");
            }
        } else {
            if (log.isDebugEnabled())
                log.debug("Method '" + relatedObjectGetter.getName() + "' does not match the pattern '" + RELATED_OBJECT_GETTER + "'. Will invoke the method directly to obtain the related object");
        }
    } catch (Exception e) {
        // do nothing
        if (log.isDebugEnabled())
            log.debug("Exception thrown during creation of the criteria for the related object or while retrieving it. Will invoke the method '" + relatedObjectGetter.getName() + "' directly to obtain the related object", e);
    }
    // There must have been an error in invoking the findXyzCriteria() method. Simply invoke the Getter
    return (Object[]) relatedObjectGetter.invoke(domain);
}
Also used : IPersistent(org.jaffa.persistence.IPersistent) Matcher(java.util.regex.Matcher) Collection(java.util.Collection) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) GraphCriteria(org.jaffa.soa.graph.GraphCriteria) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW) InvalidForeignKeyException(org.jaffa.datatypes.exceptions.InvalidForeignKeyException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

AccessibleObject (java.lang.reflect.AccessibleObject)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Matcher (java.util.regex.Matcher)1 InvalidForeignKeyException (org.jaffa.datatypes.exceptions.InvalidForeignKeyException)1 Criteria (org.jaffa.persistence.Criteria)1 IPersistent (org.jaffa.persistence.IPersistent)1 UOW (org.jaffa.persistence.UOW)1 GraphCriteria (org.jaffa.soa.graph.GraphCriteria)1 GraphDataObject (org.jaffa.soa.graph.GraphDataObject)1