Search in sources :

Example 66 with FrameworkException

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

the class UOW method commit.

/**
 * Objects that have been added, objects that have been deleted,
 * and objects that have been updated, will all be persisted via
 * an invocation of this method.
 * Note: After a successful commit, this object will free up its connection to the database,
 * and will no longer be available.
 *
 * @throws AddFailedException    if any error occurs during the addition of objects to the persistent store.
 * @throws UpdateFailedException if any error occurs while updating the objects of the persistent store.
 * @throws DeleteFailedException if any error occurs while deleting the objects of the persistent store.
 * @throws CommitFailedException if any error occurs during the commit.
 */
public void commit() throws AddFailedException, UpdateFailedException, DeleteFailedException, CommitFailedException {
    ensureActiveState();
    Collection deletes = m_engine.getDeletes();
    if (deletes != null) {
        if (m_messagingEngine == null) {
            try {
                m_messagingEngine = MessagingEngineFactory.newInstance(this);
            } catch (FrameworkException e) {
            // failed creating the messaging engine, do not fail the overall commit
            } catch (ApplicationExceptions applicationExceptions) {
            // failed creating the messaging engine, do not fail the overall commit
            }
        }
        if (m_messagingEngine != null) {
            m_messagingEngine.prepareDeletesForCommit(deletes);
        }
    }
    // commit the persistence engine
    m_engine.commit();
    // commit the messaging engine
    if (m_messagingEngine != null) {
        try {
            m_messagingEngine.commit();
        } catch (Exception e) {
            log.fatal("The database transaction has been committed. Exception raised while sending Message(s) to the JMS system", e);
            throw new CommitFailedException(null, e);
        }
    }
    close();
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) FrameworkException(org.jaffa.exceptions.FrameworkException) Collection(java.util.Collection) CommitFailedException(org.jaffa.persistence.exceptions.CommitFailedException) InvalidUowRuntimeException(org.jaffa.persistence.exceptions.InvalidUowRuntimeException) UpdateFailedException(org.jaffa.persistence.exceptions.UpdateFailedException) DeleteFailedException(org.jaffa.persistence.exceptions.DeleteFailedException) UOWException(org.jaffa.persistence.exceptions.UOWException) CommitFailedException(org.jaffa.persistence.exceptions.CommitFailedException) PostLoadFailedException(org.jaffa.persistence.exceptions.PostLoadFailedException) FrameworkException(org.jaffa.exceptions.FrameworkException) AddFailedException(org.jaffa.persistence.exceptions.AddFailedException) InactiveUowRuntimeException(org.jaffa.persistence.exceptions.InactiveUowRuntimeException) QueryFailedException(org.jaffa.persistence.exceptions.QueryFailedException) AlreadyLockedObjectException(org.jaffa.persistence.exceptions.AlreadyLockedObjectException) RollbackFailedException(org.jaffa.persistence.exceptions.RollbackFailedException)

Example 67 with FrameworkException

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

the class MessagingEngineFactory method newInstance.

/**
 * Returns an instance of a {@link IMessagingEngine}.  If a factory has been set, use it.  Otherwise create
 * an instance of the messaging engine as defined by the static engine name string.
 *
 * @param uow the unit of work the messaging engine will use to communicate with the persistence layer
 * @throws FrameworkException    in case any internal error occurs.
 * @throws ApplicationExceptions Indicates application error(s).
 */
public static IMessagingEngine newInstance(UOW uow) throws FrameworkException, ApplicationExceptions {
    if (messagingEngineFactory != null) {
        return messagingEngineFactory.newInstance(uow);
    }
    try {
        if (log.isDebugEnabled()) {
            log.debug("Creating an instance of the Messaging Engine by invoking the 'public static " + "IMessagingEngine getInstance()' method of " + c_engineName);
        }
        Class engineClass = Class.forName(c_engineName);
        Method m = engineClass.getMethod("getInstance", new Class[] { UOW.class });
        return (IMessagingEngine) m.invoke(null, uow);
    } catch (Exception e) {
        String str = "The Messaging Engine '" + c_engineName + "' could not be instantiated";
        log.error(str, e);
        throw ExceptionHelper.throwAFR(e);
    }
}
Also used : Method(java.lang.reflect.Method) FrameworkException(org.jaffa.exceptions.FrameworkException)

Example 68 with FrameworkException

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

the class UserTimeEntryViewerAction method do_Update_Clicked.

