Search in sources :

Example 1 with PropertyAuditingData

use of org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData in project hibernate-orm by hibernate.

the class ComponentMetadataGenerator method addComponent.

@SuppressWarnings({ "unchecked" })
public void addComponent(Element parent, PropertyAuditingData propertyAuditingData, Value value, CompositeMapperBuilder mapper, String entityName, EntityXmlMappingData xmlMappingData, boolean firstPass) {
    final Component propComponent = (Component) value;
    final Class componentClass;
    if (propComponent.isDynamic()) {
        componentClass = ReflectionTools.loadClass(Map.class.getCanonicalName(), mainGenerator.getClassLoaderService());
    } else {
        componentClass = ReflectionTools.loadClass(propComponent.getComponentClassName(), mainGenerator.getClassLoaderService());
    }
    final CompositeMapperBuilder componentMapper = mapper.addComponent(propertyAuditingData.getPropertyData(), componentClass);
    // The property auditing data must be for a component.
    final ComponentAuditingData componentAuditingData = (ComponentAuditingData) propertyAuditingData;
    // Adding all properties of the component
    final Iterator<Property> properties = (Iterator<Property>) propComponent.getPropertyIterator();
    while (properties.hasNext()) {
        final Property property = properties.next();
        final PropertyAuditingData componentPropertyAuditingData = componentAuditingData.getPropertyAuditingData(property.getName());
        // Checking if that property is audited
        if (componentPropertyAuditingData != null) {
            mainGenerator.addValue(parent, property.getValue(), componentMapper, entityName, xmlMappingData, componentPropertyAuditingData, property.isInsertable(), firstPass, false);
        }
    }
}
Also used : CompositeMapperBuilder(org.hibernate.envers.internal.entities.mapper.CompositeMapperBuilder) ComponentAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData) Iterator(java.util.Iterator) PropertyAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property)

Example 2 with PropertyAuditingData

use of org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData in project hibernate-orm by hibernate.

the class ClassesAuditingData method addSyntheticIndexProperty.

private void addSyntheticIndexProperty(List value, String propertyAccessorName, ClassAuditingData classAuditingData) {
    final Value indexValue = value.getIndex();
    if (indexValue != null && indexValue.getColumnIterator().hasNext()) {
        final String indexColumnName = indexValue.getColumnIterator().next().getText();
        if (indexColumnName != null) {
            final PropertyAuditingData auditingData = new PropertyAuditingData(indexColumnName, propertyAccessorName, ModificationStore.FULL, RelationTargetAuditMode.AUDITED, null, null, false, true, indexValue);
            classAuditingData.addPropertyAuditingData(indexColumnName, auditingData);
        }
    }
}
Also used : Value(org.hibernate.mapping.Value) PropertyAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData)

Example 3 with PropertyAuditingData

use of org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData in project hibernate-orm by hibernate.

the class AuditMetadataGenerator method addProperties.

private void addProperties(Element parent, Iterator<Property> properties, CompositeMapperBuilder currentMapper, ClassAuditingData auditingData, String entityName, EntityXmlMappingData xmlMappingData, boolean firstPass) {
    while (properties.hasNext()) {
        final Property property = properties.next();
        final String propertyName = property.getName();
        final PropertyAuditingData propertyAuditingData = auditingData.getPropertyAuditingData(propertyName);
        if (propertyAuditingData != null) {
            // and if so, this eliminates the mapping property as it isn't needed.
            if (property instanceof SyntheticProperty) {
                if (property.getValue().isAlternateUniqueKey()) {
                    continue;
                }
            }
            addValue(parent, property.getValue(), currentMapper, entityName, xmlMappingData, propertyAuditingData, isPropertyInsertable(property), firstPass, true);
        }
    }
}
Also used : SyntheticProperty(org.hibernate.mapping.SyntheticProperty) PropertyAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Example 4 with PropertyAuditingData

use of org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData in project hibernate-orm by hibernate.

the class CollectionMetadataGenerator method addValueToMiddleTable.

/**
 * @param value Value, which should be mapped to the middle-table, either as a relation to another entity,
 * or as a simple value.
 * @param xmlMapping If not <code>null</code>, xml mapping for this value is added to this element.
 * @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.
 * @param joinColumns Names of columns to use in the xml mapping, if this array isn't null and has any elements.
 *
 * @return Data for mapping this component.
 */
