Search in sources :

Example 86 with ApplicationException

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

the class UserRequestMaintenanceTx method delete.

// .//GEN-END:_update_1_be
// .//GEN-BEGIN:_delete_1_be
/**
 * Deletes an existing instance of UserRequest.
 * @param input The key values for the domain object to be deleted.
 * @throws ApplicationExceptions This will be thrown if the input contains invalid data.
 * @throws FrameworkException Indicates some system error.
 */
public void delete(UserRequestMaintenanceDeleteInDto input) throws FrameworkException, ApplicationExceptions {
    UOW uow = null;
    try {
        // create the UOW
        uow = new UOW();
        // invoke the delete passing the UOW
        delete(input, uow);
        // Commit the changes
        uow.commit();
        // Print Debug Information for the output
        if (log.isDebugEnabled())
            log.debug("Successfully deleted the domain object");
    } catch (FrameworkException e) {
        // If it is, then re-throw as ApplicationsExceptions, else throw the FrameworkException.
        if (e.getCause() != null && e.getCause() instanceof ApplicationExceptions) {
            throw (ApplicationExceptions) e.getCause();
        } else if (e.getCause() != null && e.getCause() instanceof ApplicationException) {
            ApplicationExceptions appExps = new ApplicationExceptions();
            appExps.add((ApplicationException) e.getCause());
            throw appExps;
        } else
            throw e;
    } finally {
        if (uow != null)
            uow.rollback();
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ApplicationException(org.jaffa.exceptions.ApplicationException) FrameworkException(org.jaffa.exceptions.FrameworkException) UOW(org.jaffa.persistence.UOW)

Example 87 with ApplicationException

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

the class UserRequestMaintenanceTx method prevalidateUpdate.

// .//GEN-END:_retrieve_1_be
// .//GEN-BEGIN:_prevalidateUpdate_1_be
/**
 * This method is used to perform prevalidations before updating an existing instance of UserRequest.
 * @param input The new values for the domain object.
 * @throws ApplicationExceptions This will be thrown if the input contains invalid data.
 * @throws FrameworkException Indicates some system error.
 * @return The object details.
 */
public UserRequestMaintenancePrevalidateOutDto prevalidateUpdate(UserRequestMaintenanceUpdateInDto input) throws FrameworkException, ApplicationExceptions {
    UOW uow = null;
    try {
        // Print Debug Information for the input
        if (log.isDebugEnabled())
            log.debug("Input: " + input);
        // create the UOW
        uow = new UOW();
        // Preprocess the input
        preprocess(uow, input);
        // Retrieve the object
        UserRequest domain = load(uow, input);
        // Validate the foreign objects
        validateForeignObjects(uow, input);
        // Update the domain object
        updateDomain(uow, input, domain, true);
        // Build the outbound dto
        UserRequestMaintenancePrevalidateOutDto output = createPrevalidateOutDto(uow, domain, input);
        // Print Debug Information for the output
        if (log.isDebugEnabled())
            log.debug("Output: " + output);
        return output;
    } catch (FrameworkException e) {
        // If it is, then re-throw as ApplicationsExceptions, else throw the FrameworkException.
        if (e.getCause() != null && e.getCause() instanceof ApplicationExceptions) {
            throw (ApplicationExceptions) e.getCause();
        } else if (e.getCause() != null && e.getCause() instanceof ApplicationException) {
            ApplicationExceptions appExps = new ApplicationExceptions();
            appExps.add((ApplicationException) e.getCause());
            throw appExps;
        } else
            throw e;
    } finally {
        if (uow != null)
            uow.rollback();
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ApplicationException(org.jaffa.exceptions.ApplicationException) FrameworkException(org.jaffa.exceptions.FrameworkException) UOW(org.jaffa.persistence.UOW) UserRequest(org.jaffa.applications.jaffa.modules.user.domain.UserRequest)

Example 88 with ApplicationException

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

the class BeanMoulder method deleteBeanData.

private static void deleteBeanData(String path, DomainDAO source, UOW uow, MouldHandler handler, GraphMapping mapping, IPersistent domainObject) throws InstantiationException, IllegalAccessException, InvocationTargetException, ApplicationExceptions, FrameworkException {
    try {
        // Fire 'startBean' handler
        if (handler != null)
            handler.startBean(path, source, domainObject);
        // ----------------------------------------------------------------
        // Now loop through children, if there is one, delete it, and leave parent alone
        boolean deleteChild = false;
        // Reflect any related objects
        for (Iterator it = mapping.getRelatedFields().iterator(); it.hasNext(); ) {
            String field = (String) it.next();
            if (source.hasChanged(field)) {
                Object value = getProperty(mapping.getDataFieldDescriptor(field), source);
                if (value != null) {
                    if (value.getClass().isArray()) {
                        // The related field is an array of objects (one-to-many)
                        Object[] values = (Object[]) value;
                        for (int i = 0; i < values.length; i++) {
                            // Assumes its a DAO....what else could it be?
                            DomainDAO dao = (DomainDAO) values[i];
                            if (dao != null) {
                                deleteChild = true;
                                deleteChildBean(path + "." + field + "[" + i + "]", dao, uow, handler, domainObject, mapping, field);
                            }
                        }
                    } else {
                        // The related field is a single object (one-to-many)
                        // Assumes its a DAO....what else could it be?
                        DomainDAO dao = (DomainDAO) value;
                        deleteChild = true;
                        deleteChildBean(path + "." + field, dao, uow, handler, domainObject, mapping, field);
                    }
                }
            }
        }
        // Delete this record, as it had no children
        if (!deleteChild) {
            log.debug("UOW.Delete Domain Object");
            // Fire 'startBeanDelete' handler
            if (handler != null)
                handler.startBeanDelete(path, source, domainObject);
            uow.delete(domainObject);
            // Fire 'endBeanDelete' handler
            if (handler != null)
                handler.endBeanDelete(path, source, domainObject);
        }
        // Fire 'endBean' handler
        if (handler != null)
            handler.endBean(path, source, domainObject);
    } catch (ApplicationException e) {
        ApplicationExceptions aes = new ApplicationExceptions();
        aes.add(e);
        throw aes;
    }
}
Also used : ApplicationException(org.jaffa.exceptions.ApplicationException) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) Iterator(java.util.Iterator) DomainDAO(org.jaffa.beans.moulding.data.domain.DomainDAO)

Example 89 with ApplicationException

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

the class BeanMoulder method moldFromDomain.

/**
 * Mould data from domain object and its related objects into a new JavaBean based
 * domain object graph, based on the defined mapping rules.
 * @param source Source object to mould data from, typically extends Persistent
 * @param target Target object to mould data to, typically extends DomainDAO
 * @param graph The mapping class with the rules of how to map this source object
 * @param filter Filter object that it is used to control what fields are populated or the target objects
 * @param objectPath  The path of this object being processed. This identifies possible parent
 * and/or indexed entries where this object is contained.
 * @param includeKeys true if key fields should be included in results regardless of the filters
 * @throws ApplicationExceptions Thrown if one or more application logic errors are generated during moulding
 * @throws FrameworkException Thrown if any runtime moulding error has occured.
 */
public static void moldFromDomain(Object source, Object target, GraphMapping graph, MappingFilter filter, String objectPath, boolean includeKeys) throws ApplicationExceptions, FrameworkException {
    if (graph == null)
        graph = MappingFactory.getInstance(target);
    // throw new InstantiationException("A GraphMapping must be supplied");
    if (filter == null)
        filter = new MappingFilter(graph);
    try {
        // get list of target fileds to populate
        String[] tFields = graph.getDataFieldNames();
        if (tFields != null && tFields.length != 0)
            for (int i = 0; i < tFields.length; i++) {
                // Try to map a source to a target
                String tName = tFields[i];
                String fullName = tName;
                if (objectPath != null)
                    fullName = objectPath + "." + fullName;
                if (filter == null || filter.isFieldIncluded(fullName) || (includeKeys && graph.isKeyField(tName))) {
                    String sName = graph.getDomainFieldName(tName);
                    PropertyDescriptor tDesc = graph.getDataFieldDescriptor(tName);
                    PropertyDescriptor sDesc = graph.getDomainFieldDescriptor(tName);
                    // DAO descriptor with a setter, and a DO descriptor with a getter
                    if (sDesc == null) {
                        log.error("No Getter for " + tName + " in path " + fullName);
                    } else {
                        // in case getter is not public, make it available
                        Method sm = sDesc.getReadMethod();
                        if (!sm.isAccessible())
                            sm.setAccessible(true);
                        // get the setter, and make is available if needed
                        Method tm = tDesc.getWriteMethod();
                        if (!tm.isAccessible())
                            tm.setAccessible(true);
                        // Set the value if the source and target are the same datatype
                        Class tClass = tDesc.getPropertyType();
                        Class sClass = sDesc.getPropertyType();
                        if (tClass.isAssignableFrom(sClass)) {
                            // Get the value being copied
                            Object sValue = sm.invoke(source, (Object[]) null);
                            if (sValue != null) {
                                tm.invoke(target, new Object[] { sValue });
                                log.debug("Set " + tName + " = " + sValue);
                            } else
                                log.debug(tName + " no set, NULL value");
                        // See if there is a datatype mapper for these classes
                        } else if (DataTypeMapper.instance().isMappable(sClass, tClass)) {
                            // Get the value being copied
                            Object sValue = sm.invoke(source, (Object[]) null);
                            if (sValue != null) {
                                sValue = DataTypeMapper.instance().map(sValue, tClass);
                                tm.invoke(target, new Object[] { sValue });
                                log.debug("Set " + tName + " = " + sValue);
                            } else
                                log.debug(tName + " no set, NULL value");
                        // See if target is a DAO, this could be a foreign object or one-to-one relationship...
                        } else if (DomainDAO.class.isAssignableFrom(tClass) && IPersistent.class.isAssignableFrom(sClass)) {
                            // Get the mapper for the related DAO, if it has keys, it must be a foriegn object
                            if (graph.isForeignField(tName)) {
                                // look at foreign key fields, and make sure they are not null
                                List foreignKeys = graph.getForeignKeys(tName);
                                List foreignKeyValues = new ArrayList();
                                boolean nullKey = false;
                                for (Iterator k = foreignKeys.iterator(); k.hasNext(); ) {
                                    String doProp = (String) k.next();
                                    Object value = null;
                                    PropertyDescriptor doPd = graph.getRealDomainFieldDescriptor(doProp);
                                    if (doPd != null && doPd.getReadMethod() != null) {
                                        Method m = doPd.getReadMethod();
                                        if (!m.isAccessible())
                                            m.setAccessible(true);
                                        value = m.invoke(source, new Object[] {});
                                        if (value == null)
                                            nullKey = true;
                                        foreignKeyValues.add(value);
                                    } else {
                                        throw new MouldException(MouldException.INVALID_FK_MAPPING, objectPath, doProp, graph.getDomainClassShortName());
                                    }
                                }
                                if (nullKey) {
                                    log.debug("Did not create skeleton object '" + tClass.getName() + "': one or more foreign key values missing.");
                                } else {
                                    // Create the foreign object
                                    log.debug("Creating foreign object - " + tClass.getName());
                                    Object newDAO = newDAO(tClass);
                                    boolean createSkeleton = true;
                                    // Only retrieve related domain object and introspect if need
                                    if (filter.areSubFieldsIncluded(fullName)) {
                                        // read object and introspect all
                                        log.debug("Read foreign object '" + fullName + "' and mold");
                                        try {
                                            Object sValue = sm.invoke(source, (Object[]) null);
                                            if (sValue != null) {
                                                BeanMoulder.moldFromDomain(sValue, newDAO, null, filter, fullName, true);
                                                createSkeleton = false;
                                            }
                                        } catch (InvocationTargetException e) {
                                            // If the foreign object is not found, warn and create the skeleton
                                            if (e.getCause() != null && e.getCause() instanceof InvalidForeignKeyException)
                                                log.warn("All foreign keys present, but foreign object does not exist");
                                            else
                                                throw e;
                                        }
                                    }
                                    if (createSkeleton) {
                                        // just set foreign keys from current object
                                        log.debug("Set keys on skeleton foreign object only");
                                        GraphMapping graph2 = MappingFactory.getInstance(newDAO);
                                        Set keys = graph2.getKeyFields();
                                        if (keys == null || keys.size() != foreignKeyValues.size()) {
                                            throw new MouldException(MouldException.MISMATCH_FK_MAPPING, objectPath, target.getClass().getName(), newDAO.getClass().getName());
                                        }
                                        int k2 = 0;
                                        // Look through all the foreign keys on the skeleton object
                                        for (Iterator k = keys.iterator(); k.hasNext(); k2++) {
                                            String keyField = (String) k.next();
                                            Object keyValue = foreignKeyValues.get(k2);
                                            PropertyDescriptor pd = graph2.getDataFieldDescriptor(keyField);
                                            if (pd != null && pd.getWriteMethod() != null) {
                                                Method m = pd.getWriteMethod();
                                                if (!m.isAccessible())
                                                    m.setAccessible(true);
                                                m.invoke(newDAO, new Object[] { keyValue });
                                            } else {
                                                throw new MouldException(MouldException.CANT_SET_KEY_FIELD, objectPath, keyField, newDAO.getClass().getName());
                                            }
                                        }
                                    }
                                    tm.invoke(target, new Object[] { newDAO });
                                    log.debug("Set " + tName + " = " + newDAO);
                                }
                            } else {
                                // This is not a foreign object, must be a related object
                                if (filter.areSubFieldsIncluded(fullName)) {
                                    // Create the related object
                                    log.debug("Creating One-To-One object - " + tClass.getName());
                                    Object newDAO = newDAO(tClass);
                                    // read object and introspect all
                                    log.debug("Read related object '" + fullName + "' and mold");
                                    Object sValue = sm.invoke(source, (Object[]) null);
                                    if (sValue != null) {
                                        BeanMoulder.moldFromDomain(sValue, newDAO, null, filter, fullName, true);
                                    } else {
                                        log.debug("Related object '" + fullName + "' not found. Ignore it!");
                                    }
                                    tm.invoke(target, new Object[] { newDAO });
                                    log.debug("Set " + tName + " = " + newDAO);
                                } else
                                    log.debug("No subfields for object " + fullName + " included. Object not retrieved");
                            }
                        // END-related object
                        // See if Target may be an array of DAO's
                        } else if (tClass.isArray() && DomainDAO.class.isAssignableFrom(tClass.getComponentType()) && filter.areSubFieldsIncluded(fullName)) {
                            log.debug("Target is an array of DAO's");
                            log.debug("Read related objects '" + fullName + "' and mold");
                            Object sValue = sm.invoke(source, (Object[]) null);
                            if (sClass.isArray() && IPersistent.class.isAssignableFrom(sClass.getComponentType())) {
                                log.debug("Source is an array of Persistent Objects");
                                Object[] sArray = (Object[]) sValue;
                                if (sArray.length > 0) {
                                    Object[] tArray = (Object[]) Array.newInstance(tClass.getComponentType(), sArray.length);
                                    log.debug("Translate Array of Size " + sArray.length);
                                    for (int j = 0; j < sArray.length; j++) {
                                        Object newDAO = newDAO(tClass.getComponentType());
                                        BeanMoulder.moldFromDomain(sArray[j], newDAO, null, filter, fullName, false);
                                        tArray[j] = newDAO;
                                        log.debug("Add to array [" + j + "] : " + newDAO);
                                    }
                                    tm.invoke(target, new Object[] { (Object) tArray });
                                    log.debug("Set Array " + tName);
                                } else
                                    log.debug("Source Array is empty! Do Nothing");
                            }
                        // source is DO array
                        // Error... No way to map property
                        } else {
                            String err = "Can't Mold Property " + fullName + " from " + sClass.getName() + " to " + tClass.getName();
                            log.error(err);
                            throw new RuntimeException(err);
                        }
                    }
                }
            // is included in filtered fields
            }
        // Clear changed fields on updated DAO
        if (target != null && target instanceof DomainDAO)
            ((DomainDAO) target).clearChanges();
    } catch (IllegalAccessException e) {
        MouldException me = new MouldException(MouldException.ACCESS_ERROR, objectPath, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    } catch (InvocationTargetException e) {
        if (e.getCause() != null) {
            if (e.getCause() instanceof FrameworkException)
                throw (FrameworkException) e.getCause();
            if (e.getCause() instanceof ApplicationExceptions)
                throw (ApplicationExceptions) e.getCause();
            if (e.getCause() instanceof ApplicationException) {
                ApplicationExceptions aes = new ApplicationExceptions();
                aes.add((ApplicationException) e.getCause());
                throw aes;
            }
        }
        MouldException me = new MouldException(MouldException.INVOCATION_ERROR, objectPath, e);
        log.error(me.getLocalizedMessage(), me.getCause());
        throw me;
    } catch (InstantiationException e) {
        MouldException me = new MouldException(MouldException.INSTANTICATION_ERROR, objectPath, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    }
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) MappingFilter(org.jaffa.beans.moulding.mapping.MappingFilter) IPersistent(org.jaffa.persistence.IPersistent) Iterator(java.util.Iterator) DomainDAO(org.jaffa.beans.moulding.data.domain.DomainDAO) ArrayList(java.util.ArrayList) List(java.util.List) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) PropertyDescriptor(java.beans.PropertyDescriptor) FrameworkException(org.jaffa.exceptions.FrameworkException) InvalidForeignKeyException(org.jaffa.datatypes.exceptions.InvalidForeignKeyException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApplicationException(org.jaffa.exceptions.ApplicationException) GraphMapping(org.jaffa.beans.moulding.mapping.GraphMapping)

Example 90 with ApplicationException

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

the class FinderAction method do_DeleteQuery_Clicked.

public FormKey do_DeleteQuery_Clicked() {
    FormKey fk = null;
    FinderForm myForm = (FinderForm) form;
    FinderComponent2 myComp = (FinderComponent2) myForm.getComponent();
    if (myForm.doValidateForLoadQuery(request)) {
        if (myComp.getSavedQueryId() != null) {
            try {
                myComp.deleteQuery();
                myForm.doValidate(request);
            } catch (ApplicationException e) {
                if (log.isDebugEnabled())
                    log.debug("ModifySearch Failed");
                myForm.raiseError(request, ActionMessages.GLOBAL_MESSAGE, e);
            }
        }
    }
    try {
        fk = myComp.displayCriteria();
    } catch (ApplicationExceptions e) {
        if (log.isDebugEnabled())
            log.debug("ModifySearch Failed");
        myForm.raiseError(request, ActionMessages.GLOBAL_MESSAGE, e);
    } catch (FrameworkException e) {
        log.error(null, e);
        myForm.raiseError(request, ActionMessages.GLOBAL_MESSAGE, "error.framework.general");
    }
    // Direct User back to current form
    if (fk == null)
        fk = myComp.getCriteriaFormKey();
    return fk;
}
Also used : ApplicationException(org.jaffa.exceptions.ApplicationException) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) FrameworkException(org.jaffa.exceptions.FrameworkException) FormKey(org.jaffa.presentation.portlet.FormKey)

Aggregations

ApplicationException (org.jaffa.exceptions.ApplicationException)125 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)114 FrameworkException (org.jaffa.exceptions.FrameworkException)97 UOW (org.jaffa.persistence.UOW)79 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 FormTemplate (org.jaffa.modules.printing.domain.FormTemplate)11 Criteria (org.jaffa.persistence.Criteria)11 Method (java.lang.reflect.Method)10 Iterator (java.util.Iterator)10 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 GraphMapping (org.jaffa.beans.moulding.mapping.GraphMapping)6 OutputCommand (org.jaffa.modules.printing.domain.OutputCommand)6 PrinterDefinition (org.jaffa.modules.printing.domain.PrinterDefinition)6 PrinterOutputType (org.jaffa.modules.printing.domain.PrinterOutputType)6 User (org.jaffa.applications.jaffa.modules.admin.domain.User)5 UserRequest (org.jaffa.applications.jaffa.modules.user.domain.UserRequest)5 UserTimeEntry (org.jaffa.applications.test.modules.time.domain.UserTimeEntry)5 Attachment (org.jaffa.components.attachment.domain.Attachment)5