Author: adamw
Date: 2008-07-29 04:44:59 -0400 (Tue, 29 Jul 2008)
New Revision: 97
Added:
trunk/src/main/org/jboss/envers/RevisionType.java
trunk/src/main/org/jboss/envers/entity/RevisionTypeType.java
trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java
Removed:
trunk/src/main/org/jboss/envers/metadata/RevisionType.java
Modified:
trunk/src/main/org/jboss/envers/entity/EntityInstantiator.java
trunk/src/main/org/jboss/envers/metadata/VersionsMetadataGenerator.java
trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java
trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java
trunk/src/main/org/jboss/envers/synchronization/work/AbstractVersionsWorkUnit.java
trunk/src/main/org/jboss/envers/synchronization/work/AddWorkUnit.java
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java
trunk/src/main/org/jboss/envers/synchronization/work/DelWorkUnit.java
trunk/src/main/org/jboss/envers/synchronization/work/ModWorkUnit.java
trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java
Log:
ENVERS-32: revision type returned by the query for revisions
Copied: trunk/src/main/org/jboss/envers/RevisionType.java (from rev 95,
trunk/src/main/org/jboss/envers/metadata/RevisionType.java)
===================================================================
--- trunk/src/main/org/jboss/envers/RevisionType.java (rev 0)
+++ trunk/src/main/org/jboss/envers/RevisionType.java 2008-07-29 08:44:59 UTC (rev 97)
@@ -0,0 +1,65 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * 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, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers;
+
+/**
+ * Type of the revision.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public enum RevisionType {
+ /**
+ * Indicates that the entity was added (persisted) at that revision.
+ */
+ ADD((byte) 0),
+ /**
+ * Indicates that the entity was modified (one or more of its fields) at that
revision.
+ */
+ MOD((byte) 1),
+ /**
+ * Indicates that the entity was deleted (removed) at that revision.
+ */
+ DEL((byte) 2);
+
+ private Byte representation;
+
+ RevisionType(byte representation) {
+ this.representation = representation;
+ }
+
+ public Byte getRepresentation() {
+ return representation;
+ }
+
+ public static RevisionType fromRepresentation(Object representation) {
+ if (representation == null || !(representation instanceof Byte)) {
+ return null;
+ }
+
+ switch ((Byte) representation) {
+ case 0: return ADD;
+ case 1: return MOD;
+ case 2: return DEL;
+ }
+
+ throw new IllegalArgumentException("Unknown representation: " +
representation);
+ }
+}
Modified: trunk/src/main/org/jboss/envers/entity/EntityInstantiator.java
===================================================================
--- trunk/src/main/org/jboss/envers/entity/EntityInstantiator.java 2008-07-28 15:19:50 UTC
(rev 96)
+++ trunk/src/main/org/jboss/envers/entity/EntityInstantiator.java 2008-07-29 08:44:59 UTC
(rev 97)
@@ -21,7 +21,7 @@
*/
package org.jboss.envers.entity;
-import org.jboss.envers.metadata.RevisionType;
+import org.jboss.envers.RevisionType;
import org.jboss.envers.exception.VersionsException;
import org.jboss.envers.configuration.VersionsConfiguration;
import org.jboss.envers.mapper.id.IdMapper;
Added: trunk/src/main/org/jboss/envers/entity/RevisionTypeType.java
===================================================================
--- trunk/src/main/org/jboss/envers/entity/RevisionTypeType.java
(rev 0)
+++ trunk/src/main/org/jboss/envers/entity/RevisionTypeType.java 2008-07-29 08:44:59 UTC
(rev 97)
@@ -0,0 +1,85 @@
+package org.jboss.envers.entity;
+
+import org.hibernate.usertype.UserType;
+import org.hibernate.HibernateException;
+import org.jboss.envers.RevisionType;
+
+import java.sql.ResultSet;
+import java.sql.Types;
+import java.sql.SQLException;
+import java.sql.PreparedStatement;
+import java.io.Serializable;
+
+/**
+ * A hibernate type for the {@link RevisionType} enum.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class RevisionTypeType implements UserType {
+ private static final int[] SQL_TYPES = { Types.TINYINT };
+
+ public int[] sqlTypes() {
+ return SQL_TYPES;
+ }
+
+ public Class returnedClass() {
+ return RevisionType.class;
+ }
+
+ public RevisionType nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {
+ byte representation = (byte) resultSet.getInt(names[0]);
+ RevisionType result = null;
+
+ if (!resultSet.wasNull()) {
+ result = RevisionType.fromRepresentation(representation);
+ }
+
+ return result;
+ }
+
+ public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
throws HibernateException, SQLException {
+ if (null == value) {
+ preparedStatement.setNull(index, Types.TINYINT);
+ } else {
+ preparedStatement.setInt(index, ((RevisionType) value).getRepresentation());
+ }
+ }
+
+ public Object deepCopy(Object value) throws HibernateException{
+ return value;
+ }
+
+ public boolean isMutable() {
+ return false;
+ }
+
+ public Object assemble(Serializable cached, Object owner) throws HibernateException
{
+ return cached;
+ }
+
+ public Serializable disassemble(Object value) throws HibernateException {
+ return (Serializable)value;
+ }
+
+ public Object replace(Object original, Object target, Object owner) throws
HibernateException {
+ return original;
+ }
+
+ public int hashCode(Object x) throws HibernateException {
+ return x.hashCode();
+ }
+
+ public boolean equals(Object x, Object y) throws HibernateException {
+ //noinspection ObjectEquality
+ if (x == y) {
+ return true;
+ }
+
+ if (null == x || null == y) {
+ return false;
+ }
+
+ return x.equals(y);
+ }
+}
+
+
Deleted: trunk/src/main/org/jboss/envers/metadata/RevisionType.java
===================================================================
--- trunk/src/main/org/jboss/envers/metadata/RevisionType.java 2008-07-28 15:19:50 UTC
(rev 96)
+++ trunk/src/main/org/jboss/envers/metadata/RevisionType.java 2008-07-29 08:44:59 UTC
(rev 97)
@@ -1,33 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * 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, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.metadata;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class RevisionType {
- private RevisionType() { }
-
- public final static Byte ADD = 0;
- public final static Byte MOD = 1;
- public final static Byte DEL = 2;
-}
Modified: trunk/src/main/org/jboss/envers/metadata/VersionsMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/metadata/VersionsMetadataGenerator.java 2008-07-28
15:19:50 UTC (rev 96)
+++ trunk/src/main/org/jboss/envers/metadata/VersionsMetadataGenerator.java 2008-07-29
08:44:59 UTC (rev 97)
@@ -558,8 +558,9 @@
class_mapping.add((Element) idMapper.getXmlMapping().clone());
// Adding the "revision type" property
- MetadataTools.addProperty(class_mapping,
verEntCfg.getRevisionTypePropName(),
+ Element revTypeProperty = MetadataTools.addProperty(class_mapping,
verEntCfg.getRevisionTypePropName(),
verEntCfg.getRevisionTypePropType(), false);
+ revTypeProperty.addAttribute("type",
"org.jboss.envers.entity.RevisionTypeType");
break;
case SINGLE:
Copied: trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java (from rev 95,
trunk/src/main/org/jboss/envers/query/RevisionProperty.java)
===================================================================
--- trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java
(rev 0)
+++ trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java 2008-07-29 08:44:59
UTC (rev 97)
@@ -0,0 +1,46 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * 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, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.query;
+
+import org.jboss.envers.query.projection.VersionsProjection;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.hibernate.criterion.Projection;
+import org.hibernate.criterion.Projections;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@SuppressWarnings({"JavaDoc"})
+public class RevisionTypeProperty implements VersionsProjection {
+ private RevisionTypeProperty() { }
+
+ /**
+ * Select the revision type
+ */
+ public static VersionsProjection revisionType() {
+ return new RevisionTypeProperty();
+ }
+
+ public Projection getProjection(String entityName, VersionsReaderImplementor
versionsReader) {
+ return
Projections.property(versionsReader.getEntitiesCfg().getRevisionTypePropName());
+ }
+}
\ No newline at end of file
Modified: trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-07-28 15:19:50
UTC (rev 96)
+++ trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-07-29 08:44:59
UTC (rev 97)
@@ -52,11 +52,12 @@
}
/**
- * Creates a query, which will return a list of two-element arrays, containing the
entity instance and the number
- * of the revision (unless a projection is set), at which instances of the given
entity were modified, and which
- * satisfy some conditions.
+ * Creates a query, which will return a list of three-element arrays, containing the
entity instance, the number
+ * of the revision (unless a projection is set), at which instances of the given
entity were modified, and the type
+ * of the revision ({@link org.jboss.envers.RevisionType}). Additional conditions
that the results must satisfy
+ * may be specified.
* @param c Class of the entities for which to query.
- * @param selectEntitiesOnly If true, instead of a list of two-element arrays, a list
of entites will be
+ * @param selectEntitiesOnly If true, instead of a list of three-element arrays, a
list of entites will be
* returned as a result of executing this query.
* @return A query for revisions at which instances of the given entity were
modified, to which
* conditions can be added (for example - a specific id of an entity of class
<code>c</code>), and which
Modified: trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-07-28
15:19:50 UTC (rev 96)
+++ trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-07-29
08:44:59 UTC (rev 97)
@@ -24,6 +24,7 @@
import org.jboss.envers.exception.VersionsException;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.query.criteria.VersionsCriterion;
+import org.jboss.envers.RevisionType;
import org.hibernate.criterion.Order;
import java.util.List;
@@ -68,13 +69,15 @@
List entities = new ArrayList();
String originalId = versionsReader.getEntitiesCfg().getOriginalIdPropName();
String revisionPropertyName =
versionsReader.getEntitiesCfg().getRevisionPropName();
+ String revisionTypePropertyName =
versionsReader.getEntitiesCfg().getRevisionTypePropName();
for (Map versionsEntity : queryResult) {
Number revision = (Number) ((Map)
versionsEntity.get(originalId)).get(revisionPropertyName);
+
Object entity =
entityInstantiator.createInstanceFromVersionsEntity(entityName, versionsEntity,
revision);
if (!selectEntitiesOnly) {
- entities.add(new Object[] { entity, revision });
+ entities.add(new Object[] { entity, revision,
versionsEntity.get(revisionTypePropertyName) });
} else {
entities.add(entity);
}
Modified:
trunk/src/main/org/jboss/envers/synchronization/work/AbstractVersionsWorkUnit.java
===================================================================
---
trunk/src/main/org/jboss/envers/synchronization/work/AbstractVersionsWorkUnit.java 2008-07-28
15:19:50 UTC (rev 96)
+++
trunk/src/main/org/jboss/envers/synchronization/work/AbstractVersionsWorkUnit.java 2008-07-29
08:44:59 UTC (rev 97)
@@ -23,6 +23,7 @@
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
import org.jboss.envers.configuration.VersionsConfiguration;
+import org.jboss.envers.RevisionType;
import org.hibernate.Session;
import java.util.Map;
@@ -45,7 +46,7 @@
this.entityName = entityName;
}
- protected void fillDataWithId(Map<String, Object> data, Number revision, Byte
revisionType) {
+ protected void fillDataWithId(Map<String, Object> data, Number revision,
RevisionType revisionType) {
VersionsEntitiesConfiguration entitiesCfg = verCfg.getEntitiesCfg();
Map<String, Object> originalId = new HashMap<String, Object>();
Modified: trunk/src/main/org/jboss/envers/synchronization/work/AddWorkUnit.java
===================================================================
--- trunk/src/main/org/jboss/envers/synchronization/work/AddWorkUnit.java 2008-07-28
15:19:50 UTC (rev 96)
+++ trunk/src/main/org/jboss/envers/synchronization/work/AddWorkUnit.java 2008-07-29
08:44:59 UTC (rev 97)
@@ -22,7 +22,7 @@
package org.jboss.envers.synchronization.work;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.metadata.RevisionType;
+import org.jboss.envers.RevisionType;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.Session;
Modified:
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java
===================================================================
---
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java 2008-07-28
15:19:50 UTC (rev 96)
+++
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java 2008-07-29
08:44:59 UTC (rev 97)
@@ -23,7 +23,7 @@
import org.hibernate.Session;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.metadata.RevisionType;
+import org.jboss.envers.RevisionType;
import java.io.Serializable;
import java.util.Map;
Modified: trunk/src/main/org/jboss/envers/synchronization/work/DelWorkUnit.java
===================================================================
--- trunk/src/main/org/jboss/envers/synchronization/work/DelWorkUnit.java 2008-07-28
15:19:50 UTC (rev 96)
+++ trunk/src/main/org/jboss/envers/synchronization/work/DelWorkUnit.java 2008-07-29
08:44:59 UTC (rev 97)
@@ -22,7 +22,7 @@
package org.jboss.envers.synchronization.work;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.metadata.RevisionType;
+import org.jboss.envers.RevisionType;
import org.hibernate.Session;
import java.io.Serializable;
Modified: trunk/src/main/org/jboss/envers/synchronization/work/ModWorkUnit.java
===================================================================
--- trunk/src/main/org/jboss/envers/synchronization/work/ModWorkUnit.java 2008-07-28
15:19:50 UTC (rev 96)
+++ trunk/src/main/org/jboss/envers/synchronization/work/ModWorkUnit.java 2008-07-29
08:44:59 UTC (rev 97)
@@ -22,7 +22,7 @@
package org.jboss.envers.synchronization.work;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.metadata.RevisionType;
+import org.jboss.envers.RevisionType;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.Session;
Modified: trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-07-28
15:19:50 UTC (rev 96)
+++ trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-07-29
08:44:59 UTC (rev 97)
@@ -5,6 +5,8 @@
import org.jboss.envers.test.tools.TestTools;
import org.jboss.envers.query.VersionsRestrictions;
import org.jboss.envers.query.RevisionProperty;
+import org.jboss.envers.query.RevisionTypeProperty;
+import org.jboss.envers.RevisionType;
import org.hibernate.ejb.Ejb3Configuration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -66,6 +68,15 @@
site3.setStr1("a");
em.getTransaction().commit();
+
+ // Revision 4
+ em.getTransaction().begin();
+
+ site1 = em.find(StrIntTestEntity.class, id1);
+
+ em.remove(site1);
+
+ em.getTransaction().commit();
}
@Test
@@ -175,8 +186,27 @@
assert ((Object []) result.get(0))[0].equals(new StrIntTestEntity("a",
10, id1));
assert ((Object []) result.get(1))[0].equals(new StrIntTestEntity("c",
10, id1));
+ assert ((Object []) result.get(2))[0] == null;
assert ((Object []) result.get(0))[1].equals(1);
assert ((Object []) result.get(1))[1].equals(2);
+ assert ((Object []) result.get(2))[1].equals(4);
+
+ assert ((Object []) result.get(0))[2].equals(RevisionType.ADD);
+ assert ((Object []) result.get(1))[2].equals(RevisionType.MOD);
+ assert ((Object []) result.get(2))[2].equals(RevisionType.DEL);
}
+
+ @Test
+ public void testSelectRevisionTypeQuery() {
+ List result = getVersionsReader().createQuery()
+ .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .setProjection(RevisionTypeProperty.revisionType())
+ .add(VersionsRestrictions.idEq(id1))
+ .getResultList();
+
+ assert result.get(0).equals(RevisionType.ADD);
+ assert result.get(1).equals(RevisionType.MOD);
+ assert result.get(2).equals(RevisionType.DEL);
+ }
}