@SuppressWarnings({ "unchecked" })
private MiddleComponentData addValueToMiddleTable(Value value, Element xmlMapping, QueryGeneratorBuilder queryGeneratorBuilder, String prefix, JoinColumn[] joinColumns, boolean key) {
    final Type type = value.getType();
    if (type instanceof ManyToOneType) {
        final String prefixRelated = prefix + "_";
        final String referencedEntityName = MappingTools.getReferencedEntityName(value);
        final IdMappingData referencedIdMapping = mainGenerator.getReferencedIdMappingData(referencingEntityName, referencedEntityName, propertyAuditingData, true);
        // relation isn't inverse (so when <code>xmlMapping</code> is not null).
        if (xmlMapping != null) {
            addRelatedToXmlMapping(xmlMapping, prefixRelated, joinColumns != null && joinColumns.length > 0 ? MetadataTools.getColumnNameIterator(joinColumns) : MetadataTools.getColumnNameIterator(value.getColumnIterator()), referencedIdMapping);
        }
        // Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name.
        final MiddleIdData referencedIdData = createMiddleIdData(referencedIdMapping, prefixRelated, referencedEntityName);
        // And adding it to the generator builder.
        queryGeneratorBuilder.addRelation(referencedIdData);
        return new MiddleComponentData(new MiddleRelatedComponentMapper(referencedIdData), queryGeneratorBuilder.getCurrentIndex());
    } else if (type instanceof ComponentType) {
        // Collection of embeddable elements.
        final Component component = (Component) value;
        final Class componentClass = ReflectionTools.loadClass(component.getComponentClassName(), mainGenerator.getClassLoaderService());
        final MiddleEmbeddableComponentMapper componentMapper = new MiddleEmbeddableComponentMapper(new MultiPropertyMapper(), componentClass);
        final Element parentXmlMapping = xmlMapping.getParent();
        final ComponentAuditingData auditData = new ComponentAuditingData();
        final ReflectionManager reflectionManager = mainGenerator.getMetadata().getMetadataBuildingOptions().getReflectionManager();
        new ComponentAuditedPropertiesReader(ModificationStore.FULL, new AuditedPropertiesReader.ComponentPropertiesSource(reflectionManager, component), auditData, mainGenerator.getGlobalCfg(), reflectionManager, "").read();
        // Emulating first pass.
        for (String auditedPropertyName : auditData.getPropertyNames()) {
            final PropertyAuditingData nestedAuditingData = auditData.getPropertyAuditingData(auditedPropertyName);
            mainGenerator.addValue(parentXmlMapping, component.getProperty(auditedPropertyName).getValue(), componentMapper, prefix, xmlMappingData, nestedAuditingData, true, true, true);
        }
        // Emulating second pass so that the relations can be mapped too.
        for (String auditedPropertyName : auditData.getPropertyNames()) {
            final PropertyAuditingData nestedAuditingData = auditData.getPropertyAuditingData(auditedPropertyName);
            mainGenerator.addValue(parentXmlMapping, component.getProperty(auditedPropertyName).getValue(), componentMapper, referencingEntityName, xmlMappingData, nestedAuditingData, true, false, true);
        }
        // Embeddable properties may contain null values, so cannot be stored within composite primary key.
        if (propertyValue.isSet()) {
            final String setOrdinalPropertyName = mainGenerator.getVerEntCfg().getEmbeddableSetOrdinalPropertyName();
            final Element ordinalProperty = MetadataTools.addProperty(xmlMapping, setOrdinalPropertyName, "integer", true, true);
            MetadataTools.addColumn(ordinalProperty, setOrdinalPropertyName, null, null, null, null, null, null, false);
        }
        return new MiddleComponentData(componentMapper, 0);
    } else {
        // Last but one parameter: collection components are always insertable
        final boolean mapped = mainGenerator.getBasicMetadataGenerator().addBasic(key ? xmlMapping : xmlMapping.getParent(), new PropertyAuditingData(prefix, "field", ModificationStore.FULL, RelationTargetAuditMode.AUDITED, null, null, false), value, null, true, key);
        if (mapped && key) {
            // Simple values are always stored in the first item of the array returned by the query generator.
            return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
        } else if (mapped && !key) {
            // when mapped but not part of the key, its stored as a dummy mapper??
            return new MiddleComponentData(new MiddleMapElementNotKeyComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
        } else {
            mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName);
            // Impossible to get here.
            throw new AssertionError();
        }
    }
}
Also used : AuditedPropertiesReader(org.hibernate.envers.configuration.internal.metadata.reader.AuditedPropertiesReader) ComponentAuditedPropertiesReader(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditedPropertiesReader) ComponentType(org.hibernate.type.ComponentType) ManyToOneType(org.hibernate.type.ManyToOneType) ReflectionManager(org.hibernate.annotations.common.reflection.ReflectionManager) MiddleSimpleComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleSimpleComponentMapper) MiddleRelatedComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper) Element(org.dom4j.Element) MultiPropertyMapper(org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper) PropertyAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData) MiddleMapElementNotKeyComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleMapElementNotKeyComponentMapper) ListType(org.hibernate.type.ListType) ManyToOneType(org.hibernate.type.ManyToOneType) MaterializedNClobType(org.hibernate.type.MaterializedNClobType) SetType(org.hibernate.type.SetType) MaterializedClobType(org.hibernate.type.MaterializedClobType) MapType(org.hibernate.type.MapType) ComponentType(org.hibernate.type.ComponentType) SortedSetType(org.hibernate.type.SortedSetType) SortedMapType(org.hibernate.type.SortedMapType) BagType(org.hibernate.type.BagType) Type(org.hibernate.type.Type) ComponentAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData) MiddleIdData(org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData) ComponentAuditedPropertiesReader(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditedPropertiesReader) MiddleEmbeddableComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleEmbeddableComponentMapper) PersistentClass(org.hibernate.mapping.PersistentClass) MiddleComponentData(org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData) Component(org.hibernate.mapping.Component) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 5 with PropertyAuditingData

