Search in sources :

Example 1 with ApplicationExceptionWithContext

use of org.jaffa.exceptions.ApplicationExceptionWithContext in project jaffa-framework by jaffa-projects.

the class TransformerUtils method updateChildBean.

/**
 * Take a source object and try and mold it back it its domain object.
 * This is the same as updateParent, except from the way it retrieved the
 * record, and the way it creates a new record.
 */
static void updateChildBean(String path, GraphDataObject source, UOW uow, ITransformationHandler handler, IPersistent parentDomain, GraphMapping parentMapping, String parentField, DataTransformer.Mode mode, GraphDataObject newGraph) throws ApplicationExceptions, FrameworkException {
    if (log.isDebugEnabled())
        log.debug("Update Child Bean " + path);
    String relationshipName = parentMapping.getDomainFieldName(parentField);
    if (relationshipName.endsWith("Array"))
        relationshipName = relationshipName.substring(0, relationshipName.length() - 5);
    if (relationshipName.endsWith("Object"))
        relationshipName = relationshipName.substring(0, relationshipName.length() - 6);
    try {
        IPersistent domainObject = null;
        GraphMapping mapping = MappingFactory.getInstance(source);
        Map keys = new LinkedHashMap();
        Class doClass = mapping.getDomainClass();
        boolean gotKeys = false;
        // if (mapping.getKeyFields() == null || mapping.getKeyFields().size() == 0) {
        if (path == null || path.charAt(path.length() - 1) != ']') {
            if (log.isDebugEnabled())
                log.debug("Find 'one-to-one' object - " + path);
            // Just use the getXxxObject method to get the related domain object,
            // if there is one...
            domainObject = (IPersistent) getProperty(parentMapping.getDomainFieldDescriptor(parentField), parentDomain);
            if (domainObject == null) {
                if (log.isDebugEnabled())
                    log.debug("Not Found - " + path);
            }
        } else if (mode == DataTransformer.Mode.CLONE) {
            // In CLONE mode, get the keys from the new graph, and force the creation of the domain object
            if (newGraph != null)
                fillInKeys(path, newGraph, mapping, keys);
        } else {
            // Get the key fields used in the domain object. Use the findXxxxxCriteria() method,
            // then add the extra fields to the criteria object, to get the unique record.
            gotKeys = fillInKeys(path, source, mapping, keys);
            // read DO based on key
            if (gotKeys) {
                // get the method to get the PK criteria (i.e. public Criteria findVendorSiteCriteria(); )
                Method findCriteria = null;
                String methodName = "find" + StringHelper.getUpper1(relationshipName) + "Criteria";
                try {
                    findCriteria = parentDomain.getClass().getMethod(methodName, new Class[] {});
                } catch (NoSuchMethodException e) {
                    log.error("Find method '" + methodName + "' not found!");
                }
                if (findCriteria == null)
                    throw new TransformException(TransformException.METHOD_NOT_FOUND, path, methodName);
                // Find Criteria For Related Object
                Criteria criteria = (Criteria) findCriteria.invoke(parentDomain, new Object[] {});
                // Add extra key info...
                for (Iterator it = keys.keySet().iterator(); it.hasNext(); ) {
                    String keyField = (String) it.next();
                    Object value = keys.get(keyField);
                    keyField = StringHelper.getUpper1(mapping.getDomainFieldName(keyField));
                    criteria.addCriteria(keyField, value);
                    if (log.isDebugEnabled())
                        log.debug(path + "- Add to criteria:" + keyField + '=' + value);
                }
                // See if we get an object :-)
                Iterator itr = uow.query(criteria).iterator();
                if (itr.hasNext())
                    domainObject = (IPersistent) itr.next();
                if (itr.hasNext()) {
                    // Error, multiple objects found
                    throw new ApplicationExceptions(new ApplicationExceptionWithContext(path, new MultipleDomainObjectsFoundException(findDomainLabel(criteria.getTable()))));
                }
            } else {
                if (log.isDebugEnabled())
                    log.debug("Object " + path + " has either missing or null key values - Assume Create is needed");
            }
        }
        // Create object if not found
        if (domainObject == null) {
            // In MASS_UPDATE mode, error if DO not found
            if (mode == DataTransformer.Mode.MASS_UPDATE)
                throw new ApplicationExceptions(new ApplicationExceptionWithContext(path, new DomainObjectNotFoundException(TransformerUtils.findDomainLabel(doClass))));
            // NEW OBJECT, create and reflect keys
            if (log.isDebugEnabled())
                log.debug("DO '" + mapping.getDomainClassShortName() + "' not found with key, create a new one...");
            // find method on parent used to create object
            Method newObject = null;
            String methodName = "new" + StringHelper.getUpper1(relationshipName) + "Object";
            try {
                newObject = parentDomain.getClass().getMethod(methodName, new Class[] {});
            } catch (NoSuchMethodException e) {
                log.error("Method '" + methodName + "()' not found!");
            }
            if (newObject == null)
                throw new TransformException(TransformException.METHOD_NOT_FOUND, path, methodName);
            // Call method to create object
            domainObject = (IPersistent) newObject.invoke(parentDomain, new Object[] {});
            // Set the key fields
            for (Iterator it = keys.keySet().iterator(); it.hasNext(); ) {
                String keyField = (String) it.next();
                if (mapping.isReadOnly(keyField))
                    continue;
                Object value = keys.get(keyField);
                updateProperty(mapping.getDomainFieldDescriptor(keyField), value, domainObject);
            }
        } else {
            if (log.isDebugEnabled())
                log.debug("Found DO '" + mapping.getDomainClassShortName() + "' with key,");
        }
        // Now update all domain fields
        updateBeanData(path, source, uow, handler, mapping, domainObject, mode, newGraph);
    } catch (IllegalAccessException e) {
        TransformException me = new TransformException(TransformException.ACCESS_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    } catch (InvocationTargetException e) {
        ApplicationExceptions appExps = ExceptionHelper.extractApplicationExceptions(e);
        if (appExps != null)
            throw appExps;
        FrameworkException fe = ExceptionHelper.extractFrameworkException(e);
        if (fe != null)
            throw fe;
        TransformException me = new TransformException(TransformException.INVOCATION_ERROR, path, e);
        log.error(me.getLocalizedMessage(), me.getCause());
        throw me;
    } catch (InstantiationException e) {
        TransformException me = new TransformException(TransformException.INSTANTICATION_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) FrameworkException(org.jaffa.exceptions.FrameworkException) ApplicationExceptionWithContext(org.jaffa.exceptions.ApplicationExceptionWithContext) Method(java.lang.reflect.Method) Criteria(org.jaffa.persistence.Criteria) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) IPersistent(org.jaffa.persistence.IPersistent) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException) MultipleDomainObjectsFoundException(org.jaffa.exceptions.MultipleDomainObjectsFoundException) Iterator(java.util.Iterator) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with ApplicationExceptionWithContext

use of org.jaffa.exceptions.ApplicationExceptionWithContext in project jaffa-framework by jaffa-projects.

the class TransformerUtils method deleteChildBean.

/**
 * Take a source object and try and mold it back it its domain object.
 * This is the same as updateParent, except from the way it retrieved the
 * record, and the way it creates a new record.
 */
static void deleteChildBean(String path, GraphDataObject source, UOW uow, ITransformationHandler handler, IPersistent parentDomain, GraphMapping parentMapping, String parentField) throws ApplicationExceptions, FrameworkException {
    if (log.isDebugEnabled())
        log.debug("Delete Child Bean " + path);
    String relationshipName = parentMapping.getDomainFieldName(parentField);
    if (relationshipName.endsWith("Array"))
        relationshipName = relationshipName.substring(0, relationshipName.length() - 5);
    if (relationshipName.endsWith("Object"))
        relationshipName = relationshipName.substring(0, relationshipName.length() - 6);
    try {
        IPersistent domainObject = null;
        GraphMapping mapping = MappingFactory.getInstance(source);
        Map keys = new LinkedHashMap();
        Class doClass = mapping.getDomainClass();
        boolean gotKeys = false;
        // if (mapping.getKeyFields() == null || mapping.getKeyFields().size() == 0) {
        if (path == null || path.charAt(path.length() - 1) != ']') {
            if (log.isDebugEnabled())
                log.debug("Find 'one-to-one' object - " + path);
            // Just use the getXxxObject method to get the related domain object,
            // if there is one...
            domainObject = (IPersistent) getProperty(parentMapping.getDomainFieldDescriptor(parentField), parentDomain);
            if (domainObject == null) {
                if (log.isDebugEnabled())
                    log.debug("Not Found - " + path);
            }
        } else {
            // Get the key fields used in the domain object. Use the findXxxxxCriteria() method,
            // then add the extra fields to the criteria object, to get the unique record.
            gotKeys = fillInKeys(path, source, mapping, keys);
            // read DO based on key
            if (gotKeys) {
                // get the method to get the PK criteria (i.e. public Criteria findVendorSiteCriteria(); )
                Method findCriteria = null;
                String methodName = "find" + StringHelper.getUpper1(relationshipName) + "Criteria";
                try {
                    findCriteria = parentDomain.getClass().getMethod(methodName, new Class[] {});
                } catch (NoSuchMethodException e) {
                    log.error("Find method '" + methodName + "' not found!");
                }
                if (findCriteria == null)
                    throw new TransformException(TransformException.METHOD_NOT_FOUND, path, methodName);
                // Find Criteria For Related Object
                Criteria criteria = (Criteria) findCriteria.invoke(parentDomain, new Object[] {});
                // Add extra key info...
                for (Iterator it = keys.keySet().iterator(); it.hasNext(); ) {
                    String keyField = (String) it.next();
                    Object value = keys.get(keyField);
                    keyField = StringHelper.getUpper1(mapping.getDomainFieldName(keyField));
                    criteria.addCriteria(keyField, value);
                    if (log.isDebugEnabled())
                        log.debug(path + "- Add to criteria:" + keyField + '=' + value);
                }
                // See if we get an object :-)
                Iterator itr = uow.query(criteria).iterator();
                if (itr.hasNext())
                    domainObject = (IPersistent) itr.next();
                if (itr.hasNext()) {
                    // Error, multiple objects found
                    throw new ApplicationExceptions(new ApplicationExceptionWithContext(path, new MultipleDomainObjectsFoundException(findDomainLabel(criteria.getTable()))));
                }
            }
        }
        // Error if DO not found
        if (domainObject == null)
            throw new ApplicationExceptions(new ApplicationExceptionWithContext(path, new DomainObjectNotFoundException(findDomainLabel(doClass))));
        // Process the delete, either on this DO, or a related DO if there is one
        deleteBeanData(path, source, uow, handler, mapping, domainObject);
    } catch (IllegalAccessException e) {
        TransformException me = new TransformException(TransformException.ACCESS_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    } catch (InvocationTargetException e) {
        ApplicationExceptions appExps = ExceptionHelper.extractApplicationExceptions(e);
        if (appExps != null)
            throw appExps;
        FrameworkException fe = ExceptionHelper.extractFrameworkException(e);
        if (fe != null)
            throw fe;
        TransformException me = new TransformException(TransformException.INVOCATION_ERROR, path, e);
        log.error(me.getLocalizedMessage(), me.getCause());
        throw me;
    } catch (InstantiationException e) {
        TransformException me = new TransformException(TransformException.INSTANTICATION_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) FrameworkException(org.jaffa.exceptions.FrameworkException) ApplicationExceptionWithContext(org.jaffa.exceptions.ApplicationExceptionWithContext) Method(java.lang.reflect.Method) Criteria(org.jaffa.persistence.Criteria) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) IPersistent(org.jaffa.persistence.IPersistent) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException) MultipleDomainObjectsFoundException(org.jaffa.exceptions.MultipleDomainObjectsFoundException) Iterator(java.util.Iterator) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with ApplicationExceptionWithContext

use of org.jaffa.exceptions.ApplicationExceptionWithContext in project jaffa-framework by jaffa-projects.

the class TransformerUtils method domainObjectChangedTest.

/**
 * Performs the dirty-read check to ensure that the underlying record in the database hasn't
 * been changed by another user, while the current user has been trying to modify it.
 */
private static void domainObjectChangedTest(String path, GraphDataObject source, GraphMapping mapping, IPersistent domainObject) throws InstantiationException, IllegalAccessException, InvocationTargetException, FrameworkException, ApplicationExceptions {
    if (source.getPerformDirtyReadCheck() != null && source.getPerformDirtyReadCheck() && mapping.getDirtyReadDataFieldName() != null) {
        if (log.isDebugEnabled())
            log.debug("Performing dirty-read check for " + domainObject);
        Object lastKnownValue = getProperty(mapping.getDataFieldDescriptor(mapping.getDirtyReadDataFieldName()), source);
        Object currentValue = getProperty(mapping.getDomainFieldDescriptor(mapping.getDirtyReadDataFieldName()), domainObject);
        if (lastKnownValue == null ? currentValue != null : !lastKnownValue.equals(currentValue)) {
            if (log.isDebugEnabled())
                log.debug("Dirty-read check failed: lastKnownValue='" + lastKnownValue + "', currentValue='" + currentValue + '\'');
            if (!domainObject.isDatabaseOccurence()) {
                if (log.isDebugEnabled())
                    log.debug("Dirty-read check failed: " + domainObject + " may have been removed.");
                throw new ApplicationExceptions(new ApplicationExceptionWithContext(path, new ApplicationException(DomainObjectChangedException.OBJECT_REMOVED, null, null)));
            } else {
                String[] errorParamNames = mapping.getDirtyReadErrorParams();
                Object[] errorParamValues = new Object[errorParamNames.length];
                try {
                    for (int i = 0; i < errorParamNames.length; i++) errorParamValues[i] = BeanHelper.getField(domainObject, errorParamNames[i]);
                } catch (Exception e) {
                    log.warn("Error in creation of the argument array to pass to the DomainObjectChangedException from the list: " + Arrays.toString(mapping.getDirtyReadErrorParams()), e);
                }
                throw new ApplicationExceptions(new ApplicationExceptionWithContext(path, new DomainObjectChangedException(mapping.getDirtyReadErrorLabelToken(), errorParamValues, null)));
            }
        } else {
            if (log.isDebugEnabled())
                log.debug("Dirty-read check succeeded. Both lastKnownValue and currentValue equal '" + currentValue + '\'');
        }
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ApplicationException(org.jaffa.exceptions.ApplicationException) ApplicationExceptionWithContext(org.jaffa.exceptions.ApplicationExceptionWithContext) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) DomainObjectChangedException(org.jaffa.exceptions.DomainObjectChangedException) DomainObjectChangedException(org.jaffa.exceptions.DomainObjectChangedException) FrameworkException(org.jaffa.exceptions.FrameworkException) ApplicationException(org.jaffa.exceptions.ApplicationException) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultipleDomainObjectsFoundException(org.jaffa.exceptions.MultipleDomainObjectsFoundException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ApplicationExceptionWithContext (org.jaffa.exceptions.ApplicationExceptionWithContext)3 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)3 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)3 FrameworkException (org.jaffa.exceptions.FrameworkException)3 MultipleDomainObjectsFoundException (org.jaffa.exceptions.MultipleDomainObjectsFoundException)3 GraphDataObject (org.jaffa.soa.graph.GraphDataObject)3 Method (java.lang.reflect.Method)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Criteria (org.jaffa.persistence.Criteria)2 IPersistent (org.jaffa.persistence.IPersistent)2 IntrospectionException (java.beans.IntrospectionException)1 ApplicationException (org.jaffa.exceptions.ApplicationException)1 DomainObjectChangedException (org.jaffa.exceptions.DomainObjectChangedException)1