// .//GEN-END:_do_Close_Clicked_2_be
// .//GEN-BEGIN:_do_Update_Clicked_1_be
/**
 * Invokes the updateObject() method on the component.
 * @return The FormKey for the Update screen.
 */
public FormKey do_Update_Clicked() {
    FormKey fk = null;
    // .//GEN-END:_do_Update_Clicked_1_be
    // Add custom code before processing the action //GEN-FIRST:_do_Update_Clicked_1
    // .//GEN-LAST:_do_Update_Clicked_1
    // .//GEN-BEGIN:_do_Update_Clicked_2_be
    UserTimeEntryViewerForm myForm = (UserTimeEntryViewerForm) form;
    UserTimeEntryViewerComponent myComp = (UserTimeEntryViewerComponent) myForm.getComponent();
    try {
        // .//GEN-END:_do_Update_Clicked_2_be
        // Add custom code before invoking the component //GEN-FIRST:_do_Update_Clicked_2
        // .//GEN-LAST:_do_Update_Clicked_2
        // .//GEN-BEGIN:_do_Update_Clicked_3_be
        fk = myComp.updateObject();
    } catch (ApplicationExceptions e) {
        if (log.isDebugEnabled())
            log.debug("Update 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.getViewerFormKey();
    return fk;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) FrameworkException(org.jaffa.exceptions.FrameworkException) FormKey(org.jaffa.presentation.portlet.FormKey)

Example 69 with FrameworkException

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

the class BeanMoulder method updateBean.

/**
 * Take a source object and try and mold it back it its domain object
 * @param path The path of this object being processed. This identifies possible parent
 * and/or indexed entries where this object is contained.
 * @param source Source object to mould from, typically a DomainDAO
 * @param uow Transaction handle all creates/update will be performed within.
 * Throws an exception if null.
 * @param handler Possible bean handler to be used when processing this source object graph
 * @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 updateBean(String path, DomainDAO source, UOW uow, MouldHandler handler) throws ApplicationExceptions, FrameworkException {
    log.debug("Update Bean " + path);
    // Call custom validation code in the DAO
    source.validate();
    ApplicationExceptions aes = new ApplicationExceptions();
    if (uow == null) {
        String err = "UOW Required";
        log.error(err);
        throw new RuntimeException(err);
    }
    try {
        IPersistent domainObject = null;
        GraphMapping mapping = MappingFactory.getInstance(source);
        Map keys = new LinkedHashMap();
        Class doClass = mapping.getDomainClass();
        // Get the key fields used in the domain object
        boolean gotKeys = fillInKeys(path, source, mapping, keys);
        // read DO based on key
        if (gotKeys) {
            // get the method on the DO to read via PK
            Method[] ma = doClass.getMethods();
            Method findByPK = null;
            for (int i = 0; i < ma.length; i++) {
                if (ma[i].getName().equals("findByPK")) {
                    if (ma[i].getParameterTypes().length == (keys.size() + 1) && (ma[i].getParameterTypes())[0] == UOW.class) {
                        // Found with name and correct no. of input params
                        findByPK = ma[i];
                        break;
                    }
                }
            }
            if (findByPK == null) {
                aes.add(new DomainObjectNotFoundException(doClass.getName() + " @ " + path));
                throw aes;
            }
            // Build input array
            Object[] inputs = new Object[keys.size() + 1];
            {
                inputs[0] = uow;
                int i = 1;
                for (Iterator it = keys.values().iterator(); it.hasNext(); i++) {
                    inputs[i] = it.next();
                }
            }
            // Find Object based on key
            domainObject = (IPersistent) findByPK.invoke(null, inputs);
        } else
            log.debug("Object " + path + " has either missing or null key values - Assume Create is needed");
        // Create object if not found
        if (domainObject == null) {
            // NEW OBJECT, create and reflect keys
            log.debug("DO '" + mapping.getDomainClassShortName() + "' not found with key, create a new one...");
            domainObject = (IPersistent) doClass.newInstance();
            // set the key fields
            for (Iterator it = keys.keySet().iterator(); it.hasNext(); ) {
                String keyField = (String) it.next();
                Object value = keys.get(keyField);
                updateProperty(mapping.getDomainFieldDescriptor(keyField), value, domainObject);
            }
        } else {
            log.debug("Found DO '" + mapping.getDomainClassShortName() + "' with key,");
        }
        // Now update all domain fields
        updateBeanData(path, source, uow, handler, mapping, domainObject);
    } catch (IllegalAccessException e) {
        MouldException me = new MouldException(MouldException.ACCESS_ERROR, path, 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) {
                aes.add((ApplicationException) e.getCause());
                throw aes;
            }
        }
        MouldException me = new MouldException(MouldException.INVOCATION_ERROR, path, e);
        log.error(me.getLocalizedMessage(), me.getCause());
        throw me;
    } catch (InstantiationException e) {
        MouldException me = new MouldException(MouldException.INSTANTICATION_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) FrameworkException(org.jaffa.exceptions.FrameworkException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) ApplicationException(org.jaffa.exceptions.ApplicationException) IPersistent(org.jaffa.persistence.IPersistent) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException) Iterator(java.util.Iterator) UOW(org.jaffa.persistence.UOW) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) GraphMapping(org.jaffa.beans.moulding.mapping.GraphMapping)

Example 70 with FrameworkException

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

the class BeanMoulder 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.
 */
private static void updateChildBean(String path, DomainDAO source, UOW uow, MouldHandler handler, IPersistent parentDomain, GraphMapping parentMapping, String parentField) throws ApplicationExceptions, FrameworkException {
    log.debug("Update Child Bean " + path);
    // Call custom validation code in the DAO
    source.validate();
    ApplicationExceptions aes = new ApplicationExceptions();
    if (uow == null) {
        String err = "UOW Required";
        log.error(err);
        throw new RuntimeException(err);
    }
    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) {
            // No keys, must be one-to-one
            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)
                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 MouldException(MouldException.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);
                    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
                    MultipleDomainObjectsFoundException m = new MultipleDomainObjectsFoundException(criteria.getTable() + " @ " + path);
                    aes.add(m);
                    throw aes;
                }
            } else {
                log.debug("Object " + path + " has either missing or null key values - Assume Create is needed");
            }
        }
        // Create object if not found
        if (domainObject == null) {
            // NEW OBJECT, create and reflect keys
            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 MouldException(MouldException.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();
                Object value = keys.get(keyField);
                updateProperty(mapping.getDomainFieldDescriptor(keyField), value, domainObject);
            }
        } else {
            log.debug("Found DO '" + mapping.getDomainClassShortName() + "' with key,");
        }
        // Now update all domain fields
        updateBeanData(path, source, uow, handler, mapping, domainObject);
    } catch (IllegalAccessException e) {
        MouldException me = new MouldException(MouldException.ACCESS_ERROR, path, 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) {
                aes.add((ApplicationException) e.getCause());
                throw aes;
            }
        }
        MouldException me = new MouldException(MouldException.INVOCATION_ERROR, path, e);
        log.error(me.getLocalizedMessage(), me.getCause());
        throw me;
    } catch (InstantiationException e) {
        MouldException me = new MouldException(MouldException.INSTANTICATION_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) FrameworkException(org.jaffa.exceptions.FrameworkException) Method(java.lang.reflect.Method) Criteria(org.jaffa.persistence.Criteria) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) ApplicationException(org.jaffa.exceptions.ApplicationException) IPersistent(org.jaffa.persistence.IPersistent) MultipleDomainObjectsFoundException(org.jaffa.exceptions.MultipleDomainObjectsFoundException) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) GraphMapping(org.jaffa.beans.moulding.mapping.GraphMapping)

Aggregations

FrameworkException (org.jaffa.exceptions.FrameworkException)348 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)293 FormKey (org.jaffa.presentation.portlet.FormKey)204 ApplicationException (org.jaffa.exceptions.ApplicationException)101 GridModel (org.jaffa.presentation.portlet.widgets.model.GridModel)100 GridModelRow (org.jaffa.presentation.portlet.widgets.model.GridModelRow)99 UOW (org.jaffa.persistence.UOW)87 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 Method (java.lang.reflect.Method)14 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)12 Iterator (java.util.Iterator)11 IPersistent (org.jaffa.persistence.IPersistent)10 Map (java.util.Map)9 FormTemplate (org.jaffa.modules.printing.domain.FormTemplate)9 Criteria (org.jaffa.persistence.Criteria)9 LinkedHashMap (java.util.LinkedHashMap)7 Attachment (org.jaffa.components.attachment.domain.Attachment)6 InvalidForeignKeyException (org.jaffa.datatypes.exceptions.InvalidForeignKeyException)6 MultipleDomainObjectsFoundException (org.jaffa.exceptions.MultipleDomainObjectsFoundException)6 FormDefinition (org.jaffa.modules.printing.domain.FormDefinition)6