Exemple #1
0
 public static Stream GenerateBulkFromPackage(DocumentTemplatePackage package, DiscoDataContext Database, User CreatorUser, DateTime Timestamp, bool InsertBlankPages, List<IAttachmentTarget> DataObjects)
 {
     if (DataObjects.Count > 0)
     {
         List<Stream> generatedPdfs = new List<Stream>(DataObjects.Count);
         using (var state = DocumentState.DefaultState())
         {
             foreach (var d in DataObjects)
             {
                 generatedPdfs.Add(package.GeneratePdfPackage(Database, d, CreatorUser, Timestamp, state));
                 state.SequenceNumber++;
                 state.FlushScopeCache();
             }
         }
         if (generatedPdfs.Count == 1)
         {
             return generatedPdfs[0];
         }
         else
         {
             Stream bulkPdf = Utilities.JoinPdfs(package.InsertBlankPages || InsertBlankPages, generatedPdfs);
             foreach (Stream singlePdf in generatedPdfs)
                 singlePdf.Dispose();
             return bulkPdf;
         }
     }
     return null;
 }
Exemple #2
0
        public static Stream GenerateBulkFromPackage(DocumentTemplatePackage package, DiscoDataContext Database, User CreatorUser, DateTime Timestamp, bool InsertBlankPages, List<string> DataObjectsIds)
        {
            List<IAttachmentTarget> DataObjects;

            switch (package.Scope)
            {
                case AttachmentTypes.Device:
                    DataObjects = Database.Devices.Where(d => DataObjectsIds.Contains(d.SerialNumber)).ToList<IAttachmentTarget>();
                    break;
                case AttachmentTypes.Job:
                    int[] intDataObjectsIds = DataObjectsIds.Select(i => int.Parse(i)).ToArray();
                    DataObjects = Database.Jobs.Where(j => intDataObjectsIds.Contains(j.Id)).ToList<IAttachmentTarget>();
                    break;
                case AttachmentTypes.User:
                    DataObjects = new List<IAttachmentTarget>(DataObjectsIds.Count);
                    for (int idIndex = 0; idIndex < DataObjectsIds.Count; idIndex++)
                    {
                        string dataObjectId = DataObjectsIds[idIndex];
                        var user = UserService.GetUser(ActiveDirectory.ParseDomainAccountId(dataObjectId), Database, true);
                        if (user == null)
                            throw new Exception($"Unknown Username specified: {dataObjectId}");
                        DataObjects.Add(user);
                    }
                    break;
                default:
                    throw new InvalidOperationException("Invalid DocumentType Scope");
            }

            return GenerateBulkFromPackage(package, Database, CreatorUser, Timestamp, InsertBlankPages, DataObjects);
        }
Exemple #3
0
        public static Stream GenerateFromPackage(DocumentTemplatePackage package, DiscoDataContext Database, IAttachmentTarget Data, User CreatorUser, DateTime Timestamp, DocumentState State)
        {
            var templates = package.GetDocumentTemplates(Database);

            if (templates.Count == 0)
                return null;

            bool generateExpression = !string.IsNullOrEmpty(package.OnGenerateExpression);
            string generateExpressionResult = null;

            if (generateExpression)
                generateExpressionResult = package.EvaluateOnGenerateExpression(Data, Database, CreatorUser, Timestamp, State);

            List<Stream> generatedPdfs = new List<Stream>(templates.Count);
            foreach (var template in templates)
            {
                generatedPdfs.Add(template.GeneratePdf(Database, Data, CreatorUser, Timestamp, State, true));

                State.SequenceNumber++;
                State.FlushScopeCache();
            }

            if (generateExpression)
                DocumentsLog.LogDocumentPackageGenerated(package, Data, CreatorUser, generateExpressionResult);
            else
                DocumentsLog.LogDocumentPackageGenerated(package, Data, CreatorUser);

            if (generatedPdfs.Count == 1)
            {
                return generatedPdfs[0];
            }
            else
            {
                Stream bulkPdf = Utilities.JoinPdfs(package.InsertBlankPages, generatedPdfs);
                foreach (Stream singlePdf in generatedPdfs)
                    singlePdf.Dispose();
                return bulkPdf;
            }
        }
