Search in sources :

Example 1 with IPersistenceLoggingPlugin

use of org.jaffa.persistence.logging.IPersistenceLoggingPlugin in project jaffa-framework by jaffa-projects.

the class PersistentTransaction method commit.

/**
 * Commits all changes executed against the persistent store.
 *
 * @throws CommitFailedException if any error occurs during the commit.
 * @throws SQLException          if any database error occurs.
 */
public void commit() throws CommitFailedException, SQLException {
    // If there is a persistence logging plugin commit the changes
    if (persistenceLoggingPlugins != null) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("Invoking the writeLog method on the IPersistenceLoggingPlugin instances");
            }
            commitFlag = true;
            for (IPersistenceLoggingPlugin persistenceLoggingPlugin : persistenceLoggingPlugins) {
                persistenceLoggingPlugin.writeLog();
            }
        } catch (Exception e) {
            ApplicationExceptions appExps = ExceptionHelper.extractApplicationExceptions(e);
            if (appExps != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Logic Error in writing the logs for a transaction", appExps);
                }
                e = appExps;
            } else {
                log.error("Error in writing the logs for a transaction", e);
            }
            throw new CommitFailedException(null, e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Invoking the commit on the DataSource");
    }
    // Commit the change to the DB
    DataSource dataSource = dataSourceContainer.get(false);
    if (dataSource != null) {
        dataSource.commit();
    }
    // Clean up changes and free the data source
    clearCollections();
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) IPersistenceLoggingPlugin(org.jaffa.persistence.logging.IPersistenceLoggingPlugin) DataSourceCreationException(org.jaffa.persistence.engines.jdbcengine.datasource.exceptions.DataSourceCreationException) FrameworkException(org.jaffa.exceptions.FrameworkException) SQLException(java.sql.SQLException)

Example 2 with IPersistenceLoggingPlugin

use of org.jaffa.persistence.logging.IPersistenceLoggingPlugin in project jaffa-framework by jaffa-projects.

the class PersistentTransaction method addObject.

/**
 * Adds an object to the transaction to be written.
 * The PreAdd trigger for will be invoked and the domain object will be validated before the addition to the transaction.
 *
 * @param object                the object to be created.
 * @param invokeLifecycleEvents Lifecycle events will be invoked if this parameter is true.
 * @throws AddFailedException if any error occurs during the validation of the persistent object.
 * @throws IllegalPersistentStateRuntimeException
 *                            this RuntimeException will be thrown if the domain object has been submitted to the UOW for an Add/Update/Delete and commit hasnt yet been performed.
 */
