public static ExceptionObjectRepository PrepareExceptionRepository(
            [NotNull] QualitySpecification qualitySpecification,
            [NotNull] IDatasetContext datasetContext,
            [NotNull] IQualityConditionObjectDatasetResolver datasetResolver,
            [CanBeNull] AreaOfInterest areaOfInterest,
            [CanBeNull] XmlVerificationOptions verificationOptions)
        {
            if (verificationOptions == null)
            {
                return(null);
            }

            IWorkspace workspace = VerificationOptionUtils.GetExceptionWorkspace(
                verificationOptions);

            if (workspace == null)
            {
                return(null);
            }

            bool useDbfFieldNames = WorkspaceUtils.IsShapefileWorkspace(workspace);

            IIssueTableFieldManagement issueTableFields =
                IssueTableFieldsFactory.GetIssueTableFields(true, useDbfFieldNames);

            var result = new ExceptionObjectRepository(workspace, issueTableFields,
                                                       datasetContext, datasetResolver,
                                                       areaOfInterest?.Geometry);

            List <QualityCondition> qualityConditions =
                qualitySpecification.Elements.Select(element => element.QualityCondition)
                .ToList();

            InvolvedObjectsMatchCriteria involvedObjectsMatchCriteria =
                VerificationOptionUtils.GetInvolvedObjectMatchCriteria(verificationOptions);

            result.ReadExceptions(qualityConditions,
                                  VerificationOptionUtils.GetDefaultShapeMatchCriterion(
                                      verificationOptions),
                                  VerificationOptionUtils.GetDefaultExceptionObjectStatus(
                                      verificationOptions),
                                  involvedObjectsMatchCriteria);
            return(result);
        }
        private bool Verify([NotNull] QualitySpecification qualitySpecification,
                            double tileSize,
                            [NotNull] string directory,
                            IssueRepositoryType issueRepositoryType,
                            [NotNull] IEnumerable <KeyValuePair <string, string> > properties,
                            [CanBeNull] XmlVerificationOptions verificationOptions,
                            [CanBeNull] AreaOfInterest areaOfInterest,
                            [CanBeNull] ITrackCancel trackCancel,
                            out int errorCount,
                            out int warningCount,
                            out int exceptionCount,
                            out int unusedExceptionObjectCount,
                            out int rowCountWithStopConditions)
        {
            Model primaryModel = StandaloneVerificationUtils.GetPrimaryModel(qualitySpecification);

            Assert.NotNull(primaryModel, "no primary model found for quality specification");

            // TODO disable quality conditions based on primaryModel and DatasetTestParameterValue.UsedAsReferenceData?
            // TODO this would probably require an explicit identification of the primary data source
            XmlVerificationReportBuilder xmlReportBuilder = GetReportBuilder();
            var statisticsBuilder = new IssueStatisticsBuilder();

            var datasetsCollector = new InvolvedDatasetsCollector();

            var service = new StandaloneQualityVerificationService(
                new MultiReportBuilder(xmlReportBuilder,
                                       statisticsBuilder,
                                       datasetsCollector),
                (context) => new SimpleDatasetOpener(context));

            // This context excludes geometric networks, terrains, topology, etc.:
            var datasetContext  = new MasterDatabaseDatasetContext();
            var datasetResolver =
                new QualityConditionObjectDatasetResolver(
                    new MasterDatabaseWorkspaceContextLookup());

            string issueWorkspaceName =
                VerificationOptionUtils.GetIssueWorkspaceName(verificationOptions);
            string verificationReportFileName =
                VerificationOptionUtils.GetXmlReportFileName(verificationOptions);

            ISpatialReference spatialReference =
                primaryModel.SpatialReferenceDescriptor?.SpatialReference;

            var  issueGdbWritten = false;
            bool fulfilled;

            List <string> htmlReportFilePaths;
            List <string> specificationReportFilePaths;
            string        gdbPath = null;

            Func <IObjectDataset, string> getKeyField =
                StandaloneVerificationUtils.GetKeyFieldLookupFunction(verificationOptions);

            ExceptionObjectRepository exceptionObjectRepository =
                StandaloneVerificationUtils.PrepareExceptionRepository(
                    qualitySpecification, datasetContext, datasetResolver, areaOfInterest,
                    verificationOptions);

            using (IIssueRepository issueRepository =
                       ExternalIssueRepositoryUtils.GetIssueRepository(
                           directory, issueWorkspaceName, spatialReference, issueRepositoryType,
                           addExceptionFields: true))
            {
                fulfilled = service.Verify(qualitySpecification, datasetContext, datasetResolver,
                                           issueRepository, exceptionObjectRepository, tileSize,
                                           getKeyField,
                                           areaOfInterest, trackCancel,
                                           out errorCount,
                                           out warningCount,
                                           out rowCountWithStopConditions);

                if (issueRepository != null)
                {
                    issueGdbWritten = true;

                    gdbPath = ((IWorkspace)issueRepository.FeatureWorkspace).PathName;

                    _msg.InfoFormat("Issues written to {0}", gdbPath);

                    issueRepository.CreateIndexes(GetForSubProcess(trackCancel),
                                                  ignoreErrors: true);
                }

                using (_msg.IncrementIndentation("Documenting verification results..."))
                {
                    XmlVerificationReport verificationReport = GetVerificationReport(
                        xmlReportBuilder, qualitySpecification, properties);

                    string verificationReportPath = Path.Combine(directory,
                                                                 verificationReportFileName);
                    XmlUtils.Serialize(verificationReport, verificationReportPath);

                    _msg.InfoFormat("Verification report written to {0}", verificationReportPath);

                    IssueStatistics issueStatistics = statisticsBuilder.IssueStatistics;

                    if (issueRepository != null)
                    {
                        var issueStatisticsWriter =
                            new IssueStatisticsWriter(issueRepository.FeatureWorkspace);

                        issueStatisticsWriter.WriteStatistics(issueStatistics);

                        if (spatialReference != null &&
                            areaOfInterest != null &&
                            !areaOfInterest.IsEmpty)
                        {
                            var aoiWriter =
                                new AreaOfInterestWriter(issueRepository.FeatureWorkspace);
                            aoiWriter.WriteAreaOfInterest(areaOfInterest, spatialReference);
                        }
                    }

                    specificationReportFilePaths =
                        StandaloneVerificationUtils.WriteQualitySpecificationReport(
                            qualitySpecification, directory, _qualitySpecificationTemplatePath,
                            verificationOptions);

                    htmlReportFilePaths = StandaloneVerificationUtils.WriteHtmlReports(
                        qualitySpecification, directory, issueStatistics, verificationReport,
                        verificationReportFileName, _htmlReportTemplatePath, verificationOptions,
                        issueGdbWritten ? gdbPath : null,
                        null, specificationReportFilePaths);
                }
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();

            if (htmlReportFilePaths.Count > 0)
            {
                using (_msg.IncrementIndentation(htmlReportFilePaths.Count == 1
                                                                         ? "Html report:"
                                                                         : "Html reports:"))
                {
                    foreach (string path in htmlReportFilePaths)
                    {
                        _msg.Info(path);
                    }
                }
            }

            if (specificationReportFilePaths.Count > 0)
            {
                using (_msg.IncrementIndentation(specificationReportFilePaths.Count == 1
                                                                         ? "Quality specification report:"
                                                                         : "Quality specification reports:"))
                {
                    foreach (string path in specificationReportFilePaths)
                    {
                        _msg.Info(path);
                    }
                }
            }

            if (exceptionObjectRepository != null)
            {
                IExceptionStatistics stats = exceptionObjectRepository.ExceptionStatistics;
                exceptionCount             = stats.ExceptionCount;
                unusedExceptionObjectCount = stats.UnusedExceptionObjectCount;
            }
            else
            {
                exceptionCount             = 0;
                unusedExceptionObjectCount = 0;
            }

            return(fulfilled);
        }