Search in sources :

Example 1 with PreAddFailedException

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

the class AddInterceptor method invoke.

/**
 *Performs the logic associated with adding Persistent objects to the database.
 * This will add each object in the PersistentTransaction's ADD collection to the database, utilising the JdbcBridge.
 * It will then pass on the control to the next Interceptor in the chain.
 * @param pt The PersistentTransaction object, on which the Interceptor is to be executed.
 * @throws UOWException if any error occurs.
 * @return the output from the next Interceptor in the chain.
 */
public Object invoke(PersistentTransaction pt) throws UOWException {
    Collection objects = pt.getAdds();
    if (objects != null) {
        // add the objects to the database
        for (Iterator i = objects.iterator(); i.hasNext(); ) {
            IPersistent object = (IPersistent) i.next();
            try {
                if (log.isDebugEnabled())
                    log.debug("Invoking JdbcBridge.executeAdd() for the object " + object);
                JdbcBridge.executeAdd(object, pt.getDataSource());
                pt.getDataSource().clearObjectCache();
            } catch (SQLIntegrityConstraintViolationException e) {
                if (e.getErrorCode() == PRIMARY_KEY_ERROR_CODE) {
                    String str = "The primary-key is not unique: " + this;
                    log.error(str);
                    String labelToken = null;
                    try {
                        labelToken = PersistentHelper.getLabelToken(object.getClass().getName());
                    } catch (Exception ex) {
                    // don't do anything.. just return the domainClassName
                    }
                    if (labelToken == null)
                        labelToken = MessageHelper.tokenize(object.getClass().getName());
                    throw new PreAddFailedException(null, new DuplicateKeyException(labelToken));
                } else {
                    String str = "Error while adding the Persistent object to the database: " + object;
                    log.error(str, e);
                    // was checked in pre-add previously
                    throw new PreAddFailedException(null, e);
                }
            } catch (Exception e) {
                String str = "Error while adding the Persistent object to the database: " + object;
                log.error(str, e);
                throw new AddFailedException(null, e);
            }
            i.remove();
        }
    }
    // pass control to the next interceptor in the chain
    if (getNextInterceptor() != null) {
        if (log.isDebugEnabled())
            log.debug("Invoking the next Interceptor in the chain " + getNextInterceptor().getClass().getName());
        return getNextInterceptor().invoke(pt);
    } else {
        if (log.isDebugEnabled())
            log.debug("This is the end of the Interceptor chain");
        return null;
    }
}
Also used : IPersistent(org.jaffa.persistence.IPersistent) PreAddFailedException(org.jaffa.persistence.exceptions.PreAddFailedException) AddFailedException(org.jaffa.persistence.exceptions.AddFailedException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) UOWException(org.jaffa.persistence.exceptions.UOWException) PreAddFailedException(org.jaffa.persistence.exceptions.PreAddFailedException) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) AddFailedException(org.jaffa.persistence.exceptions.AddFailedException) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException) PreAddFailedException(org.jaffa.persistence.exceptions.PreAddFailedException)

Aggregations

SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)1 DuplicateKeyException (org.jaffa.exceptions.DuplicateKeyException)1 IPersistent (org.jaffa.persistence.IPersistent)1 AddFailedException (org.jaffa.persistence.exceptions.AddFailedException)1 PreAddFailedException (org.jaffa.persistence.exceptions.PreAddFailedException)1 UOWException (org.jaffa.persistence.exceptions.UOWException)1