Author: adamw
Date: 2008-09-03 03:29:48 -0400 (Wed, 03 Sep 2008)
New Revision: 141
Modified:
trunk/src/main/org/jboss/envers/DefaultRevisionEntity.java
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java
trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java
trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java
trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java
trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java
trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java
trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java
trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java
trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java
Log:
ENVERS-47: selecting all revision information in revisions-of-entity query
Modified: trunk/src/main/org/jboss/envers/DefaultRevisionEntity.java
===================================================================
--- trunk/src/main/org/jboss/envers/DefaultRevisionEntity.java 2008-09-03 06:27:36 UTC
(rev 140)
+++ trunk/src/main/org/jboss/envers/DefaultRevisionEntity.java 2008-09-03 07:29:48 UTC
(rev 141)
@@ -24,6 +24,9 @@
import javax.persistence.MappedSuperclass;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
+import javax.persistence.Transient;
+import java.util.Date;
+import java.text.DateFormat;
/**
* @author Adam Warski (adam at warski dot org)
@@ -46,6 +49,11 @@
this.id = id;
}
+ @Transient
+ public Date getRevisionDate() {
+ return new Date(timestamp);
+ }
+
public long getTimestamp() {
return timestamp;
}
@@ -72,4 +80,8 @@
result = 31 * result + (int) (timestamp ^ (timestamp >>> 32));
return result;
}
+
+ public String toString() {
+ return "DefaultRevisionEntity(id = " + id + ", revisionDate =
" + DateFormat.getDateTimeInstance().format(getRevisionDate()) + ")";
+ }
}
Modified: trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-03
06:27:36 UTC (rev 140)
+++
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-03
07:29:48 UTC (rev 141)
@@ -164,7 +164,7 @@
revisionInfoEntityName = pc.getEntityName();
revisionInfoClass = pc.getMappedClass();
- revisionInfoGenerator = new
DefaultRevisionInfoGenerator(revisionInfoEntityName, pc.getMappedClass(),
+ revisionInfoGenerator = new
DefaultRevisionInfoGenerator(revisionInfoEntityName, revisionInfoClass,
revisionEntity.value(), revisionInfoTimestampName);
}
}
@@ -173,10 +173,8 @@
Document revisionInfoXmlMapping = null;
if (revisionInfoGenerator == null) {
- // TODO - remove default
- //revisionInfoGenerator = new
DefaultRevisionInfoGenerator(revisionInfoEntityName, revisionInfoTimestampName);
revisionInfoClass = DefaultRevisionEntity.class;
- revisionInfoGenerator = new
DefaultRevisionInfoGenerator(revisionInfoEntityName, DefaultRevisionEntity.class,
+ revisionInfoGenerator = new
DefaultRevisionInfoGenerator(revisionInfoEntityName, revisionInfoClass,
RevisionListener.class, revisionInfoTimestampName);
revisionInfoXmlMapping = generateDefaultRevisionInfoXmlMapping();
}
@@ -184,8 +182,8 @@
return new RevisionInfoConfigurationResult(
revisionInfoGenerator, revisionPropType, revisionInfoXmlMapping,
new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdName,
revisionInfoTimestampName),
- generateRevisionInfoRelationMapping(), revisionInfoIdName,
- new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdName));
+ generateRevisionInfoRelationMapping(),
+ new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdName),
revisionInfoEntityName);
}
}
@@ -195,21 +193,20 @@
private final Document revisionInfoXmlMapping;
private final RevisionInfoQueryCreator revisionInfoQueryCreator;
private final Element revisionInfoRelationMapping;
- // TODO - remove ?
- private final String revisionInfoIdName;
private final RevisionInfoNumberReader revisionInfoNumberReader;
+ private final String revisionInfoEntityName;
RevisionInfoConfigurationResult(RevisionInfoGenerator revisionInfoGenerator, String
revisionPropType,
Document revisionInfoXmlMapping,
RevisionInfoQueryCreator revisionInfoQueryCreator,
- Element revisionInfoRelationMapping, String
revisionInfoIdName,
- RevisionInfoNumberReader revisionInfoNumberReader) {
+ Element revisionInfoRelationMapping,
+ RevisionInfoNumberReader revisionInfoNumberReader,
String revisionInfoEntityName) {
this.revisionInfoGenerator = revisionInfoGenerator;
this.revisionPropType = revisionPropType;
this.revisionInfoXmlMapping = revisionInfoXmlMapping;
this.revisionInfoQueryCreator = revisionInfoQueryCreator;
this.revisionInfoRelationMapping = revisionInfoRelationMapping;
- this.revisionInfoIdName = revisionInfoIdName;
this.revisionInfoNumberReader = revisionInfoNumberReader;
+ this.revisionInfoEntityName = revisionInfoEntityName;
}
public RevisionInfoGenerator getRevisionInfoGenerator() {
@@ -232,11 +229,11 @@
return revisionInfoRelationMapping;
}
- public String getRevisionInfoIdName() {
- return revisionInfoIdName;
- }
-
public RevisionInfoNumberReader getRevisionInfoNumberReader() {
return revisionInfoNumberReader;
}
+
+ public String getRevisionInfoEntityName() {
+ return revisionInfoEntityName;
+ }
}
\ No newline at end of file
Modified: trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-09-03
06:27:36 UTC (rev 140)
+++ trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-09-03
07:29:48 UTC (rev 141)
@@ -73,7 +73,7 @@
RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration();
RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure(cfg,
reflectionManager);
verEntCfg = new VersionsEntitiesConfiguration(properties,
revInfoCfgResult.getRevisionPropType(),
- revInfoCfgResult.getRevisionInfoIdName());
+ revInfoCfgResult.getRevisionInfoEntityName());
globalCfg = new GlobalConfiguration(properties);
versionsSyncManager = new
VersionsSyncManager(revInfoCfgResult.getRevisionInfoGenerator());
revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator();
Modified:
trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-03
06:27:36 UTC (rev 140)
+++
trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-03
07:29:48 UTC (rev 141)
@@ -42,12 +42,12 @@
private final String revisionTypePropName;
private final String revisionTypePropType;
- private final String revisionInfoIdName;
+ private final String revisionInfoEntityName;
private final Map<String, String> customVersionsTablesNames;
- public VersionsEntitiesConfiguration(Properties properties, String revisionPropType,
String revisionInfoIdName) {
- this.revisionInfoIdName = revisionInfoIdName;
+ public VersionsEntitiesConfiguration(Properties properties, String revisionPropType,
String revisionInfoEntityName) {
+ this.revisionInfoEntityName = revisionInfoEntityName;
versionsTablePrefix =
properties.getProperty("org.jboss.envers.versionsTablePrefix", "");
versionsTableSuffix =
properties.getProperty("org.jboss.envers.versionsTableSuffix",
"_versions");
@@ -90,14 +90,14 @@
return revisionTypePropType;
}
- public String getRevisionInfoIdName() {
- return revisionInfoIdName;
- }
-
public String getRevisionEntityPath() {
return revisionEntityPath;
}
+ public String getRevisionInfoEntityName() {
+ return revisionInfoEntityName;
+ }
+
//
public void addCustomVersionsTableName(String entityName, String tableName) {
Modified: trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-03 06:27:36
UTC (rev 140)
+++ trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-03 07:29:48
UTC (rev 141)
@@ -55,10 +55,14 @@
}
/**
- * 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.
+ * Creates a query, which selects the revisions, at which the given entity was
modified.
+ * Unless an explicit projection is set, the result will be a list of three-element
arrays, containing:
+ * <ol>
+ * <li>the entity instance</li>
+ * <li>revision entity, corresponding to the revision at which the entity was
modified. If no custom
+ * revision entity is used, this will be an instance of {@link
org.jboss.envers.DefaultRevisionEntity}</li>
+ * <li>type of the revision (an enum instance of class {@link
org.jboss.envers.RevisionType})</li>.
+ * 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 three-element arrays, a
list of entites will be
* returned as a result of executing this query.
Modified: trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-03
06:27:36 UTC (rev 140)
+++ trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-03
07:29:48 UTC (rev 141)
@@ -84,9 +84,6 @@
setQueryProperties(query);
- System.out.println("QUERY: " + querySb);
- System.out.println("PARAMS: " + queryParamValues);
-
return query.list();
}
Modified: trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 06:27:36 UTC
(rev 140)
+++ trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 07:29:48 UTC
(rev 141)
@@ -45,8 +45,7 @@
/**
*
- * @param entityName Main entity which should be selected. Its properties don't
have to be referred to
- * using the alias.
+ * @param entityName Main entity which should be selected.
* @param alias Alias of the entity
*/
public QueryBuilder(String entityName, String alias) {
@@ -70,8 +69,7 @@
/**
* Add an entity from which to select.
- * @param entityName Name of the entity from which to select. Its properties always
have to be referred to
- * using the alias.
+ * @param entityName Name of the entity from which to select.
* @param alias Alias of the entity. Should be different than all other aliases.
*/
public void addFrom(String entityName, String alias) {
@@ -167,7 +165,7 @@
private List<String> getOrderList() {
List<String> orderList = new ArrayList<String>();
for (Pair<String, Boolean> order : orders) {
- orderList.add(order.getFirst() + " " + (order.getSecond() ?
"asc" : "desc"));
+ orderList.add(alias + "." + order.getFirst() + " " +
(order.getSecond() ? "asc" : "desc"));
}
return orderList;
Modified: trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-09-03
06:27:36 UTC (rev 140)
+++ trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-09-03
07:29:48 UTC (rev 141)
@@ -72,8 +72,9 @@
/*
The query that should be executed in the versions table:
- SELECT e (unless another projection is specified) FROM ent_ver e WHERE
- e.revision_type != DEL AND (if selectDeletedEntities == false)
+ SELECT e (unless another projection is specified) FROM ent_ver e, rev_entity r
WHERE
+ e.revision_type != DEL (if selectDeletedEntities == false) AND
+ e.revision = r.revision AND
(all specified conditions, transformed, on the "e" entity)
ORDER BY e.revision ASC (unless another order or projection is specified)
*/
@@ -92,26 +93,37 @@
qb.addOrder(revisionPropertyPath, true);
}
- // TODO
if (!selectEntitiesOnly) {
- //versionsCriteria.setFetchMode("e._revision", FetchMode.JOIN);
-
//versionsCriteria.createCriteria("e.originalId._revision").add(Property.forName("id").eq(1));
+ qb.addFrom(verCfg.getVerEntCfg().getRevisionInfoEntityName(),
"r");
+ qb.getRootParameters().addWhere(verCfg.getVerEntCfg().getRevisionPropPath(),
true, "=", "r.id", false);
}
- List<Map> queryResult = buildAndExecuteQuery();
+ List<Object> queryResult = buildAndExecuteQuery();
if (hasProjection) {
return queryResult;
} else {
List entities = new ArrayList();
String revisionTypePropertyName = verEntCfg.getRevisionTypePropName();
- for (Map versionsEntity : queryResult) {
+ for (Object resultRow : queryResult) {
+ Map versionsEntity;
+ Object revisionData;
+
+ if (selectEntitiesOnly) {
+ versionsEntity = (Map) resultRow;
+ revisionData = null;
+ } else {
+ Object[] arrayResultRow = (Object[]) resultRow;
+ versionsEntity = (Map) arrayResultRow[0];
+ revisionData = arrayResultRow[1];
+ }
+
Number revision = getRevisionNumber(versionsEntity);
Object entity =
entityInstantiator.createInstanceFromVersionsEntity(entityName, versionsEntity,
revision);
if (!selectEntitiesOnly) {
- entities.add(new Object[] { entity, revision,
versionsEntity.get(revisionTypePropertyName) });
+ entities.add(new Object[] { entity, revisionData,
versionsEntity.get(revisionTypePropertyName) });
} else {
entities.add(entity);
}
Modified: trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java 2008-09-03
06:27:36 UTC (rev 140)
+++ trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java 2008-09-03
07:29:48 UTC (rev 141)
@@ -4,6 +4,7 @@
import org.jboss.envers.test.entities.StrIntTestEntity;
import org.jboss.envers.query.VersionsRestrictions;
import org.jboss.envers.RevisionType;
+import org.jboss.envers.DefaultRevisionEntity;
import org.hibernate.ejb.Ejb3Configuration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -69,7 +70,7 @@
assert result.size() == 1;
assert ((Object []) result.get(0))[0].equals(new StrIntTestEntity("b",
11, id2));
- assert ((Object []) result.get(0))[1].equals(1);
+ assert ((DefaultRevisionEntity) ((Object []) result.get(0))[1]).getId() == 1;
assert ((Object []) result.get(0))[2].equals(RevisionType.ADD);
}
}
Modified: trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-09-03
06:27:36 UTC (rev 140)
+++ trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-09-03
07:29:48 UTC (rev 141)
@@ -7,6 +7,7 @@
import org.jboss.envers.query.RevisionProperty;
import org.jboss.envers.query.RevisionTypeProperty;
import org.jboss.envers.RevisionType;
+import org.jboss.envers.DefaultRevisionEntity;
import org.hibernate.ejb.Ejb3Configuration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -192,9 +193,9 @@
assert ((Object []) result.get(1))[0].equals(new StrIntTestEntity("c",
10, id1));
assert ((Object []) result.get(2))[0].equals(new StrIntTestEntity(null, null,
id1));
- 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 ((DefaultRevisionEntity) ((Object []) result.get(0))[1]).getId() == 1;
+ assert ((DefaultRevisionEntity) ((Object []) result.get(1))[1]).getId() == 2;
+ assert ((DefaultRevisionEntity) ((Object []) result.get(2))[1]).getId() == 4;
assert ((Object []) result.get(0))[2].equals(RevisionType.ADD);
assert ((Object []) result.get(1))[2].equals(RevisionType.MOD);