public void AddComponent(XmlElement parent, PropertyAuditingData propertyAuditingData,
								 IValue value, ICompositeMapperBuilder mapper, string entityName,
								 EntityXmlMappingData xmlMappingData, bool firstPass, bool insertable)
        {
            var propComponent = (Component) value;

            var componentMapper = mapper.AddComponent(propertyAuditingData.GetPropertyData(),
                                                                          propComponent.ComponentClassName);

            // The property auditing data must be for a component.
            var componentAuditingData = (ComponentAuditingData) propertyAuditingData;

            // Adding all properties of the component
            foreach (var property in propComponent.PropertyIterator)
            {
                var componentPropertyAuditingData = componentAuditingData.GetPropertyAuditingData(property.Name);

                // Checking if that property is audited
                if (componentPropertyAuditingData != null)
                {
                    mainGenerator.AddValue(parent, property.Value, componentMapper, entityName, xmlMappingData,
                            componentPropertyAuditingData, property.IsInsertable && insertable, firstPass);
                }
            }
        }
        //@SuppressWarnings({"unchecked"})
        public void AddComponent(XmlElement parent, PropertyAuditingData propertyAuditingData,
            IValue value, ICompositeMapperBuilder mapper, String entityName,
            EntityXmlMappingData xmlMappingData, bool firstPass)
        {
            Component prop_component = (Component) value;

            ICompositeMapperBuilder componentMapper = mapper.AddComponent(propertyAuditingData.getPropertyData(),
                    prop_component.ComponentClassName);

            // The property auditing data must be for a component.
            ComponentAuditingData componentAuditingData = (ComponentAuditingData) propertyAuditingData;

            // Adding all properties of the component
            IEnumerator<Property> properties = (IEnumerator<Property>) prop_component.PropertyIterator.GetEnumerator();
            while (properties.MoveNext()) {
                Property property = properties.Current;

                PropertyAuditingData componentPropertyAuditingData =
                        componentAuditingData.getPropertyAuditingData(property.Name);

                // Checking if that property is audited
                if (componentPropertyAuditingData != null) {
                    mainGenerator.AddValue(parent, property.Value, componentMapper, entityName, xmlMappingData,
                            componentPropertyAuditingData, property.IsInsertable, firstPass);
                }
            }
        }
	    /**
         * @param mainGenerator Main generator, giving access to configuration and the basic mapper.
         * @param propertyValue Value of the collection, as mapped by Hibernate.
         * @param currentMapper Mapper, to which the appropriate {@link org.hibernate.envers.entities.mapper.PropertyMapper}
         * will be added.
         * @param referencingEntityName Name of the entity that owns this collection.
         * @param xmlMappingData In case this collection requires a middle table, additional mapping documents will
         * be created using this object.
         * @param propertyAuditingData Property auditing (meta-)data. Among other things, holds the name of the
         * property that references the collection in the referencing entity, the user data for middle (join)
         * table and the value of the <code>@MapKey</code> annotation, if there was one.
         */
        public CollectionMetadataGenerator(AuditMetadataGenerator mainGenerator,
                                           Mapping.Collection propertyValue, ICompositeMapperBuilder currentMapper,
                                           String referencingEntityName, EntityXmlMappingData xmlMappingData,
                                           PropertyAuditingData propertyAuditingData) {
            this.mainGenerator = mainGenerator;
            this.propertyValue = propertyValue;
            this.currentMapper = currentMapper;
            this.referencingEntityName = referencingEntityName;
            this.xmlMappingData = xmlMappingData;
            this.propertyAuditingData = propertyAuditingData;

            this.propertyName = propertyAuditingData.Name;

            referencingEntityConfiguration = mainGenerator.EntitiesConfigurations[referencingEntityName];
            if (referencingEntityConfiguration == null) {
                throw new MappingException("Unable to read auditing configuration for " + referencingEntityName + "!");
            }

            referencedEntityName = MappingTools.getReferencedEntityName(propertyValue.Element);
        }
        private Triple<XmlElement, IExtendedPropertyMapper, String> GenerateInheritanceMappingData(
                PersistentClass pc, EntityXmlMappingData xmlMappingData, AuditTableData auditTableData,
                String inheritanceMappingType)
        {
            String extendsEntityName = VerEntCfg.GetAuditEntityName(pc.Superclass.EntityName);
            XmlElement class_mapping = MetadataTools.CreateSubclassEntity(xmlMappingData.MainXmlMapping,
                    inheritanceMappingType, auditTableData, extendsEntityName, pc.DiscriminatorValue);

            // The id and revision type is already mapped in the parent

            // Getting the property mapper of the parent - when mapping properties, they need to be included
            String parentEntityName = pc.Superclass.EntityName;

            EntityConfiguration parentConfiguration = EntitiesConfigurations[parentEntityName];
            if (parentConfiguration == null)
            {
                throw new MappingException("Entity '" + pc.EntityName + "' is audited, but its superclass: '" +
                        parentEntityName + "' is not.");
            }

            IExtendedPropertyMapper parentPropertyMapper = parentConfiguration.PropertyMapper;
            IExtendedPropertyMapper propertyMapper = new SubclassPropertyMapper(new MultiPropertyMapper(), parentPropertyMapper);

            return Triple<XmlElement, IExtendedPropertyMapper, String>.Make<XmlElement, IExtendedPropertyMapper, String>(class_mapping, propertyMapper, parentEntityName);
        }
        //@SuppressWarnings({"unchecked"})
        private Triple<XmlElement, IExtendedPropertyMapper, String> GenerateMappingData(
                PersistentClass pc, EntityXmlMappingData xmlMappingData, AuditTableData auditTableData,
                IdMappingData idMapper)
        {
            bool hasDiscriminator = pc.Discriminator != null;

            XmlElement class_mapping = MetadataTools.CreateEntity(xmlMappingData.MainXmlMapping, auditTableData,
                    hasDiscriminator ? pc.DiscriminatorValue : null);
            IExtendedPropertyMapper propertyMapper = new MultiPropertyMapper();

            // Checking if there is a discriminator column
            if (hasDiscriminator)
            {
                XmlElement discriminator_element = class_mapping.OwnerDocument.CreateElement("discriminator");
                class_mapping.AppendChild(discriminator_element);
                MetadataTools.AddColumns(discriminator_element, (IEnumerator<ISelectable>)pc.Discriminator.ColumnIterator.GetEnumerator());
                discriminator_element.SetAttribute("type", pc.Discriminator.Type.Name);
            }

            InheritanceType.Type parentInheritance = InheritanceType.GetForParent(pc);
            switch (parentInheritance)
            {
                case InheritanceType.Type.NONE:
                    break;

                case InheritanceType.Type.SINGLE:
                    AddSingleInheritancePersisterHack(class_mapping);
                    break;

                case InheritanceType.Type.JOINED:
                    AddJoinedInheritancePersisterHack(class_mapping);
                    break;

                case InheritanceType.Type.TABLE_PER_CLASS:
                    AddTablePerClassInheritancePersisterHack(class_mapping);
                    break;
            }

            // Adding the id mapping
            XmlNode xmlMp = class_mapping.OwnerDocument.ImportNode(idMapper.XmlMapping,true);
            class_mapping.AppendChild(xmlMp);

            // Adding the "revision type" property
            AddRevisionType(class_mapping);

            return Triple<XmlElement, IExtendedPropertyMapper, string>.Make<XmlElement, IExtendedPropertyMapper, string>(class_mapping, propertyMapper, null);
        }
        //@SuppressWarnings({"unchecked"})
        private void AddJoins(PersistentClass pc, ICompositeMapperBuilder currentMapper, ClassAuditingData auditingData,
                              String entityName, EntityXmlMappingData xmlMappingData, bool firstPass)
        {
            IEnumerator<Join> joins = pc.JoinIterator.GetEnumerator();

            while (joins.MoveNext())
            {
                Join join = joins.Current;
                XmlElement joinElement = entitiesJoins[entityName][join];

                if (joinElement != null)
                {
                    AddProperties(joinElement, (IEnumerator<Property>)join.PropertyIterator.GetEnumerator(), currentMapper, auditingData, entityName,
                            xmlMappingData, firstPass);
                }
            }
        }
 //@SuppressWarnings({"unchecked"})
 private void AddProperties(XmlElement parent, IEnumerator<Property> properties, ICompositeMapperBuilder currentMapper,
                            ClassAuditingData auditingData, String entityName, EntityXmlMappingData xmlMappingData,
                            bool firstPass)
 {
     while (properties.MoveNext())
     {
         Property property = properties.Current;
         String propertyName = property.Name;
         PropertyAuditingData propertyAuditingData = auditingData.getPropertyAuditingData(propertyName);
         if (propertyAuditingData != null)
         {
             AddValue(parent, property.Value, currentMapper, entityName, xmlMappingData, propertyAuditingData,
                     property.IsInsertable, firstPass);
         }
     }
 }
        //@SuppressWarnings({"unchecked"})
        public void AddValue(XmlElement parent, IValue value, ICompositeMapperBuilder currentMapper, String entityName,
                      EntityXmlMappingData xmlMappingData, PropertyAuditingData propertyAuditingData,
                      bool insertable, bool firstPass)
        {
            IType type = value.Type;

            // only first pass
            if (firstPass)
            {
                if (BasicMetadataGenerator.AddBasic(parent, propertyAuditingData, value, currentMapper,
                        insertable, false))
                {
                    // The property was mapped by the basic generator.
                    return;
                }
            }

            if (type is ComponentType)
            {
                // both passes
                componentMetadataGenerator.AddComponent(parent, propertyAuditingData, value, currentMapper,
                        entityName, xmlMappingData, firstPass);
            }
            else if (type is ManyToOneType)
            {
                // only second pass
                if (!firstPass)
                {
                    toOneRelationMetadataGenerator.AddToOne(parent, propertyAuditingData, value, currentMapper,
                            entityName, insertable);
                }
            }
            else if (type is OneToOneType)
            {
                // only second pass
                if (!firstPass)
                {
                    toOneRelationMetadataGenerator.AddOneToOneNotOwning(propertyAuditingData, value,
                            currentMapper, entityName);
                }
            }
            else if (type is CollectionType)
            {
                // only second pass
                if (!firstPass)
                {
                    CollectionMetadataGenerator collectionMetadataGenerator = new CollectionMetadataGenerator(this,
                            (Mapping.Collection)value, currentMapper, entityName, xmlMappingData,
                            propertyAuditingData);
                    collectionMetadataGenerator.AddCollection();
                }
            }
            else
            {
                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, propertyAuditingData.Name);
                }
            }
        }
        public void GenerateFirstPass(PersistentClass pc, ClassAuditingData auditingData,
									  EntityXmlMappingData xmlMappingData, bool isAudited)
        {
            var schema = GetSchema(auditingData.AuditTable.Schema, pc.Table);
            var catalog = GetCatalog(auditingData.AuditTable.Catalog, pc.Table);

            var entityName = pc.EntityName;
            if (!isAudited)
            {
                var _idMapper = idMetadataGenerator.AddId(pc);

                if (_idMapper == null)
                {
                    // Unsupported id mapping, e.g. key-many-to-one. If the entity is used in auditing, an exception
                    // will be thrown later on.
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Unable to create auditing id mapping for entity {0}" +
                            ", because of an unsupported Hibernate id mapping (e.g. key-many-to-one).", entityName);
                    }
                    return;
                }

                //ORIG:
                //IExtendedPropertyMapper propertyMapper = null;
                //String parentEntityName = null;
                var _entityCfg = new EntityConfiguration(entityName, pc.ClassName, _idMapper, null, null);
                NotAuditedEntitiesConfigurations.Add(entityName, _entityCfg);
                return;
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Generating first-pass auditing mapping for entity {0}.", entityName);
            }

            var auditEntityName = VerEntCfg.GetAuditEntityName(entityName);
            var auditTableName = VerEntCfg.GetAuditTableName(entityName, pc.Table.Name);

            // Registering the audit entity name, now that it is known
            AuditEntityNameRegister.Register(auditEntityName);

            var auditTableData = new AuditTableData(auditEntityName, auditTableName, schema, catalog);

            // Generating a mapping for the id
            var idMapper = idMetadataGenerator.AddId(pc);

            var inheritanceType = pc.GetInheritanceType();

            // These properties will be read from the mapping data

            Triple<XmlElement, IExtendedPropertyMapper, string> mappingData;

            // Reading the mapping data depending on inheritance type (if any)
            switch (inheritanceType)
            {
                case InheritanceType.None:
                    mappingData = GenerateMappingData(pc, xmlMappingData, auditTableData, idMapper);
                    break;

                case InheritanceType.Single:
                    auditTableData = new AuditTableData(auditEntityName, null, schema, catalog);
                    mappingData = GenerateInheritanceMappingData(pc, xmlMappingData, auditTableData, "subclass");
                    break;

                case InheritanceType.Joined:
                    mappingData = GenerateInheritanceMappingData(pc, xmlMappingData, auditTableData, "joined-subclass");

                    // Adding the "key" element with all id columns...
                    var keyMapping = mappingData.First.OwnerDocument.CreateElement("key");
                    mappingData.First.AppendChild(keyMapping);
                    MetadataTools.AddColumns(keyMapping, pc.Table.PrimaryKey.ColumnIterator);

                    // ... and the revision number column, read from the revision info relation mapping.
                    keyMapping.AppendChild(CloneAndSetupRevisionInfoRelationMapping(keyMapping.OwnerDocument).GetElementsByTagName("column")[0].Clone());
                    break;

                case InheritanceType.TablePerClass:
                    mappingData = GenerateInheritanceMappingData(pc, xmlMappingData, auditTableData, "union-subclass");

                    break;

                default:
                    throw new AssertionFailure("AuditMetadataGenerator.GenerateFirstPass: Impossible enum value.");
            }

            var class_mapping = mappingData.First;
            var propertyMapper = mappingData.Second;
            var parentEntityName = mappingData.Third;

            xmlMappingData.ClassMapping = class_mapping;

            // Mapping unjoined properties
            AddProperties(class_mapping, pc.UnjoinedPropertyIterator, propertyMapper,
                    auditingData, pc.EntityName, xmlMappingData,
                    true);

            // Creating and mapping joins (first pass)
            CreateJoins(pc, class_mapping, auditingData);
            AddJoins(pc, propertyMapper, auditingData, pc.EntityName, xmlMappingData, true);

            // Storing the generated configuration
            var entityCfg = new EntityConfiguration(auditEntityName, pc.ClassName, idMapper,
                    propertyMapper, parentEntityName);
            EntitiesConfigurations.Add(pc.EntityName, entityCfg);
        }
