private List <ValidationDocument> GetValidationPackageHelperFiles(IObjectRepository tdb, int implementationGuideId)
        {
            List <ValidationDocument> documents = new List <ValidationDocument>();

            // Get the list of sch helper files, and vocabulary files that we need to return
            var helperFiles = (from vigf in tdb.ViewImplementationGuideFiles
                               where vigf.ImplementationGuideId == implementationGuideId &&
                               (vigf.ContentType == ImplementationGuideFile.ContentTypeSchematronHelper || vigf.ContentType == ImplementationGuideFile.ContentTypeVocabulary)
                               select vigf);

            foreach (ViewImplementationGuideFile currentFile in helperFiles)
            {
                ValidationDocument newValidationDoc = new ValidationDocument()
                {
                    Name        = currentFile.FileName,
                    ContentType = currentFile.ContentType,
                    MimeType    = currentFile.MimeType,
                    Content     = currentFile.Data
                };

                documents.Add(newValidationDoc);
            }

            return(documents);
        }
        private ValidationDocument GetGeneratedValidationDocument(IObjectRepository tdb, int implementationGuideId)
        {
            ImplementationGuide     ig      = tdb.ImplementationGuides.Single(y => y.Id == implementationGuideId);
            ImplementationGuideFile vocFile = tdb.ImplementationGuideFiles.FirstOrDefault(y => y.ContentType == ImplementationGuideFile.ContentTypeVocabulary && y.ImplementationGuideId == ig.Id);
            string vocFilename = "voc.xml";

            if (vocFile != null)
            {
                vocFilename = vocFile.FileName;
            }

            List <Template> templates = ig.GetRecursiveTemplates(tdb);

            SchematronGenerator schGenerator = new SchematronGenerator(tdb, ig, templates, true, vocFileName: vocFilename);
            string schContent = schGenerator.Generate();
            string schName    = Trifolia.Shared.Helper.NormalizeName(ig.Name) + ".sch";

            ValidationDocument schDoc = new ValidationDocument()
            {
                Content     = ASCIIEncoding.UTF8.GetBytes(schContent),
                ContentType = ImplementationGuideFile.ContentTypeSchematron,
                MimeType    = "text/xml",
                Name        = schName
            };

            return(schDoc);
        }
        public List <ValidationDocument> GetValidationPackage(IObjectRepository tdb, int implementationGuideId, GenerationOptions options, DateTime?lastRetrieveDate)
        {
            List <ValidationDocument> documents = GetValidationPackageHelperFiles(tdb, implementationGuideId);

            if (options == GenerationOptions.RetrieveExisting)
            {
                ValidationDocument existingValidationDoc = GetStoredValidationDocument(tdb, implementationGuideId, null);

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

                documents.Add(existingValidationDoc);
            }
            else if (options == GenerationOptions.RegenerateChanges)
            {
                ValidationDocument existingValidationDoc = GetStoredValidationDocument(tdb, implementationGuideId, lastRetrieveDate);

                if (existingValidationDoc == null)
                {
                    ValidationDocument generatedValidationDoc = GetGeneratedValidationDocument(tdb, implementationGuideId);
                    documents.Add(generatedValidationDoc);
                }
            }
            else if (options == GenerationOptions.Generate)
            {
                ValidationDocument generatedValidationDoc = GetGeneratedValidationDocument(tdb, implementationGuideId);
                documents.Add(generatedValidationDoc);
            }

            return(documents);
        }
        private ValidationDocument GetStoredValidationDocument(IObjectRepository tdb, int implementationGuideId, DateTime?lastRetrieveDate)
        {
            ViewImplementationGuideFile igFile = tdb.ViewImplementationGuideFiles.FirstOrDefault(y => y.ImplementationGuideId == implementationGuideId && y.ContentType == ImplementationGuideFile.ContentTypeSchematron);

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

            if (lastRetrieveDate != null)
            {
                var igConstraintIds = (from tc in tdb.TemplateConstraints
                                       join t in tdb.Templates on tc.TemplateId equals t.Id
                                       where t.OwningImplementationGuideId == implementationGuideId
                                       select tc.Id);
                var newerConstraints = (from audit in tdb.AuditEntries
                                        join tcId in igConstraintIds on audit.TemplateConstraintId equals tcId
                                        where audit.AuditDate > lastRetrieveDate
                                        select tcId);

                if (newerConstraints.Count() > 0)
                {
                    return(null);
                }
            }

            ValidationDocument schDoc = new ValidationDocument()
            {
                Name        = igFile.FileName,
                Content     = igFile.Data,
                ContentType = igFile.ContentType,
                MimeType    = igFile.MimeType
            };

            return(schDoc);
        }