Esempio n. 1
0
 public AuditMetadataGenerator(IMetaDataStore metaDataStore,
                               Cfg.Configuration cfg,
                               GlobalConfiguration globalCfg,
                               AuditEntitiesConfiguration verEntCfg,
                               XElement revisionInfoRelationMapping,
                               AuditEntityNameRegister auditEntityNameRegister)
 {
     Cfg            = cfg;
     GlobalCfg      = globalCfg;
     VerEntCfg      = verEntCfg;
     _metaDataStore = metaDataStore;
     this.revisionInfoRelationMapping = revisionInfoRelationMapping;
     BasicMetadataGenerator           = new BasicMetadataGenerator();
     componentMetadataGenerator       = new ComponentMetadataGenerator(this);
     idMetadataGenerator              = new IdMetadataGenerator(this);
     toOneRelationMetadataGenerator   = new ToOneRelationMetadataGenerator(this);
     AuditEntityNameRegister          = auditEntityNameRegister;
     EntitiesConfigurations           = new Dictionary <string, EntityConfiguration>();
     NotAuditedEntitiesConfigurations = new Dictionary <string, EntityConfiguration>();
     entitiesJoins = new Dictionary <string, IDictionary <Join, XElement> >();
 }
Esempio n. 2
0
        public AuditMetadataGenerator(Cfg.Configuration cfg, 
										GlobalConfiguration globalCfg,
										AuditEntitiesConfiguration verEntCfg,
										IAuditStrategy auditStrategy,
										XmlElement revisionInfoRelationMapping,
										AuditEntityNameRegister auditEntityNameRegister)
        {
            Cfg = cfg;
            GlobalCfg = globalCfg;
            VerEntCfg = verEntCfg;
            AuditStrategy = auditStrategy;
            this.revisionInfoRelationMapping = revisionInfoRelationMapping;
            BasicMetadataGenerator = new BasicMetadataGenerator();
            componentMetadataGenerator = new ComponentMetadataGenerator(this);
            idMetadataGenerator = new IdMetadataGenerator(this);
            toOneRelationMetadataGenerator = new ToOneRelationMetadataGenerator(this);
            AuditEntityNameRegister = auditEntityNameRegister;
            EntitiesConfigurations = new Dictionary<string, EntityConfiguration>();
            NotAuditedEntitiesConfigurations = new Dictionary<string, EntityConfiguration>();
            entitiesJoins = new Dictionary<string, IDictionary<Join, XmlElement>>();
        }
        public AuditMetadataGenerator(Cfg.Configuration cfg, GlobalConfiguration globalCfg,
                                      AuditEntitiesConfiguration verEntCfg,
                                      XmlElement revisionInfoRelationMapping,
                                      AuditEntityNameRegister auditEntityNameRegister,
                                      ClassesAuditingData classesAuditingData)
        {
            this.Cfg = cfg;
            this.GlobalCfg = globalCfg;
            this.VerEntCfg = verEntCfg;
            this.revisionInfoRelationMapping = revisionInfoRelationMapping;

            this.BasicMetadataGenerator = new BasicMetadataGenerator();
            this.componentMetadataGenerator = new ComponentMetadataGenerator(this);
            this.idMetadataGenerator = new IdMetadataGenerator(this);
            this.toOneRelationMetadataGenerator = new ToOneRelationMetadataGenerator(this);

            this.AuditEntityNameRegister = auditEntityNameRegister;
            this.ClassesAuditingData = classesAuditingData;

            EntitiesConfigurations = new Dictionary<String, EntityConfiguration>();
            NotAuditedEntitiesConfigurations = new Dictionary<String, EntityConfiguration>();
            entitiesJoins = new Dictionary<String, IDictionary<Join, XmlElement>>();
        }
        //@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 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);
        }