Example usage for org.hibernate CallbackException CallbackException

List of usage examples for org.hibernate CallbackException CallbackException

Introduction

In this page you can find the example usage for org.hibernate CallbackException CallbackException.

Prototype

public CallbackException(String message, Exception cause) 

Source Link

Document

Creates a CallbackException using the given message and underlying cause.

Usage

From source file:gov.nih.nci.caarray.security.SecurityInterceptor.java

License:BSD License

/**
 * If enabled, checks whether the given user has WRITE or PARTIAL_WRITE privilege to given object, and if not,
 * throws a PermissionDeniedException.//from   w  ww  .j  av  a2 s  . c o m
 *
 * @param o the Object to check
 * @param user the user to check
 */
private static void verifyWritePrivilege(PersistentObject o, User user) {
    if (isEnabled() && !SecurityUtils.canWrite(o, user)) {
        throw new CallbackException("Attempted operation not allowed by security",
                new PermissionDeniedException(o, SecurityUtils.WRITE_PRIVILEGE, user.getLoginName()));
    }
}

From source file:no.abmu.common.hibernate3.AuditInterceptorH3.java

License:Open Source License

/**
 * Record the changes in the HashMap. Unfortunately, these changes can't be
 * done immediately (TransientObjectException), so they are recorded in a
 * seperate Set.//from   w  w w. j a v a2 s. c  o  m
 *
 * @see Interceptor#onFlushDirty(java.lang.Object, java.io.Serializable,
    *      java.lang.Object[], java.lang.Object[], java.lang.String[],
    *      net.sf.hibernate.type.Type[])
 */
public boolean onFlushDirty(Object obj, Serializable id, Object[] newValues, Object[] oldValues,
        String[] properties, Type[] types) {

    if (logger.isDebugEnabled()) {
        logger.debug("Updating " + obj + " with id " + id + " new="
                + (newValues == null ? "null" : Arrays.asList(newValues).toString()) + " old="
                + (oldValues == null ? "null" : Arrays.asList(oldValues).toString()) + " props="
                + Arrays.asList(properties));
    }

    if (!(obj instanceof Auditable)) {
        return false;
    }
    Auditable h = (Auditable) obj;

    Session session = null;
    Session newSession = null;
    try {
        session = SessionFactoryUtils.getSession(getSessionFactory(), true);
        newSession = sessionFactory.openSession(session.connection());

        Auditable oldObject = null;
        try {
            oldObject = (Auditable) newSession.load(obj.getClass(), h.getId());
        } catch (ObjectNotFoundException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("nada if object not already saved");
            }
            //nada if object not already saved
        }

        if (oldValues == null && newValues == null) {
            return false;
        }

        for (int i = 0; i < properties.length; i++) {
            // Skip the historyEntries
            if (properties[i].equals("auditEntries")) {
                continue;
            }

            Object newValue = null;
            Object oldValue = null;
            try {
                newValue = BeanUtils.getSimpleProperty(obj, properties[i]);
                oldValue = oldObject == null ? null : BeanUtils.getSimpleProperty(oldObject, properties[i]);
            } catch (IllegalAccessException e) {
                String message = "Cannot access property " + properties[i] + " in class "
                        + obj.getClass().getName();
                logger.error(message, e);
                throw new CallbackException(message, e);
            } catch (InvocationTargetException e) {
                String message = "Problem when reading " + properties[i] + " in class "
                        + obj.getClass().getName();
                logger.error(message, e);
                throw new CallbackException(message, e);
            } catch (NoSuchMethodException e) {
                String message = "No setter for property " + properties[i] + " in class "
                        + obj.getClass().getName();
                logger.error(message, e);
                throw new CallbackException(message, e);
            }

            if (oldValue == null && newValue == null) {
                continue;
            }

            // TO DO FIX THIS. 2008.12.03
            /*
                            if (newValue instanceof PersistentCollection) {
            // Collections must be compared against the snapshot
            PersistentCollection collection = (PersistentCollection) newValues[i];
            if (!collection.isDirectlyAccessible()) {
                continue;
            }
            // retrieve Snapshot
                    
            oldValue = collection.getCollectionSnapshot().getSnapshot();
            if (oldValue instanceof Map && newValue instanceof Set) {
                // a Set is internally stored as Map
                oldValue = ((Map) oldValue).values();
                    
            }
                            }
            */
            if (oldValue != null && oldValue.equals(newValue)) {
                continue;
            }
            // Generate a new entry

            AuditEntry entry = new AuditEntry();
            //      entry.setWho(getName());
            entry.setPointInTime(new Date());
            entry.setWhat("update");
            entry.setProperty(properties[i]);
            entry.setOldValue(format(oldValue));
            entry.setNewValue(format(newValue));
            entry.setIp(h.getEditCause() == null ? null : h.getEditCause().getIp());
            entry.setReason(h.getEditCause() == null ? null : h.getEditCause().getReason());
            if (logger.isDebugEnabled()) {
                logger.debug("Changed " + properties[i] + " from " + oldValue + " to " + newValue);
            }
            newSession.save(entry);
            if (logger.isInfoEnabled()) {
                logger.debug("added audit entry to " + h.getClass().getName());
            }
            h.getAuditEntries().add(entry);

        }
    } catch (HibernateException e) {
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    } finally {
        try {
            if (newSession != null) {
                newSession.flush();
                newSession.close();
            }
        } catch (HibernateException e) {
            throw SessionFactoryUtils.convertHibernateAccessException(e);
        }
    }

    //reset edit cause
    h.setEditCause(null);

    return false;
}

From source file:no.abmu.organisationregister.domain.HistoricObject.java

License:Open Source License

public boolean onUpdate(Session session) {
    HistoricObject fromDb = null;/*from ww w. j  a v  a2  s. co m*/
    HistoricObject newFromDb = null;
    Serializable newDbId = null;
    try {
        fromDb = (HistoricObject) session.load(this.getClass(), getId());

        if (!this.equals(fromDb)) {
            newFromDb = (HistoricObject) this.getClass().newInstance();
            BeanUtils.copyProperties(newFromDb, fromDb);

            newFromDb.setWasReplacedBy(this);
            newFromDb.setEndDate(new Date());
            newFromDb.setStartDate(fromDb.getStartDate());

            session.evict(fromDb);
            newDbId = session.save(newFromDb);
        }
        if (newDbId != null) {
            newFromDb = (StreetAddress) session.load(getClass(), newDbId);
            this.setReplaced(newFromDb);
        }
        this.setStartDate(new Date());
    } catch (HibernateException e) {
        logger.error("Problem when creating historic object for id " + getId(), e);
        throw new CallbackException("Problem when creating historic object for id " + getId(), e);
    } catch (IllegalAccessException e) {
        logger.error("could not access the default constructor for " + getClass().getName(), e);
        throw new CallbackException("could not access the default constructor for " + getClass().getName(), e);
    } catch (InstantiationException e) {
        logger.error("could not call the default constructor for " + getClass().getName(), e);
        throw new CallbackException("could not call the default constructor for " + getClass().getName(), e);
    } catch (InvocationTargetException e) {
        logger.error(e.getMessage(), e);
        throw new CallbackException("problem copying properties for " + getClass().getName(), e);
    }

    return false;
}