diff --git a/envers/src/main/java/org/hibernate/envers/AuditReader.java b/envers/src/main/java/org/hibernate/envers/AuditReader.java index 28049dafe6..1fe244f356 100644 --- a/envers/src/main/java/org/hibernate/envers/AuditReader.java +++ b/envers/src/main/java/org/hibernate/envers/AuditReader.java @@ -26,7 +26,7 @@ package org.hibernate.envers; import java.util.Date; import java.util.List; -import org.hibernate.envers.exception.NotVersionedException; +import org.hibernate.envers.exception.NotAuditedException; import org.hibernate.envers.exception.RevisionDoesNotExistException; import org.hibernate.envers.query.AuditQueryCreator; @@ -43,11 +43,11 @@ public interface AuditReader { * if not all properties are versioned) or null, if an entity with that id didn't exist at that * revision. * @throws IllegalArgumentException If cls or primaryKey is null or revision is less or equal to 0. - * @throws NotVersionedException When entities of the given class are not versioned. + * @throws NotAuditedException When entities of the given class are not audited. * @throws IllegalStateException If the associated entity manager is closed. */ T find(Class cls, Object primaryKey, Number revision) throws - IllegalArgumentException, NotVersionedException, IllegalStateException; + IllegalArgumentException, NotAuditedException, IllegalStateException; /** * Get a list of revision numbers, at which an entity was modified. @@ -55,12 +55,12 @@ public interface AuditReader { * @param primaryKey Primary key of the entity. * @return A list of revision numbers, at which the entity was modified, sorted in ascending order (so older * revisions come first). - * @throws NotVersionedException When entities of the given class are not versioned. + * @throws NotAuditedException When entities of the given class are not audited. * @throws IllegalArgumentException If cls or primaryKey is null. * @throws IllegalStateException If the associated entity manager is closed. */ List getRevisions(Class cls, Object primaryKey) - throws IllegalArgumentException, NotVersionedException, IllegalStateException; + throws IllegalArgumentException, NotAuditedException, IllegalStateException; /** * Get the date, at which a revision was created. @@ -102,7 +102,7 @@ public interface AuditReader { /** * - * @return A query creator, associated with this VersionsReader instance, with which queries can be + * @return A query creator, associated with this AuditReader instance, with which queries can be * created and later executed. Shouldn't be used after the associated Session or EntityManager * is closed. */ diff --git a/envers/src/main/java/org/hibernate/envers/AuditReaderFactory.java b/envers/src/main/java/org/hibernate/envers/AuditReaderFactory.java index abe6ccd504..053328a003 100644 --- a/envers/src/main/java/org/hibernate/envers/AuditReaderFactory.java +++ b/envers/src/main/java/org/hibernate/envers/AuditReaderFactory.java @@ -25,7 +25,7 @@ package org.hibernate.envers; import javax.persistence.EntityManager; -import org.hibernate.envers.event.VersionsEventListener; +import org.hibernate.envers.event.AuditEventListener; import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.reader.AuditReaderImpl; import static org.hibernate.envers.tools.ArraysTools.arrayIncludesInstanceOf; @@ -48,7 +48,7 @@ public class AuditReaderFactory { * @param session An open session. * @return A versions reader associated with the given sesison. It shouldn't be used * after the session is closed. - * @throws org.hibernate.envers.exception.AuditException When the given required listeners aren't installed. + * @throws AuditException When the given required listeners aren't installed. */ public static AuditReader get(Session session) throws AuditException { SessionImplementor sessionImpl = (SessionImplementor) session; @@ -56,16 +56,16 @@ public class AuditReaderFactory { EventListeners listeners = sessionImpl.getListeners(); for (PostInsertEventListener listener : listeners.getPostInsertEventListeners()) { - if (listener instanceof VersionsEventListener) { - if (arrayIncludesInstanceOf(listeners.getPostUpdateEventListeners(), VersionsEventListener.class) && - arrayIncludesInstanceOf(listeners.getPostDeleteEventListeners(), VersionsEventListener.class)) { - return new AuditReaderImpl(((VersionsEventListener) listener).getVerCfg(), session, + if (listener instanceof AuditEventListener) { + if (arrayIncludesInstanceOf(listeners.getPostUpdateEventListeners(), AuditEventListener.class) && + arrayIncludesInstanceOf(listeners.getPostDeleteEventListeners(), AuditEventListener.class)) { + return new AuditReaderImpl(((AuditEventListener) listener).getVerCfg(), session, sessionImpl); } } } - throw new AuditException("You need install the org.hibernate.envers.event.VersionsEventListener " + + throw new AuditException("You need to install the org.hibernate.envers.event.AuditEventListener " + "class as post insert, update and delete event listener."); } @@ -74,7 +74,7 @@ public class AuditReaderFactory { * @param entityManager An open entity manager. * @return A versions reader associated with the given entity manager. It shouldn't be used * after the entity manager is closed. - * @throws org.hibernate.envers.exception.AuditException When the given entity manager is not based on Hibernate, or if the required + * @throws AuditException When the given entity manager is not based on Hibernate, or if the required * listeners aren't installed. */ public static AuditReader get(EntityManager entityManager) throws AuditException { diff --git a/envers/src/main/java/org/hibernate/envers/Audited.java b/envers/src/main/java/org/hibernate/envers/Audited.java index 825f4b6555..b665677b11 100644 --- a/envers/src/main/java/org/hibernate/envers/Audited.java +++ b/envers/src/main/java/org/hibernate/envers/Audited.java @@ -29,8 +29,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * When applied to a class, indicates that all of its properties should be versioned. - * When applied to a field, indicates that this field should be versioned. + * When applied to a class, indicates that all of its properties should be audited. + * When applied to a field, indicates that this field should be audited. * @author Adam Warski (adam at warski dot org) */ @Retention(RetentionPolicy.RUNTIME) diff --git a/envers/src/main/java/org/hibernate/envers/event/VersionsEventListener.java b/envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java similarity index 99% rename from envers/src/main/java/org/hibernate/envers/event/VersionsEventListener.java rename to envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java index 978d533e8e..e094724fa3 100644 --- a/envers/src/main/java/org/hibernate/envers/event/VersionsEventListener.java +++ b/envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java @@ -60,7 +60,7 @@ import org.hibernate.persister.entity.EntityPersister; /** * @author Adam Warski (adam at warski dot org) */ -public class VersionsEventListener implements PostInsertEventListener, PostUpdateEventListener, +public class AuditEventListener implements PostInsertEventListener, PostUpdateEventListener, PostDeleteEventListener, PreCollectionUpdateEventListener, PreCollectionRemoveEventListener, PostCollectionRecreateEventListener, Initializable { private AuditConfiguration verCfg; diff --git a/envers/src/main/java/org/hibernate/envers/exception/AuditException.java b/envers/src/main/java/org/hibernate/envers/exception/AuditException.java index f6a5c8ecb5..85357130f7 100644 --- a/envers/src/main/java/org/hibernate/envers/exception/AuditException.java +++ b/envers/src/main/java/org/hibernate/envers/exception/AuditException.java @@ -23,12 +23,12 @@ */ package org.hibernate.envers.exception; -import org.hibernate.HibernateException; +import org.jboss.envers.exception.VersionsException; /** * @author Adam Warski (adam at warski dot org) */ -public class AuditException extends HibernateException { +public class AuditException extends VersionsException { public AuditException(String message) { super(message); } diff --git a/envers/src/main/java/org/hibernate/envers/exception/NotAuditedException.java b/envers/src/main/java/org/hibernate/envers/exception/NotAuditedException.java new file mode 100644 index 0000000000..840c2fcaed --- /dev/null +++ b/envers/src/main/java/org/hibernate/envers/exception/NotAuditedException.java @@ -0,0 +1,35 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.envers.exception; + +import org.jboss.envers.exception.NotVersionedException; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class NotAuditedException extends NotVersionedException { + public NotAuditedException(String entityName, String message) { + super(entityName, message); + } +} diff --git a/envers/src/main/java/org/hibernate/envers/exception/RevisionDoesNotExistException.java b/envers/src/main/java/org/hibernate/envers/exception/RevisionDoesNotExistException.java index b85ff1c544..8ddb7dc3ef 100644 --- a/envers/src/main/java/org/hibernate/envers/exception/RevisionDoesNotExistException.java +++ b/envers/src/main/java/org/hibernate/envers/exception/RevisionDoesNotExistException.java @@ -28,25 +28,12 @@ import java.util.Date; /** * @author Adam Warski (adam at warski dot org) */ -public class RevisionDoesNotExistException extends AuditException { - private Number revision; - private Date date; - +public class RevisionDoesNotExistException extends org.jboss.envers.exception.RevisionDoesNotExistException { public RevisionDoesNotExistException(Number revision) { - super("Revision " + revision + " does not exist."); - this.revision = revision; + super(revision); } public RevisionDoesNotExistException(Date date) { - super("There is no revision before or at " + date + "."); - this.date = date; - } - - public Number getRevision() { - return revision; - } - - public Date getDate() { - return date; + super(date); } } diff --git a/envers/src/main/java/org/hibernate/envers/query/AuditQueryCreator.java b/envers/src/main/java/org/hibernate/envers/query/AuditQueryCreator.java index 2674d43715..c0b0113917 100644 --- a/envers/src/main/java/org/hibernate/envers/query/AuditQueryCreator.java +++ b/envers/src/main/java/org/hibernate/envers/query/AuditQueryCreator.java @@ -34,12 +34,12 @@ import static org.hibernate.envers.tools.ArgumentsTools.checkPositive; * @author Adam Warski (adam at warski dot org) */ public class AuditQueryCreator { - private final AuditConfiguration verCfg; - private final AuditReaderImplementor versionsReaderImplementor; + private final AuditConfiguration auditCfg; + private final AuditReaderImplementor auditReaderImplementor; - public AuditQueryCreator(AuditConfiguration verCfg, AuditReaderImplementor versionsReaderImplementor) { - this.verCfg = verCfg; - this.versionsReaderImplementor = versionsReaderImplementor; + public AuditQueryCreator(AuditConfiguration auditCfg, AuditReaderImplementor auditReaderImplementor) { + this.auditCfg = auditCfg; + this.auditReaderImplementor = auditReaderImplementor; } /** @@ -54,7 +54,7 @@ public class AuditQueryCreator { public AuditQuery forEntitiesAtRevision(Class c, Number revision) { checkNotNull(revision, "Entity revision"); checkPositive(revision, "Entity revision"); - return new EntitiesAtRevisionQuery(verCfg, versionsReaderImplementor, c, revision); + return new EntitiesAtRevisionQuery(auditCfg, auditReaderImplementor, c, revision); } /** @@ -78,6 +78,6 @@ public class AuditQueryCreator { * unless an order or projection is added. */ public AuditQuery forRevisionsOfEntity(Class c, boolean selectEntitiesOnly, boolean selectDeletedEntities) { - return new RevisionsOfEntityQuery(verCfg, versionsReaderImplementor, c, selectEntitiesOnly,selectDeletedEntities); + return new RevisionsOfEntityQuery(auditCfg, auditReaderImplementor, c, selectEntitiesOnly,selectDeletedEntities); } } diff --git a/envers/src/main/java/org/hibernate/envers/query/impl/AbstractVersionsQuery.java b/envers/src/main/java/org/hibernate/envers/query/impl/AbstractAuditQuery.java similarity index 97% rename from envers/src/main/java/org/hibernate/envers/query/impl/AbstractVersionsQuery.java rename to envers/src/main/java/org/hibernate/envers/query/impl/AbstractAuditQuery.java index 83c0b3170a..f043b5f8a1 100644 --- a/envers/src/main/java/org/hibernate/envers/query/impl/AbstractVersionsQuery.java +++ b/envers/src/main/java/org/hibernate/envers/query/impl/AbstractAuditQuery.java @@ -50,7 +50,7 @@ import org.hibernate.Query; /** * @author Adam Warski (adam at warski dot org) */ -public abstract class AbstractVersionsQuery implements AuditQuery { +public abstract class AbstractAuditQuery implements AuditQuery { protected EntityInstantiator entityInstantiator; protected List criterions; @@ -64,7 +64,7 @@ public abstract class AbstractVersionsQuery implements AuditQuery { protected final AuditConfiguration verCfg; private final AuditReaderImplementor versionsReader; - protected AbstractVersionsQuery(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, + protected AbstractAuditQuery(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, Class cls) { this.verCfg = verCfg; this.versionsReader = versionsReader; diff --git a/envers/src/main/java/org/hibernate/envers/query/impl/EntitiesAtRevisionQuery.java b/envers/src/main/java/org/hibernate/envers/query/impl/EntitiesAtRevisionQuery.java index 5ca119a449..9783cb77ec 100644 --- a/envers/src/main/java/org/hibernate/envers/query/impl/EntitiesAtRevisionQuery.java +++ b/envers/src/main/java/org/hibernate/envers/query/impl/EntitiesAtRevisionQuery.java @@ -36,7 +36,7 @@ import org.hibernate.envers.tools.query.QueryBuilder; /** * @author Adam Warski (adam at warski dot org) */ -public class EntitiesAtRevisionQuery extends AbstractVersionsQuery { +public class EntitiesAtRevisionQuery extends AbstractAuditQuery { private final Number revision; public EntitiesAtRevisionQuery(AuditConfiguration verCfg, diff --git a/envers/src/main/java/org/hibernate/envers/query/impl/RevisionsOfEntityQuery.java b/envers/src/main/java/org/hibernate/envers/query/impl/RevisionsOfEntityQuery.java index d2c87c4702..0e46d02cf4 100644 --- a/envers/src/main/java/org/hibernate/envers/query/impl/RevisionsOfEntityQuery.java +++ b/envers/src/main/java/org/hibernate/envers/query/impl/RevisionsOfEntityQuery.java @@ -39,7 +39,7 @@ import org.hibernate.proxy.HibernateProxy; /** * @author Adam Warski (adam at warski dot org) */ -public class RevisionsOfEntityQuery extends AbstractVersionsQuery { +public class RevisionsOfEntityQuery extends AbstractAuditQuery { private final boolean selectEntitiesOnly; private final boolean selectDeletedEntities; diff --git a/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java b/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java index 6177b6b1d8..2c304cb22a 100644 --- a/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java +++ b/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java @@ -28,11 +28,10 @@ import java.util.List; import javax.persistence.NoResultException; import org.hibernate.envers.configuration.AuditConfiguration; -import org.hibernate.envers.exception.NotVersionedException; +import org.hibernate.envers.exception.NotAuditedException; import org.hibernate.envers.exception.RevisionDoesNotExistException; import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.query.RevisionProperty; -import org.hibernate.envers.query.AuditQueryCreator; import org.hibernate.envers.query.AuditRestrictions; import static org.hibernate.envers.tools.ArgumentsTools.checkNotNull; import static org.hibernate.envers.tools.ArgumentsTools.checkPositive; @@ -41,6 +40,7 @@ import org.hibernate.NonUniqueResultException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.engine.SessionImplementor; +import org.jboss.envers.query.VersionsQueryCreator; /** * @author Adam Warski (adam at warski dot org) @@ -80,7 +80,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { @SuppressWarnings({"unchecked"}) public T find(Class cls, Object primaryKey, Number revision) throws - IllegalArgumentException, NotVersionedException, IllegalStateException { + IllegalArgumentException, NotAuditedException, IllegalStateException { checkNotNull(cls, "Entity class"); checkNotNull(primaryKey, "Primary key"); checkNotNull(revision, "Entity revision"); @@ -90,7 +90,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { String entityName = cls.getName(); if (!verCfg.getEntCfg().isVersioned(entityName)) { - throw new NotVersionedException(entityName, entityName + " is not versioned!"); + throw new NotAuditedException(entityName, entityName + " is not versioned!"); } if (firstLevelCache.contains(entityName, revision, primaryKey)) { @@ -113,7 +113,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { @SuppressWarnings({"unchecked"}) public List getRevisions(Class cls, Object primaryKey) - throws IllegalArgumentException, NotVersionedException, IllegalStateException { + throws IllegalArgumentException, NotAuditedException, IllegalStateException { // todo: if a class is not versioned from the beginning, there's a missing ADD rev - what then? checkNotNull(cls, "Entity class"); checkNotNull(primaryKey, "Primary key"); @@ -122,7 +122,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { String entityName = cls.getName(); if (!verCfg.getEntCfg().isVersioned(entityName)) { - throw new NotVersionedException(entityName, entityName + " is not versioned!"); + throw new NotAuditedException(entityName, entityName + " is not versioned!"); } return createQuery().forRevisionsOfEntity(cls, false, true) @@ -191,7 +191,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { } } - public AuditQueryCreator createQuery() { - return new AuditQueryCreator(verCfg, this); + public VersionsQueryCreator createQuery() { + return new VersionsQueryCreator(verCfg, this); } } diff --git a/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImplementor.java b/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImplementor.java index b8ba1f6414..2e3f04298a 100644 --- a/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImplementor.java +++ b/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImplementor.java @@ -27,12 +27,13 @@ import org.hibernate.envers.AuditReader; import org.hibernate.Session; import org.hibernate.engine.SessionImplementor; +import org.jboss.envers.VersionsReader; /** * An interface exposed by a VersionsReader to library-facing classes. * @author Adam Warski (adam at warski dot org) */ -public interface AuditReaderImplementor extends AuditReader { +public interface AuditReaderImplementor extends AuditReader, VersionsReader { SessionImplementor getSessionImplementor(); Session getSession(); FirstLevelCache getFirstLevelCache(); diff --git a/envers/src/main/java/org/jboss/envers/ModificationStore.java b/envers/src/main/java/org/jboss/envers/ModificationStore.java new file mode 100644 index 0000000000..bfcf30362c --- /dev/null +++ b/envers/src/main/java/org/jboss/envers/ModificationStore.java @@ -0,0 +1,34 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ + +package org.jboss.envers; + +/** + * @see org.hibernate.envers.ModificationStore + * @deprecated + * @author Adam Warski (adam at warski dot org) + */ +public enum ModificationStore { + FULL +} \ No newline at end of file diff --git a/envers/src/main/java/org/jboss/envers/Versioned.java b/envers/src/main/java/org/jboss/envers/Versioned.java new file mode 100644 index 0000000000..43dcf356cf --- /dev/null +++ b/envers/src/main/java/org/jboss/envers/Versioned.java @@ -0,0 +1,41 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ + +package org.jboss.envers; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @see org.hibernate.envers.Audited + * @deprecated + * @author Adam Warski (adam at warski dot org) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) +public @interface Versioned { + public abstract ModificationStore modStore() default ModificationStore.FULL; +} \ No newline at end of file diff --git a/envers/src/main/java/org/jboss/envers/VersionsReader.java b/envers/src/main/java/org/jboss/envers/VersionsReader.java new file mode 100644 index 0000000000..d4bf938487 --- /dev/null +++ b/envers/src/main/java/org/jboss/envers/VersionsReader.java @@ -0,0 +1,57 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ + +package org.jboss.envers; + +import org.hibernate.envers.AuditReader; +import org.jboss.envers.query.VersionsQueryCreator; +import org.jboss.envers.exception.NotVersionedException; +import org.jboss.envers.exception.RevisionDoesNotExistException; + +import java.util.List; +import java.util.Date; + +/** + * @see org.hibernate.envers.AuditReader + * @deprecated + * @author Adam Warski (adam at warski dot org) + */ +public interface VersionsReader extends AuditReader { + T find(Class cls, Object primaryKey, Number revision) throws + IllegalArgumentException, NotVersionedException, IllegalStateException; + + List getRevisions(Class cls, Object primaryKey) + throws IllegalArgumentException, NotVersionedException, IllegalStateException; + + Date getRevisionDate(Number revision) throws IllegalArgumentException, RevisionDoesNotExistException, + IllegalStateException; + + Number getRevisionNumberForDate(Date date) throws IllegalStateException, RevisionDoesNotExistException, + IllegalArgumentException; + + T findRevision(Class revisionEntityClass, Number revision) throws IllegalArgumentException, + RevisionDoesNotExistException, IllegalStateException; + + VersionsQueryCreator createQuery(); +} \ No newline at end of file diff --git a/envers/src/main/java/org/jboss/envers/VersionsReaderFactory.java b/envers/src/main/java/org/jboss/envers/VersionsReaderFactory.java new file mode 100644 index 0000000000..25de4b2436 --- /dev/null +++ b/envers/src/main/java/org/jboss/envers/VersionsReaderFactory.java @@ -0,0 +1,96 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ + +package org.jboss.envers; + +import javax.persistence.EntityManager; + +import org.hibernate.envers.event.AuditEventListener; +import org.hibernate.envers.reader.AuditReaderImpl; +import static org.hibernate.envers.tools.ArraysTools.arrayIncludesInstanceOf; + +import org.hibernate.Session; +import org.hibernate.engine.SessionImplementor; +import org.hibernate.event.EventListeners; +import org.hibernate.event.PostInsertEventListener; +import org.jboss.envers.exception.VersionsException; + +/** + * @see org.hibernate.envers.AuditReaderFactory + * @deprecated + * @author Adam Warski (adam at warski dot org) + */ +public class VersionsReaderFactory { + private VersionsReaderFactory() { } + + /** + * Create a versions reader associated with an open session. + * WARNING: Using Envers with Hibernate (not with Hibernate Entity Manager/JPA) is experimental, + * if possible, use {@link VersionsReaderFactory#get(javax.persistence.EntityManager)}. + * @param session An open session. + * @return A versions reader associated with the given sesison. It shouldn't be used + * after the session is closed. + * @throws org.hibernate.envers.exception.AuditException When the given required listeners aren't installed. + */ + public static VersionsReader get(Session session) throws VersionsException { + SessionImplementor sessionImpl = (SessionImplementor) session; + + EventListeners listeners = sessionImpl.getListeners(); + + for (PostInsertEventListener listener : listeners.getPostInsertEventListeners()) { + if (listener instanceof AuditEventListener) { + if (arrayIncludesInstanceOf(listeners.getPostUpdateEventListeners(), AuditEventListener.class) && + arrayIncludesInstanceOf(listeners.getPostDeleteEventListeners(), AuditEventListener.class)) { + return new AuditReaderImpl(((AuditEventListener) listener).getVerCfg(), session, + sessionImpl); + } + } + } + + throw new VersionsException("You need install the org.hibernate.envers.event.VersionsEventListener " + + "class as post insert, update and delete event listener."); + } + + /** + * Create a versions reader associated with an open entity manager. + * @param entityManager An open entity manager. + * @return A versions reader associated with the given entity manager. It shouldn't be used + * after the entity manager is closed. + * @throws org.hibernate.envers.exception.AuditException When the given entity manager is not based on Hibernate, or if the required + * listeners aren't installed. + */ + public static VersionsReader get(EntityManager entityManager) throws VersionsException { + if (entityManager.getDelegate() instanceof Session) { + return get((Session) entityManager.getDelegate()); + } + + if (entityManager.getDelegate() instanceof EntityManager) { + if (entityManager.getDelegate() instanceof Session) { + return get((Session) entityManager.getDelegate()); + } + } + + throw new VersionsException("Hibernate EntityManager not present!"); + } +} \ No newline at end of file diff --git a/envers/src/main/java/org/jboss/envers/event/VersionsEventListener.java b/envers/src/main/java/org/jboss/envers/event/VersionsEventListener.java new file mode 100644 index 0000000000..2ba71f7139 --- /dev/null +++ b/envers/src/main/java/org/jboss/envers/event/VersionsEventListener.java @@ -0,0 +1,34 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ + +package org.jboss.envers.event; + +import org.hibernate.envers.event.AuditEventListener; + +/** + * @see AuditEventListener + * @deprecated + * @author Adam Warski (adam at warski dot org) + */ +public class VersionsEventListener extends AuditEventListener { } \ No newline at end of file diff --git a/envers/src/main/java/org/hibernate/envers/exception/NotVersionedException.java b/envers/src/main/java/org/jboss/envers/exception/NotVersionedException.java similarity index 83% rename from envers/src/main/java/org/hibernate/envers/exception/NotVersionedException.java rename to envers/src/main/java/org/jboss/envers/exception/NotVersionedException.java index 4f5101d11a..982197768f 100644 --- a/envers/src/main/java/org/hibernate/envers/exception/NotVersionedException.java +++ b/envers/src/main/java/org/jboss/envers/exception/NotVersionedException.java @@ -21,20 +21,25 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.envers.exception; + +package org.jboss.envers.exception; + +import org.hibernate.envers.exception.AuditException; /** + * @see org.hibernate.envers.exception.NotAuditedException + * @deprecated * @author Adam Warski (adam at warski dot org) */ -public class NotVersionedException extends AuditException { +public class NotVersionedException extends VersionsException { private final String entityName; public NotVersionedException(String entityName, String message) { super(message); - this.entityName = entityName; + this.entityName = entityName; } public String getEntityName() { return entityName; } -} +} \ No newline at end of file diff --git a/envers/src/main/java/org/jboss/envers/exception/RevisionDoesNotExistException.java b/envers/src/main/java/org/jboss/envers/exception/RevisionDoesNotExistException.java new file mode 100644 index 0000000000..c16e7a9170 --- /dev/null +++ b/envers/src/main/java/org/jboss/envers/exception/RevisionDoesNotExistException.java @@ -0,0 +1,55 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ + +package org.jboss.envers.exception; + +import java.util.Date; + +/** + * @see org.hibernate.envers.exception.RevisionDoesNotExistException + * @deprecated + * @author Adam Warski (adam at warski dot org) + */ +public class RevisionDoesNotExistException extends VersionsException { + private Number revision; + private Date date; + + public RevisionDoesNotExistException(Number revision) { + super("Revision " + revision + " does not exist."); + this.revision = revision; + } + + public RevisionDoesNotExistException(Date date) { + super("There is no revision before or at " + date + "."); + this.date = date; + } + + public Number getRevision() { + return revision; + } + + public Date getDate() { + return date; + } +} \ No newline at end of file diff --git a/envers/src/main/java/org/jboss/envers/exception/VersionsException.java b/envers/src/main/java/org/jboss/envers/exception/VersionsException.java new file mode 100644 index 0000000000..3775382eef --- /dev/null +++ b/envers/src/main/java/org/jboss/envers/exception/VersionsException.java @@ -0,0 +1,46 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ + +package org.jboss.envers.exception; + +import org.hibernate.HibernateException; + +/** + * @see org.hibernate.envers.exception.AuditException + * @deprecated + * @author Adam Warski (adam at warski dot org) + */ +public class VersionsException extends HibernateException { + public VersionsException(String message) { + super(message); + } + + public VersionsException(String message, Throwable cause) { + super(message, cause); + } + + public VersionsException(Throwable cause) { + super(cause); + } +} \ No newline at end of file diff --git a/envers/src/main/java/org/jboss/envers/query/VersionsQueryCreator.java b/envers/src/main/java/org/jboss/envers/query/VersionsQueryCreator.java new file mode 100644 index 0000000000..208ca97ebc --- /dev/null +++ b/envers/src/main/java/org/jboss/envers/query/VersionsQueryCreator.java @@ -0,0 +1,40 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ + +package org.jboss.envers.query; + +import org.hibernate.envers.configuration.AuditConfiguration; +import org.hibernate.envers.query.AuditQueryCreator; +import org.hibernate.envers.reader.AuditReaderImplementor; + +/** + * @link AuditQueryCreator + * @deprecated + * @author Adam Warski (adam at warski dot org) + */ +public class VersionsQueryCreator extends AuditQueryCreator { + public VersionsQueryCreator(AuditConfiguration verCfg, AuditReaderImplementor versionsReaderImplementor) { + super(verCfg, versionsReaderImplementor); + } +} \ No newline at end of file diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/basic/NotVersioned.java b/envers/src/test/java/org/hibernate/envers/test/integration/basic/NotVersioned.java index 036ba20c31..1ac2a7792c 100644 --- a/envers/src/test/java/org/hibernate/envers/test/integration/basic/NotVersioned.java +++ b/envers/src/test/java/org/hibernate/envers/test/integration/basic/NotVersioned.java @@ -25,7 +25,7 @@ package org.hibernate.envers.test.integration.basic; import javax.persistence.EntityManager; -import org.hibernate.envers.exception.NotVersionedException; +import org.hibernate.envers.exception.NotAuditedException; import org.hibernate.envers.test.AbstractEntityTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -58,12 +58,12 @@ public class NotVersioned extends AbstractEntityTest { em.getTransaction().commit(); } - @Test(expectedExceptions = NotVersionedException.class) + @Test(expectedExceptions = NotAuditedException.class) public void testRevisionsCounts() { getVersionsReader().getRevisions(BasicTestEntity3.class, id1); } - @Test(expectedExceptions = NotVersionedException.class) + @Test(expectedExceptions = NotAuditedException.class) public void testHistoryOfId1() { getVersionsReader().find(BasicTestEntity3.class, id1, 1); } diff --git a/envers/src/test/resources/hibernate.test.cfg.xml b/envers/src/test/resources/hibernate.test.cfg.xml index ada7d6513a..c48f2074f6 100644 --- a/envers/src/test/resources/hibernate.test.cfg.xml +++ b/envers/src/test/resources/hibernate.test.cfg.xml @@ -24,22 +24,22 @@ --> - + - + - + - + - + - + \ No newline at end of file