Search in sources :

Example 1 with ILifecycleHandler

use of org.jaffa.persistence.ILifecycleHandler 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 2 with ILifecycleHandler

use of org.jaffa.persistence.ILifecycleHandler 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 3 with ILifecycleHandler

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

the class LifecycleHandlerConfig method addLifecycleHandler.

/**
 * Create an instance of a lifecycle handler and add it to the factory as a prepended or appended handler.
 */
private void addLifecycleHandler(String ruleName, final RuleMetaData rule, String targetClassName) throws ClassNotFoundException {
    ILifecycleHandlerFactory.ILifecycleHandlerProvider provider = null;
    if (ruleName.equals(SoaEventRuleName)) {
        provider = new ILifecycleHandlerFactory.ILifecycleHandlerProvider() {

            @Override
            public ILifecycleHandler getHandler() {
                return new SOAEventLifecycleHandler(rule);
            }
        };
    } else if (ruleName.equals(ScriptRuleName)) {
        provider = new ILifecycleHandlerFactory.ILifecycleHandlerProvider() {

            @Override
            public ILifecycleHandler getHandler() {
                return new ScriptLifecycleHandler(rule);
            }
        };
    }
    // if the handler is not defined, we are done here
    if (provider == null) {
        log.debug("No handler provider defined for rule: " + rule);
        return;
    }
    // append or prepend based on the input flag
    Boolean executeOnReturn = Parser.parseBoolean(rule.getParameter(RuleMetaData.PARAMETER_EXECUTE_ON_RETURN));
    if (executeOnReturn) {
        lifecycleHandlerFactory.addAppendedHandlerProvider(Class.forName(targetClassName), provider);
    } else {
        lifecycleHandlerFactory.addPrependedHandlerProvider(Class.forName(targetClassName), provider);
    }
}
Also used : ILifecycleHandler(org.jaffa.persistence.ILifecycleHandler) ScriptLifecycleHandler(org.jaffa.rules.rulemeta.ScriptLifecycleHandler) ILifecycleHandlerFactory(org.jaffa.beans.factory.ILifecycleHandlerFactory) SOAEventLifecycleHandler(org.jaffa.soa.dataaccess.SOAEventLifecycleHandler)

Example 4 with ILifecycleHandler

use of org.jaffa.persistence.ILifecycleHandler 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)

Aggregations

ILifecycleHandler (org.jaffa.persistence.ILifecycleHandler)4 SQLException (java.sql.SQLException)3 FrameworkException (org.jaffa.exceptions.FrameworkException)3 IPersistent (org.jaffa.persistence.IPersistent)3 DataSourceCreationException (org.jaffa.persistence.engines.jdbcengine.datasource.exceptions.DataSourceCreationException)3 IPersistenceLoggingPlugin (org.jaffa.persistence.logging.IPersistenceLoggingPlugin)3 ILifecycleHandlerFactory (org.jaffa.beans.factory.ILifecycleHandlerFactory)1 ScriptLifecycleHandler (org.jaffa.rules.rulemeta.ScriptLifecycleHandler)1 SOAEventLifecycleHandler (org.jaffa.soa.dataaccess.SOAEventLifecycleHandler)1