public OneAuditEntityQueryGenerator(GlobalConfiguration globalCfg, AuditEntitiesConfiguration verEntCfg,
            MiddleIdData referencingIdData, String referencedEntityName,
            IIdMapper referencedIdMapper)
        {
            this.referencingIdData = referencingIdData;

            /*
             * The query that we need to create:
             *   SELECT new list(e) FROM versionsReferencedEntity e
             *   WHERE
             * (only entities referenced by the association; id_ref_ing = id of the referencing entity)
             *     e.id_ref_ing = :id_ref_ing AND
             * (selecting e entities at revision :revision)
             *     e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2
             *       WHERE e2.revision <= :revision AND e2.id = e.id) AND
             * (only non-deleted entities)
             *     e.revision_type != DEL
             */
            String revisionPropertyPath = verEntCfg.RevisionNumberPath;
            String originalIdPropertyName = verEntCfg.OriginalIdPropName;

            String versionsReferencedEntityName = verEntCfg.GetAuditEntityName(referencedEntityName);

            // SELECT new list(e) FROM versionsEntity e
            QueryBuilder qb = new QueryBuilder(versionsReferencedEntityName, "e");
            qb.AddProjection("new list", "e", false, false);
            // WHERE
            Parameters rootParameters = qb.RootParameters;
            // e.id_ref_ed = :id_ref_ed
            referencingIdData.PrefixedMapper.AddNamedIdEqualsToQuery(rootParameters, null, true);

            // SELECT max(e.revision) FROM versionsReferencedEntity e2
            QueryBuilder maxERevQb = qb.NewSubQueryBuilder(versionsReferencedEntityName, "e2");
            maxERevQb.AddProjection("max", revisionPropertyPath, false);
            // WHERE
            Parameters maxERevQbParameters = maxERevQb.RootParameters;
            // e2.revision <= :revision
            maxERevQbParameters.AddWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
            // e2.id = e.id
            referencedIdMapper.AddIdsEqualToQuery(maxERevQbParameters,
                    "e." + originalIdPropertyName, "e2." + originalIdPropertyName);

            // e.revision = (SELECT max(...) ...)
            rootParameters.AddWhere(revisionPropertyPath, false, globalCfg.getCorrelatedSubqueryOperator(), maxERevQb);

            // e.revision_type != DEL
            rootParameters.AddWhereWithNamedParam(verEntCfg.RevisionTypePropName, false, "!=", "delrevisiontype");

            StringBuilder sb = new StringBuilder();
            qb.Build(sb, EmptyDictionary<String, object>.Instance);
            queryString = sb.ToString();
        }
        //public AuditEntitiesConfiguration getAuditEntCfg() {
        //    return auditEntCfg;
        //}

        //public AuditSyncManager getSyncManager() {
        //    return auditSyncManager;
        //}

        //public GlobalConfiguration getGlobalCfg() {
        //    return globalCfg;
        //}

        //public EntitiesConfigurations getEntCfg() {
        //    return entCfg;
        //}

        //public RevisionInfoQueryCreator getRevisionInfoQueryCreator() {
        //    return revisionInfoQueryCreator;
        //}

        //public RevisionInfoNumberReader getRevisionInfoNumberReader() {
        //    return revisionInfoNumberReader;
        //}

        //TODO Simon @SuppressWarnings({"unchecked"})

        public AuditConfiguration(NHibernate.Cfg.Configuration cfg) {
            IDictionary<string,string> properties = cfg.Properties;

            //ReflectionManager reflectionManager = ((AnnotationConfiguration) cfg).getReflectionManager();
            RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration();
            RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure(cfg);
            AuditEntCfg = new AuditEntitiesConfiguration(properties, revInfoCfgResult.RevisionInfoEntityName);
            GlobalCfg = new GlobalConfiguration(properties);
            AuditSyncManager = new AuditSyncManager(revInfoCfgResult.RevisionInfoGenerator);
            RevisionInfoQueryCreator = revInfoCfgResult.RevisionInfoQueryCreator;
            RevisionInfoNumberReader = revInfoCfgResult.RevisionInfoNumberReader;
            EntCfg = new EntitiesConfigurator().Configure(cfg, GlobalCfg, AuditEntCfg,
                    revInfoCfgResult.RevisionInfoXmlMapping, revInfoCfgResult.RevisionInfoRelationMapping);
        }
        public static void AddEntityAtRevision(GlobalConfiguration globalCfg, QueryBuilder qb, Parameters rootParameters,
            MiddleIdData idData, String revisionPropertyPath, String originalIdPropertyName,
            String alias1, String alias2)
        {
            // SELECT max(e.revision) FROM versionsReferencedEntity e2
            QueryBuilder maxERevQb = qb.NewSubQueryBuilder(idData.AuditEntityName, alias2);
            maxERevQb.AddProjection("max", revisionPropertyPath, false);
            // WHERE
            Parameters maxERevQbParameters = maxERevQb.RootParameters;
            // e2.revision <= :revision
            maxERevQbParameters.AddWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
            // e2.id_ref_ed = e.id_ref_ed
            idData.OriginalMapper.AddIdsEqualToQuery(maxERevQbParameters,
                    alias1 + "." + originalIdPropertyName, alias2 +"." + originalIdPropertyName);

            // e.revision = (SELECT max(...) ...)
            rootParameters.AddWhere("e." + revisionPropertyPath, false, globalCfg.getCorrelatedSubqueryOperator(), maxERevQb);
        }
