public EntitiesConfigurations Configure(Cfg.Configuration cfg,
                                                IMetaDataStore metaDataStore,
                                                GlobalConfiguration globalCfg,
                                                AuditEntitiesConfiguration verEntCfg,
                                                XDocument revisionInfoXmlMapping,
                                                XElement revisionInfoRelationMapping)
        {
            // Creating a name register to capture all audit entity names created.
            var auditEntityNameRegister = new AuditEntityNameRegister();

            // Sorting the persistent class topologically - superclass always before subclass
            var classes = GraphTopologicalSort.Sort(new PersistentClassGraphDefiner(cfg));

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

            // Reading metadata from annotations
            foreach (var pc in classes)
            {
                // Collecting information from annotations on the persistent class pc
                var annotationsMetadataReader = new AnnotationsMetadataReader(metaDataStore, globalCfg, pc);
                var auditData = annotationsMetadataReader.GetAuditData();

                classesAuditingData.AddClassAuditingData(pc, auditData);
            }

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

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

            // First pass
            foreach (var pcDatasEntry in classesAuditingData.AllClassAuditedData)
            {
                var pc        = pcDatasEntry.Key;
                var auditData = pcDatasEntry.Value;

                var xmlMappingData = new EntityXmlMappingData();
                if (auditData.IsAudited())
                {
                    if (!string.IsNullOrEmpty(auditData.AuditTable.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 (var pcDatasEntry in classesAuditingData.AllClassAuditedData)
            {
                var xmlMappingData = xmlMappings[pcDatasEntry.Key];

                if (pcDatasEntry.Value.IsAudited())
                {
                    auditMetaGen.GenerateSecondPass(pcDatasEntry.Key, pcDatasEntry.Value, xmlMappingData);

                    cfg.AddXml(xmlMappingData.MainXmlMapping.ToString());

                    foreach (var additionalMapping in xmlMappingData.AdditionalXmlMappings)
                    {
                        cfg.AddXml(additionalMapping.ToString());
                    }
                }
            }

            // Only if there are any versioned classes
            if (auditMetaGen.EntitiesConfigurations.Count > 0)
            {
                if (revisionInfoXmlMapping != null)
                {
                    cfg.AddXml(revisionInfoXmlMapping.ToString());
                }
            }

            return(new EntitiesConfigurations(auditMetaGen.EntitiesConfigurations,
                                              auditMetaGen.NotAuditedEntitiesConfigurations));
        }
Exemple #2
0
        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));
        }