Exemple #1
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);
        }