public void addObject(IPersistent object, boolean invokeLifecycleEvents) throws AddFailedException, IllegalPersistentStateRuntimeException {
    if (object.isQueued()) {
        String str = "The domain object has already been submitted to the UOW for an Add/Update/Delete. No more updates can be performed until after a commit";
        log.error(str);
        throw new IllegalPersistentStateRuntimeException(str);
    }
    List<ILifecycleHandler> handlers = null;
    if (object != null) {
        handlers = object.getLifecycleHandlers();
    }
    if (invokeLifecycleEvents) {
        if (log.isDebugEnabled())
            log.debug("Invoking the PreAdd trigger on the Persistent object");
        for (ILifecycleHandler lifeCycleHandler : handlers) {
            lifeCycleHandler.preAdd();
        }
    }
    if (adds == null) {
        adds = new ArrayList<IPersistent>();
    }
    adds.add(object);
    if (log.isDebugEnabled()) {
        log.debug("Added the Persistent object to the ADD collection");
    }
    object.setQueued(true);
    if (invokeLifecycleEvents) {
        // If there is a persistence logging plugin add the object to it
        if (persistenceLoggingPlugins != null) {
            try {
                if (log.isDebugEnabled()) {
                    log.debug("Invoking the add method on the IPersistenceLoggingPlugin instances");
                }
                for (IPersistenceLoggingPlugin persistenceLoggingPlugin : persistenceLoggingPlugins) {
                    persistenceLoggingPlugin.add(object);
                }
            } catch (Exception e) {
                log.error("Error in logging the ADD event", e);
                throw new AddFailedException(null, e);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Invoking the PostAdd trigger on the Persistent object");
        }
        // Invoke the post add behaviors
        for (ILifecycleHandler lifecycleHandler : handlers) {
            lifecycleHandler.postAdd();
        }
    }
}
Also used : ILifecycleHandler(org.jaffa.persistence.ILifecycleHandler) IPersistent(org.jaffa.persistence.IPersistent) IPersistenceLoggingPlugin(org.jaffa.persistence.logging.IPersistenceLoggingPlugin) DataSourceCreationException(org.jaffa.persistence.engines.jdbcengine.datasource.exceptions.DataSourceCreationException) FrameworkException(org.jaffa.exceptions.FrameworkException) SQLException(java.sql.SQLException)

Example 3 with IPersistenceLoggingPlugin

use of org.jaffa.persistence.logging.IPersistenceLoggingPlugin in project jaffa-framework by jaffa-projects.

the class PersistentTransaction method deleteObject.

/**
 * Adds an object to the transaction to be deleted.
 * The PreDelete trigger will be invoked before the deletion from the transaction.
 *
 * @param object                the object to be deleted.
 * @param invokeLifecycleEvents Lifecycle events will be invoked if this parameter is true.
 * @throws DeleteFailedException if any error occurs during the process.
 * @throws IllegalPersistentStateRuntimeException
 *                               this RuntimeException will be thrown if the domain object has been submitted to the UOW for an Add/Update/Delete and commit hasnt yet been performed.
 */
public void deleteObject(IPersistent object, boolean invokeLifecycleEvents) throws DeleteFailedException, IllegalPersistentStateRuntimeException {
    if (object.isQueued()) {
        String str = "The domain object has already been submitted to the UOW for an Add/Update/Delete. No more updates can be performed until after a commit";
        log.error(str);
        throw new IllegalPersistentStateRuntimeException(str);
    }
    List<ILifecycleHandler> handlers = null;
    if (object != null) {
        handlers = object.getLifecycleHandlers();
    }
    if (invokeLifecycleEvents) {
        if (log.isDebugEnabled())
            log.debug("Invoking the PreDelete trigger on the Persistent object");
        for (ILifecycleHandler lifecycleHandler : handlers) {
            lifecycleHandler.preDelete();
        }
    }
    if (deletes == null) {
        deletes = new ArrayList<IPersistent>();
    }
    deletes.add(object);
    if (log.isDebugEnabled()) {
        log.debug("Added the Persistent object to the DELETE collection");
    }
    object.setQueued(true);
    if (invokeLifecycleEvents) {
        // If there is a persistence logging plugin delete the object
        if (persistenceLoggingPlugins != null) {
            try {
                if (log.isDebugEnabled()) {
                    log.debug("Invoking the delete method on the IPersistenceLoggingPlugin instances");
                }
                for (IPersistenceLoggingPlugin persistenceLoggingPlugin : persistenceLoggingPlugins) {
                    persistenceLoggingPlugin.delete(object);
                }
            } catch (Exception e) {
                log.error("Error in logging the DELETE event", e);
                throw new DeleteFailedException(null, e);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Invoking the PostDelete trigger on the Persistent object");
        }
        // Invoke the post delete behaviors
        for (ILifecycleHandler lifecycleHandler : handlers) {
            lifecycleHandler.postDelete();
        }
    }
}
Also used : ILifecycleHandler(org.jaffa.persistence.ILifecycleHandler) IPersistent(org.jaffa.persistence.IPersistent) IPersistenceLoggingPlugin(org.jaffa.persistence.logging.IPersistenceLoggingPlugin) DataSourceCreationException(org.jaffa.persistence.engines.jdbcengine.datasource.exceptions.DataSourceCreationException) FrameworkException(org.jaffa.exceptions.FrameworkException) SQLException(java.sql.SQLException)

Example 4 with IPersistenceLoggingPlugin

use of org.jaffa.persistence.logging.IPersistenceLoggingPlugin in project jaffa-framework by jaffa-projects.

the class PersistentTransaction method updateObject.

/**
 * Adds an object to the transaction to be updated.
 * The PreUpdate trigger will be invoked and the domain object will be validated before the updation to the transaction.
 *
 * @param object                the object to be updated.
 * @param invokeLifecycleEvents Lifecycle events will be invoked if this parameter is true.
 * @throws UpdateFailedException if any error occurs during the validation of the persistent object.
 * @throws IllegalPersistentStateRuntimeException
 *                               this RuntimeException will be thrown if the domain object has been submitted to the UOW for an Add/Update/Delete and commit hasnt yet been performed.
 */
public void updateObject(IPersistent object, boolean invokeLifecycleEvents) throws UpdateFailedException, IllegalPersistentStateRuntimeException {
    if (object.isQueued()) {
        String str = "The domain object has already been submitted to the UOW for an Add/Update/Delete. No more updates can be performed until after a commit";
        log.error(str);
        throw new IllegalPersistentStateRuntimeException(str);
    }
    if (!object.isModified()) {
        if (log.isDebugEnabled())
            log.debug("The Persistent object has not been modified. So it will not be added to the Interceptor queue for an Add");
        return;
    }
    List<ILifecycleHandler> handlers = null;
    if (object != null) {
        handlers = object.getLifecycleHandlers();
    }
    if (invokeLifecycleEvents) {
        if (log.isDebugEnabled())
            log.debug("Invoking the PreUpdate trigger on the Persistent object");
        for (ILifecycleHandler lifecycleHandler : handlers) {
            lifecycleHandler.preUpdate();
        }
    }
    if (updates == null) {
        updates = new ArrayList<IPersistent>();
    }
    updates.add(object);
    if (log.isDebugEnabled()) {
        log.debug("Added the Persistent object to the UPDATE collection");
    }
    object.setQueued(true);
    if (invokeLifecycleEvents) {
        // If there is a persistence logging plugin update the object
        if (persistenceLoggingPlugins != null) {
            try {
                if (log.isDebugEnabled()) {
                    log.debug("Invoking the update method on the IPersistenceLoggingPlugin instances");
                }
                for (IPersistenceLoggingPlugin persistenceLoggingPlugin : persistenceLoggingPlugins) {
                    persistenceLoggingPlugin.update(object);
                }
            } catch (Exception e) {
                log.error("Error in logging the UPDATE event", e);
                throw new UpdateFailedException(null, e);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Invoking the PostUpdate trigger on the Persistent object");
        }
        // Invoke the post update behaviors
        for (ILifecycleHandler lifecycleHandler : handlers) {
            lifecycleHandler.postUpdate();
        }
    }
}
Also used : ILifecycleHandler(org.jaffa.persistence.ILifecycleHandler) IPersistent(org.jaffa.persistence.IPersistent) IPersistenceLoggingPlugin(org.jaffa.persistence.logging.IPersistenceLoggingPlugin) DataSourceCreationException(org.jaffa.persistence.engines.jdbcengine.datasource.exceptions.DataSourceCreationException) FrameworkException(org.jaffa.exceptions.FrameworkException) SQLException(java.sql.SQLException)

Example 5 with IPersistenceLoggingPlugin

use of org.jaffa.persistence.logging.IPersistenceLoggingPlugin in project jaffa-framework by jaffa-projects.

the class PersistentTransaction method obtainPersistenceLoggingPlugins.

/**
 * Creates and initializes IPersistenceLoggingPlugin instances.
 */
private List<IPersistenceLoggingPlugin> obtainPersistenceLoggingPlugins() throws Exception {
    List<IPersistenceLoggingPlugin> persistenceLoggers = null;
    String implementationClassNames = (String) ContextManagerFactory.instance().getProperty("jaffa.persistence.IPersistenceLoggingPlugin");
    if (implementationClassNames != null) {
        String[] implementationClassNameArray = implementationClassNames.split(",");
        persistenceLoggers = new LinkedList<IPersistenceLoggingPlugin>();
        for (String implementationClassName : implementationClassNameArray) {
            if (log.isDebugEnabled()) {
                log.debug("Instantiating the IPersistenceLoggingPlugin implemenation class " + implementationClassName);
            }
            IPersistenceLoggingPlugin persistenceLogger = (IPersistenceLoggingPlugin) Class.forName(implementationClassName.trim()).newInstance();
            persistenceLogger.initialize(uow);
            persistenceLoggers.add(persistenceLogger);
        }
    }
    return persistenceLoggers;
}
Also used : IPersistenceLoggingPlugin(org.jaffa.persistence.logging.IPersistenceLoggingPlugin)

Aggregations

IPersistenceLoggingPlugin (org.jaffa.persistence.logging.IPersistenceLoggingPlugin)5 SQLException (java.sql.SQLException)4 FrameworkException (org.jaffa.exceptions.FrameworkException)4 DataSourceCreationException (org.jaffa.persistence.engines.jdbcengine.datasource.exceptions.DataSourceCreationException)4 ILifecycleHandler (org.jaffa.persistence.ILifecycleHandler)3 IPersistent (org.jaffa.persistence.IPersistent)3 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1