Beispiel #10
0
 private void addProperties(XElement parent, IEnumerable <Property> properties, ICompositeMapperBuilder currentMapper,
                            ClassAuditingData auditingData, string entityName, EntityXmlMappingData xmlMappingData,
                            bool firstPass)
 {
     foreach (var property in properties)
     {
         var propertyName         = property.Name;
         var propertyAuditingData = auditingData.GetPropertyAuditingData(propertyName);
         if (propertyAuditingData != null)
         {
             AddValue(parent, property.Value, currentMapper, entityName, xmlMappingData, propertyAuditingData,
                      property.IsInsertable, firstPass, true);
         }
     }
 }
        private Triple<XmlElement, IExtendedPropertyMapper, string> GenerateMappingData(
				PersistentClass pc, EntityXmlMappingData xmlMappingData, AuditTableData auditTableData,
				IdMappingData idMapper)
        {
            var hasDiscriminator = pc.Discriminator != null;

            var classMapping = MetadataTools.CreateEntity(xmlMappingData.MainXmlMapping, auditTableData,
                    hasDiscriminator ? pc.DiscriminatorValue : null);
            var propertyMapper = new MultiPropertyMapper();

            // Adding the id mapping
            var xmlMp = classMapping.OwnerDocument.ImportNode(idMapper.XmlMapping, true);
            classMapping.AppendChild(xmlMp);

            // Checking if there is a discriminator column
            if (hasDiscriminator)
            {
                var discriminatorElement = classMapping.OwnerDocument.CreateElement("discriminator");
                classMapping.AppendChild(discriminatorElement);
                // Database column or SQL formula allowed to distinguish entity types
                MetadataTools.AddColumnsOrFormulas(discriminatorElement, pc.Discriminator.ColumnIterator);
                discriminatorElement.SetAttribute("type", pc.Discriminator.Type.Name);
            }

            // Adding the "revision type" property
            AddRevisionType(classMapping);

            return new Triple<XmlElement, IExtendedPropertyMapper, string>(classMapping, propertyMapper, null);
        }
        private void AddProperties(XmlElement parent, IEnumerable<Property> properties, ICompositeMapperBuilder currentMapper,
									ClassAuditingData auditingData, string entityName, EntityXmlMappingData xmlMappingData,
									bool firstPass)
        {
            foreach (var property in properties)
            {
                var propertyName = property.Name;
                var propertyAuditingData = auditingData.GetPropertyAuditingData(propertyName);
                if (propertyAuditingData != null)
                {
                    AddValue(parent, property.Value, currentMapper, entityName, xmlMappingData, propertyAuditingData,
                            property.IsInsertable, firstPass);
                }
            }
        }
        private void AddJoins(PersistentClass pc, ICompositeMapperBuilder currentMapper, ClassAuditingData auditingData,
							  string entityName, EntityXmlMappingData xmlMappingData, bool firstPass)
        {
            foreach (var join in pc.JoinIterator)
            {
                var joinElement = entitiesJoins[entityName][join];

                if (joinElement != null)
                {
                    AddProperties(joinElement, join.PropertyIterator, currentMapper, auditingData, entityName,
                            xmlMappingData, firstPass);
                }
            }
        }
        public void GenerateSecondPass(PersistentClass pc, ClassAuditingData auditingData,
										EntityXmlMappingData xmlMappingData)
        {
            var entityName = pc.EntityName;
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Generating second-pass auditing mapping for entity {0}.", entityName);
            }

            var propertyMapper = EntitiesConfigurations[entityName].PropertyMapper;

            // Mapping unjoined properties
            var parent = xmlMappingData.ClassMapping;

            AddProperties(parent, pc.UnjoinedPropertyIterator,
                    propertyMapper, auditingData, entityName, xmlMappingData, false);

            // Mapping joins (second pass)
            AddJoins(pc, propertyMapper, auditingData, entityName, xmlMappingData, false);
        }
        //@SuppressWarnings({"unchecked"})
        public void GenerateFirstPass(PersistentClass pc, ClassAuditingData auditingData,
                                      EntityXmlMappingData xmlMappingData, bool isAudited)
        {
            String schema = GetSchema(auditingData.AuditTable.schema, pc.Table);
            String catalog = GetCatalog(auditingData.AuditTable.catalog, pc.Table);

            String entityName = pc.EntityName;
            if (!isAudited)
            {
                IdMappingData _idMapper = idMetadataGenerator.AddId(pc);

                if (_idMapper == null)
                {
                    // Unsupported id mapping, e.g. key-many-to-one. If the entity is used in auditing, an exception
                    // will be thrown later on.
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Unable to create auditing id mapping for entity " + entityName +
                            ", because of an unsupported Hibernate id mapping (e.g. key-many-to-one).");
                    }
                    return;
                }

                //ORIG:
                //IExtendedPropertyMapper propertyMapper = null;
                //String parentEntityName = null;
                EntityConfiguration _entityCfg = new EntityConfiguration(entityName, _idMapper, null,
                        null);
                NotAuditedEntitiesConfigurations.Add(entityName, _entityCfg);
                return;
            }

            if (log.IsDebugEnabled)
            {
                log.Debug("Generating first-pass auditing mapping for entity " + entityName + ".");
            }

            String auditEntityName = VerEntCfg.GetAuditEntityName(entityName);
            String auditTableName = VerEntCfg.GetAuditTableName(entityName, pc.Table.Name);

            // Registering the audit entity name, now that it is known
            AuditEntityNameRegister.register(auditEntityName);

            AuditTableData auditTableData = new AuditTableData(auditEntityName, auditTableName, schema, catalog);

            // Generating a mapping for the id
            IdMappingData idMapper = idMetadataGenerator.AddId(pc);

            InheritanceType.Type inheritanceType = InheritanceType.GetForChild(pc);

            // These properties will be read from the mapping data
            XmlElement class_mapping;
            IExtendedPropertyMapper propertyMapper;
            String parentEntityName;

            Triple<XmlElement, IExtendedPropertyMapper, String> mappingData;

            // Reading the mapping data depending on inheritance type (if any)
            switch (inheritanceType)
            {
                case InheritanceType.Type.NONE:
                    mappingData = GenerateMappingData(pc, xmlMappingData, auditTableData, idMapper);
                    break;

                case InheritanceType.Type.SINGLE:
                    mappingData = GenerateInheritanceMappingData(pc, xmlMappingData, auditTableData, "subclass");
                    break;

                case InheritanceType.Type.JOINED:
                    mappingData = GenerateInheritanceMappingData(pc, xmlMappingData, auditTableData, "joined-subclass");

                    AddJoinedInheritancePersisterHack(mappingData.First);

                    // Adding the "key" element with all id columns...
                    XmlElement keyMapping = mappingData.First.OwnerDocument.CreateElement("key");
                    mappingData.First.AppendChild(keyMapping);
                    MetadataTools.AddColumns(keyMapping, (IEnumerator<ISelectable>)pc.Table.PrimaryKey.ColumnIterator.GetEnumerator());

                    // ... and the revision number column, read from the revision info relation mapping.
                    keyMapping.AppendChild((XmlElement)CloneAndSetupRevisionInfoRelationMapping(keyMapping.OwnerDocument).GetElementsByTagName("column")[0].Clone());
                    break;

                case InheritanceType.Type.TABLE_PER_CLASS:
                    mappingData = GenerateInheritanceMappingData(pc, xmlMappingData, auditTableData, "union-subclass");

                    AddTablePerClassInheritancePersisterHack(mappingData.First);

                    break;

                default:
                    throw new AssertionFailure("Envers.NET: AuditMetadataGenerator.GenerateFirstPass: Impossible enum value.");
            }

            class_mapping = mappingData.First;
            propertyMapper = mappingData.Second;
            parentEntityName = mappingData.Third;

            xmlMappingData.ClassMapping = class_mapping;

            // Mapping unjoined properties
            AddProperties(class_mapping, (IEnumerator<Property>)pc.UnjoinedPropertyIterator.GetEnumerator(), propertyMapper,
                    auditingData, pc.EntityName, xmlMappingData,
                    true);

            // Creating and mapping joins (first pass)
            CreateJoins(pc, class_mapping, auditingData);
            AddJoins(pc, propertyMapper, auditingData, pc.EntityName, xmlMappingData, true);

            // Storing the generated configuration
            EntityConfiguration entityCfg = new EntityConfiguration(auditEntityName, idMapper,
                    propertyMapper, parentEntityName);
            EntitiesConfigurations.Add(pc.EntityName, entityCfg);
        }
        public void GenerateSecondPass(PersistentClass pc, ClassAuditingData auditingData,
                                       EntityXmlMappingData xmlMappingData) {
            String entityName = pc.EntityName;
            if (log.IsDebugEnabled)
            {
                log.Debug("Generating first-pass auditing mapping for entity " + entityName + ".");
            }

            ICompositeMapperBuilder propertyMapper = EntitiesConfigurations[entityName].PropertyMapper;

            // Mapping unjoined properties
            XmlElement parent = xmlMappingData.ClassMapping;

            AddProperties(parent, (IEnumerator<Property>) pc.UnjoinedPropertyIterator.GetEnumerator(),
                    propertyMapper, auditingData, entityName, xmlMappingData, false);

            // Mapping joins (second pass)
            AddJoins(pc, propertyMapper, auditingData, entityName, xmlMappingData, false);
        }
        public EntitiesConfigurations Configure(NHibernate.Cfg.Configuration cfg, 
            GlobalConfiguration globalCfg, AuditEntitiesConfiguration verEntCfg,
            XmlDocument revisionInfoXmlMapping, XmlElement revisionInfoRelationMapping)
        {
            // Creating a name register to capture all audit entity names created.
            AuditEntityNameRegister auditEntityNameRegister = new AuditEntityNameRegister();
            //XmlWriter writer = new XmlTextWriter(..

            // Sorting the persistent class topologically - superclass always before subclass
            IEnumerator<PersistentClass> classes = GraphTopologicalSort.Sort<PersistentClass, String>(new PersistentClassGraphDefiner(cfg)).GetEnumerator();

            ClassesAuditingData classesAuditingData = new ClassesAuditingData();
            IDictionary<PersistentClass, EntityXmlMappingData> xmlMappings = new Dictionary<PersistentClass, EntityXmlMappingData>();

            // Reading metadata from annotations
            while (classes.MoveNext()) {
                PersistentClass pc = classes.Current;

                // Collecting information from annotations on the persistent class pc
                AnnotationsMetadataReader annotationsMetadataReader =
                        new AnnotationsMetadataReader(globalCfg, pc);
                ClassAuditingData auditData = annotationsMetadataReader.AuditData;

                classesAuditingData.AddClassAuditingData(pc, auditData);
            }

            // Now that all information is read we can update the calculated fields.
            classesAuditingData.UpdateCalculatedFields();

            AuditMetadataGenerator auditMetaGen = new AuditMetadataGenerator(cfg, globalCfg, verEntCfg,
                    revisionInfoRelationMapping, auditEntityNameRegister, classesAuditingData);

            // First pass
            foreach (KeyValuePair<PersistentClass, ClassAuditingData> pcDatasEntry in classesAuditingData.GetAllClassAuditedData())
            {
                PersistentClass pc = pcDatasEntry.Key;
                ClassAuditingData auditData = pcDatasEntry.Value;

                EntityXmlMappingData xmlMappingData = new EntityXmlMappingData();
                if (auditData.IsAudited()) {
                    if (!String.IsNullOrEmpty(auditData.AuditTable.value)){ //  .getAuditTable().value())) {
                        verEntCfg.AddCustomAuditTableName(pc.EntityName, auditData.AuditTable.value);
                    }

                    auditMetaGen.GenerateFirstPass(pc, auditData, xmlMappingData, true);
                } else {
                    auditMetaGen.GenerateFirstPass(pc, auditData, xmlMappingData, false);
                }

                xmlMappings.Add(pc, xmlMappingData);
            }

            // Second pass
            foreach (KeyValuePair<PersistentClass, ClassAuditingData> pcDatasEntry in classesAuditingData.GetAllClassAuditedData())
            {
                EntityXmlMappingData xmlMappingData = xmlMappings[pcDatasEntry.Key];

                if (pcDatasEntry.Value.IsAudited()) {
                    auditMetaGen.GenerateSecondPass(pcDatasEntry.Key, pcDatasEntry.Value, xmlMappingData);
                    try {
                        //cfg.AddDocument(writer.write(xmlMappingData.MainXmlMapping));
                        cfg.AddDocument(xmlMappingData.MainXmlMapping);
                        //WriteDocument(xmlMappingData.getMainXmlMapping());

                        foreach (XmlDocument additionalMapping in  xmlMappingData.AdditionalXmlMappings) {
                            //cfg.AddDocument(writer.write(additionalMapping));
                            cfg.AddDocument(additionalMapping);
                            //WriteDocument(additionalMapping);
                        }
                    }
                    catch (MappingException e)
                    { //catch (DocumentException e) {  //?Catalina DocumentException NOT IMPLEMENTED
                        throw new MappingException(e);
                    }
                }
            }

            // Only if there are any versioned classes
            if (classesAuditingData.GetAllClassAuditedData().Count > 0)
            {
                try {
                    if (revisionInfoXmlMapping !=  null) {
                        //WriteDocument(revisionInfoXmlMapping);
                        //cfg.addDocument(writer.write(revisionInfoXmlMapping));
                        cfg.AddDocument((revisionInfoXmlMapping));
                    }
                }
                catch (MappingException e)
                { //catch (DocumentException e) { //?Catalina
                    throw new MappingException(e);
                }
            }

            return new EntitiesConfigurations(auditMetaGen.EntitiesConfigurations,
                    auditMetaGen.NotAuditedEntitiesConfigurations);
        }