Ejemplo n.º 4
0
        public void AddEntityAtRevisionRestriction(GlobalConfiguration globalCfg, QueryBuilder rootQueryBuilder, string revisionProperty, string revisionEndProperty, bool addAlias, MiddleIdData idData, string revisionPropertyPath, string originalIdPropertyName, string alias1, string alias2)
        {
            var rootParameters = rootQueryBuilder.RootParameters;

            // create a subquery builder
            // SELECT max(e.revision) FROM versionsReferencedEntity e2
            var maxERevQb = rootQueryBuilder.NewSubQueryBuilder(idData.AuditEntityName, alias2);
            maxERevQb.AddProjection("max", revisionPropertyPath, false);
            // WHERE
            var maxERevQbParameters = maxERevQb.RootParameters;
            // e2.revision <= :revision
            maxERevQbParameters.AddWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
            // e2.id_ref_ed = e.id_ref_ed
            idData.OriginalMapper.AddIdsEqualToQuery(maxERevQbParameters,
                    alias1 + "." + originalIdPropertyName, alias2 + "." + originalIdPropertyName);

            // add subquery to rootParameters
            var subqueryOperator = globalCfg.CorrelatedSubqueryOperator;
            rootParameters.AddWhere(revisionProperty, addAlias, subqueryOperator, maxERevQb);
        }
        public ThreeEntityQueryGenerator(GlobalConfiguration globalCfg,
            AuditEntitiesConfiguration verEntCfg,
            String versionsMiddleEntityName,
            MiddleIdData referencingIdData,
            MiddleIdData referencedIdData,
            MiddleIdData indexIdData,
            IEnumerable<MiddleComponentData> componentDatas)
        {
            this.referencingIdData = referencingIdData;

            /*
             * The query that we need to create:
             *   SELECT new list(ee, e, f) FROM versionsReferencedEntity e, versionsIndexEntity f, middleEntity ee
             *   WHERE
             * (entities referenced by the middle table; id_ref_ed = id of the referenced entity)
             *     ee.id_ref_ed = e.id_ref_ed AND
             * (entities referenced by the middle table; id_ref_ind = id of the index entity)
             *     ee.id_ref_ind = f.id_ref_ind AND
             * (only entities referenced by the association; id_ref_ing = id of the referencing entity)
             *     ee.id_ref_ing = :id_ref_ing AND
             * (selecting e entities at revision :revision)
             *     e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2
             *       WHERE e2.revision <= :revision AND e2.id_ref_ed = e.id_ref_ed) AND
             * (selecting f entities at revision :revision)
             *     f.revision = (SELECT max(f2.revision) FROM versionsIndexEntity f2
             *       WHERE f2.revision <= :revision AND f2.id_ref_ed = f.id_ref_ed) AND
             * (the association at revision :revision)
             *     ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2
             *       WHERE ee2.revision <= :revision AND ee2.originalId.* = ee.originalId.*) AND
             * (only non-deleted entities and associations)
             *     ee.revision_type != DEL AND
             *     e.revision_type != DEL AND
             *     f.revision_type != DEL
             */
            String revisionPropertyPath = verEntCfg.RevisionNumberPath;
            String originalIdPropertyName = verEntCfg.OriginalIdPropName;

            String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName;

            // SELECT new list(ee) FROM middleEntity ee
            QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee");
            qb.AddFrom(referencedIdData.AuditEntityName, "e");
            qb.AddFrom(indexIdData.AuditEntityName, "f");
            qb.AddProjection("new list", "ee, e, f", false, false);
            // WHERE
            Parameters rootParameters = qb.RootParameters;
            // ee.id_ref_ed = e.id_ref_ed
            referencedIdData.PrefixedMapper.AddIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath,
                    referencedIdData.OriginalMapper, "e." + originalIdPropertyName);
            // ee.id_ref_ind = f.id_ref_ind
            indexIdData.PrefixedMapper.AddIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath,
                    indexIdData.OriginalMapper, "f." + originalIdPropertyName);
            // ee.originalId.id_ref_ing = :id_ref_ing
            referencingIdData.PrefixedMapper.AddNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);

            // e.revision = (SELECT max(...) ...)
            QueryGeneratorTools.AddEntityAtRevision(globalCfg, qb, rootParameters, referencedIdData, revisionPropertyPath,
                    originalIdPropertyName, "e", "e2");

            // f.revision = (SELECT max(...) ...)
            QueryGeneratorTools.AddEntityAtRevision(globalCfg, qb, rootParameters, indexIdData, revisionPropertyPath,
                    originalIdPropertyName, "f", "f2");

            // ee.revision = (SELECT max(...) ...)
            QueryGeneratorTools.AddAssociationAtRevision(qb, rootParameters, referencingIdData, versionsMiddleEntityName,
                    eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas);

            // ee.revision_type != DEL
            rootParameters.AddWhereWithNamedParam(verEntCfg.RevisionTypePropName, "!=", "delrevisiontype");
            // e.revision_type != DEL
            rootParameters.AddWhereWithNamedParam("e." + verEntCfg.RevisionTypePropName, false, "!=", "delrevisiontype");
            // f.revision_type != DEL
            rootParameters.AddWhereWithNamedParam("f." + verEntCfg.RevisionTypePropName, false, "!=", "delrevisiontype");

            StringBuilder sb = new StringBuilder();
            qb.Build(sb, EmptyDictionary<String, Object>.Instance);
            queryString = sb.ToString();
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public TwoEntityQueryGenerator(GlobalConfiguration globalCfg,
										AuditEntitiesConfiguration verEntCfg,
										IAuditStrategy auditStrategy,
										string versionsMiddleEntityName,
										MiddleIdData referencingIdData,
										MiddleIdData referencedIdData,
										IEnumerable<MiddleComponentData> componentDatas)
        {
            this.referencingIdData = referencingIdData;

            /*
             * The query that we need to create:
             *   SELECT new list(ee, e) FROM versionsReferencedEntity e, middleEntity ee
             *   WHERE
             * (entities referenced by the middle table; id_ref_ed = id of the referenced entity)
             *     ee.id_ref_ed = e.id_ref_ed AND
             * (only entities referenced by the association; id_ref_ing = id of the referencing entity)
             *     ee.id_ref_ing = :id_ref_ing AND
             *
             * (selecting e entities at revision :revision)
             *	--> for DefaultAuditStrategy:
             *		e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2
             *			WHERE e2.revision <= :revision AND e2.id = e.id)
             *	--> for ValidTimeAuditStrategy:
             *		e.revision <= :revision and (e.endRevision > :revision or e.endRevision is null)
             *
             * (the association at revision :revision)
             *	--> for DefaultAuditStrategy:
             *		ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2
             *			WHERE ee2.revision <= :revision AND ee2.originalId.* = ee.originalId.*)
             *	--> for ValidityAuditStrategy:
             *		ee.revision <= :revision and (ee.endRevision > :revision or ee.endRevision is null)
             *
             * AND
             *
             * (only non-deleted entities and associations)
             *     ee.revision_type != DEL AND
             *     e.revision_type != DEL
             */
            var revisionPropertyPath = verEntCfg.RevisionNumberPath;
            var originalIdPropertyName = verEntCfg.OriginalIdPropName;

            var eeOriginalIdPropertyPath = "ee." + originalIdPropertyName;

            // SELECT new list(ee) FROM middleEntity ee
            var qb = new QueryBuilder(versionsMiddleEntityName, "ee");
            qb.AddFrom(referencedIdData.AuditEntityName, "e");
            qb.AddProjection("new list", "ee, e", false, false);
            // WHERE
            var rootParameters = qb.RootParameters;
            // ee.id_ref_ed = e.id_ref_ed
            referencedIdData.PrefixedMapper.AddIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath,
                    referencedIdData.OriginalMapper, "e." + originalIdPropertyName);
            // ee.originalId.id_ref_ing = :id_ref_ing
            referencingIdData.PrefixedMapper.AddNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);

            // (selecting e entities at revision :revision)
            // --> based on auditStrategy (see above)
            auditStrategy.AddEntityAtRevisionRestriction(globalCfg, qb, "e." + revisionPropertyPath,
                                                         "e." + verEntCfg.RevisionEndFieldName, false,
                                                         referencedIdData, revisionPropertyPath, originalIdPropertyName, "e", "e2");

            // (with ee association at revision :revision)
            // --> based on auditStrategy (see above)
            auditStrategy.AddAssociationAtRevisionRestriction(qb, revisionPropertyPath,
                                                              verEntCfg.RevisionEndFieldName, true, referencingIdData,
                                                              versionsMiddleEntityName,
                                                              eeOriginalIdPropertyPath, revisionPropertyPath,
                                                              originalIdPropertyName, componentDatas.ToArray());

            // ee.revision_type != DEL
            rootParameters.AddWhereWithNamedParam(verEntCfg.RevisionTypePropName, "!=", "delrevisiontype");
            // e.revision_type != DEL
            rootParameters.AddWhereWithNamedParam("e." + verEntCfg.RevisionTypePropName, false, "!=", "delrevisiontype");

            var sb = new StringBuilder();
            qb.Build(sb, null);
            queryString = sb.ToString();
        }