Exemple #4
0
 public static void LogDocumentPackageGenerated(DocumentTemplatePackage Package, IAttachmentTarget Data, User Author)
 {
     Log(EventTypeIds.DocumentPackageGenerated, new object[]
         {
             Package.Id,
             Data.AttachmentReferenceId,
             Author.UserId
         });
 }
Exemple #5
0
 public static void LogDocumentPackageGenerated(DocumentTemplatePackage Package, IAttachmentTarget Data, User Author, string ExpressionResult)
 {
     Log(EventTypeIds.DocumentPackageGeneratedWithExpression, new object[]
         {
             Package.Id,
             Data.AttachmentReferenceId,
             Author.UserId,
             ExpressionResult
         });
 }
        public static DocumentTemplatePackage CreatePackage(DocumentTemplatePackage Package)
        {
            if (string.IsNullOrWhiteSpace(Package.Id))
                throw new ArgumentNullException(nameof(Package), "The Package Id is required");
            if (cache.ContainsKey(Package.Id)) // Name Unique
                throw new ArgumentException("Another Package already exists with that Id", nameof(Package));
            if (string.IsNullOrWhiteSpace(Package.Description))
                throw new ArgumentNullException(nameof(Package), "The Package Description is required");

            if (cache.TryAdd(Package.Id, Package))
            {
                PersistCache();
                return Package;
            }
            else
                throw new Exception("Unable to add the Package to the Cache, check the Package Id and try again");
        }
        public static DocumentTemplatePackage UpdatePackage(DocumentTemplatePackage Package)
        {
            DocumentTemplatePackage existingPackage;

            if (string.IsNullOrWhiteSpace(Package.Id))
                throw new ArgumentNullException(nameof(Package), "The Package Id is required");
            if (!cache.TryGetValue(Package.Id, out existingPackage)) // Name Unique
                throw new ArgumentException("The Package Id does not exist", nameof(Package));
            if (string.IsNullOrWhiteSpace(Package.Description))
                throw new ArgumentNullException(nameof(Package), "The Package Description is required");

            if (cache.TryUpdate(Package.Id, Package, existingPackage))
            {
                PersistCache();
                return Package;
            }
            else
                throw new Exception("Unable to update the Package in the Cache, there were concurrent updates to the same package");
        }
        private void UpdateInsertBlankPages(DocumentTemplatePackage Package, string InsertBlankPages)
        {
            var insertBlankPages = false;

            if (!string.IsNullOrWhiteSpace(InsertBlankPages) && !bool.TryParse(InsertBlankPages, out insertBlankPages))
                throw new ArgumentOutOfRangeException(nameof(InsertBlankPages));

            if (Package.InsertBlankPages != insertBlankPages)
            {
                Package.InsertBlankPages = insertBlankPages;
                DocumentTemplatePackages.UpdatePackage(Package);
            }
        }
        private void UpdateJobSubTypes(DocumentTemplatePackage Package, List<string> JobSubTypes)
        {
            List<string> jobSubTypes = null;

            if (JobSubTypes != null && JobSubTypes.Count > 0)
            {
                var subTypeIds = Database.JobSubTypes.Select(jst => jst.JobTypeId + "_" + jst.Id).ToList();

                jobSubTypes = subTypeIds
                    .Where(id => JobSubTypes.Contains(id, StringComparer.OrdinalIgnoreCase))
                    .OrderBy(id => id)
                    .ToList();
            }

            if (jobSubTypes == null)
            {
                if (Package.JobSubTypes != null)
                {
                    Package.JobSubTypes = null;
                    DocumentTemplatePackages.UpdatePackage(Package);
                }
            }
            else
            {
                if (Package.JobSubTypes == null || Package.JobSubTypes.Count != jobSubTypes.Count)
                {
                    Package.JobSubTypes = jobSubTypes;
                    DocumentTemplatePackages.UpdatePackage(Package);
                }
                else
                {
                    if (Package.JobSubTypes.Zip(jobSubTypes, (a, b) => a != b).Any(r => r))
                    {
                        Package.JobSubTypes = jobSubTypes;
                        DocumentTemplatePackages.UpdatePackage(Package);
                    }
                }
            }

        }
        private void UpdateIsHidden(DocumentTemplatePackage Package, string IsHidden)
        {
            var isHidden = false;

            if (!string.IsNullOrWhiteSpace(IsHidden) && !bool.TryParse(IsHidden, out isHidden))
                throw new ArgumentOutOfRangeException(nameof(IsHidden));

            if (Package.IsHidden != isHidden)
            {
                Package.IsHidden = isHidden;
                DocumentTemplatePackages.UpdatePackage(Package);
            }
        }
        private void UpdateOnGenerateExpression(DocumentTemplatePackage Package, string OnGenerateExpression)
        {
            string expression;
            if (string.IsNullOrWhiteSpace(OnGenerateExpression))
                expression = null;
            else
                expression = OnGenerateExpression.Trim();

            if (Package.OnGenerateExpression != expression)
            {
                Package.OnGenerateExpression = expression;
                DocumentTemplatePackages.UpdatePackage(Package);
                Package.OnGenerateExpressionInvalidateCache();
            }
        }
        private void UpdateScope(DocumentTemplatePackage Package, string Scope)
        {
            AttachmentTypes scope;
            if (!Enum.TryParse<AttachmentTypes>(Scope, true, out scope))
                throw new ArgumentException("Invalid Scope", nameof(Scope));

            if (Package.Scope != scope)
            {
                Package.Scope = scope;

                // Remove all Templates (none can be of the same scope)
                Package.DocumentTemplateIds = null;

                DocumentTemplatePackages.UpdatePackage(Package);
            }
        }
        private void UpdateDocumentTemplates(DocumentTemplatePackage Package, List<string> DocumentTemplates)
        {
            List<string> documentTemplateIds = null;

            if (DocumentTemplates != null && DocumentTemplates.Count > 0)
            {
                var packageScope = Package.Scope.ToString();

                // Collect Valid from Database (maintain order)
                documentTemplateIds = new List<string>(DocumentTemplates.Count);
                foreach (var templateId in DocumentTemplates)
                {
                    var dbId = Database.DocumentTemplates
                        .Where(dt => dt.Scope == packageScope && dt.Id == templateId)
                        .Select(dt => dt.Id).FirstOrDefault();
                    if (dbId != null)
                    {
                        documentTemplateIds.Add(dbId);
                    }
                }
            }

            if (documentTemplateIds == null)
            {
                if (Package.DocumentTemplateIds != null)
                {
                    Package.DocumentTemplateIds = null;
                    DocumentTemplatePackages.UpdatePackage(Package);
                }
            }
            else
            {
                if (Package.DocumentTemplateIds == null || Package.DocumentTemplateIds.Count != documentTemplateIds.Count)
                {
                    Package.DocumentTemplateIds = documentTemplateIds;
                    DocumentTemplatePackages.UpdatePackage(Package);
                }
                else
                {
                    if (Package.DocumentTemplateIds.Zip(documentTemplateIds, (a, b) => a != b).Any(r => r))
                    {
                        Package.DocumentTemplateIds = documentTemplateIds;
                        DocumentTemplatePackages.UpdatePackage(Package);
                    }
                }
            }

        }
 private void UpdateDescription(DocumentTemplatePackage Package, string Description)
 {
     if (!string.IsNullOrWhiteSpace(Description))
     {
         var description = Description.Trim();
         if (Package.Description != description)
         {
             Package.Description = description;
             DocumentTemplatePackages.UpdatePackage(Package);
         }
     }
     throw new Exception("Invalid Description");
 }