use of org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData in project hibernate-orm by hibernate.

the class IdMetadataGenerator method generateSecondPass.

private void generateSecondPass(String entityName, Component component) {
    Iterator properties = component.getPropertyIterator();
    while (properties.hasNext()) {
        final Property property = (Property) properties.next();
        if (property.getValue() instanceof ToOne) {
            final PropertyAuditingData propertyData = getIdPersistentPropertyAuditingData(property);
            final String referencedEntityName = ((ToOne) property.getValue()).getReferencedEntityName();
            final String prefix = mainGenerator.getVerEntCfg().getOriginalIdPropName() + "." + propertyData.getName();
            final IdMapper relMapper = mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMapper();
            final IdMapper prefixedMapper = relMapper.prefixMappedProperties(prefix + ".");
            mainGenerator.getEntitiesConfigurations().get(entityName).addToOneRelation(prefix, referencedEntityName, prefixedMapper, true, false);
        }
    }
}
Also used : Iterator(java.util.Iterator) ToOne(org.hibernate.mapping.ToOne) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) SingleIdMapper(org.hibernate.envers.internal.entities.mapper.id.SingleIdMapper) MultipleIdMapper(org.hibernate.envers.internal.entities.mapper.id.MultipleIdMapper) EmbeddedIdMapper(org.hibernate.envers.internal.entities.mapper.id.EmbeddedIdMapper) PropertyAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData) Property(org.hibernate.mapping.Property)

Aggregations

PropertyAuditingData (org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData)7 Property (org.hibernate.mapping.Property)5 ComponentAuditingData (org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData)3 Component (org.hibernate.mapping.Component)3 Iterator (java.util.Iterator)2 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)2 Element (org.dom4j.Element)1 ReflectionManager (org.hibernate.annotations.common.reflection.ReflectionManager)1 AuditedPropertiesReader (org.hibernate.envers.configuration.internal.metadata.reader.AuditedPropertiesReader)1 ClassAuditingData (org.hibernate.envers.configuration.internal.metadata.reader.ClassAuditingData)1 ComponentAuditedPropertiesReader (org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditedPropertiesReader)1 IdMappingData (org.hibernate.envers.internal.entities.IdMappingData)1 CompositeMapperBuilder (org.hibernate.envers.internal.entities.mapper.CompositeMapperBuilder)1 MultiPropertyMapper (org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper)1 EmbeddedIdMapper (org.hibernate.envers.internal.entities.mapper.id.EmbeddedIdMapper)1 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)1 MultipleIdMapper (org.hibernate.envers.internal.entities.mapper.id.MultipleIdMapper)1 SingleIdMapper (org.hibernate.envers.internal.entities.mapper.id.SingleIdMapper)1 MiddleComponentData (org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData)1 MiddleIdData (org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData)1