Author: adamw
Date: 2008-09-21 15:13:24 -0400 (Sun, 21 Sep 2008)
New Revision: 154
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java
trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java
trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java
Removed:
trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java
Modified:
trunk/envers.iml
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java
trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java
trunk/src/main/org/jboss/envers/tools/query/Parameters.java
trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java
trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java
trunk/src/test/org/jboss/envers/test/tools/TestTools.java
Log:
ENVERS-42: fixed support for sets of simple values, added support for maps of simple
values
Modified: trunk/envers.iml
===================================================================
--- trunk/envers.iml 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/envers.iml 2008-09-21 19:13:24 UTC (rev 154)
@@ -11,7 +11,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/test"
isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
- <orderEntry type="inheritedJdk" />
+ <orderEntry type="jdk" jdkName="1.5"
jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -6,12 +6,11 @@
import org.hibernate.type.*;
import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
import org.jboss.envers.entities.mapper.relation.*;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.SetProxy;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy;
+import org.jboss.envers.entities.mapper.relation.lazy.proxy.*;
import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
-import org.jboss.envers.entities.mapper.relation.component.MiddleDummyComponentMapper;
import org.jboss.envers.entities.mapper.relation.component.MiddleRelatedComponentMapper;
import org.jboss.envers.entities.mapper.relation.component.MiddleSimpleComponentMapper;
+import org.jboss.envers.entities.mapper.relation.component.MiddleDummyComponentMapper;
import org.jboss.envers.entities.mapper.id.IdMapper;
import org.jboss.envers.entities.EntityConfiguration;
import org.jboss.envers.entities.IdMappingData;
@@ -23,6 +22,7 @@
import java.util.*;
import java.util.Set;
import java.util.List;
+import java.util.Map;
/**
* Generates metadata for collection-valued properties.
@@ -169,8 +169,8 @@
// Only valid for an inverse relation; null otherwise.
String mappedBy;
- String referencingPrefix;
- // Only valid if referencedEntityName isn't null.
+ // The referencing prefix is always for a related entity. So it has always the
"_" at the end added.
+ String referencingPrefixRelated;
String referencedPrefix;
if (value.isInverse()) {
@@ -180,19 +180,19 @@
throw new MappingException("Unable to read the mapped by attribute
for " + name);
}
- referencingPrefix = mappedBy + "_";
- referencedPrefix = StringTools.getLastComponent(referencedEntityName) +
"_";
+ referencingPrefixRelated = mappedBy + "_";
+ referencedPrefix = referencedEntityName == null ? "element" :
StringTools.getLastComponent(referencedEntityName);
} else {
mappedBy = null;
- referencingPrefix = StringTools.getLastComponent(entityName) +
"_";
- referencedPrefix = name + "_";
+ referencingPrefixRelated = StringTools.getLastComponent(entityName) +
"_";
+ referencedPrefix = referencedEntityName == null ? "element" :
name;
}
// Storing the id data of the referencing entity: original mapper, prefixed
mapper and entity name.
MiddleIdData referencingIdData = new MiddleIdData(
referencingIdMapping.getIdMapper(),
-
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix),
+
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefixRelated),
entityName,
mainGenerator.getVerEntCfg().getVersionsEntityName(entityName));
@@ -207,7 +207,7 @@
middleEntityXmlId.addAttribute("name",
mainGenerator.getVerEntCfg().getOriginalIdPropName());
// Adding related-entity (in this case: the referencing's entity id) id
mapping to the xml.
- addRelatedToXmlMapping(middleEntityXmlId, referencingPrefix, value.getKey(),
referencingIdMapping);
+ addRelatedToXmlMapping(middleEntityXmlId, referencingPrefixRelated,
value.getKey(), referencingIdMapping);
// Adding the revision number as a foreign key to the revision info entity to
the composite id of the
// middle table.
@@ -229,7 +229,8 @@
MiddleComponentData indexComponentData;
if (value instanceof IndexedCollection) {
IndexedCollection indexedValue = (IndexedCollection) value;
- indexComponentData = null;
+ indexComponentData = addValueToMiddleTable(indexedValue.getIndex(),
middleEntityXmlId,
+ queryGeneratorBuilder, "mapkey");
// TODO
} else {
// No index - creating a dummy mapper.
@@ -242,18 +243,31 @@
// ******
// Building the query generator.
- MiddleTableQueryGenerator queryGenerator = queryGeneratorBuilder.build();
+ MiddleTableQueryGenerator queryGenerator =
queryGeneratorBuilder.build(elementComponentData, indexComponentData);
+ // Creating common data
+ CommonCollectionMapperData commonCollectionMapperData = new
CommonCollectionMapperData(
+ mainGenerator.getVerEntCfg(), versionsMiddleEntityName, name,
referencingIdData, queryGenerator);
+
// Checking the type of the collection and adding an appropriate mapper.
Type type = value.getType();
- if (type instanceof SetType) {
- currentMapper.addComposite(name, new
BasicCollectionMapper<Set>(mainGenerator.getVerEntCfg(),
- versionsMiddleEntityName, referencingIdData, HashSet.class,
SetProxy.class, queryGenerator,
- name, elementComponentData));
+ if (type instanceof SortedSetType) {
+ currentMapper.addComposite(name, new
BasicCollectionMapper<Set>(commonCollectionMapperData,
+ TreeSet.class, SortedSetProxy.class, elementComponentData));
+ } else if (type instanceof SetType) {
+ currentMapper.addComposite(name, new
BasicCollectionMapper<Set>(commonCollectionMapperData,
+ HashSet.class, SetProxy.class, elementComponentData));
+ } else if (type instanceof SortedMapType) {
+ // Indexed collection, so <code>indexComponentData</code> is not
null.
+ currentMapper.addComposite(name, new
MapCollectionMapper<Map>(commonCollectionMapperData,
+ TreeMap.class, SortedMapProxy.class, elementComponentData,
indexComponentData));
+ } else if (type instanceof MapType) {
+ // Indexed collection, so <code>indexComponentData</code> is not
null.
+ currentMapper.addComposite(name, new
MapCollectionMapper<Map>(commonCollectionMapperData,
+ HashMap.class, MapProxy.class, elementComponentData,
indexComponentData));
} else if (type instanceof BagType) {
- currentMapper.addComposite(name, new
BasicCollectionMapper<List>(mainGenerator.getVerEntCfg(),
- versionsMiddleEntityName, referencingIdData, ArrayList.class,
ListProxy.class, queryGenerator,
- name, elementComponentData));
+ currentMapper.addComposite(name, new
BasicCollectionMapper<List>(commonCollectionMapperData,
+ ArrayList.class, ListProxy.class, elementComponentData));
} else {
throw new RuntimeException();
}
@@ -289,6 +303,8 @@
String prefix) {
Type type = value.getType();
if (type instanceof ManyToOneType) {
+ String prefixRelated = prefix + "_";
+
ToOne toOneValue = (ToOne) value;
String referencedEntityName = toOneValue.getReferencedEntityName();
IdMappingData referencedIdMapping =
mainGenerator.getEntitiesConfigurations().get(
@@ -297,11 +313,11 @@
// Adding related-entity (in this case: the referenced entities id) id
mapping to the xml only if the
// relation isn't inverse (so when
<code>middleEntityXml</code> is not null).
if (middleEntityXml != null) {
- addRelatedToXmlMapping(middleEntityXml, prefix, value,
referencedIdMapping);
+ addRelatedToXmlMapping(middleEntityXml, prefixRelated, value,
referencedIdMapping);
}
// Storing the id data of the referenced entity: original mapper, prefixed
mapper and entity name.
- IdMapper referencedPrefixedIdMapper =
referencedIdMapping.getIdMapper().prefixMappedProperties(prefix);
+ IdMapper referencedPrefixedIdMapper =
referencedIdMapping.getIdMapper().prefixMappedProperties(prefixRelated);
MiddleIdData referencedIdData = new MiddleIdData(
referencedIdMapping.getIdMapper(),
referencedPrefixedIdMapper,
@@ -314,9 +330,9 @@
queryGeneratorBuilder.getCurrentIndex());
} else if (type instanceof ImmutableType || type instanceof MutableType) {
// TODO: add support for enums, components, custom types
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml,
"element", value, null, ModificationStore.FULL, true);
+ mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml,
prefix, value, null, ModificationStore.FULL, true);
- return new MiddleComponentData(new
MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), "element"),
+ return new MiddleComponentData(new
MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix),
queryGeneratorBuilder.getCurrentIndex());
} else {
// TODO: throw an exception
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -1,6 +1,7 @@
package org.jboss.envers.configuration.metadata;
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
import org.jboss.envers.entities.mapper.relation.query.OneEntityQueryGenerator;
import org.jboss.envers.entities.mapper.relation.query.TwoEntityQueryGenerator;
@@ -34,9 +35,10 @@
idDatas.add(idData);
}
- MiddleTableQueryGenerator build() {
+ MiddleTableQueryGenerator build(MiddleComponentData... componentDatas) {
if (idDatas.size() == 0) {
- return new OneEntityQueryGenerator(verEntCfg, versionsMiddleEntityName,
referencingIdData);
+ return new OneEntityQueryGenerator(verEntCfg, versionsMiddleEntityName,
referencingIdData,
+ componentDatas);
} else if (idDatas.size() == 1) {
return new TwoEntityQueryGenerator(verEntCfg, versionsMiddleEntityName,
referencingIdData,
idDatas.get(0));
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -30,15 +30,18 @@
* @author Adam Warski (adam at warski dot org)
*/
public abstract class AbstractIdMapper implements IdMapper {
+ private Parameters getParametersToUse(Parameters parameters,
List<QueryParameterData> paramDatas) {
+ if (paramDatas.size() > 1) {
+ return parameters.addSubParameters("and");
+ } else {
+ return parameters;
+ }
+ }
+
public void addIdsEqualToQuery(Parameters parameters, String prefix1, String prefix2)
{
List<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
- Parameters parametersToUse;
- if (paramDatas.size() > 1) {
- parametersToUse = parameters.addSubParameters("and");
- } else {
- parametersToUse = parameters;
- }
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
for (QueryParameterData paramData : paramDatas) {
parametersToUse.addWhere(paramData.getProperty(prefix1), false,
"=", paramData.getProperty(prefix2), false);
@@ -48,18 +51,23 @@
public void addIdEqualsToQuery(Parameters parameters, Object id, String prefix,
boolean equals) {
List<QueryParameterData> paramDatas = mapToQueryParametersFromId(id);
- Parameters parametersToUse;
- if (paramDatas.size() > 1) {
- parametersToUse = parameters.addSubParameters("and");
- } else {
- parametersToUse = parameters;
- }
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
for (QueryParameterData paramData : paramDatas) {
parametersToUse.addWhereWithParam(paramData.getProperty(prefix), equals ?
"=" : "<>", paramData.getValue());
}
}
+ public void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean
equals) {
+ List<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
+
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
+
+ for (QueryParameterData paramData : paramDatas) {
+ parametersToUse.addWhereWithNamedParam(paramData.getProperty(prefix), equals
? "=" : "<>", paramData.getQueryParameterName());
+ }
+ }
+
public String getIdsEqualQuery(String prefix1, String prefix2) {
List<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 12:51:09
UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 19:13:24
UTC (rev 154)
@@ -83,6 +83,16 @@
void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean
equals);
/**
+ * Adds query statements, which contains named parameters, which express the property
that the id of the entity
+ * with alias prefix, is equal to the given object. It is the responsibility of the
using method to read
+ * parameter values from the id and specify them on the final query object.
+ * @param parameters Parameters, to which to add the statements.
+ * @param prefix Prefix to add to the properties (may be null).
+ * @param equals Should this query express the "=" relation or the
"<>" relation.
+ */
+ void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean equals);
+
+ /**
* Gets a query string, which contains equalities, which express the property that
the id of the entity
* with alias prefix, is equal to some values (which are left to be specified later,
by specifiying parameter
* values)
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -2,28 +2,39 @@
import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
import org.jboss.envers.entities.mapper.PropertyMapper;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
import org.jboss.envers.RevisionType;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.exception.VersionsException;
+import org.jboss.envers.tools.reflection.ReflectionTools;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.configuration.VersionsConfiguration;
import org.hibernate.collection.PersistentCollection;
+import org.hibernate.property.Setter;
import java.util.*;
import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Constructor;
/**
* @author Adam Warski (adam at warski dot org)
*/
-public abstract class AbstractCollectionMapper implements PropertyMapper {
- private final VersionsEntitiesConfiguration verEntCfg;
- private final String versionsMiddleEntityName;
- private final String collectionReferencingPropertyName;
- private final MiddleIdData referencingIdData;
+public abstract class AbstractCollectionMapper<T> implements PropertyMapper {
+ protected final CommonCollectionMapperData commonCollectionMapperData;
+ protected final Class<? extends T> collectionClass;
- protected AbstractCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String
versionsMiddleEntityName,
- String collectionReferencingPropertyName,
MiddleIdData referencingIdData) {
- this.verEntCfg = verEntCfg;
- this.versionsMiddleEntityName = versionsMiddleEntityName;
- this.collectionReferencingPropertyName = collectionReferencingPropertyName;
- this.referencingIdData = referencingIdData;
+ private final Constructor<? extends T> proxyConstructor;
+
+ protected AbstractCollectionMapper(CommonCollectionMapperData
commonCollectionMapperData,
+ Class<? extends T> collectionClass,
Class<? extends T> proxyClass) {
+ this.commonCollectionMapperData = commonCollectionMapperData;
+ this.collectionClass = collectionClass;
+
+ try {
+ proxyConstructor = proxyClass.getConstructor(Initializor.class);
+ } catch (NoSuchMethodException e) {
+ throw new VersionsException(e);
+ }
}
protected abstract Collection getNewCollectionContent(PersistentCollection
newCollection);
@@ -41,16 +52,17 @@
for (Object changedObj : changed) {
Map<String, Object> entityData = new HashMap<String, Object>();
Map<String, Object> originalId = new HashMap<String, Object>();
- entityData.put(verEntCfg.getOriginalIdPropName(), originalId);
+
entityData.put(commonCollectionMapperData.getVerEntCfg().getOriginalIdPropName(),
originalId);
- collectionChanges.add(new
PersistentCollectionChangeData(versionsMiddleEntityName, entityData, changedObj));
+ collectionChanges.add(new PersistentCollectionChangeData(
+ commonCollectionMapperData.getVersionsMiddleEntityName(), entityData,
changedObj));
// Mapping the collection owner's id.
- referencingIdData.getPrefixedMapper().mapToMapFromId(originalId, id);
+
commonCollectionMapperData.getReferencingIdData().getPrefixedMapper().mapToMapFromId(originalId,
id);
// Mapping collection element and index (if present).
mapToMapFromObject(originalId, changedObj);
- entityData.put(verEntCfg.getRevisionTypePropName(), revisionType);
+
entityData.put(commonCollectionMapperData.getVerEntCfg().getRevisionTypePropName(),
revisionType);
}
}
@@ -58,7 +70,7 @@
public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
PersistentCollection
newColl,
Serializable
oldColl, Serializable id) {
- if (!collectionReferencingPropertyName.equals(referencingPropertyName)) {
+ if
(!commonCollectionMapperData.getCollectionReferencingPropertyName().equals(referencingPropertyName))
{
return null;
}
@@ -87,4 +99,23 @@
// Changes are mapped in the "mapCollectionChanges" method.
return false;
}
+
+ protected abstract Initializor<T> getInitializor(VersionsConfiguration verCfg,
+ VersionsReaderImplementor
versionsReader, Object primaryKey,
+ Number revision);
+
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey,
+ VersionsReaderImplementor versionsReader, Number
revision) {
+ Setter setter = ReflectionTools.getSetter(obj.getClass(),
+ commonCollectionMapperData.getCollectionReferencingPropertyName());
+ try {
+ setter.set(obj, proxyConstructor.newInstance(getInitializor(verCfg,
versionsReader, primaryKey, revision)), null);
+ } catch (InstantiationException e) {
+ throw new VersionsException(e);
+ } catch (IllegalAccessException e) {
+ throw new VersionsException(e);
+ } catch (InvocationTargetException e) {
+ throw new VersionsException(e);
+ }
+ }
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -1,71 +1,33 @@
package org.jboss.envers.entities.mapper.relation;
import org.jboss.envers.entities.mapper.PropertyMapper;
-import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
import
org.jboss.envers.entities.mapper.relation.lazy.initializor.BasicCollectionInitializor;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.tools.reflection.ReflectionTools;
-import org.jboss.envers.exception.VersionsException;
import org.hibernate.collection.PersistentCollection;
-import org.hibernate.property.Setter;
import java.util.Map;
-import java.util.List;
import java.util.Collection;
import java.io.Serializable;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
/**
* @author Adam Warski (adam at warski dot org)
*/
-public final class BasicCollectionMapper<T extends Collection> extends
AbstractCollectionMapper implements PropertyMapper {
- private final Class<? extends T> collectionClass;
- private final MiddleTableQueryGenerator queryGenerator;
- private final String propertyName;
+public final class BasicCollectionMapper<T extends Collection> extends
AbstractCollectionMapper<T> implements PropertyMapper {
private final MiddleComponentData elementComponentData;
- private final Constructor<? extends T> proxyConstructor;
-
- public BasicCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String
versionsMiddleEntityName,
- MiddleIdData referencingIdData,
+ public BasicCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
Class<? extends T> collectionClass, Class<?
extends T> proxyClass,
- MiddleTableQueryGenerator queryGenerator,
- String propertyName, MiddleComponentData
elementComponentData) {
- super(verEntCfg, versionsMiddleEntityName, propertyName, referencingIdData);
- this.collectionClass = collectionClass;
- this.queryGenerator = queryGenerator;
- this.propertyName = propertyName;
+ MiddleComponentData elementComponentData) {
+ super(commonCollectionMapperData, collectionClass, proxyClass);
this.elementComponentData = elementComponentData;
-
- try {
- proxyConstructor = proxyClass.getConstructor(Initializor.class);
- } catch (NoSuchMethodException e) {
- throw new VersionsException(e);
- }
}
- public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey,
- VersionsReaderImplementor versionsReader, Number
revision) {
- // Creating the initializator and passing to the proxy, which will use it to
initialize the collection
- // when and if it is accessed.
- Initializor<? extends Collection> collectionInitializor = new
BasicCollectionInitializor<T>(verCfg,
- versionsReader, queryGenerator, primaryKey, revision, collectionClass,
elementComponentData);
-
- Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
- try {
- setter.set(obj, proxyConstructor.newInstance(collectionInitializor), null);
- } catch (InstantiationException e) {
- throw new VersionsException(e);
- } catch (IllegalAccessException e) {
- throw new VersionsException(e);
- } catch (InvocationTargetException e) {
- throw new VersionsException(e);
- }
+ protected Initializor<T> getInitializor(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
+ Object primaryKey, Number revision) {
+ return new BasicCollectionInitializor<T>(verCfg, versionsReader,
commonCollectionMapperData.getQueryGenerator(),
+ primaryKey, revision, collectionClass, elementComponentData);
}
protected Collection getNewCollectionContent(PersistentCollection newCollection) {
@@ -73,7 +35,9 @@
}
protected Collection getOldCollectionContent(Serializable oldCollection) {
- if (oldCollection instanceof Map) {
+ if (oldCollection == null) {
+ return null;
+ } else if (oldCollection instanceof Map) {
return ((Map) oldCollection).keySet();
} else {
return (Collection) oldCollection;
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -0,0 +1,46 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+
+/**
+ * Data that is used by all collection mappers, regardless of the type.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class CommonCollectionMapperData {
+ private final VersionsEntitiesConfiguration verEntCfg;
+ private final String versionsMiddleEntityName;
+ private final String collectionReferencingPropertyName;
+ private final MiddleIdData referencingIdData;
+ private final MiddleTableQueryGenerator queryGenerator;
+
+ public CommonCollectionMapperData(VersionsEntitiesConfiguration verEntCfg, String
versionsMiddleEntityName,
+ String collectionReferencingPropertyName,
MiddleIdData referencingIdData,
+ MiddleTableQueryGenerator queryGenerator) {
+ this.verEntCfg = verEntCfg;
+ this.versionsMiddleEntityName = versionsMiddleEntityName;
+ this.collectionReferencingPropertyName = collectionReferencingPropertyName;
+ this.referencingIdData = referencingIdData;
+ this.queryGenerator = queryGenerator;
+ }
+
+ public VersionsEntitiesConfiguration getVerEntCfg() {
+ return verEntCfg;
+ }
+
+ public String getVersionsMiddleEntityName() {
+ return versionsMiddleEntityName;
+ }
+
+ public String getCollectionReferencingPropertyName() {
+ return collectionReferencingPropertyName;
+ }
+
+ public MiddleIdData getReferencingIdData() {
+ return referencingIdData;
+ }
+
+ public MiddleTableQueryGenerator getQueryGenerator() {
+ return queryGenerator;
+ }
+}
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -1,82 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- *
- * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
- * by the @authors tag. All rights reserved.
- *
- * See the copyright.txt in the distribution for a full listing of individual
- * contributors. 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.entities.mapper.relation;
-
-import org.jboss.envers.entities.mapper.PropertyMapper;
-import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.entities.mapper.relation.lazy.DetachedRelationInitializor;
-import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
-import org.jboss.envers.entities.mapper.id.IdMapper;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-import org.jboss.envers.configuration.VersionsConfiguration;
-import org.hibernate.collection.PersistentCollection;
-
-import java.util.*;
-import java.io.Serializable;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class ManyToManyNotOwningMapper extends AbstractOneToManyMapper implements
PropertyMapper {
- /**
- * Name of the entity that declares the relation.
- */
- private final String referencedEntityName;
-
- // private final DetachedRelationQueryGenerator queryGenerator;
-
- public ManyToManyNotOwningMapper(VersionsEntitiesConfiguration verEntCfg, String
referencingEntityName,
- String referencedEntityName, String
collectionReferencingPropertyName,
- String versionsReferencedEntityName, String
versionsMiddleEntityName,
- IdMapper referencingMiddleIdMapper, IdMapper
referencedMiddleIdMapper,
- IdMapper referencedIdMapper) {
- super(referencingEntityName, collectionReferencingPropertyName);
-
- this.referencedEntityName = referencedEntityName;
-
- // queryGenerator = new DetachedRelationQueryGenerator(verEntCfg,
versionsReferencedEntityName,
- // versionsMiddleEntityName, referencingMiddleIdMapper,
referencedMiddleIdMapper, referencedIdMapper);
- }
-
- public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
- PersistentCollection
newColl,
- Serializable
oldColl, Serializable id) {
- // This is the not-owning side of the collection.
- return null;
- }
-
- public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
- // This is the not-owning side of the collection.
- return false;
- }
-
- protected <T> Initializor<T> getInitializator(VersionsConfiguration
verCfg,
-
VersionsReaderImplementor versionsReader,
- Class<?>
entityClass, Object primaryKey,
- Number revision,
Class<T> collectionClass) {
- return null; //new DetachedRelationInitializor<T>(verCfg,
referencedEntityName, queryGenerator,
- // versionsReader, primaryKey, revision, collectionClass);
- }
-}
\ No newline at end of file
Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java
(from rev 151,
trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java)
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -0,0 +1,55 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.entities.mapper.PropertyMapper;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
+import
org.jboss.envers.entities.mapper.relation.lazy.initializor.MapCollectionInitializor;
+import org.jboss.envers.configuration.VersionsConfiguration;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.hibernate.collection.PersistentCollection;
+
+import java.util.Map;
+import java.util.Collection;
+import java.io.Serializable;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MapCollectionMapper<T extends Map> extends
AbstractCollectionMapper<T> implements PropertyMapper {
+ private final MiddleComponentData elementComponentData;
+ private final MiddleComponentData indexComponentData;
+
+ public MapCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
+ Class<? extends T> collectionClass, Class<?
extends T> proxyClass,
+ MiddleComponentData elementComponentData,
MiddleComponentData indexComponentData) {
+ super(commonCollectionMapperData, collectionClass, proxyClass);
+ this.elementComponentData = elementComponentData;
+ this.indexComponentData = indexComponentData;
+ }
+
+ protected Initializor<T> getInitializor(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
+ Object primaryKey, Number revision) {
+ return new MapCollectionInitializor<T>(verCfg, versionsReader,
commonCollectionMapperData.getQueryGenerator(),
+ primaryKey, revision, collectionClass, elementComponentData,
indexComponentData);
+ }
+
+ protected Collection getNewCollectionContent(PersistentCollection newCollection) {
+ if (newCollection == null) {
+ return null;
+ } else {
+ return ((Map) newCollection).entrySet();
+ }
+ }
+
+ protected Collection getOldCollectionContent(Serializable oldCollection) {
+ if (oldCollection == null) {
+ return null;
+ } else {
+ return ((Map) oldCollection).entrySet();
+ }
+ }
+
+ protected void mapToMapFromObject(Map<String, Object> data, Object changed) {
+ elementComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry)
changed).getValue());
+ indexComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry)
changed).getKey());
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -1,144 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- *
- * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
- * by the @authors tag. All rights reserved.
- *
- * See the copyright.txt in the distribution for a full listing of individual
- * contributors. 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.entities.mapper.relation;
-
-import org.jboss.envers.entities.mapper.PropertyMapper;
-import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.entities.mapper.relation.lazy.DetachedRelationInitializor;
-import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
-import org.jboss.envers.entities.mapper.id.IdMapper;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.RevisionType;
-import org.hibernate.collection.PersistentCollection;
-
-import java.util.*;
-import java.io.Serializable;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class OneToManyDetachedMapper extends AbstractOneToManyMapper implements
PropertyMapper {
- private final VersionsEntitiesConfiguration verEntCfg;
- /**
- * Name of the entity that declares the relation.
- */
- private final String referencedEntityName;
- /**
- * Name of the property in <code>referencedEntityName</code> that holds
the value of the relation.
- */
- private final String collectionReferencingPropertyName;
- /**
- * Generated middle entity, which stores the bindings of the relation.
- */
- private final String versionsMiddleEntityName;
- private final IdMapper referencingMiddleIdMapper;
- private final IdMapper referencedMiddleIdMapper;
-
- //private final DetachedRelationQueryGenerator queryGenerator;
-
- public OneToManyDetachedMapper(VersionsEntitiesConfiguration verEntCfg, String
referencingEntityName,
- String referencedEntityName, String
collectionReferencingPropertyName,
- String versionsReferencedEntityName, String
versionsMiddleEntityName,
- IdMapper referencingMiddleIdMapper, IdMapper
referencedMiddleIdMapper,
- IdMapper referencedIdMapper) {
- super(referencingEntityName, collectionReferencingPropertyName);
-
- this.verEntCfg = verEntCfg;
- this.referencedEntityName = referencedEntityName;
- this.collectionReferencingPropertyName = collectionReferencingPropertyName;
- this.versionsMiddleEntityName = versionsMiddleEntityName;
- this.referencingMiddleIdMapper = referencingMiddleIdMapper;
- this.referencedMiddleIdMapper = referencedMiddleIdMapper;
-
- //queryGenerator = new DetachedRelationQueryGenerator(verEntCfg,
versionsReferencedEntityName,
- // versionsMiddleEntityName, referencingMiddleIdMapper,
referencedMiddleIdMapper, referencedIdMapper);
- }
-
- private Collection getOldCollection(Serializable oldColl) {
- if (oldColl instanceof Map) {
- return ((Map) oldColl).keySet();
- } else {
- return (Collection) oldColl;
- }
- }
-
- private void addCollectionChanges(List<PersistentCollectionChangeData>
collectionChanges, Set<Object> changed,
- RevisionType revisionType, Serializable id) {
- for (Object changedEntity : changed) {
- Map<String, Object> entityData = new HashMap<String, Object>();
- Map<String, Object> originalId = new HashMap<String, Object>();
- entityData.put(verEntCfg.getOriginalIdPropName(), originalId);
-
- collectionChanges.add(new
PersistentCollectionChangeData(versionsMiddleEntityName, entityData, changedEntity));
- referencingMiddleIdMapper.mapToMapFromId(originalId, id);
- referencedMiddleIdMapper.mapToMapFromEntity(originalId, changedEntity);
-
- entityData.put(verEntCfg.getRevisionTypePropName(), revisionType);
- }
- }
-
- @SuppressWarnings({"unchecked"})
- public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
- PersistentCollection
newColl,
- Serializable
oldColl, Serializable id) {
- if (!collectionReferencingPropertyName.equals(referencingPropertyName)) {
- return null;
- }
-
- List<PersistentCollectionChangeData> collectionChanges = new
ArrayList<PersistentCollectionChangeData>();
-
- Collection newCollection = (Collection) newColl;
- Collection oldCollection = getOldCollection(oldColl);
-
- Set<Object> added = new HashSet<Object>();
- if (newColl != null) { added.addAll(newCollection); }
- if (oldColl != null) { added.removeAll(oldCollection); }
-
- addCollectionChanges(collectionChanges, added, RevisionType.ADD, id);
-
- Set<Object> deleted = new HashSet<Object>();
- if (oldColl != null) { deleted.addAll(oldCollection); }
- if (newColl != null) { deleted.removeAll(newCollection); }
-
- addCollectionChanges(collectionChanges, deleted, RevisionType.DEL, id);
-
- return collectionChanges;
- }
-
- @SuppressWarnings({"unchecked"})
- public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
- // Changes are mapped in the "mapCollectionChanges" method.
- return false;
- }
-
- protected <T> Initializor<T> getInitializator(VersionsConfiguration
verCfg,
-
VersionsReaderImplementor versionsReader,
- Class<?>
entityClass, Object primaryKey,
- Number revision,
Class<T> collectionClass) {
- return null;// new DetachedRelationInitializor<T>(verCfg,
referencedEntityName, queryGenerator,
- //versionsReader, primaryKey, revision, collectionClass);
- }
-}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -1,6 +1,7 @@
package org.jboss.envers.entities.mapper.relation.component;
import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
@@ -24,4 +25,14 @@
* @param obj Object to map from.
*/
void mapToMapFromObject(Map<String, Object> data, Object obj);
+
+ /**
+ * Adds query statements, which contains restrictions, which express the property
that part of the middle
+ * entity with alias prefix1, is equal to part of the middle entity with alias
prefix2 (the entity is the same).
+ * The part is the component's representation in the middle entity.
+ * @param parameters Parameters, to which to add the statements.
+ * @param prefix1 First alias of the entity + prefix to add to the properties.
+ * @param prefix2 Second alias of the entity + prefix to add to the properties.
+ */
+ void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2);
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -1,12 +1,12 @@
package org.jboss.envers.entities.mapper.relation.component;
import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
/**
* @author Adam Warski (adam at warski dot org)
- * @deprecated
*/
public final class MiddleDummyComponentMapper implements MiddleComponentMapper {
public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data, Number revision) {
@@ -15,4 +15,7 @@
public void mapToMapFromObject(Map<String, Object> data, Object obj) {
}
+
+ public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String
prefix2) {
+ }
}
\ No newline at end of file
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -2,6 +2,7 @@
import org.jboss.envers.entities.EntityInstantiator;
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
@@ -26,4 +27,8 @@
public void mapToMapFromObject(Map<String, Object> data, Object obj) {
// Doing nothing.
}
+
+ public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String
prefix2) {
+ // Doing nothing.
+ }
}
\ No newline at end of file
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -2,6 +2,7 @@
import org.jboss.envers.entities.EntityInstantiator;
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
@@ -23,4 +24,8 @@
public void mapToMapFromObject(Map<String, Object> data, Object obj) {
relatedIdData.getPrefixedMapper().mapToMapFromEntity(data, obj);
}
+
+ public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String
prefix2) {
+ relatedIdData.getPrefixedMapper().addIdsEqualToQuery(parameters, prefix1,
prefix2);
+ }
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -2,6 +2,7 @@
import org.jboss.envers.entities.EntityInstantiator;
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
@@ -25,4 +26,8 @@
public void mapToMapFromObject(Map<String, Object> data, Object obj) {
data.put(propertyName, obj);
}
+
+ public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String
prefix2) {
+ parameters.addWhere(prefix1 + "." + propertyName, false, "=",
prefix2 + "." + propertyName, false);
+ }
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -12,8 +12,8 @@
* Initializes a map.
* @author Adam Warski (adam at warski dot org)
*/
-public class MapCollectionInitializor<T extends Map<Object, Object>> extends
AbstractCollectionInitializor<T> {
- private final Class<T> collectionClass;
+public class MapCollectionInitializor<T extends Map> extends
AbstractCollectionInitializor<T> {
+ private final Class<? extends T> collectionClass;
private final MiddleComponentData elementComponentData;
private final MiddleComponentData indexComponentData;
@@ -21,7 +21,7 @@
VersionsReaderImplementor versionsReader,
MiddleTableQueryGenerator queryGenerator,
Object primaryKey, Number revision,
- Class<T> collectionClass,
+ Class<? extends T> collectionClass,
MiddleComponentData elementComponentData,
MiddleComponentData indexComponentData) {
super(verCfg, versionsReader, queryGenerator, primaryKey, revision);
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -74,6 +74,7 @@
}
public boolean add(U o) {
+ checkInit();
return delegate.add(o);
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -106,12 +106,20 @@
return delegate.entrySet();
}
+ @Override
+ public String toString() {
+ checkInit();
+ return delegate.toString();
+ }
+
@SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"})
- public boolean equals(Object o) {
+ @Override
+ public boolean equals(Object obj) {
checkInit();
- return delegate.equals(o);
+ return delegate.equals(obj);
}
+ @Override
public int hashCode() {
checkInit();
return delegate.hashCode();
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-21
12:51:09 UTC (rev 153)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -1,12 +1,17 @@
package org.jboss.envers.entities.mapper.relation.query;
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
import org.jboss.envers.entities.mapper.id.QueryParameterData;
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.RevisionType;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
import org.hibernate.Query;
+import java.util.Collections;
+
/**
* @author Adam Warski (adam at warski dot org)
*/
@@ -16,42 +21,53 @@
public OneEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
String versionsMiddleEntityName,
- MiddleIdData referencingIdData) {
+ MiddleIdData referencingIdData,
+ MiddleComponentData... componentDatas) {
this.referencingIdData = referencingIdData;
/*
* The query that we need to create:
* SELECT new list(ee) FROM middleEntity ee WHERE
- * (only entities referenced by the association)
- * ee.id2 = :id2 AND
+ * (only entities referenced by the association; id_ref_ing = id of the
referencing entity)
+ * ee.originalId.id_ref_ing = :id_ref_ing AND
* (the association at revision :revision)
* ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2
- * WHERE ee2.revision <= :revision AND ee2.id2 = :id2) AND
+ * WHERE ee2.revision <= :revision AND ee2.originalId.* =
ee.originalId.*) AND
* (only non-deleted entities and associations)
* ee.revision_type != DEL
*/
String revisionPropertyPath = verEntCfg.getRevisionPropPath();
String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
- StringBuilder querySB = new StringBuilder();
- querySB
- .append("SELECT new list(ee) FROM
").append(versionsMiddleEntityName).append(" ee ")
- .append("WHERE ")
- // ee.id2 = :id2
-
.append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee." +
originalIdPropertyName, true))
- .append(" AND ")
- // ee.revision = (SELECT max(ee2.revision) ...
- .append("ee.").append(revisionPropertyPath).append(" =
(SELECT max(ee2.").append(revisionPropertyPath)
- .append(") FROM
").append(versionsMiddleEntityName).append(" ee2 ")
- // ee2.revision <= :revision
- .append("WHERE
ee2.").append(revisionPropertyPath).append(" <= :revision AND ")
- // ee2.id2 = :id2)
-
.append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee2." +
originalIdPropertyName, true))
- .append(") AND ")
- // ee.revision_type != DEL
-
.append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" !=
").append(":delrevisiontype");
+ // SELECT new list(ee) FROM middleEntity ee
+ QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee");
+ qb.addProjection("new list", "ee", false, false);
+ // WHERE
+ Parameters rootParameters = qb.getRootParameters();
+ // ee.originalId.id_ref_ing = :id_ref_ing
+ referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters,
originalIdPropertyName, true);
+ // SELECT max(ee2.revision) FROM middleEntity ee2
+ QueryBuilder maxRevQb = qb.newSubQueryBuilder(versionsMiddleEntityName,
"ee2");
+ maxRevQb.addProjection("max", revisionPropertyPath, false);
+ // WHERE
+ Parameters maxRevQbParameters = maxRevQb.getRootParameters();
+ // ee2.revision <= :revision
+ maxRevQbParameters.addWhereWithNamedParam(revisionPropertyPath,
"<=", "revision");
+ // ee2.originalId.* = ee.originalId.*
+ String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName;
+ String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName;
+ referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxRevQbParameters,
eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
+ for (MiddleComponentData componentData : componentDatas) {
+ componentData.getComponentMapper().addMiddleEqualToQuery(maxRevQbParameters,
eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
+ }
+ // ee.revision = (SELECT max(...) ...)
+ rootParameters.addWhere(revisionPropertyPath, "=", maxRevQb);
+ // ee.revision_type != DEL
+ rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(),
"!=", "delrevisiontype");
- queryString = querySB.toString();
+ StringBuilder sb = new StringBuilder();
+ qb.build(sb, Collections.<String, Object>emptyMap());
+ queryString = sb.toString();
}
public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey,
Number revision) {
Modified: trunk/src/main/org/jboss/envers/tools/query/Parameters.java
===================================================================
--- trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 12:51:09 UTC
(rev 153)
+++ trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 19:13:24 UTC
(rev 154)
@@ -135,15 +135,22 @@
}
public void addWhereWithParam(String left, boolean addAlias, String op, Object
paramValue) {
+ String paramName = generateQueryParam();
+ localQueryParamValues.put(paramName, paramValue);
+
+ addWhereWithNamedParam(left, addAlias, op, paramName);
+ }
+
+ public void addWhereWithNamedParam(String left, String op, String paramName) {
+ addWhereWithNamedParam(left, true, op, paramName);
+ }
+
+ public void addWhereWithNamedParam(String left, boolean addAlias, String op, String
paramName) {
StringBuilder expression = new StringBuilder();
if (addAlias) { expression.append(alias).append("."); }
expression.append(left);
-
expression.append(" ").append(op).append(" ");
-
- String paramName = generateQueryParam();
- localQueryParamValues.put(paramName, paramValue);
expression.append(":").append(paramName);
expressions.add(expression.toString());
Modified: trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java
===================================================================
--- trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java 2008-09-21 12:51:09 UTC
(rev 153)
+++ trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java 2008-09-21 19:13:24 UTC
(rev 154)
@@ -130,10 +130,14 @@
}
public void addProjection(String function, String propertyName, boolean distinct) {
+ addProjection(function, propertyName, distinct, true);
+ }
+
+ public void addProjection(String function, String propertyName, boolean distinct,
boolean addAlias) {
if (function == null) {
- projections.add((distinct ? "distinct " : "") + alias +
"." + propertyName);
+ projections.add((distinct ? "distinct " : "") + (addAlias
? alias+ "." : "") + propertyName);
} else {
- projections.add(function + "(" + (distinct ? "distinct "
: "") + alias + "." + propertyName + ")");
+ projections.add(function + "(" + (distinct ? "distinct "
: "") + (addAlias ? alias + "." : "") + propertyName +
")");
}
}
@@ -141,7 +145,7 @@
* Builds the given query, appending results to the given string buffer, and adding
all query parameter values
* that are used to the map provided.
* @param sb String builder to which the query will be appended.
- * @param queryParamValues Values of parameters used in the query.
+ * @param queryParamValues Map to which name and values of parameters used in the
query should be added.
*/
public void build(StringBuilder sb, Map<String, Object> queryParamValues) {
sb.append("select ");
Copied: trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java
(from rev 153,
trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -0,0 +1,63 @@
+package org.jboss.envers.test.entities.collection;
+
+import org.jboss.envers.Versioned;
+import org.hibernate.annotations.CollectionOfElements;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class StringMapEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Versioned
+ @CollectionOfElements
+ private Map<String, String> strings;
+
+ public StringMapEntity() {
+ strings = new HashMap<String, String>();
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Map<String, String> getStrings() {
+ return strings;
+ }
+
+ public void setStrings(Map<String, String> strings) {
+ this.strings = strings;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof StringMapEntity)) return false;
+
+ StringMapEntity that = (StringMapEntity) o;
+
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return (id != null ? id.hashCode() : 0);
+ }
+
+ public String toString() {
+ return "SME(id = " + id + ", strings = " + strings +
")";
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java (from
rev 153, trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java
(rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -0,0 +1,115 @@
+package org.jboss.envers.test.integration.collection;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.tools.TestTools;
+import org.jboss.envers.test.entities.collection.StringMapEntity;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.hibernate.ejb.Ejb3Configuration;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.Collections;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class StringMap extends AbstractEntityTest {
+ private Integer sme1_id;
+ private Integer sme2_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StringMapEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StringMapEntity sme1 = new StringMapEntity();
+ StringMapEntity sme2 = new StringMapEntity();
+
+ // Revision 1 (sme1: initialy empty, sme2: initialy 1 mapping)
+ em.getTransaction().begin();
+
+ sme2.getStrings().put("1", "a");
+
+ em.persist(sme1);
+ em.persist(sme2);
+
+ em.getTransaction().commit();
+
+ // Revision 2 (sme1: adding 2 mappings, sme2: no changes)
+ em.getTransaction().begin();
+
+ sme1 = em.find(StringMapEntity.class, sme1.getId());
+ sme2 = em.find(StringMapEntity.class, sme2.getId());
+
+ sme1.getStrings().put("1", "a");
+ sme1.getStrings().put("2", "b");
+
+ em.getTransaction().commit();
+
+ // Revision 3 (sme1: removing an existing mapping, sme2: replacing a value)
+ em.getTransaction().begin();
+
+ sme1 = em.find(StringMapEntity.class, sme1.getId());
+ sme2 = em.find(StringMapEntity.class, sme2.getId());
+
+ sme1.getStrings().remove("1");
+ sme2.getStrings().put("1", "b");
+
+ em.getTransaction().commit();
+
+ // Revision 4 (sme1: removing a non-existing mapping, sme2: replacing with the
same value)
+ em.getTransaction().begin();
+
+ sme1 = em.find(StringMapEntity.class, sme1.getId());
+ sme2 = em.find(StringMapEntity.class, sme2.getId());
+
+ sme1.getStrings().remove("3");
+ sme2.getStrings().put("1", "b");
+
+ em.getTransaction().commit();
+
+ //
+
+ sme1_id = sme1.getId();
+ sme2_id = sme2.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2,
3).equals(getVersionsReader().getRevisions(StringMapEntity.class, sme1_id));
+ assert Arrays.asList(1,
3).equals(getVersionsReader().getRevisions(StringMapEntity.class, sme2_id));
+ }
+
+ @Test
+ public void testHistoryOfSse1() {
+ StringMapEntity rev1 = getVersionsReader().find(StringMapEntity.class, sme1_id,
1);
+ StringMapEntity rev2 = getVersionsReader().find(StringMapEntity.class, sme1_id,
2);
+ StringMapEntity rev3 = getVersionsReader().find(StringMapEntity.class, sme1_id,
3);
+ StringMapEntity rev4 = getVersionsReader().find(StringMapEntity.class, sme1_id,
4);
+
+ assert rev1.getStrings().equals(Collections.EMPTY_MAP);
+ assert rev2.getStrings().equals(TestTools.makeMap("1", "a",
"2", "b"));
+ System.out.println(rev2.getStrings());
+ System.out.println(rev3.getStrings());
+ System.out.println(rev4.getStrings());
+ assert rev3.getStrings().equals(TestTools.makeMap("2",
"b"));
+ assert rev4.getStrings().equals(TestTools.makeMap("2",
"b"));
+ }
+
+ @Test
+ public void testHistoryOfSse2() {
+ StringMapEntity rev1 = getVersionsReader().find(StringMapEntity.class, sme2_id,
1);
+ StringMapEntity rev2 = getVersionsReader().find(StringMapEntity.class, sme2_id,
2);
+ StringMapEntity rev3 = getVersionsReader().find(StringMapEntity.class, sme2_id,
3);
+ StringMapEntity rev4 = getVersionsReader().find(StringMapEntity.class, sme2_id,
4);
+
+ assert rev1.getStrings().equals(TestTools.makeMap("1",
"a"));
+ assert rev2.getStrings().equals(TestTools.makeMap("1",
"a"));
+ assert rev3.getStrings().equals(TestTools.makeMap("1",
"b"));
+ assert rev4.getStrings().equals(TestTools.makeMap("1",
"b"));
+ }
+}
\ No newline at end of file
Modified: trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java 2008-09-21
12:51:09 UTC (rev 153)
+++ trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java 2008-09-21
19:13:24 UTC (rev 154)
@@ -10,7 +10,6 @@
import javax.persistence.EntityManager;
import java.util.Arrays;
import java.util.Collections;
-import java.util.HashSet;
/**
* @author Adam Warski (adam at warski dot org)
@@ -30,18 +29,18 @@
StringSetEntity sse1 = new StringSetEntity();
StringSetEntity sse2 = new StringSetEntity();
- // Revision 1
+ // Revision 1 (sse1: initialy empty, sse2: initialy 2 elements)
em.getTransaction().begin();
sse2.getStrings().add("sse2_string1");
+ sse2.getStrings().add("sse2_string2");
em.persist(sse1);
em.persist(sse2);
em.getTransaction().commit();
- // Revision 2
-
+ // Revision 2 (sse1: adding 2 elements, sse2: adding an existing element)
em.getTransaction().begin();
sse1 = em.find(StringSetEntity.class, sse1.getId());
@@ -54,7 +53,7 @@
em.getTransaction().commit();
- // Revision 3
+ // Revision 3 (sse1: removing a non-existing element, sse2: removing one
element)
em.getTransaction().begin();
sse1 = em.find(StringSetEntity.class, sse1.getId());
@@ -94,8 +93,8 @@
StringSetEntity rev2 = getVersionsReader().find(StringSetEntity.class, sse2_id,
2);
StringSetEntity rev3 = getVersionsReader().find(StringSetEntity.class, sse2_id,
3);
- assert rev1.getStrings().equals(TestTools.makeSet("sse2_string1"));
- assert rev2.getStrings().equals(TestTools.makeSet("sse2_string1"));
- assert rev3.getStrings().equals(Collections.EMPTY_SET);
+ assert rev1.getStrings().equals(TestTools.makeSet("sse2_string1",
"sse2_string2"));
+ assert rev2.getStrings().equals(TestTools.makeSet("sse2_string1",
"sse2_string2"));
+ assert rev3.getStrings().equals(TestTools.makeSet("sse2_string2"));
}
}
\ No newline at end of file
Modified: trunk/src/test/org/jboss/envers/test/tools/TestTools.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/tools/TestTools.java 2008-09-21 12:51:09 UTC (rev
153)
+++ trunk/src/test/org/jboss/envers/test/tools/TestTools.java 2008-09-21 19:13:24 UTC (rev
154)
@@ -1,8 +1,6 @@
package org.jboss.envers.test.tools;
-import java.util.Set;
-import java.util.HashSet;
-import java.util.List;
+import java.util.*;
/**
* @author Adam Warski (adam at warski dot org)
@@ -18,6 +16,17 @@
return ret;
}
+ public static Map<Object, Object> makeMap(Object... objects) {
+ Map<Object, Object> ret = new HashMap<Object, Object>();
+ // The number of objects must be divisable by 2.
+ //noinspection ManualArrayToCollectionCopy
+ for (int i=0; i<objects.length; i+=2) {
+ ret.put(objects[i], objects[i+1]);
+ }
+
+ return ret;
+ }
+
public static <T> boolean checkList(List<T> list, T... objects) {
if (list.size() != objects.length) {
return false;