Author: adamw
Date: 2008-09-22 12:45:44 -0400 (Mon, 22 Sep 2008)
New Revision: 161
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java
Modified:
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
Log:
ENVERS-44: restructuring the basic mapper
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22
15:33:44 UTC (rev 160)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22
16:45:44 UTC (rev 161)
@@ -5,12 +5,11 @@
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Property;
-import org.hibernate.type.CustomType;
+import org.hibernate.type.*;
import org.hibernate.util.StringHelper;
import org.jboss.envers.entities.mapper.SimpleMapperBuilder;
import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
import org.jboss.envers.ModificationStore;
-import org.jboss.envers.exception.VersionsException;
import java.util.Iterator;
@@ -19,14 +18,52 @@
* @author Adam Warski (adam at warski dot org)
*/
public final class BasicMetadataGenerator {
- private final VersionsMetadataGenerator mainGenerator;
+ boolean addBasic(Element parent, String name, Value value, CompositeMapperBuilder
mapper,
+ ModificationStore store, String entityName, boolean key) {
+ Type type = value.getType();
- BasicMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) {
- mainGenerator = versionsMetadataGenerator;
+ if (type instanceof ComponentType) {
+ addComponent(parent, name, value, mapper, entityName, key);
+ } else if (type instanceof ImmutableType || type instanceof MutableType) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else if (type instanceof CustomType &&
"org.hibernate.type.EnumType".equals(type.getName())) {
+ addEnumValue(parent, name, value, mapper, store, key);
+ } else if (type instanceof CustomType &&
+
("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) ||
+
"org.hibernate.type.StringClobType".equals(type.getName()))) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else if
("org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName()))
{
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else {
+ return false;
+ }
+
+ return true;
}
+ boolean addBasicNoComponent(Element parent, String name, Value value,
SimpleMapperBuilder mapper,
+ ModificationStore store, boolean key) {
+ Type type = value.getType();
+
+ if (type instanceof ImmutableType || type instanceof MutableType) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else if (type instanceof CustomType &&
"org.hibernate.type.EnumType".equals(type.getName())) {
+ addEnumValue(parent, name, value, mapper, store, key);
+ } else if (type instanceof CustomType &&
+
("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) ||
+
"org.hibernate.type.StringClobType".equals(type.getName()))) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else if
("org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName()))
{
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else {
+ return false;
+ }
+
+ return true;
+ }
+
@SuppressWarnings({"unchecked"})
- void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder
mapper,
+ private void addSimpleValue(Element parent, String name, Value value,
SimpleMapperBuilder mapper,
ModificationStore store, boolean key) {
if (parent != null) {
Element prop_mapping = MetadataTools.addProperty(parent, name,
@@ -41,8 +78,8 @@
}
@SuppressWarnings({"unchecked"})
- void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder
mapper,
- ModificationStore store) {
+ private void addEnumValue(Element parent, String name, Value value,
SimpleMapperBuilder mapper,
+ ModificationStore store, boolean key) {
if (parent != null) {
Element prop_mapping = parent.addElement("property");
prop_mapping.addAttribute("name", name);
@@ -56,9 +93,9 @@
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());
}
@@ -73,13 +110,14 @@
}
@SuppressWarnings({"unchecked"})
- void addComponent(Element parent, String name, Value value, CompositeMapperBuilder
mapper,
- String entityName, EntityXmlMappingData xmlMappingData,
- boolean firstPass) {
+ private void addComponent(Element parent, String name, Value value,
CompositeMapperBuilder mapper,
+ String entityName, boolean key) {
Element component_mapping = null;
Component prop_component = (Component) value;
if (parent != null) {
+ /*
+ TODO: investigate relations inside components
if (!firstPass) {
// The required element already exists.
Iterator<Element> iter =
parent.elementIterator("component");
@@ -95,11 +133,12 @@
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);
- }
+ component_mapping = parent.addElement("component");
+ component_mapping.addAttribute("name", name);
+
+ addComponentClassName(component_mapping, prop_component);
}
CompositeMapperBuilder componentMapper = mapper.addComposite(name);
@@ -107,8 +146,8 @@
Iterator<Property> properties = (Iterator<Property>)
prop_component.getPropertyIterator();
while (properties.hasNext()) {
Property property = properties.next();
- mainGenerator.addValue(component_mapping, property.getName(),
property.getValue(),
- componentMapper, ModificationStore.FULL, entityName, xmlMappingData,
null, firstPass);
+ addBasic(component_mapping, property.getName(), property.getValue(),
componentMapper,
+ ModificationStore.FULL, entityName, key);
}
}
}
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22
15:33:44 UTC (rev 160)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22
16:45:44 UTC (rev 161)
@@ -306,13 +306,15 @@
queryGeneratorBuilder.getCurrentIndex());
} else if (type instanceof ImmutableType || type instanceof MutableType) {
// TODO: add support for enums, components, custom types
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(xmlMapping, prefix,
value, null, ModificationStore.FULL, true);
+ mainGenerator.getBasicMetadataGenerator().addBasic(xmlMapping, prefix, value,
null, ModificationStore.FULL,
+ referencingEntityName, true);
// Simple values are always stored in the first entity read by the query
generator.
return new MiddleComponentData(new
MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
} else {
- // TODO: throw an exception
- throw new RuntimeException();
+ mainGenerator.throwUnsupportedTypeException(type, referencingEntityName,
propertyName);
+ // Impossible to get here.
+ throw new AssertionError();
}
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-22
15:33:44 UTC (rev 160)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-22
16:45:44 UTC (rev 161)
@@ -36,7 +36,7 @@
Type propertyType = property.getType();
if (!"_identifierMapper".equals(property.getName())) {
if (propertyType instanceof ImmutableType) {
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(parent,
property.getName(),
+ mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(parent,
property.getName(),
property.getValue(), mapper, ModificationStore.FULL, key);
} else {
throw new MappingException("Type not supported: " +
propertyType.getClass().getName());
@@ -57,12 +57,16 @@
SimpleIdMapperBuilder mapper;
if (id_mapper != null) {
+ // Multiple id
+
mapper = new MultipleIdMapper(((Component)
pc.getIdentifier()).getComponentClassName());
addIdProperties(rel_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), mapper, false);
// null mapper - the mapping where already added the first time, now we only
want to generate the xml
addIdProperties(orig_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), null, true);
} else if (id_prop.isComposite()) {
+ // Embedded id
+
Component id_component = (Component) id_prop.getValue();
mapper = new EmbeddedIdMapper(id_prop.getName(),
id_component.getComponentClassName());
@@ -71,13 +75,15 @@
// null mapper - the mapping where already added the first time, now we only
want to generate the xml
addIdProperties(orig_id_mapping, (Iterator<Property>)
id_component.getPropertyIterator(), null, true);
} else {
+ // Single id
+
mapper = new SingleIdMapper();
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(rel_id_mapping,
id_prop.getName(),
+ mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(rel_id_mapping,
id_prop.getName(),
id_prop.getValue(), mapper, ModificationStore.FULL, false);
// null mapper - the mapping where already added the first time, now we only
want to generate the xml
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(orig_id_mapping,
id_prop.getName(),
+
mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(orig_id_mapping,
id_prop.getName(),
id_prop.getValue(), null, ModificationStore.FULL, true);
}
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22
15:33:44 UTC (rev 160)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22
16:45:44 UTC (rev 161)
@@ -7,7 +7,7 @@
import org.hibernate.MappingException;
import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
import org.jboss.envers.entities.mapper.relation.ToOneIdMapper;
-import org.jboss.envers.entities.mapper.relation.OneToOneNotOwningIdMapper;
+import org.jboss.envers.entities.mapper.relation.OneToOneNotOwningMapper;
import org.jboss.envers.entities.mapper.id.IdMapper;
import org.jboss.envers.entities.EntityConfiguration;
import org.jboss.envers.entities.IdMappingData;
@@ -82,7 +82,7 @@
referencedEntityName, ownedIdMapper);
// Adding mapper for the id
- mapper.addComposite(name, new
OneToOneNotOwningIdMapper(owningReferencePropertyName,
+ mapper.addComposite(name, new
OneToOneNotOwningMapper(owningReferencePropertyName,
referencedEntityName, name));
}
}
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22
15:33:44 UTC (rev 160)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22
16:45:44 UTC (rev 161)
@@ -72,7 +72,7 @@
this.verEntCfg = verEntCfg;
this.revisionInfoRelationMapping = revisionInfoRelationMapping;
- this.basicMetadataGenerator = new BasicMetadataGenerator(this);
+ this.basicMetadataGenerator = new BasicMetadataGenerator();
this.idMetadataGenerator = new IdMetadataGenerator(this);
this.toOneRelationMetadataGenerator = new ToOneRelationMetadataGenerator(this);
@@ -122,36 +122,15 @@
VersionsJoinTable joinTable, boolean firstPass) {
Type type = value.getType();
- if (type instanceof ComponentType) {
- // only first pass
- if (firstPass) {
- basicMetadataGenerator.addComponent(parent, name, value, currentMapper,
entityName, xmlMappingData, firstPass);
+ // only first pass
+ if (firstPass) {
+ if (basicMetadataGenerator.addBasic(parent, name, value, currentMapper,
store, entityName, false)) {
+ // The property was mapped by the basic generator.
+ return;
}
- } else if (type instanceof ImmutableType || type instanceof MutableType) {
- // only first pass
- if (firstPass) {
- basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper,
store, false);
- }
- } else if (type instanceof CustomType &&
- "org.hibernate.type.EnumType".equals(type.getName())) {
- // only first pass
- if (firstPass) {
- basicMetadataGenerator.addEnumValue(parent, name, value, currentMapper,
store);
- }
- } 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 ("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 ManyToOneType) {
+ }
+
+ if (type instanceof ManyToOneType) {
// only second pass
if (!firstPass) {
toOneRelationMetadataGenerator.addToOne(parent, name, value,
currentMapper, entityName);
@@ -169,7 +148,11 @@
collectionMetadataGenerator.addCollection();
}
} else {
- throwUnsupportedTypeException(type, entityName, name);
+ if (firstPass) {
+ // If we got here in the first pass, it means the basic mapper didn't
map it, and none of the
+ // above branches either.
+ throwUnsupportedTypeException(type, entityName, name);
+ }
}
}
Copied:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java
(from rev 148,
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningIdMapper.java)
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java 2008-09-22
16:45:44 UTC (rev 161)
@@ -0,0 +1,87 @@
+/*
+ * 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;
+
+import org.jboss.envers.entities.mapper.PropertyMapper;
+import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.tools.reflection.ReflectionTools;
+import org.jboss.envers.query.VersionsRestrictions;
+import org.jboss.envers.exception.VersionsException;
+import org.jboss.envers.configuration.VersionsConfiguration;
+import org.hibernate.property.Setter;
+import org.hibernate.NonUniqueResultException;
+import org.hibernate.collection.PersistentCollection;
+
+import javax.persistence.NoResultException;
+import java.util.Map;
+import java.util.List;
+import java.io.Serializable;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class OneToOneNotOwningMapper implements PropertyMapper {
+ private String owningReferencePropertyName;
+ private String owningEntityName;
+ private String propertyName;
+
+ public OneToOneNotOwningMapper(String owningReferencePropertyName, String
owningEntityName, String propertyName) {
+ this.owningReferencePropertyName = owningReferencePropertyName;
+ this.owningEntityName = owningEntityName;
+ this.propertyName = propertyName;
+ }
+
+ public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
+ return false;
+ }
+
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey, VersionsReaderImplementor versionsReader, Number revision) {
+ if (obj == null) {
+ return;
+ }
+
+ Class<?> entityClass = ReflectionTools.loadClass(owningEntityName);
+
+ Object value;
+
+ try {
+ value = versionsReader.createQuery().forEntitiesAtRevision(entityClass,
revision)
+ .add(VersionsRestrictions.relatedIdEq(owningReferencePropertyName,
primaryKey)).getSingleResult();
+ } catch (NoResultException e) {
+ value = null;
+ } catch (NonUniqueResultException e) {
+ throw new VersionsException("Many versions results for one-to-one
relationship: (" + owningEntityName +
+ ", " + owningReferencePropertyName + ")");
+ }
+
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
+ setter.set(obj, value, null);
+ }
+
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
+
PersistentCollection newColl,
+
Serializable oldColl,
+
Serializable id) {
+ return null;
+ }
+}