Author: adamw
Date: 2008-09-19 10:51:22 -0400 (Fri, 19 Sep 2008)
New Revision: 151
Added:
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.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/MiddleComponentData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/
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/
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/Initializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.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/SortedMapProxy.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java
Removed:
trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java
Modified:
trunk/src/main/org/jboss/envers/VersionsReader.java
trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.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/ListProxy.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java
trunk/src/main/org/jboss/envers/event/VersionsEventListener.java
trunk/src/main/org/jboss/envers/exception/NotVersionedException.java
trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java
trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java
Log:
ENVERS-42, ENVERS-44: initial support of redesigned collection handling, ENVERS-50:
merging patch
Modified: trunk/src/main/org/jboss/envers/VersionsReader.java
===================================================================
--- trunk/src/main/org/jboss/envers/VersionsReader.java 2008-09-17 15:38:55 UTC (rev 150)
+++ trunk/src/main/org/jboss/envers/VersionsReader.java 2008-09-19 14:51:22 UTC (rev 151)
@@ -40,7 +40,7 @@
* @return The found entity instance at the given revision (its properties may be
partially filled
* if not all properties are versioned) or null, if an entity with that id didn't
exist at that
* revision.
- * @throws IllegalArgumentException If cls, primaryKey is null or revision is less or
equal to 0.
+ * @throws IllegalArgumentException If cls or primaryKey is null or revision is less
or equal to 0.
* @throws NotVersionedException When entities of the given class are not versioned.
* @throws IllegalStateException If the associated entity manager is closed.
*/
@@ -48,18 +48,13 @@
IllegalArgumentException, NotVersionedException, IllegalStateException;
/**
- * Get revisions, at which an entity was modified.
- * <b>Warning!</b> Only revisions, at which properties that the entity
"owns" changed will be returned.
- * Properties not owned by an entity are:
- * <ul>
- * <li>@OneToOne(mappedBy="...")</li>
- * <li>@OneToMany</li>
- * </ul>
+ * Get a list of revision numbers, at which an entity was modified.
* @param cls Class of the entity.
* @param primaryKey Primary key of the entity.
- * @return A list of revision numbers, at which the entity was modified, sorted in
ascending order.
+ * @return A list of revision numbers, at which the entity was modified, sorted in
ascending order (so older
+ * revisions come first).
* @throws NotVersionedException When entities of the given class are not versioned.
- * @throws IllegalArgumentException If cls, primaryKey is null.
+ * @throws IllegalArgumentException If cls or primaryKey is null.
* @throws IllegalStateException If the associated entity manager is closed.
*/
List<Number> getRevisions(Class<?> cls, Object primaryKey)
@@ -78,8 +73,8 @@
/**
* Gets the revision number, that corresponds to the given date. More precisely,
returns
- * the number of the highest revision, which was created before the given date. So:
- * <code>getRevisionDate(getRevisionNumberForDate(date)) < date</code>
and
+ * the number of the highest revision, which was created on or before the given date.
So:
+ * <code>getRevisionDate(getRevisionNumberForDate(date)) <=
date</code> and
* <code>getRevisionDate(getRevisionNumberForDate(date)+1) >
date</code>.
* @param date Date for which to get the revision.
* @return Revision number corresponding to the given date.
Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-17
15:38:55 UTC (rev 150)
+++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -94,12 +94,12 @@
try {
cfg.addDocument(writer.write(xmlMappingData.getMainXmlMapping()));
// TODO
- //writeDocument(xmlMappingData.getMainXmlMapping());
+ writeDocument(xmlMappingData.getMainXmlMapping());
for (Document additionalMapping :
xmlMappingData.getAdditionalXmlMappings()) {
cfg.addDocument(writer.write(additionalMapping));
// TODO
- //writeDocument(additionalMapping);
+ writeDocument(additionalMapping);
}
} catch (DocumentException e) {
throw new MappingException(e);
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -18,7 +18,7 @@
* Generates metadata for basic properties: immutable types (including enums) and
components
* @author Adam Warski (adam at warski dot org)
*/
-public class BasicMetadataGenerator {
+public final class BasicMetadataGenerator {
private final VersionsMetadataGenerator mainGenerator;
BasicMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) {
@@ -28,9 +28,11 @@
@SuppressWarnings({"unchecked"})
void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder
mapper,
ModificationStore store, boolean key) {
- Element prop_mapping = MetadataTools.addProperty(parent, name,
- value.getType().getName(), key);
- MetadataTools.addColumns(prop_mapping, (Iterator<Column>)
value.getColumnIterator());
+ if (parent != null) {
+ Element prop_mapping = MetadataTools.addProperty(parent, name,
+ value.getType().getName(), key);
+ MetadataTools.addColumns(prop_mapping, (Iterator<Column>)
value.getColumnIterator());
+ }
// A null mapper means that we only want to add xml mappings (while building the
id mapping)
if (mapper != null) {
@@ -41,23 +43,25 @@
@SuppressWarnings({"unchecked"})
void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder
mapper,
ModificationStore store) {
- Element prop_mapping = parent.addElement("property");
- prop_mapping.addAttribute("name", name);
+ if (parent != null) {
+ Element prop_mapping = parent.addElement("property");
+ prop_mapping.addAttribute("name", name);
- CustomType propertyType = (CustomType) value.getType();
+ CustomType propertyType = (CustomType) value.getType();
- Element type_mapping = prop_mapping.addElement("type");
- type_mapping.addAttribute("name", propertyType.getName());
+ Element type_mapping = prop_mapping.addElement("type");
+ type_mapping.addAttribute("name", propertyType.getName());
- Element type_param1 = type_mapping.addElement("param");
- type_param1.addAttribute("name", "enumClass");
- type_param1.setText(propertyType.getReturnedClass().getName());
+ Element type_param1 = type_mapping.addElement("param");
+ type_param1.addAttribute("name", "enumClass");
+ type_param1.setText(propertyType.getReturnedClass().getName());
- Element type_param2 = type_mapping.addElement("param");
- type_param2.addAttribute("name", "type");
- type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
+ Element type_param2 = type_mapping.addElement("param");
+ type_param2.addAttribute("name", "type");
+ type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
- MetadataTools.addColumns(prop_mapping, (Iterator<Column>)
value.getColumnIterator());
+ MetadataTools.addColumns(prop_mapping, (Iterator<Column>)
value.getColumnIterator());
+ }
mapper.add(name, store);
}
@@ -75,25 +79,27 @@
Element component_mapping = null;
Component prop_component = (Component) value;
- if (!firstPass) {
- // The required element already exists.
- Iterator<Element> iter =
parent.elementIterator("component");
- while (iter.hasNext()) {
- Element child = iter.next();
- if (child.attribute("name").getText().equals(name)) {
- component_mapping = child;
- break;
+ if (parent != null) {
+ if (!firstPass) {
+ // The required element already exists.
+ Iterator<Element> iter =
parent.elementIterator("component");
+ while (iter.hasNext()) {
+ Element child = iter.next();
+ if (child.attribute("name").getText().equals(name)) {
+ component_mapping = child;
+ break;
+ }
}
- }
- if (component_mapping == null) {
- throw new VersionsException("Element for component not found during
second pass!");
+ if (component_mapping == null) {
+ throw new VersionsException("Element for component not found
during second pass!");
+ }
+ } else {
+ component_mapping = parent.addElement("component");
+ component_mapping.addAttribute("name", name);
+
+ addComponentClassName(component_mapping, prop_component);
}
- } else {
- component_mapping = parent.addElement("component");
- component_mapping.addAttribute("name", name);
-
- addComponentClassName(component_mapping, prop_component);
}
CompositeMapperBuilder componentMapper = mapper.addComposite(name);
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -3,28 +3,50 @@
import org.hibernate.mapping.*;
import org.hibernate.mapping.Collection;
import org.hibernate.MappingException;
+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.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.id.IdMapper;
import org.jboss.envers.entities.EntityConfiguration;
import org.jboss.envers.entities.IdMappingData;
import org.jboss.envers.tools.Tools;
import org.jboss.envers.tools.StringTools;
+import org.jboss.envers.ModificationStore;
import org.dom4j.Element;
import java.util.*;
+import java.util.Set;
+import java.util.List;
/**
* Generates metadata for collection-valued properties.
* @author Adam Warski (adam at warski dot org)
*/
-public class CollectionMetadataGenerator {
+public final class CollectionMetadataGenerator {
private final VersionsMetadataGenerator mainGenerator;
CollectionMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) {
mainGenerator = versionsMetadataGenerator;
}
+ void addCollection(String name, Collection value, CompositeMapperBuilder
currentMapper,
+ String entityName, EntityXmlMappingData xmlMappingData) {
+ Type type = value.getType();
+
+ if ((type instanceof BagType || type instanceof SetType) &&
+ (value.getElement() instanceof OneToMany)) {
+ addOneToManyAttached(name, value, currentMapper, entityName);
+ } else {
+ addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData);
+ }
+ }
+
@SuppressWarnings({"unchecked"})
private String getMappedBy(Collection collectionValue) {
Iterator<Property> assocClassProps =
@@ -43,22 +65,37 @@
}
@SuppressWarnings({"unchecked"})
- void addOneToManyAttached(String name, Value value, CompositeMapperBuilder mapper,
String entityName) {
- Collection propertyValue = (Collection) value;
+ private String getMappedBy(Table collectionTable, PersistentClass referencedClass) {
+ Iterator<Property> properties = referencedClass.getPropertyIterator();
+ while (properties.hasNext()) {
+ Property property = properties.next();
+ if (property.getValue() instanceof Collection) {
+ // The equality is intentional. We want to find a collection property
with the same collection table.
+ //noinspection ObjectEquality
+ if (((Collection) property.getValue()).getCollectionTable() ==
collectionTable) {
+ return property.getName();
+ }
+ }
+ }
- String owningReferencePropertyName = getMappedBy(propertyValue);
+ return null;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addOneToManyAttached(String name, Collection value,
CompositeMapperBuilder mapper, String entityName) {
+ String owningReferencePropertyName = getMappedBy(value);
if (owningReferencePropertyName == null) {
- throw new MappingException("Unable to read the mapped by attribute for
" + name);
+ throw new MappingException("Unable to read the mapped by attribute for
" + name + " in " + entityName + "!");
}
EntityConfiguration configuration =
mainGenerator.getEntitiesConfigurations().get(entityName);
if (configuration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ throw new MappingException("Unable to read versioning configuration for
" + entityName + "!");
}
IdMappingData referencingIdMapping = configuration.getIdMappingData();
- String owningEntityName = ((OneToMany)
propertyValue.getElement()).getReferencedEntityName();
+ String owningEntityName = ((OneToMany)
value.getElement()).getReferencedEntityName();
String lastPropertyPrefix = owningReferencePropertyName + "_";
// Generating the id mapper for the relation
@@ -73,15 +110,207 @@
name));
}
+ private String getReferencedEntityName(Collection value) {
+ if (value.getElement() instanceof ToOne) {
+ return ((ToOne) value.getElement()).getReferencedEntityName();
+ } else {
+ return null;
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder
currentMapper,
+ String entityName, EntityXmlMappingData
xmlMappingData) {
+ // Generating the name of the middle table, its schema and catalog
+ // TODO: add support for @VersionsJoinTable
+ String middleTableName = value.getCollectionTable().getName();
+ String middleEntityName = middleTableName;
+ String versionsMiddleTableName =
mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName);
+ String versionsMiddleEntityName =
mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName);
+
+ String schema = value.getCollectionTable().getSchema();
+ String catalog = value.getCollectionTable().getCatalog();
+
+ // Generating the XML mapping for the middle entity, only if the relation
isn't inverse.
+ // If the relation is inverse, will be later checked by comparing middleEntityXml
with null.
+ Element middleEntityXml;
+ Element middleEntityXmlId;
+ Iterator<Column> middleEntityOriginalColumns;
+ if (!value.isInverse()) {
+ middleEntityXml =
MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(),
+ versionsMiddleEntityName, versionsMiddleTableName, schema, catalog,
null);
+ middleEntityXmlId = middleEntityXml.addElement("composite-id");
+ middleEntityOriginalColumns =
value.getCollectionTable().getColumnIterator();
+ } else {
+ middleEntityXml = null;
+ middleEntityXmlId = null;
+ middleEntityOriginalColumns = null;
+ }
+
+ // ******
+ // Generating the mapping for the referencing entity (it must be an entity).
+ // ******
+ EntityConfiguration referencingConfiguration =
mainGenerator.getEntitiesConfigurations().get(entityName);
+ if (referencingConfiguration == null) {
+ throw new MappingException("Unable to read versioning configuration for
" + entityName + "!");
+ }
+
+ // Getting the id-mapping data of the referencing entity (the entity that
"owns" this collection).
+ IdMappingData referencingIdMapping =
referencingConfiguration.getIdMappingData();
+
+ // Null if this collection doesn't reference an entity.
+ String referencedEntityName = getReferencedEntityName(value);
+ // Only valid for an inverse relation; null otherwise.
+ String mappedBy;
+
+ String referencingPrefix;
+ // Only valid if referencedEntityName isn't null.
+ String referencedPrefix;
+
+ if (value.isInverse()) {
+ // If the relation is inverse, then referencedEntityName is not null.
+ mappedBy = getMappedBy(value.getCollectionTable(),
mainGenerator.getCfg().getClassMapping(referencedEntityName));
+ if (mappedBy == null) {
+ throw new MappingException("Unable to read the mapped by attribute
for " + name);
+ }
+
+ referencingPrefix = mappedBy + "_";
+ referencedPrefix = StringTools.getLastComponent(referencedEntityName) +
"_";
+ } else {
+ mappedBy = null;
+
+ referencingPrefix = StringTools.getLastComponent(entityName) +
"_";
+ referencedPrefix = 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),
+ entityName,
+ mainGenerator.getVerEntCfg().getVersionsEntityName(entityName));
+
+ // Creating a query generator builder, to which additional id data will be added,
in case this collection
+ // references some entities (either from the element or index). At the end, this
will be used to build
+ // a query generator to read the raw data collection from the middle table.
+ QueryGeneratorBuilder queryGeneratorBuilder = new
QueryGeneratorBuilder(mainGenerator.getVerEntCfg(),
+ referencingIdData, versionsMiddleEntityName);
+
+ // Adding the XML mapping for the referencing entity, if the relation isn't
inverse.
+ if (middleEntityXml != null) {
+ middleEntityXmlId.addAttribute("name",
mainGenerator.getVerEntCfg().getOriginalIdPropName());
+
+ Element properties = (Element)
referencingIdMapping.getXmlRelationMapping().clone();
+ MetadataTools.prefixNamesInPropertyElement(properties, referencingPrefix,
middleEntityOriginalColumns, true);
+ for (Element idProperty : (java.util.List<Element>)
properties.elements()) {
+ middleEntityXmlId.add((Element) idProperty.clone());
+ }
+
+ // Adding the revision number as a foreign key to the revision info entity to
the composite id of the
+ // middle table.
+ mainGenerator.addRevisionInfoRelation(middleEntityXmlId);
+
+ // Adding the revision type property to the entity xml.
+ mainGenerator.addRevisionType(middleEntityXml);
+ }
+
+ // ******
+ // Generating the element mapping.
+ // ******
+ MiddleComponentData elementComponentData =
addValueToMiddleTable(value.getElement(), middleEntityXmlId,
+ middleEntityOriginalColumns, queryGeneratorBuilder, referencedPrefix);
+
+ // ******
+ // Optionally, generating the index mapping.
+ // ******
+ MiddleComponentData indexComponentData;
+ if (value instanceof IndexedCollection) {
+ IndexedCollection indexedValue = (IndexedCollection) value;
+ indexComponentData = null;
+ // TODO
+ } else {
+ // No index - creating a dummy mapper.
+ indexComponentData = new MiddleComponentData(new
MiddleDummyComponentMapper(),
+ queryGeneratorBuilder.getCurrentIndex());
+ }
+
+ // ******
+ // Generating the property mapper.
+ // ******
+
+ // Building the query generator.
+ MiddleTableQueryGenerator queryGenerator = queryGeneratorBuilder.build();
+
+ // 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));
+ } else if (type instanceof BagType) {
+ currentMapper.addComposite(name, new
BasicCollectionMapper<List>(mainGenerator.getVerEntCfg(),
+ versionsMiddleEntityName, referencingIdData, ArrayList.class,
ListProxy.class, queryGenerator,
+ name, elementComponentData));
+ } else {
+ throw new RuntimeException();
+ }
+ }
+
+ /**
+ *
+ * @param value Value, which should be mapped to the middle-table, either as a
relation to another entity,
+ * or as a simple value.
+ * @param middleEntityXml If not <code>null</code>, xml mapping for this
value is added to this element.
+ * @param middleEntityOriginalColumns If <code>middleEntityXml</code> is
not null, this iterator is used
+ * to read column names for the generated xml mapping.
+ * @param queryGeneratorBuilder In case <code>value</code> is a relation
to another entity, information about it
+ * should be added to the given.
+ * @param prefix Prefix for proeprty names of related entities identifiers.
+ * @return Data for mapping this component.
+ */
+ private MiddleComponentData addValueToMiddleTable(Value value, Element
middleEntityXml,
+ Iterator<Column>
middleEntityOriginalColumns,
+ QueryGeneratorBuilder
queryGeneratorBuilder,
+ String prefix) {
+ Type type = value.getType();
+ if (type instanceof ManyToOneType) {
+ ToOne toOneValue = (ToOne) value;
+ String referencedEntityName = toOneValue.getReferencedEntityName();
+ IdMappingData referencedIdMapping =
mainGenerator.getEntitiesConfigurations().get(
+ toOneValue.getReferencedEntityName()).getIdMappingData();
+
+ // Storing the id data of the referenced entity: original mapper, prefixed
mapper and entity name.
+ IdMapper referencedPrefixedIdMapper =
referencedIdMapping.getIdMapper().prefixMappedProperties(prefix);
+ MiddleIdData referencedIdData = new MiddleIdData(
+ referencedIdMapping.getIdMapper(),
+ referencedPrefixedIdMapper,
+ referencedEntityName,
+
mainGenerator.getVerEntCfg().getVersionsEntityName(referencedEntityName));
+ // And adding it to the generator builder.
+ queryGeneratorBuilder.addRelation(referencedIdData);
+
+ return new MiddleComponentData(new
MiddleRelatedComponentMapper(referencedIdData),
+ 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);
+
+ return new MiddleComponentData(new
MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), "element"),
+ queryGeneratorBuilder.getCurrentIndex());
+ } else {
+ // TODO: throw an exception
+ throw new RuntimeException();
+ }
+ }
+
private String getMiddleEntityName(String entityName, String referencedEntityName,
ManyToOne mto) {
return entityName + "_" + referencedEntityName + "_" +
mto.getTable().getName();
}
@SuppressWarnings({"unchecked"})
- void addOneToManyDetached(String name, Value value, CompositeMapperBuilder mapper,
String entityName,
+ private void addOneToManyDetached(String name, Collection value,
CompositeMapperBuilder mapper, String entityName,
EntityXmlMappingData xmlMappingData) {
- Collection propertyValue = (Collection) value;
- ManyToOne mto = (ManyToOne) propertyValue.getElement();
+ ManyToOne mto = (ManyToOne) value.getElement();
String referencedEntityName = mto.getReferencedEntityName();
@@ -141,27 +370,9 @@
mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMapper()));
}
- @SuppressWarnings({"unchecked"})
- private String getMappedBy(Table collectionTable, PersistentClass referencedClass) {
- Iterator<Property> properties = referencedClass.getPropertyIterator();
- while (properties.hasNext()) {
- Property property = properties.next();
- if (property.getValue() instanceof Collection) {
- // The equality is intentional. We want to find a collection property
with the same collection table.
- //noinspection ObjectEquality
- if (((Collection) property.getValue()).getCollectionTable() ==
collectionTable) {
- return property.getName();
- }
- }
- }
+ private void addManyToManyNotOwning(String name, Collection value,
CompositeMapperBuilder mapper, String entityName) {
+ ManyToOne mto = (ManyToOne) value.getElement();
- return null;
- }
-
- void addManyToManyNotOwning(String name, Value value, CompositeMapperBuilder mapper,
String entityName) {
- Collection propertyValue = (Collection) value;
- ManyToOne mto = (ManyToOne) propertyValue.getElement();
-
String referencedEntityName = mto.getReferencedEntityName();
EntityConfiguration configuration =
mainGenerator.getEntitiesConfigurations().get(entityName);
@@ -174,7 +385,7 @@
throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
}
- String mappedBy = getMappedBy(propertyValue.getCollectionTable(),
mainGenerator.getCfg().getClassMapping(referencedEntityName));
+ String mappedBy = getMappedBy(value.getCollectionTable(),
mainGenerator.getCfg().getClassMapping(referencedEntityName));
if (mappedBy == null) {
throw new MappingException("Unable to read the mapped by attribute for
" + name);
}
Deleted: trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -1,58 +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.configuration.metadata;
-
-import org.dom4j.Document;
-import org.dom4j.DocumentHelper;
-
-import java.util.List;
-import java.util.ArrayList;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class EntityMappingData {
- private Document mainMapping;
- private List<Document> additionalMappings;
-
- public EntityMappingData() {
- mainMapping = DocumentHelper.createDocument();
- additionalMappings = new ArrayList<Document>();
- }
-
- public Document getMainMapping() {
- return mainMapping;
- }
-
- public List<Document> getAdditionalMappings() {
- return additionalMappings;
- }
-
- public Document newAdditionalMapping() {
- Document additionalMapping = DocumentHelper.createDocument();
- additionalMappings.add(additionalMapping);
-
- return additionalMapping;
- }
-}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -22,7 +22,7 @@
* Generates metadata for primary identifiers (ids) of versions entities.
* @author Adam Warski (adam at warski dot org)
*/
-public class IdMetadataGenerator {
+public final class IdMetadataGenerator {
private final VersionsMetadataGenerator mainGenerator;
IdMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) {
Added: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,58 @@
+package org.jboss.envers.configuration.metadata;
+
+import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+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;
+import org.jboss.envers.entities.mapper.relation.query.ThreeEntityQueryGenerator;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * Builds query generators, for reading collection middle tables, along with any related
entities.
+ * The related entities information can be added gradually, and when complete, the query
generator can be built.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class QueryGeneratorBuilder {
+ private final VersionsEntitiesConfiguration verEntCfg;
+ private final MiddleIdData referencingIdData;
+ private final String versionsMiddleEntityName;
+ private final List<MiddleIdData> idDatas;
+
+ QueryGeneratorBuilder(VersionsEntitiesConfiguration verEntCfg, MiddleIdData
referencingIdData,
+ String versionsMiddleEntityName) {
+ this.verEntCfg = verEntCfg;
+ this.referencingIdData = referencingIdData;
+ this.versionsMiddleEntityName = versionsMiddleEntityName;
+
+ idDatas = new ArrayList<MiddleIdData>();
+ }
+
+ void addRelation(MiddleIdData idData) {
+ idDatas.add(idData);
+ }
+
+ MiddleTableQueryGenerator build() {
+ if (idDatas.size() == 0) {
+ return new OneEntityQueryGenerator(verEntCfg, versionsMiddleEntityName,
referencingIdData);
+ } else if (idDatas.size() == 1) {
+ return new TwoEntityQueryGenerator(verEntCfg, versionsMiddleEntityName,
referencingIdData,
+ idDatas.get(0));
+ } else if (idDatas.size() == 1) {
+ return new ThreeEntityQueryGenerator(verEntCfg, versionsMiddleEntityName,
referencingIdData,
+ idDatas.get(0), idDatas.get(1));
+ } else {
+ throw new IllegalStateException("Illegal number of related
entities.");
+ }
+ }
+
+ /**
+ * @return Current index of data in the array, which will be the element of a list,
returned when executing a query
+ * generated by the built query generator.
+ */
+ int getCurrentIndex() {
+ return idDatas.size();
+ }
+}
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -16,7 +16,7 @@
* Generates metadata for to-one relations (reference-valued properties).
* @author Adam Warski (adam at warski dot org)
*/
-public class ToOneRelationMetadataGenerator {
+public final class ToOneRelationMetadataGenerator {
private final VersionsMetadataGenerator mainGenerator;
ToOneRelationMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator)
{
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -46,7 +46,7 @@
* @author Adam Warski (adam at warski dot org)
* @author Sebastian Komander
*/
-public class VersionsMetadataGenerator {
+public final class VersionsMetadataGenerator {
private final Configuration cfg;
private final GlobalConfiguration globalCfg;
private final VersionsEntitiesConfiguration verEntCfg;
@@ -57,10 +57,10 @@
private final CollectionMetadataGenerator collectionMetadataGenerator;
private final ToOneRelationMetadataGenerator toOneRelationMetadataGenerator;
- private Map<String, EntityConfiguration> entitiesConfigurations;
+ private final Map<String, EntityConfiguration> entitiesConfigurations;
// Map entity name -> (join descriptor -> element describing the
"versioned" join)
- private Map<String, Map<Join, Element>> entitiesJoins;
+ private final Map<String, Map<Join, Element>> entitiesJoins;
private YLog log =
YLogManager.getLogManager().getLog(VersionsMetadataGenerator.class);
@@ -119,8 +119,8 @@
@SuppressWarnings({"unchecked"})
void addValue(Element parent, String name, Value value, CompositeMapperBuilder
currentMapper,
- ModificationStore store, String entityName,
EntityXmlMappingData xmlMappingData,
- boolean firstPass) {
+ ModificationStore store, String entityName, EntityXmlMappingData
xmlMappingData,
+ boolean firstPass) {
Type type = value.getType();
if (type instanceof ComponentType) {
@@ -139,48 +139,34 @@
if (firstPass) {
basicMetadataGenerator.addEnumValue(parent, name, value, currentMapper,
store);
}
- } else if (type instanceof ManyToOneType) {
- // only second pass
- if (!firstPass) {
- toOneRelationMetadataGenerator.addToOne(parent, name, value,
currentMapper, entityName);
- }
- } else if (type instanceof OneToOneType) {
- // only second pass
- if (!firstPass) {
- toOneRelationMetadataGenerator.addOneToOneNotOwning(name, value,
currentMapper, entityName);
- }
- } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(
- type.getClass().getName())) {
+ } else if (type instanceof CustomType &&
+
("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) ||
+
"org.hibernate.type.StringClobType".equals(type.getName()))) {
// only first pass
if (firstPass) {
basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper,
store, false);
}
- } else if (type instanceof CustomType &&
-
("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) ||
-
"org.hibernate.type.StringClobType".equals(type.getName()))) {
+ } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(
+ type.getClass().getName())) {
// only first pass
if (firstPass) {
basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper,
store, false);
}
- } else if ((type instanceof BagType || type instanceof SetType) &&
- ((((Collection) value).getElement() instanceof OneToMany))) {
+ } else if (type instanceof ManyToOneType) {
// only second pass
if (!firstPass) {
- collectionMetadataGenerator.addOneToManyAttached(name, value,
currentMapper, entityName);
+ toOneRelationMetadataGenerator.addToOne(parent, name, value,
currentMapper, entityName);
}
- } else if ((type instanceof BagType || type instanceof SetType) &&
- ((((Collection) value).getElement() instanceof ManyToOne)) &&
- !((Collection) value).isInverse()) {
+ } else if (type instanceof OneToOneType) {
// only second pass
if (!firstPass) {
- collectionMetadataGenerator.addOneToManyDetached(name, value,
currentMapper, entityName, xmlMappingData);
+ toOneRelationMetadataGenerator.addOneToOneNotOwning(name, value,
currentMapper, entityName);
}
- } else if ((type instanceof BagType || type instanceof SetType) &&
- ((((Collection) value).getElement() instanceof ManyToOne)) &&
- ((Collection) value).isInverse()) {
+ } else if (type instanceof CollectionType) {
// only second pass
if (!firstPass) {
- collectionMetadataGenerator.addManyToManyNotOwning(name, value,
currentMapper, entityName);
+ collectionMetadataGenerator.addCollection(name, (Collection) value,
currentMapper, entityName,
+ xmlMappingData);
}
} else {
String message = "Type not supported for versioning: " +
type.getClass().getName() +
@@ -395,26 +381,10 @@
return basicMetadataGenerator;
}
- IdMetadataGenerator getIdMetadataGenerator() {
- return idMetadataGenerator;
- }
-
- CollectionMetadataGenerator getCollectionMetadataGenerator() {
- return collectionMetadataGenerator;
- }
-
- ToOneRelationMetadataGenerator getToOneRelationMetadataGenerator() {
- return toOneRelationMetadataGenerator;
- }
-
Configuration getCfg() {
return cfg;
}
- GlobalConfiguration getGlobalCfg() {
- return globalCfg;
- }
-
VersionsEntitiesConfiguration getVerEntCfg() {
return verEntCfg;
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-09-17
15:38:55 UTC (rev 150)
+++ trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -40,7 +40,7 @@
* @param oldObj Old state of the entity.
* @return True if there are any differences between the states represented by newObj
and oldObj.
*/
- public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj);
+ boolean mapToMapFromEntity(Map<String, Object> data, Object newObj, Object
oldObj);
/**
* Maps properties from the given map to the given object.
@@ -51,8 +51,8 @@
* @param versionsReader VersionsReader for reading relations
* @param revision Revision at which the object is read, for reading relations
*/
- public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey,
- VersionsReaderImplementor versionsReader, Number
revision);
+ void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object
primaryKey,
+ VersionsReaderImplementor versionsReader, Number revision);
/**
* Maps collection changes
@@ -62,7 +62,7 @@
* @param id Id of the object owning the collection.
* @return List of changes that need to be performed on the persistent store.
*/
- public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
- PersistentCollection
newColl,
- Serializable
oldColl, Serializable id);
+ List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
+ PersistentCollection
newColl,
+ Serializable oldColl,
Serializable id);
}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,90 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
+import org.jboss.envers.entities.mapper.PropertyMapper;
+import org.jboss.envers.RevisionType;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.hibernate.collection.PersistentCollection;
+
+import java.util.*;
+import java.io.Serializable;
+
+/**
+ * @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;
+
+ protected AbstractCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String
versionsMiddleEntityName,
+ String collectionReferencingPropertyName,
MiddleIdData referencingIdData) {
+ this.verEntCfg = verEntCfg;
+ this.versionsMiddleEntityName = versionsMiddleEntityName;
+ this.collectionReferencingPropertyName = collectionReferencingPropertyName;
+ this.referencingIdData = referencingIdData;
+ }
+
+ protected abstract Collection getNewCollectionContent(PersistentCollection
newCollection);
+ protected abstract Collection getOldCollectionContent(Serializable oldCollection);
+
+ /**
+ * Maps the changed collection element to the given map.
+ * @param data Where to map the data.
+ * @param changed The changed collection element to map.
+ */
+ protected abstract void mapToMapFromObject(Map<String, Object> data, Object
changed);
+
+ private void addCollectionChanges(List<PersistentCollectionChangeData>
collectionChanges, Set<Object> changed,
+ RevisionType revisionType, Serializable id) {
+ 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);
+
+ collectionChanges.add(new
PersistentCollectionChangeData(versionsMiddleEntityName, entityData, changedObj));
+ // Mapping the collection owner's id.
+ referencingIdData.getPrefixedMapper().mapToMapFromId(originalId, id);
+
+ // Mapping collection element and index (if present).
+ mapToMapFromObject(originalId, changedObj);
+
+ 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>();
+
+ // Comparing new and old collection content.
+ Collection newCollection = getNewCollectionContent(newColl);
+ Collection oldCollection = getOldCollectionContent(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;
+ }
+
+ public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
+ // Changes are mapped in the "mapCollectionChanges" method.
+ return false;
+ }
+}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -27,7 +27,7 @@
import org.jboss.envers.tools.reflection.ReflectionTools;
import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy;
import org.jboss.envers.entities.mapper.relation.lazy.proxy.SetProxy;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
import org.jboss.envers.entities.mapper.relation.lazy.proxy.MapProxy;
import org.jboss.envers.entities.mapper.PropertyMapper;
import org.jboss.envers.exception.VersionsException;
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,86 @@
+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;
+ private final MiddleComponentData elementComponentData;
+
+ private final Constructor<? extends T> proxyConstructor;
+
+ public BasicCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String
versionsMiddleEntityName,
+ MiddleIdData referencingIdData,
+ 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;
+ 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 Collection getNewCollectionContent(PersistentCollection newCollection) {
+ return (Collection) newCollection;
+ }
+
+ protected Collection getOldCollectionContent(Serializable oldCollection) {
+ if (oldCollection instanceof Map) {
+ return ((Map) oldCollection).keySet();
+ } else {
+ return (Collection) oldCollection;
+ }
+ }
+
+ protected void mapToMapFromObject(Map<String, Object> data, Object changed) {
+ elementComponentData.getComponentMapper().mapToMapFromObject(data, changed);
+ }
+}
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -1,116 +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.id.QueryParameterData;
-import org.jboss.envers.entities.mapper.id.IdMapper;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-import org.jboss.envers.RevisionType;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.hibernate.Query;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class DetachedRelationQueryGenerator {
- private final String queryString;
- private final IdMapper referencingMiddleIdMapper;
-
- public DetachedRelationQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
- String versionsReferencedEntityName, String
versionsMiddleEntityName,
- IdMapper referencingMiddleIdMapper, IdMapper
referencedMiddleIdMapper,
- IdMapper referencedIdMapper) {
- this.referencingMiddleIdMapper = referencingMiddleIdMapper;
-
- /*
- * The query that we need to create:
- * SELECT e FROM versionsReferencedEntity e, middleEntity ee
- * WHERE
- * (selecting e entities at revision :revision)
- * e.revision = (SELECT max(e2.revision) FROM referencedEntity e2
- * WHERE e2.revision <= :revision AND e2.id1 = e.id1) AND
- * (only entities referenced by the association)
- * ee.id1 = e.id1 AND
- * ee.id2 = :id2 AND
- * (the association at revision :revision)
- * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2
- * WHERE ee2.revision <= :revision AND ee2.id2 = :id2 AND ee2.id1 =
ee.id1) AND
- * (only non-deleted entities and associations)
- * ee.revision_type != DEL AND
- * e.revision_type != DEL
- */
- String revisionPropertyPath = verEntCfg.getRevisionPropPath();
- String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
-
- StringBuilder querySB = new StringBuilder();
- querySB
- .append("SELECT e FROM
").append(versionsReferencedEntityName).append(" e")
- .append(", ").append(versionsMiddleEntityName).append(" ee
")
- .append("WHERE ")
- // e.revision = (SELECT max(e2.revision) ...
- .append("e.").append(revisionPropertyPath).append(" =
(SELECT max(e2.").append(revisionPropertyPath)
- .append(") FROM
").append(versionsReferencedEntityName).append(" e2 ")
- // e2.revision <= :revision
- .append("WHERE e2.").append(revisionPropertyPath).append("
<= :revision AND ")
- // e2.id1 = e.id1)
- .append(referencedIdMapper.getIdsEqualQuery("e." +
originalIdPropertyName, "e2." + originalIdPropertyName))
- .append(") AND ")
- // ee.id1 = e.id1
- .append(referencedMiddleIdMapper.getIdsEqualQuery("ee." +
originalIdPropertyName,
- referencedIdMapper, "e." + originalIdPropertyName))
- .append(" AND ")
- // ee.id2 = :id2
- .append(referencingMiddleIdMapper.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(referencingMiddleIdMapper.getIdEqualsQuery("ee2." +
originalIdPropertyName, true))
- .append(" AND ")
- // ee2.id1 = ee.id1)
- .append(referencedMiddleIdMapper.getIdsEqualQuery("ee." +
originalIdPropertyName, "ee2." + originalIdPropertyName))
- .append(") AND ")
- // e.revision_type != DEL AND
-
.append("e.").append(verEntCfg.getRevisionTypePropName()).append(" !=
").append(":delrevisiontype")
- .append(" AND ")
- // ee.revision_type != DEL
-
.append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" !=
").append(":delrevisiontype");
-
- queryString = querySB.toString();
- }
-
- public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey,
Number revision) {
- Query query = versionsReader.getSession().createQuery(queryString);
- query.setParameter("revision", revision);
- query.setParameter("delrevisiontype", RevisionType.DEL);
- for (QueryParameterData paramData:
referencingMiddleIdMapper.mapToQueryParametersFromId(primaryKey)) {
- paramData.setParameterValue(query);
- }
-
- return query;
- }
-}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -25,8 +25,8 @@
import org.jboss.envers.entities.mapper.PropertyMapper;
import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
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;
@@ -45,7 +45,7 @@
*/
private final String referencedEntityName;
- private final DetachedRelationQueryGenerator queryGenerator;
+ // private final DetachedRelationQueryGenerator queryGenerator;
public ManyToManyNotOwningMapper(VersionsEntitiesConfiguration verEntCfg, String
referencingEntityName,
String referencedEntityName, String
collectionReferencingPropertyName,
@@ -56,8 +56,8 @@
this.referencedEntityName = referencedEntityName;
- queryGenerator = new DetachedRelationQueryGenerator(verEntCfg,
versionsReferencedEntityName,
- versionsMiddleEntityName, referencingMiddleIdMapper,
referencedMiddleIdMapper, referencedIdMapper);
+ // queryGenerator = new DetachedRelationQueryGenerator(verEntCfg,
versionsReferencedEntityName,
+ // versionsMiddleEntityName, referencingMiddleIdMapper,
referencedMiddleIdMapper, referencedIdMapper);
}
public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
@@ -72,11 +72,11 @@
return false;
}
- protected <T extends Collection> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
+ protected <T> Initializor<T> getInitializator(VersionsConfiguration
verCfg,
VersionsReaderImplementor versionsReader,
Class<?>
entityClass, Object primaryKey,
Number revision,
Class<T> collectionClass) {
- return new DetachedRelationInitializor<T>(verCfg, referencedEntityName,
queryGenerator,
- versionsReader, primaryKey, revision, collectionClass);
+ return null; //new DetachedRelationInitializor<T>(verCfg,
referencedEntityName, queryGenerator,
+ // versionsReader, primaryKey, revision, collectionClass);
}
}
\ No newline at end of file
Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,28 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper;
+
+/**
+ * A data holder for a middle relation component (which is either the collection element
or index):
+ * - component mapper used to map the component to and from versions entities
+ * - an index, which specifies in which element of the array returned by the query for
reading the collection the data
+ * of the component is
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MiddleComponentData {
+ private final MiddleComponentMapper componentMapper;
+ private final int componentIndex;
+
+ public MiddleComponentData(MiddleComponentMapper componentMapper, int componentIndex)
{
+ this.componentMapper = componentMapper;
+ this.componentIndex = componentIndex;
+ }
+
+ public MiddleComponentMapper getComponentMapper() {
+ return componentMapper;
+ }
+
+ public int getComponentIndex() {
+ return componentIndex;
+ }
+}
Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java
(rev 0)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,38 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.entities.mapper.id.IdMapper;
+
+/**
+ * A class holding information about ids, which form a virtual "relation" from
a middle-table. Middle-tables are used
+ * when mapping collections.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MiddleIdData {
+ private final IdMapper originalMapper;
+ private final IdMapper prefixedMapper;
+ private final String entityName;
+ private final String versionsEntityName;
+
+ public MiddleIdData(IdMapper originalMapper, IdMapper prefixedMapper, String
entityName, String versionsEntityName) {
+ this.originalMapper = originalMapper;
+ this.prefixedMapper = prefixedMapper;
+ this.entityName = entityName;
+ this.versionsEntityName = versionsEntityName;
+ }
+
+ public IdMapper getOriginalMapper() {
+ return originalMapper;
+ }
+
+ public IdMapper getPrefixedMapper() {
+ return prefixedMapper;
+ }
+
+ public String getEntityName() {
+ return entityName;
+ }
+
+ public String getVersionsEntityName() {
+ return versionsEntityName;
+ }
+}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -25,7 +25,7 @@
import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.entities.mapper.relation.lazy.OneToManyAttachedInitializor;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
import org.jboss.envers.configuration.VersionsConfiguration;
import org.hibernate.collection.PersistentCollection;
@@ -48,7 +48,7 @@
return false;
}
- protected <T extends Collection> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
+ protected <T> Initializor<T> getInitializator(VersionsConfiguration
verCfg,
VersionsReaderImplementor versionsReader,
Class<?>
entityClass, Object primaryKey,
Number revision,
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -25,8 +25,8 @@
import org.jboss.envers.entities.mapper.PropertyMapper;
import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
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;
@@ -57,7 +57,7 @@
private final IdMapper referencingMiddleIdMapper;
private final IdMapper referencedMiddleIdMapper;
- private final DetachedRelationQueryGenerator queryGenerator;
+ //private final DetachedRelationQueryGenerator queryGenerator;
public OneToManyDetachedMapper(VersionsEntitiesConfiguration verEntCfg, String
referencingEntityName,
String referencedEntityName, String
collectionReferencingPropertyName,
@@ -73,8 +73,8 @@
this.referencingMiddleIdMapper = referencingMiddleIdMapper;
this.referencedMiddleIdMapper = referencedMiddleIdMapper;
- queryGenerator = new DetachedRelationQueryGenerator(verEntCfg,
versionsReferencedEntityName,
- versionsMiddleEntityName, referencingMiddleIdMapper,
referencedMiddleIdMapper, referencedIdMapper);
+ //queryGenerator = new DetachedRelationQueryGenerator(verEntCfg,
versionsReferencedEntityName,
+ // versionsMiddleEntityName, referencingMiddleIdMapper,
referencedMiddleIdMapper, referencedIdMapper);
}
private Collection getOldCollection(Serializable oldColl) {
@@ -138,7 +138,7 @@
VersionsReaderImplementor versionsReader,
Class<?>
entityClass, Object primaryKey,
Number revision,
Class<T> collectionClass) {
- return new DetachedRelationInitializor<T>(verCfg, referencedEntityName,
queryGenerator,
- versionsReader, primaryKey, revision, collectionClass);
+ return null;// new DetachedRelationInitializor<T>(verCfg,
referencedEntityName, queryGenerator,
+ //versionsReader, primaryKey, revision, collectionClass);
}
}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,27 @@
+package org.jboss.envers.entities.mapper.relation.component;
+
+import org.jboss.envers.entities.EntityInstantiator;
+
+import java.util.Map;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public interface MiddleComponentMapper {
+ /**
+ * Maps from full object data, contained in the given map, to an object.
+ * @param entityInstantiator An entity instatiator bound with an open versions
reader.
+ * @param data Full object data.
+ * @param revision Revision at which the data is read.
+ * @return An object with data corresponding to the one found in the given map.
+ */
+ Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map<String,
Object> data,
+ Number revision);
+
+ /**
+ * Maps from an object to the object's representation (for an entity - only its
id).
+ * @param data Map to which data should be added.
+ * @param obj Object to map from.
+ */
+ void mapToMapFromObject(Map<String, Object> data, Object obj);
+}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,18 @@
+package org.jboss.envers.entities.mapper.relation.component;
+
+import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper;
+
+import java.util.Map;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MiddleDummyComponentMapper implements MiddleComponentMapper {
+ public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data, Number revision) {
+ return null;
+ }
+
+ public void mapToMapFromObject(Map<String, Object> data, Object obj) {
+ }
+}
\ No newline at end of file
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,29 @@
+package org.jboss.envers.entities.mapper.relation.component;
+
+import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+
+import java.util.Map;
+
+/**
+ * A component mapper for the @MapKey mapping: the value of the map's key is the id
of the entity. This
+ * doesn't have an effect on the data stored in the versions tables, so
<code>mapToMapFromObject</code> is
+ * empty.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MiddleMapKeyComponentMapper implements MiddleComponentMapper {
+ private final MiddleIdData relatedIdData;
+
+ public MiddleMapKeyComponentMapper(MiddleIdData relatedIdData) {
+ this.relatedIdData = relatedIdData;
+ }
+
+ public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data,
+ Number revision) {
+ return relatedIdData.getOriginalMapper().mapToIdFromMap(data);
+ }
+
+ public void mapToMapFromObject(Map<String, Object> data, Object obj) {
+ // Doing nothing.
+ }
+}
\ No newline at end of file
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,27 @@
+package org.jboss.envers.entities.mapper.relation.component;
+
+import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper;
+import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+
+import java.util.Map;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MiddleRelatedComponentMapper implements MiddleComponentMapper {
+ private final MiddleIdData relatedIdData;
+
+ public MiddleRelatedComponentMapper(MiddleIdData relatedIdData) {
+ this.relatedIdData = relatedIdData;
+ }
+
+ public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data,
+ Number revision) {
+ return
entityInstantiator.createInstanceFromVersionsEntity(relatedIdData.getEntityName(), data,
revision);
+ }
+
+ public void mapToMapFromObject(Map<String, Object> data, Object obj) {
+ relatedIdData.getOriginalMapper().mapToMapFromEntity(data, obj);
+ }
+}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,29 @@
+package org.jboss.envers.entities.mapper.relation.component;
+
+import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+
+import java.util.Map;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MiddleSimpleComponentMapper implements MiddleComponentMapper {
+ private final String propertyName;
+ private final VersionsEntitiesConfiguration verEntCfg;
+
+ public MiddleSimpleComponentMapper(VersionsEntitiesConfiguration verEntCfg, String
propertyName) {
+ this.propertyName = propertyName;
+ this.verEntCfg = verEntCfg;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data, Number revision) {
+ return ((Map<String, Object>)
data.get(verEntCfg.getOriginalIdPropName())).get(propertyName);
+ }
+
+ public void mapToMapFromObject(Map<String, Object> data, Object obj) {
+ data.put(propertyName, obj);
+ }
+}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -23,8 +23,7 @@
*/
package org.jboss.envers.entities.mapper.relation.lazy;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
-import org.jboss.envers.entities.mapper.relation.DetachedRelationQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
import org.jboss.envers.entities.EntityInstantiator;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.exception.VersionsException;
@@ -39,19 +38,19 @@
public class DetachedRelationInitializor<T> implements Initializor<T> {
private final VersionsConfiguration verCfg;
private final String entityName;
- private final DetachedRelationQueryGenerator queryGenerator;
+ //private final DetachedRelationQueryGenerator queryGenerator;
private final VersionsReaderImplementor versionsReader;
private final Object primaryKey;
private final Number revision;
private final Class<T> collectionClass;
public DetachedRelationInitializor(VersionsConfiguration verCfg, String entityName,
- DetachedRelationQueryGenerator
queryGenerator,
+ //DetachedRelationQueryGenerator
queryGenerator,
VersionsReaderImplementor versionsReader,
Object primaryKey,
Number revision, Class<T>
collectionClass) {
this.verCfg = verCfg;
this.entityName = entityName;
- this.queryGenerator = queryGenerator;
+ //this.queryGenerator = queryGenerator;
this.versionsReader = versionsReader;
this.primaryKey = primaryKey;
this.revision = revision;
@@ -62,7 +61,7 @@
public T initialize() {
EntityInstantiator entityInstantiator = new EntityInstantiator(verCfg,
versionsReader);
- List queryResult = queryGenerator.getQuery(versionsReader, primaryKey,
revision).list();
+ List queryResult = null;//queryGenerator.getQuery(versionsReader, primaryKey,
revision).list();
T result;
try {
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -22,7 +22,7 @@
package org.jboss.envers.entities.mapper.relation.lazy;
import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
import org.jboss.envers.query.VersionsRestrictions;
import org.jboss.envers.exception.VersionsException;
@@ -32,7 +32,7 @@
/**
* @author Adam Warski (adam at warski dot org)
*/
-public class OneToManyAttachedInitializor<T extends Collection> implements
Initializor<T> {
+public class OneToManyAttachedInitializor<T> implements Initializor<T> {
private VersionsReaderImplementor versionsReader;
private Class<?> entityClass;
private String owningReferencePropertyName;
@@ -61,7 +61,8 @@
} else {
Collection result;
try {
- result = collectionClass.newInstance();
+ // TODO
+ result = (Collection) collectionClass.newInstance();
} catch (Exception e) {
throw new VersionsException(e);
}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,49 @@
+package org.jboss.envers.entities.mapper.relation.lazy.initializor;
+
+import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.configuration.VersionsConfiguration;
+
+import java.util.List;
+
+/**
+ * Initializes a persistent collection.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public abstract class AbstractCollectionInitializor<T> implements
Initializor<T> {
+ private final VersionsReaderImplementor versionsReader;
+ private final MiddleTableQueryGenerator queryGenerator;
+ private final Object primaryKey;
+
+ protected final Number revision;
+ protected final EntityInstantiator entityInstantiator;
+
+ public AbstractCollectionInitializor(VersionsConfiguration verCfg,
+ VersionsReaderImplementor versionsReader,
+ MiddleTableQueryGenerator queryGenerator,
+ Object primaryKey, Number revision) {
+ this.versionsReader = versionsReader;
+ this.queryGenerator = queryGenerator;
+ this.primaryKey = primaryKey;
+ this.revision = revision;
+
+ entityInstantiator = new EntityInstantiator(verCfg, versionsReader);
+ }
+
+ protected abstract T initializeCollection(int size);
+
+ protected abstract void addToCollection(T collection, Object collectionRow);
+
+ public T initialize() {
+ List<?> collectionContent = queryGenerator.getQuery(versionsReader,
primaryKey, revision).list();
+
+ T collection = initializeCollection(collectionContent.size());
+
+ for (Object collectionRow : collectionContent) {
+ addToCollection(collection, collectionRow);
+ }
+
+ return collection;
+ }
+}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,47 @@
+package org.jboss.envers.entities.mapper.relation.lazy.initializor;
+
+import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.configuration.VersionsConfiguration;
+
+import java.util.*;
+
+/**
+ * Initializes a map.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class ArrayCollectionInitializor extends
AbstractCollectionInitializor<Object[]> {
+ private final MiddleComponentData elementComponentData;
+ private final MiddleComponentData indexComponentData;
+
+ public ArrayCollectionInitializor(VersionsConfiguration verCfg,
+ VersionsReaderImplementor versionsReader,
+ MiddleTableQueryGenerator queryGenerator,
+ Object primaryKey, Number revision,
+ MiddleComponentData elementComponentData,
+ MiddleComponentData indexComponentData) {
+ super(verCfg, versionsReader, queryGenerator, primaryKey, revision);
+
+ this.elementComponentData = elementComponentData;
+ this.indexComponentData = indexComponentData;
+ }
+
+ protected Object[] initializeCollection(int size) {
+ return new Object[size];
+ }
+
+ @SuppressWarnings({"unchecked"})
+ protected void addToCollection(Object[] collection, Object collectionRow) {
+ Object elementData = ((List)
collectionRow).get(elementComponentData.getComponentIndex());
+ Object element =
elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
+ (Map<String, Object>) elementData, revision);
+
+ Object indexData = ((List)
collectionRow).get(indexComponentData.getComponentIndex());
+ Object indexObj =
indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
+ (Map<String, Object>) indexData, revision);
+ int index = ((Number) indexObj).intValue();
+
+ collection[index] = element;
+ }
+}
\ No newline at end of file
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,48 @@
+package org.jboss.envers.entities.mapper.relation.lazy.initializor;
+
+import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.exception.VersionsException;
+import org.jboss.envers.configuration.VersionsConfiguration;
+
+import java.util.*;
+
+/**
+ * Initializes a non-indexed java collection (set or list, eventually sorted).
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicCollectionInitializor<T extends Collection> extends
AbstractCollectionInitializor<T> {
+ private final Class<? extends T> collectionClass;
+ private final MiddleComponentData elementComponentData;
+
+ public BasicCollectionInitializor(VersionsConfiguration verCfg,
+ VersionsReaderImplementor versionsReader,
+ MiddleTableQueryGenerator queryGenerator,
+ Object primaryKey, Number revision,
+ Class<? extends T> collectionClass,
+ MiddleComponentData elementComponentData) {
+ super(verCfg, versionsReader, queryGenerator, primaryKey, revision);
+
+ this.collectionClass = collectionClass;
+ this.elementComponentData = elementComponentData;
+ }
+
+ protected T initializeCollection(int size) {
+ try {
+ return collectionClass.newInstance();
+ } catch (InstantiationException e) {
+ throw new VersionsException(e);
+ } catch (IllegalAccessException e) {
+ throw new VersionsException(e);
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ protected void addToCollection(T collection, Object collectionRow) {
+ Object elementData = ((List)
collectionRow).get(elementComponentData.getComponentIndex());
+ Object element =
elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
+ (Map<String, Object>) elementData, revision);
+ collection.add(element);
+ }
+}
Copied:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/Initializor.java
(from rev 149,
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java)
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/Initializor.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/Initializor.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,29 @@
+/*
+ * 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.entities.mapper.relation.lazy.initializor;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public interface Initializor<T> {
+ T initialize();
+}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,47 @@
+package org.jboss.envers.entities.mapper.relation.lazy.initializor;
+
+import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.configuration.VersionsConfiguration;
+
+import java.util.*;
+
+/**
+ * Initializes a map.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class ListCollectionInitializor extends AbstractCollectionInitializor<List>
{
+ private final MiddleComponentData elementComponentData;
+ private final MiddleComponentData indexComponentData;
+
+ public ListCollectionInitializor(VersionsConfiguration verCfg,
+ VersionsReaderImplementor versionsReader,
+ MiddleTableQueryGenerator queryGenerator,
+ Object primaryKey, Number revision,
+ MiddleComponentData elementComponentData,
+ MiddleComponentData indexComponentData) {
+ super(verCfg, versionsReader, queryGenerator, primaryKey, revision);
+
+ this.elementComponentData = elementComponentData;
+ this.indexComponentData = indexComponentData;
+ }
+
+ protected List initializeCollection(int size) {
+ return new ArrayList();
+ }
+
+ @SuppressWarnings({"unchecked"})
+ protected void addToCollection(List collection, Object collectionRow) {
+ Object elementData = ((List)
collectionRow).get(elementComponentData.getComponentIndex());
+ Object element =
elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
+ (Map<String, Object>) elementData, revision);
+
+ Object indexData = ((List)
collectionRow).get(indexComponentData.getComponentIndex());
+ Object indexObj =
indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
+ (Map<String, Object>) indexData, revision);
+ int index = ((Number) indexObj).intValue();
+
+ collection.set(index, element);
+ }
+}
\ No newline at end of file
Added:
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
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,56 @@
+package org.jboss.envers.entities.mapper.relation.lazy.initializor;
+
+import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.exception.VersionsException;
+import org.jboss.envers.configuration.VersionsConfiguration;
+
+import java.util.*;
+
+/**
+ * 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;
+ private final MiddleComponentData elementComponentData;
+ private final MiddleComponentData indexComponentData;
+
+ public MapCollectionInitializor(VersionsConfiguration verCfg,
+ VersionsReaderImplementor versionsReader,
+ MiddleTableQueryGenerator queryGenerator,
+ Object primaryKey, Number revision,
+ Class<T> collectionClass,
+ MiddleComponentData elementComponentData,
+ MiddleComponentData indexComponentData) {
+ super(verCfg, versionsReader, queryGenerator, primaryKey, revision);
+
+ this.collectionClass = collectionClass;
+ this.elementComponentData = elementComponentData;
+ this.indexComponentData = indexComponentData;
+ }
+
+ protected T initializeCollection(int size) {
+ try {
+ return collectionClass.newInstance();
+ } catch (InstantiationException e) {
+ throw new VersionsException(e);
+ } catch (IllegalAccessException e) {
+ throw new VersionsException(e);
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ protected void addToCollection(T collection, Object collectionRow) {
+ Object elementData = ((List)
collectionRow).get(elementComponentData.getComponentIndex());
+ Object element =
elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
+ (Map<String, Object>) elementData, revision);
+
+ Object indexData = ((List)
collectionRow).get(indexComponentData.getComponentIndex());
+ Object index =
indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
+ (Map<String, Object>) indexData, revision);
+
+ collection.put(index, element);
+ }
+}
\ No newline at end of file
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-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -21,6 +21,8 @@
*/
package org.jboss.envers.entities.mapper.relation.lazy.proxy;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
+
import java.util.Collection;
import java.util.Iterator;
@@ -28,7 +30,7 @@
* @author Adam Warski (adam at warski dot org)
*/
public abstract class CollectionProxy<U, T extends Collection<U>> implements
Collection<U> {
- private Initializor<T> initializor;
+ private
org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor<T>
initializor;
protected T delegate;
public CollectionProxy(Initializor<T> initializor) {
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -1,31 +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.entities.mapper.relation.lazy.proxy;
-
-import java.util.Collection;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public interface Initializor<T> {
- T initialize();
-}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/ListProxy.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/ListProxy.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/ListProxy.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -29,7 +29,7 @@
* @author Adam Warski (adam at warski dot org)
*/
public class ListProxy<U> extends CollectionProxy<U, List<U>>
implements List<U> {
- public ListProxy(Initializor<List<U>> initializor) {
+ public
ListProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor<List<U>>
initializor) {
super(initializor);
}
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-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -23,6 +23,8 @@
*/
package org.jboss.envers.entities.mapper.relation.lazy.proxy;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
+
import java.util.Map;
import java.util.Set;
import java.util.Collection;
@@ -34,7 +36,7 @@
private Initializor<Map<K, V>> initializor;
protected Map<K, V> delegate;
- public MapProxy(Initializor<Map<K, V>> initializor) {
+ public
MapProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor<Map<K,
V>> initializor) {
this.initializor = initializor;
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -27,7 +27,7 @@
* @author Adam Warski (adam at warski dot org)
*/
public class SetProxy<U> extends CollectionProxy<U, Set<U>> implements
Set<U> {
- public SetProxy(Initializor<Set<U>> initializor) {
+ public
SetProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor<Set<U>>
initializor) {
super(initializor);
}
}
Copied:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedMapProxy.java
(from rev 149,
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java)
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedMapProxy.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedMapProxy.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,147 @@
+/*
+ * 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.lazy.proxy;
+
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
+
+import java.util.*;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class SortedMapProxy<K, V> implements SortedMap<K, V> {
+ private Initializor<SortedMap<K, V>> initializor;
+ protected SortedMap<K, V> delegate;
+
+ public
SortedMapProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor<SortedMap<K,
V>> initializor) {
+ this.initializor = initializor;
+ }
+
+ private void checkInit() {
+ if (delegate == null) {
+ delegate = initializor.initialize();
+ }
+ }
+
+ public int size() {
+ checkInit();
+ return delegate.size();
+ }
+
+ public boolean isEmpty() {
+ checkInit();
+ return delegate.isEmpty();
+ }
+
+ public boolean containsKey(Object o) {
+ checkInit();
+ return delegate.containsKey(o);
+ }
+
+ public boolean containsValue(Object o) {
+ checkInit();
+ return delegate.containsValue(o);
+ }
+
+ public V get(Object o) {
+ checkInit();
+ return delegate.get(o);
+ }
+
+ public V put(K k, V v) {
+ checkInit();
+ return delegate.put(k, v);
+ }
+
+ public V remove(Object o) {
+ checkInit();
+ return delegate.remove(o);
+ }
+
+ public void putAll(Map<? extends K, ? extends V> map) {
+ checkInit();
+ delegate.putAll(map);
+ }
+
+ public void clear() {
+ checkInit();
+ delegate.clear();
+ }
+
+ public Set<K> keySet() {
+ checkInit();
+ return delegate.keySet();
+ }
+
+ public Collection<V> values() {
+ checkInit();
+ return delegate.values();
+ }
+
+ public Set<Entry<K, V>> entrySet() {
+ checkInit();
+ return delegate.entrySet();
+ }
+
+ public Comparator<? super K> comparator() {
+ checkInit();
+ return delegate.comparator();
+ }
+
+ public SortedMap<K, V> subMap(K k, K k1) {
+ checkInit();
+ return delegate.subMap(k, k1);
+ }
+
+ public SortedMap<K, V> headMap(K k) {
+ checkInit();
+ return delegate.headMap(k);
+ }
+
+ public SortedMap<K, V> tailMap(K k) {
+ checkInit();
+ return delegate.tailMap(k);
+ }
+
+ public K firstKey() {
+ checkInit();
+ return delegate.firstKey();
+ }
+
+ public K lastKey() {
+ checkInit();
+ return delegate.lastKey();
+ }
+
+ @SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"})
+ public boolean equals(Object o) {
+ checkInit();
+ return delegate.equals(o);
+ }
+
+ public int hashCode() {
+ checkInit();
+ return delegate.hashCode();
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedMapProxy.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java
(from rev 148,
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java)
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,66 @@
+/*
+ * 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.lazy.proxy;
+
+import java.util.SortedSet;
+import java.util.Comparator;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class SortedSetProxy<U> extends CollectionProxy<U, SortedSet<U>>
implements SortedSet<U> {
+ public
SortedSetProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor<SortedSet<U>>
initializor) {
+ super(initializor);
+ }
+
+ public Comparator<? super U> comparator() {
+ checkInit();
+ return delegate.comparator();
+ }
+
+ public SortedSet<U> subSet(U u, U u1) {
+ checkInit();
+ return delegate.subSet(u, u1);
+ }
+
+ public SortedSet<U> headSet(U u) {
+ checkInit();
+ return delegate.headSet(u);
+ }
+
+ public SortedSet<U> tailSet(U u) {
+ checkInit();
+ return delegate.tailSet(u);
+ }
+
+ public U first() {
+ checkInit();
+ return delegate.first();
+ }
+
+ public U last() {
+ checkInit();
+ return delegate.last();
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java
___________________________________________________________________
Name: svn:mergeinfo
+
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,14 @@
+package org.jboss.envers.entities.mapper.relation.query;
+
+import org.hibernate.Query;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+
+/**
+ * Implementations of this interface provide a method to generate queries on a
middle-table (a table used
+ * for mapping relations). The query can select, apart from selecting the middle table,
select also other "related"
+ * entities.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public interface MiddleTableQueryGenerator {
+ Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number
revision);
+}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,67 @@
+package org.jboss.envers.entities.mapper.relation.query;
+
+import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+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.hibernate.Query;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class OneEntityQueryGenerator implements MiddleTableQueryGenerator {
+ private final String queryString;
+ private final MiddleIdData referencingIdData;
+
+ public OneEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
+ String versionsMiddleEntityName,
+ MiddleIdData referencingIdData) {
+ 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
+ * (the association at revision :revision)
+ * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2
+ * WHERE ee2.revision <= :revision AND ee2.id2 = :id2) 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");
+
+ queryString = querySB.toString();
+ }
+
+ public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey,
Number revision) {
+ Query query = versionsReader.getSession().createQuery(queryString);
+ query.setParameter("revision", revision);
+ query.setParameter("delrevisiontype", RevisionType.DEL);
+ for (QueryParameterData paramData:
referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) {
+ paramData.setParameterValue(query);
+ }
+
+ return query;
+ }
+}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,29 @@
+package org.jboss.envers.entities.mapper.relation.query;
+
+import org.hibernate.Query;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class ThreeEntityQueryGenerator implements MiddleTableQueryGenerator {
+ private final String queryString;
+ private final MiddleIdData referencingIdData;
+
+ public ThreeEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
+ String versionsMiddleEntityName,
+ MiddleIdData referencingIdData,
+ MiddleIdData referencedIdData1,
+ MiddleIdData referencedIdData2) {
+ this.referencingIdData = referencingIdData;
+
+ // TODO
+ queryString = "";
+ }
+
+ public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey,
Number revision) {
+ throw new RuntimeException();
+ }
+}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -0,0 +1,93 @@
+package org.jboss.envers.entities.mapper.relation.query;
+
+import org.jboss.envers.entities.mapper.id.QueryParameterData;
+import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.RevisionType;
+import org.hibernate.Query;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class TwoEntityQueryGenerator implements MiddleTableQueryGenerator {
+ private final String queryString;
+ private final MiddleIdData referencingIdData;
+
+ public TwoEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
+ String versionsMiddleEntityName,
+ MiddleIdData referencingIdData,
+ MiddleIdData referencedIdData) {
+ this.referencingIdData = referencingIdData;
+
+ /*
+ * The query that we need to create:
+ * SELECT new list(ee, e) FROM versionsReferencedEntity e, middleEntity ee
+ * WHERE
+ * (selecting e entities at revision :revision)
+ * e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2
+ * WHERE e2.revision <= :revision AND e2.id1 = e.id1) AND
+ * (only entities referenced by the association)
+ * ee.id1 = e.id1 AND
+ * ee.id2 = :id2 AND
+ * (the association at revision :revision)
+ * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2
+ * WHERE ee2.revision <= :revision AND ee2.id2 = :id2 AND ee2.id1 =
ee.id1) AND
+ * (only non-deleted entities and associations)
+ * ee.revision_type != DEL AND
+ * e.revision_type != DEL
+ */
+ String revisionPropertyPath = verEntCfg.getRevisionPropPath();
+ String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
+
+ StringBuilder querySB = new StringBuilder();
+ querySB
+ .append("SELECT new list(ee, e) FROM
").append(referencedIdData.getVersionsEntityName()).append(" e")
+ .append(", ").append(versionsMiddleEntityName).append(" ee
")
+ .append("WHERE ")
+ // e.revision = (SELECT max(e2.revision) ...
+ .append("e.").append(revisionPropertyPath).append(" =
(SELECT max(e2.").append(revisionPropertyPath)
+ .append(") FROM
").append(referencedIdData.getVersionsEntityName()).append(" e2 ")
+ // e2.revision <= :revision
+ .append("WHERE e2.").append(revisionPropertyPath).append("
<= :revision AND ")
+ // e2.id1 = e.id1)
+
.append(referencedIdData.getOriginalMapper().getIdsEqualQuery("e." +
originalIdPropertyName, "e2." + originalIdPropertyName))
+ .append(") AND ")
+ // ee.id1 = e.id1
+
.append(referencedIdData.getPrefixedMapper().getIdsEqualQuery("ee." +
originalIdPropertyName,
+ referencedIdData.getOriginalMapper(), "e." +
originalIdPropertyName))
+ .append(" AND ")
+ // 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 ")
+ // ee2.id1 = ee.id1)
+
.append(referencedIdData.getPrefixedMapper().getIdsEqualQuery("ee." +
originalIdPropertyName, "ee2." + originalIdPropertyName))
+ .append(") AND ")
+ // e.revision_type != DEL AND
+
.append("e.").append(verEntCfg.getRevisionTypePropName()).append(" !=
").append(":delrevisiontype")
+ .append(" AND ")
+ // ee.revision_type != DEL
+
.append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" !=
").append(":delrevisiontype");
+
+ queryString = querySB.toString();
+ }
+
+ public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey,
Number revision) {
+ Query query = versionsReader.getSession().createQuery(queryString);
+ query.setParameter("revision", revision);
+ query.setParameter("delrevisiontype", RevisionType.DEL);
+ for (QueryParameterData paramData:
referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) {
+ paramData.setParameterValue(query);
+ }
+
+ return query;
+ }
+}
Modified: trunk/src/main/org/jboss/envers/event/VersionsEventListener.java
===================================================================
--- trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-17 15:38:55
UTC (rev 150)
+++ trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-19 14:51:22
UTC (rev 151)
@@ -134,7 +134,8 @@
RelationDescription relDesc =
verCfg.getEntCfg().getRelationDescription(event.getAffectedOwnerEntityName(),
workUnit.getReferencingPropertyName());
- if (relDesc.isBidirectional()) {
+ // TODO: remove the null check (relation info should be added in the metadata
generator)
+ if (relDesc != null && relDesc.isBidirectional()) {
String relatedEntityName = relDesc.getToEntityName();
IdMapper relatedIdMapper =
verCfg.getEntCfg().get(relatedEntityName).getIdMapper();
Modified: trunk/src/main/org/jboss/envers/exception/NotVersionedException.java
===================================================================
--- trunk/src/main/org/jboss/envers/exception/NotVersionedException.java 2008-09-17
15:38:55 UTC (rev 150)
+++ trunk/src/main/org/jboss/envers/exception/NotVersionedException.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -22,6 +22,7 @@
package org.jboss.envers.exception;
/**
+ * TODO: add a field describing what isn't versioned.
* @author Adam Warski (adam at warski dot org)
*/
public class NotVersionedException extends VersionsException {
Modified: trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java
===================================================================
---
trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java 2008-09-17
15:38:55 UTC (rev 150)
+++
trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java 2008-09-19
14:51:22 UTC (rev 151)
@@ -24,6 +24,7 @@
import java.util.Date;
/**
+ * TODO: add the revision number.
* @author Adam Warski (adam at warski dot org)
*/
public class RevisionDoesNotExistException extends VersionsException {
Modified: trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-17 15:38:55
UTC (rev 150)
+++ trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-19 14:51:22
UTC (rev 151)
@@ -67,7 +67,7 @@
* @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.
- * @param selectDeletedEntities If true, also revisions where entities where deleted
will be returned. The
+ * @param selectDeletedEntities If true, also revisions where entities were deleted
will be returned. The additional
* entities will have revision type "delete", and contain no data (all
fields null), except for the id field.
* @return A query for revisions at which instances of the given entity were
modified, to which
* conditions can be added (for example - a specific id of an entity of class
<code>c</code>), and which