Beispiel #18
0
        public void GenerateFirstPass(PersistentClass pc, ClassAuditingData auditingData,
                                      EntityXmlMappingData xmlMappingData, bool isAudited)
        {
            var schema  = GetSchema(auditingData.AuditTable.Schema, pc.Table);
            var catalog = GetCatalog(auditingData.AuditTable.Catalog, pc.Table);
            Func <System.Type, object> factory = auditingData.Factory.Factory.Instantiate;

            var entityName = pc.EntityName;

            if (!isAudited)
            {
                var _idMapper = idMetadataGenerator.AddId(pc);

                if (_idMapper == null)
                {
                    // Unsupported id mapping, e.g. key-many-to-one. If the entity is used in auditing, an exception
                    // will be thrown later on.
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Unable to create auditing id mapping for entity {0}" +
                                        ", because of an unsupported Hibernate id mapping (e.g. key-many-to-one).", entityName);
                    }
                    return;
                }

                //ORIG:
                //IExtendedPropertyMapper propertyMapper = null;
                //String parentEntityName = null;
                var _entityCfg = new EntityConfiguration(entityName, pc.ClassName, _idMapper, null, null, factory);
                NotAuditedEntitiesConfigurations.Add(entityName, _entityCfg);
                return;
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Generating first-pass auditing mapping for entity {0}.", entityName);
            }

            var auditEntityName = VerEntCfg.GetAuditEntityName(entityName);
            var auditTableName  = VerEntCfg.AuditTableName(entityName, pc);

            // Registering the audit entity name, now that it is known
            AuditEntityNameRegister.Register(auditEntityName);

            var auditTableData = new AuditTableData(auditEntityName, auditTableName, schema, catalog);

            // Generating a mapping for the id
            var idMapper = idMetadataGenerator.AddId(pc);

            if (idMapper == null)
            {
                throw new AuditException("Id mapping for type " + pc.ClassName +
                                         " is currently not supported in Envers. If you need composite-id, use 'Components as composite identifiers'.");
            }

            var inheritanceType = pc.GetInheritanceType();

            // These properties will be read from the mapping data

            Tuple <XElement, IExtendedPropertyMapper, string> mappingData;

            // Reading the mapping data depending on inheritance type (if any)
            switch (inheritanceType)
            {
            case InheritanceType.None:
                mappingData = generateMappingData(pc, xmlMappingData, auditTableData, idMapper);
                break;

            case InheritanceType.Single:
                auditTableData = new AuditTableData(auditEntityName, null, schema, catalog);
                mappingData    = generateInheritanceMappingData(pc, xmlMappingData, auditTableData, "subclass");
                break;

            case InheritanceType.Joined:
                mappingData = generateInheritanceMappingData(pc, xmlMappingData, auditTableData, "joined-subclass");

                // Adding the "key" element with all id columns...
                var keyMapping = new XElement(MetadataTools.CreateElementName("key"));
                mappingData.Item1.Add(keyMapping);
                MetadataTools.AddColumns(keyMapping, pc.Table.PrimaryKey.ColumnIterator);

                // ... and the revision number column, read from the revision info relation mapping.
                keyMapping.Add(cloneAndSetupRevisionInfoRelationMapping().Element(MetadataTools.CreateElementName("column")));
                break;

            case InheritanceType.TablePerClass:
                mappingData = generateInheritanceMappingData(pc, xmlMappingData, auditTableData, "union-subclass");

                break;

            default:
                throw new AssertionFailure("AuditMetadataGenerator.GenerateFirstPass: Impossible enum value.");
            }

            var classMapping     = mappingData.Item1;
            var propertyMapper   = mappingData.Item2;
            var parentEntityName = mappingData.Item3;

            xmlMappingData.ClassMapping = classMapping;

            // Mapping unjoined properties
            addProperties(classMapping, pc.UnjoinedPropertyIterator, propertyMapper,
                          auditingData, pc.EntityName, xmlMappingData,
                          true);

            // Creating and mapping joins (first pass)
            createJoins(pc, classMapping, auditingData);
            addJoins(pc, propertyMapper, auditingData, pc.EntityName, xmlMappingData, true);

            // Storing the generated configuration
            var entityCfg = new EntityConfiguration(auditEntityName, pc.ClassName, idMapper,
                                                    propertyMapper, parentEntityName, factory);

            EntitiesConfigurations.Add(pc.EntityName, entityCfg);
        }
        public void AddValue(XmlElement parent, IValue value, ICompositeMapperBuilder currentMapper, string entityName,
					  EntityXmlMappingData xmlMappingData, PropertyAuditingData propertyAuditingData,
					  bool insertable, bool firstPass)
        {
            var type = value.Type;

            // only first pass
            if (firstPass)
            {
                if (BasicMetadataGenerator.AddBasic(parent, propertyAuditingData, value, currentMapper, insertable, false))
                {
                    // The property was mapped by the basic generator.
                    return;
                }
            }

            if (type is ComponentType)
            {
                // both passes
                componentMetadataGenerator.AddComponent(parent, propertyAuditingData, value, currentMapper,
                        entityName, xmlMappingData, firstPass, insertable);
            }
            else if (type is ManyToOneType)
            {
                // only second pass
                if (!firstPass)
                {
                    toOneRelationMetadataGenerator.AddToOne(parent, propertyAuditingData, value, currentMapper,
                            entityName, insertable, null);
                }
            }
            else if (type is OneToOneType)
            {
                // only second pass
                if (!firstPass)
                {
                    var oneToOneType = (OneToOneType)type;
                    var oneToOneValue = (OneToOne)value;
                    if (oneToOneType.IsReferenceToPrimaryKey && oneToOneValue.IsConstrained)
                    {
                        //if pk onetoone is used, "value" has no corresponding columns
                        var pkColumns = new List<string>();
                        foreach (Column column in Cfg.GetClassMapping(oneToOneValue.ReferencedEntityName).Identifier.ColumnIterator)
                        {
                            pkColumns.Add("Ref" + column.Name);
                        }

                        toOneRelationMetadataGenerator.AddToOne(parent, propertyAuditingData, value, currentMapper,
                                entityName, insertable, pkColumns);
                    }
                    else
                    {
                        toOneRelationMetadataGenerator.AddOneToOneNotOwning(propertyAuditingData, oneToOneValue,
                                currentMapper, entityName);
                    }
                }
            }
            else if (type is CollectionType)
            {
                // only second pass
                if (!firstPass)
                {
                    var collectionMetadataGenerator = new CollectionMetadataGenerator(this,
                            (Mapping.Collection)value, currentMapper, entityName, xmlMappingData,
                            propertyAuditingData);
                    collectionMetadataGenerator.AddCollection();
                }
            }
            else
            {
                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, propertyAuditingData.Name);
                }
            }
        }