Author: adamw
Date: 2008-08-15 09:27:21 -0400 (Fri, 15 Aug 2008)
New Revision: 105
Added:
trunk/src/main/org/jboss/envers/revisioninfo/RevisionInfoQueryCreator.java
Removed:
trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurations.java
trunk/src/main/org/jboss/envers/configuration/EntityConfiguration.java
trunk/src/main/org/jboss/envers/configuration/metadata/data/IdMappingData.java
Modified:
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java
trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java
trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java
Log:
ENVERS-37: componentizing configuration - factoring out creating revision queries
Deleted: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurations.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurations.java 2008-08-14
15:39:48 UTC (rev 104)
+++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurations.java 2008-08-15
13:27:21 UTC (rev 105)
@@ -1,94 +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.configuration;
-
-import java.util.Map;
-import java.util.HashMap;
-
-/**
- * Configuration of the user entities: property mapping of the entities, relations,
inheritance.
- * @author Adam Warski (adam at warski dot org)
- */
-public class EntitiesConfigurations {
- private Map<String, EntityConfiguration> entitiesConfigurations;
-
- // Map versions entity name -> entity name
- private Map<String, String> entityNamesForVersionsEntityNames = new
HashMap<String, String>();
-
- public EntitiesConfigurations(Map<String, EntityConfiguration>
entitiesConfigurations) {
- this.entitiesConfigurations = entitiesConfigurations;
-
- generateBidirectionRelationInfo();
- generateVersionsEntityToEntityNames();
- }
-
- private void generateVersionsEntityToEntityNames() {
- entityNamesForVersionsEntityNames = new HashMap<String, String>();
-
- for (String entityName : entitiesConfigurations.keySet()) {
-
entityNamesForVersionsEntityNames.put(entitiesConfigurations.get(entityName).getVersionsEntityName(),
- entityName);
- }
- }
-
- private void generateBidirectionRelationInfo() {
- // Checking each relation if it is bidirectional. If so, storing that
information.
- for (String entityName : entitiesConfigurations.keySet()) {
- EntityConfiguration entCfg = entitiesConfigurations.get(entityName);
- // Iterating over all relations from that entity
- for (RelationDescription relDesc : entCfg.getRelationsIterator()) {
- // If this is an "owned" relation, checking the related entity,
if it has a relation that has
- // a mapped-by attribute to the currently checked. If so, this is a
bidirectional relation.
- if (relDesc.relationType == RelationDescription.RelationType.TO_ONE) {
- for (RelationDescription other :
entitiesConfigurations.get(relDesc.toEntityName).getRelationsIterator()) {
- if (relDesc.fromPropertyName.equals(other.mappedByPropertyName)
&&
- (entityName.equals(other.toEntityName))) {
- relDesc.bidirectional = true;
- other.bidirectional= true;
- }
- }
- }
- }
- }
- }
-
- public EntityConfiguration get(String entityName) {
- return entitiesConfigurations.get(entityName);
- }
-
- public String getEntityNameForVersionsEntityName(String versionsEntityName) {
- return entityNamesForVersionsEntityNames.get(versionsEntityName);
- }
-
- public RelationDescription getRelationDescription(String entityName, String
propertyName) {
- EntityConfiguration entCfg = entitiesConfigurations.get(entityName);
- RelationDescription relDesc = entCfg.getRelationDescription(propertyName);
- if (relDesc != null) {
- return relDesc;
- } else if (entCfg.getParentEntityName() != null) {
- // The field may be declared in a superclass ...
- return getRelationDescription(entCfg.getParentEntityName(), propertyName);
- } else {
- return null;
- }
- }
-}
Deleted: trunk/src/main/org/jboss/envers/configuration/EntityConfiguration.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/EntityConfiguration.java 2008-08-14
15:39:48 UTC (rev 104)
+++ trunk/src/main/org/jboss/envers/configuration/EntityConfiguration.java 2008-08-15
13:27:21 UTC (rev 105)
@@ -1,103 +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.configuration;
-
-import org.jboss.envers.configuration.metadata.data.IdMappingData;
-import org.jboss.envers.mapper.ExtendedPropertyMapper;
-import org.jboss.envers.mapper.id.IdMapper;
-
-import java.util.Map;
-import java.util.HashMap;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class EntityConfiguration {
- private String entityName;
- private String versionsEntityName;
- private IdMappingData idMappingData;
- private ExtendedPropertyMapper propertyMapper;
- // Maps from property name
- private Map<String, RelationDescription> relations;
- private String parentEntityName;
-
- public EntityConfiguration(String entityName, String versionsEntityName,
IdMappingData idMappingData,
- ExtendedPropertyMapper propertyMapper,
- String parentEntityName) {
- this.entityName = entityName;
- this.versionsEntityName = versionsEntityName;
- this.idMappingData = idMappingData;
- this.propertyMapper = propertyMapper;
- this.parentEntityName = parentEntityName;
-
- this.relations = new HashMap<String, RelationDescription>();
- }
-
- public void addToOneRelation(String fromPropertyName, String toEntityName, IdMapper
idMapper) {
- relations.put(fromPropertyName, new RelationDescription(fromPropertyName,
RelationDescription.RelationType.TO_ONE,
- toEntityName, null, idMapper));
- }
-
- public void addOneToOneRelation(String fromPropertyName, String mappedByPropertyName,
String toEntityName,
- IdMapper idMapper) {
- relations.put(fromPropertyName, new RelationDescription(fromPropertyName,
RelationDescription.RelationType.ONE_TO_ONE,
- toEntityName, mappedByPropertyName, idMapper));
- }
-
- public void addOneToManyRelation(String fromPropertyName, String
mappedByPropertyName, String toEntityName,
- IdMapper idMapper) {
- relations.put(fromPropertyName, new RelationDescription(fromPropertyName,
RelationDescription.RelationType.ONE_TO_MANY,
- toEntityName, mappedByPropertyName, idMapper));
- }
-
- public boolean isRelation(String propertyName) {
- return relations.get(propertyName) != null;
- }
-
- public Iterable<RelationDescription> getRelationsIterator() {
- return relations.values();
- }
-
- public RelationDescription getRelationDescription(String propertyName) {
- return relations.get(propertyName);
- }
-
- public IdMappingData getIdMappingData() {
- return idMappingData;
- }
-
- public ExtendedPropertyMapper getPropertyMapper() {
- return propertyMapper;
- }
-
- public String getParentEntityName() {
- return parentEntityName;
- }
-
- public String getEntityName() {
- return entityName;
- }
-
- public String getVersionsEntityName() {
- return versionsEntityName;
- }
-}
Modified: trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-08-14
15:39:48 UTC (rev 104)
+++
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-08-15
13:27:21 UTC (rev 105)
@@ -9,10 +9,7 @@
import org.jboss.envers.RevisionEntity;
import org.jboss.envers.Versioned;
import org.jboss.envers.configuration.metadata.MetadataTools;
-import org.jboss.envers.revisioninfo.CustomRevisionInfoGenerator;
-import org.jboss.envers.revisioninfo.DefaultRevisionInfoGenerator;
-import org.jboss.envers.revisioninfo.RevisionNumberGenerator;
-import org.jboss.envers.revisioninfo.RevisionInfoGenerator;
+import org.jboss.envers.revisioninfo.*;
import org.hibernate.MappingException;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.cfg.Configuration;
@@ -170,35 +167,27 @@
return new RevisionInfoConfigurationResult(
new RevisionNumberGenerator(revisionInfoEntityName,
revisionInfoGenerator),
revisionPropType,
- revisionInfoXmlMapping);
+ revisionInfoXmlMapping,
+ new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdName,
revisionInfoTimestampName));
}
public boolean isRevisionInfoEntityName(String entityName) {
return revisionInfoEntityName.equals(entityName);
}
-
- public String getRevisionInfoEntityName() {
- return revisionInfoEntityName;
- }
-
- public String getRevisionInfoIdName() {
- return revisionInfoIdName;
- }
-
- public String getRevisionInfoTimestampName() {
- return revisionInfoTimestampName;
- }
}
class RevisionInfoConfigurationResult {
private final RevisionNumberGenerator revisionNumberGenerator;
private final String revisionPropType;
private final Document revisionInfoXmlMapping;
+ private final RevisionInfoQueryCreator revisionInfoQueryCreator;
- RevisionInfoConfigurationResult(RevisionNumberGenerator revisionNumberGenerator,
String revisionPropType, Document revisionInfoXmlMapping) {
+ RevisionInfoConfigurationResult(RevisionNumberGenerator revisionNumberGenerator,
String revisionPropType,
+ Document revisionInfoXmlMapping,
RevisionInfoQueryCreator revisionInfoQueryCreator) {
this.revisionNumberGenerator = revisionNumberGenerator;
this.revisionPropType = revisionPropType;
this.revisionInfoXmlMapping = revisionInfoXmlMapping;
+ this.revisionInfoQueryCreator = revisionInfoQueryCreator;
}
public RevisionNumberGenerator getRevisionNumberGenerator() {
@@ -212,4 +201,8 @@
public Document getRevisionInfoXmlMapping() {
return revisionInfoXmlMapping;
}
+
+ public RevisionInfoQueryCreator getRevisionInfoQueryCreator() {
+ return revisionInfoQueryCreator;
+ }
}
\ 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-08-14
15:39:48 UTC (rev 104)
+++ trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-08-15
13:27:21 UTC (rev 105)
@@ -31,6 +31,7 @@
import org.jboss.envers.synchronization.VersionsSyncManager;
import org.jboss.envers.tools.graph.GraphTopologicalSort;
import org.jboss.envers.tools.reflection.YReflectionManager;
+import org.jboss.envers.revisioninfo.RevisionInfoQueryCreator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.XMLWriter;
@@ -52,6 +53,7 @@
private final VersionsSyncManager versionsSyncManager;
private final EntitiesConfigurations entCfg;
private final RevisionInfoConfiguration revInfoCfg;
+ private final RevisionInfoQueryCreator revisionInfoQueryCreator;
public VersionsEntitiesConfiguration getVerEntCfg() {
return verEntCfg;
@@ -73,6 +75,10 @@
return entCfg;
}
+ public RevisionInfoQueryCreator getRevisionInfoQueryGenerator() {
+ return revisionInfoQueryCreator;
+ }
+
private YReflectionManager getReflectionManager(Configuration cfg) {
try {
return new YReflectionManager(cfg);
@@ -91,6 +97,7 @@
verEntCfg = new VersionsEntitiesConfiguration(properties,
revInfoCfgResult.getRevisionPropType());
globalCfg = new GlobalConfiguration(properties);
versionsSyncManager = new
VersionsSyncManager(revInfoCfgResult.getRevisionNumberGenerator());
+ revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator();
VersionsMetadataGenerator versionsMetaGen = new VersionsMetadataGenerator(this);
DOMWriter writer = new DOMWriter();
Deleted: trunk/src/main/org/jboss/envers/configuration/metadata/data/IdMappingData.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/data/IdMappingData.java 2008-08-14
15:39:48 UTC (rev 104)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/data/IdMappingData.java 2008-08-15
13:27:21 UTC (rev 105)
@@ -1,54 +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.configuration.metadata.data;
-
-import org.jboss.envers.mapper.id.IdMapper;
-import org.dom4j.Element;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class IdMappingData {
- private IdMapper idMapper;
- // Mapping which will be used to generate the entity
- private Element xmlMapping;
- // Mapping which will be used to generate references to the entity in related
entities
- private Element xmlRelationMapping;
-
- public IdMappingData(IdMapper idMapper, Element xmlMapping, Element
xmlRelationMapping) {
- this.idMapper = idMapper;
- this.xmlMapping = xmlMapping;
- this.xmlRelationMapping = xmlRelationMapping;
- }
-
- public IdMapper getIdMapper() {
- return idMapper;
- }
-
- public Element getXmlMapping() {
- return xmlMapping;
- }
-
- public Element getXmlRelationMapping() {
- return xmlRelationMapping;
- }
-}
Modified: trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java
===================================================================
--- trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-08-14 15:39:48 UTC
(rev 104)
+++ trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-08-15 13:27:21 UTC
(rev 105)
@@ -26,8 +26,6 @@
import org.hibernate.Query;
import org.hibernate.engine.SessionImplementor;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-import org.jboss.envers.configuration.RevisionInfoConfiguration;
import org.jboss.envers.exception.VersionsException;
import org.jboss.envers.exception.NotVersionedException;
import org.jboss.envers.exception.RevisionDoesNotExistException;
@@ -49,15 +47,11 @@
private SessionImplementor sessionImplementor;
private Session session;
- private final RevisionInfoConfiguration revInfoCfg;
-
public VersionsReaderImpl(VersionsConfiguration verCfg, Session session,
SessionImplementor sessionImplementor) {
this.verCfg = verCfg;
this.sessionImplementor = sessionImplementor;
this.session = session;
-
- revInfoCfg = verCfg.getRevInfoCfg();
}
private void checkSession() {
@@ -164,13 +158,8 @@
checkPositive(revision, "Entity revision");
checkSession();
- StringBuilder queryStr = new StringBuilder();
- queryStr.append("select
rev.").append(revInfoCfg.getRevisionInfoTimestampName())
- .append(" from
").append(revInfoCfg.getRevisionInfoEntityName())
- .append(" rev where
").append(revInfoCfg.getRevisionInfoIdName()).append(" =
:_revision_number");
+ Query query =
verCfg.getRevisionInfoQueryGenerator().getRevisionDateQuery(session, revision);
- Query query =
session.createQuery(queryStr.toString()).setParameter("_revision_number",
revision);
-
try {
Long timestamp = (Long) query.uniqueResult();
if (timestamp == null) {
@@ -187,13 +176,8 @@
checkNotNull(date, "Date of revision");
checkSession();
- StringBuilder queryStr = new StringBuilder();
- queryStr.append("select
max(rev.").append(revInfoCfg.getRevisionInfoIdName())
- .append(") from
").append(revInfoCfg.getRevisionInfoEntityName())
- .append(" rev where
").append(revInfoCfg.getRevisionInfoTimestampName()).append(" <=
:_revision_date");
+ Query query =
verCfg.getRevisionInfoQueryGenerator().getRevisionNumberForDateQuery(session, date);
- Query query =
session.createQuery(queryStr.toString()).setParameter("_revision_date",
date.getTime());
-
try {
Number res = (Number) query.uniqueResult();
if (res == null) {
@@ -213,15 +197,8 @@
checkPositive(revision, "Entity revision");
checkSession();
- StringBuilder queryStr = new StringBuilder();
- queryStr.append("select rev from ")
- .append(revInfoCfg.getRevisionInfoEntityName())
- .append(" rev where ")
- .append(revInfoCfg.getRevisionInfoIdName())
- .append(" = :_revision_number");
+ Query query = verCfg.getRevisionInfoQueryGenerator().getRevisionQuery(session,
revision);
- Query query =
session.createQuery(queryStr.toString()).setParameter("_revision_number",
revision);
-
try {
T revisionData = (T) query.uniqueResult();
Added: trunk/src/main/org/jboss/envers/revisioninfo/RevisionInfoQueryCreator.java
===================================================================
--- trunk/src/main/org/jboss/envers/revisioninfo/RevisionInfoQueryCreator.java
(rev 0)
+++ trunk/src/main/org/jboss/envers/revisioninfo/RevisionInfoQueryCreator.java 2008-08-15
13:27:21 UTC (rev 105)
@@ -0,0 +1,48 @@
+package org.jboss.envers.revisioninfo;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import java.util.Date;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class RevisionInfoQueryCreator {
+ private final String revisionDateQuery;
+ private final String revisionNumberForDateQuery;
+ private final String revisionQuery;
+
+ public RevisionInfoQueryCreator(String revisionInfoEntityName, String
revisionInfoIdName,
+ String revisionInfoTimestampName) {
+ revisionDateQuery = new StringBuilder()
+ .append("select rev.").append(revisionInfoTimestampName)
+ .append(" from ").append(revisionInfoEntityName)
+ .append(" rev where ").append(revisionInfoIdName).append("
= :_revision_number")
+ .toString();
+
+ revisionNumberForDateQuery = new StringBuilder()
+ .append("select max(rev.").append(revisionInfoIdName)
+ .append(") from ").append(revisionInfoEntityName)
+ .append(" rev where
").append(revisionInfoTimestampName).append(" <= :_revision_date")
+ .toString();
+
+ revisionQuery = new StringBuilder()
+ .append("select rev from ").append(revisionInfoEntityName)
+ .append(" rev where ").append(revisionInfoIdName)
+ .append(" = :_revision_number")
+ .toString();
+ }
+
+ public Query getRevisionDateQuery(Session session, Number revision) {
+ return
session.createQuery(revisionDateQuery).setParameter("_revision_number",
revision);
+ }
+
+ public Query getRevisionNumberForDateQuery(Session session, Date date) {
+ return
session.createQuery(revisionNumberForDateQuery).setParameter("_revision_date",
date.getTime());
+ }
+
+ public Query getRevisionQuery(Session session, Number revision) {
+ return
session.createQuery(revisionQuery).setParameter("_revision_number", revision);
+ }
+}