Feature: Document Template Packages

Document Templates can be grouped into a package and generated on-demand
in the same was as individual document templates. Packages can be
generated in bulk.
This commit is contained in:
Gary Sharp
2016-11-14 01:21:23 +11:00
parent ef8df08e29
commit aca037ecf8
75 changed files with 8008 additions and 2078 deletions
@@ -6,6 +6,7 @@ using Disco.Services;
using Disco.Services.Authorization;
using Disco.Services.Devices.Exporting;
using Disco.Services.Devices.Importing;
using Disco.Services.Documents;
using Disco.Services.Interop;
using Disco.Services.Interop.ActiveDirectory;
using Disco.Services.Users;
@@ -380,9 +381,10 @@ namespace Disco.Web.Areas.API.Controllers
public virtual ActionResult GeneratePdf(string id, string DocumentTemplateId)
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(DocumentTemplateId))
throw new ArgumentNullException("AttachmentTypeId");
throw new ArgumentNullException(nameof(DocumentTemplateId));
var device = Database.Devices.Find(id);
if (device != null)
{
@@ -400,12 +402,49 @@ namespace Disco.Web.Areas.API.Controllers
}
else
{
throw new ArgumentException("Invalid Document Template Id", "id");
throw new ArgumentException("Invalid Document Template Id", nameof(DocumentTemplateId));
}
}
else
{
throw new ArgumentException("Invalid Serial Number", "id");
throw new ArgumentException("Invalid Serial Number", nameof(id));
}
}
[DiscoAuthorize(Claims.Device.Actions.GenerateDocuments)]
public virtual ActionResult GeneratePdfPackage(string id, string DocumentTemplatePackageId)
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(DocumentTemplatePackageId))
throw new ArgumentNullException(nameof(DocumentTemplatePackageId));
var device = Database.Devices.Find(id);
if (device != null)
{
var package = DocumentTemplatePackages.GetPackage(DocumentTemplatePackageId);
if (package != null)
{
if (package.Scope != AttachmentTypes.Device)
throw new ArgumentException("This package cannot be generated from the Device Scope", nameof(DocumentTemplatePackageId));
var timeStamp = DateTime.Now;
Stream pdf;
using (var generationState = DocumentState.DefaultState())
{
pdf = package.GeneratePdfPackage(Database, device, UserService.CurrentUser, timeStamp, generationState);
}
Database.SaveChanges();
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", package.Id, device.SerialNumber, timeStamp));
}
else
{
throw new ArgumentException("Invalid Document Template Package Id", nameof(DocumentTemplatePackageId));
}
}
else
{
throw new ArgumentException("Invalid Serial Number", nameof(id));
}
}
@@ -568,7 +568,7 @@ namespace Disco.Web.Areas.API.Controllers
}
[DiscoAuthorize(Claims.Config.DocumentTemplate.UndetectedPages)]
public virtual ActionResult ImporterUndetectedFile(string id, Nullable<bool> Source, Nullable<bool> Thumbnail)
public virtual ActionResult ImporterUndetectedFile(string id, bool? Source, bool? Thumbnail)
{
if (!string.IsNullOrEmpty(id))
{
@@ -659,7 +659,7 @@ namespace Disco.Web.Areas.API.Controllers
}
[DiscoAuthorize(Claims.Config.DocumentTemplate.BulkGenerate)]
public virtual ActionResult BulkGenerate(string id, string DataIds = null)
public virtual ActionResult BulkGenerate(string id, string DataIds = null, bool InsertBlankPage = false)
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
@@ -686,13 +686,13 @@ namespace Disco.Web.Areas.API.Controllers
var dataIds = DataIds.Split(new string[] { Environment.NewLine, ",", ";" }, StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()).Where(d => !string.IsNullOrEmpty(d)).ToArray();
var timeStamp = DateTime.Now;
var pdf = documentTemplate.GeneratePdfBulk(Database, UserService.CurrentUser, timeStamp, dataIds);
var pdf = documentTemplate.GeneratePdfBulk(Database, UserService.CurrentUser, timeStamp, InsertBlankPage, dataIds);
return File(pdf, "application/pdf", string.Format("{0}_Bulk_{1:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, timeStamp));
}
[DiscoAuthorize(Claims.Config.DocumentTemplate.Delete)]
public virtual ActionResult Delete(string id, Nullable<bool> redirect = false)
public virtual ActionResult Delete(string id, bool? redirect = false)
{
try
{
@@ -0,0 +1,422 @@
using Disco.BI.Extensions;
using Disco.Models.Repository;
using Disco.Models.Services.Documents;
using Disco.Services;
using Disco.Services.Authorization;
using Disco.Services.Documents;
using Disco.Services.Users;
using Disco.Services.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Disco.Web.Areas.API.Controllers
{
public partial class DocumentTemplatePackageController : AuthorizedDatabaseController
{
const string pDescription = "description";
const string pScope = "scope";
const string pFilterExpression = "filterexpression";
const string pOnGenerateExpression = "ongenerateexpression";
const string pIsHidden = "ishidden";
const string pInsertBlankPages = "insertblankpages";
[DiscoAuthorize(Claims.Config.DocumentTemplate.Configure)]
public virtual ActionResult Update(string id, string key, string value = null, bool redirect = false)
{
try
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
var package = DocumentTemplatePackages.GetPackage(id);
if (package != null)
{
switch (key.ToLower())
{
case pDescription:
UpdateDescription(package, value);
break;
case pScope:
UpdateScope(package, value);
break;
case pFilterExpression:
Authorization.Require(Claims.Config.DocumentTemplate.ConfigureFilterExpression);
UpdateFilterExpression(package, value);
break;
case pOnGenerateExpression:
UpdateOnGenerateExpression(package, value);
break;
case pIsHidden:
UpdateIsHidden(package, value);
break;
case pInsertBlankPages:
UpdateInsertBlankPages(package, value);
break;
default:
throw new Exception("Invalid Update Key");
}
}
else
{
throw new Exception("Invalid Document Template Package Id");
}
if (redirect)
return RedirectToAction(MVC.Config.DocumentTemplate.ShowPackage(package.Id));
else
return Json("OK", JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
if (redirect)
throw;
else
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
}
}
#region Update Shortcut Methods
[DiscoAuthorize(Claims.Config.DocumentTemplate.Configure)]
public virtual ActionResult UpdateDescription(string id, string Description = null, bool redirect = false)
{
return Update(id, pDescription, Description, redirect);
}
[DiscoAuthorize(Claims.Config.DocumentTemplate.Configure)]
public virtual ActionResult UpdateDocumentTemplates(string id, List<string> DocumentTemplates = null, bool redirect = false)
{
try
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
var package = DocumentTemplatePackages.GetPackage(id);
if (package == null)
throw new ArgumentException("Invalid Document Template Package Id", nameof(id));
UpdateDocumentTemplates(package, DocumentTemplates);
if (redirect)
return RedirectToAction(MVC.Config.DocumentTemplate.ShowPackage(package.Id));
else
return Json("OK", JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
if (redirect)
throw;
else
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
}
}
[DiscoAuthorizeAll(Claims.Config.DocumentTemplate.Configure, Claims.Config.DocumentTemplate.ConfigureFilterExpression)]
public virtual ActionResult UpdateFilterExpression(string id, string FilterExpression = null, bool redirect = false)
{
return Update(id, pFilterExpression, FilterExpression, redirect);
}
[DiscoAuthorizeAll(Claims.Config.DocumentTemplate.Configure, Claims.Config.DocumentTemplate.ConfigureFilterExpression)]
public virtual ActionResult UpdateOnGenerateExpression(string id, string OnGenerateExpression = null, bool redirect = false)
{
return Update(id, pOnGenerateExpression, OnGenerateExpression, redirect);
}
[DiscoAuthorize(Claims.Config.DocumentTemplate.Configure)]
public virtual ActionResult UpdateIsHidden(string id, string IsHidden = null, bool redirect = false)
{
return Update(id, pIsHidden, IsHidden, redirect);
}
[DiscoAuthorize(Claims.Config.DocumentTemplate.Configure)]
public virtual ActionResult UpdateInsertBlankPages(string id, string InsertBlankPages = null, bool redirect = false)
{
return Update(id, pInsertBlankPages, InsertBlankPages, redirect);
}
[DiscoAuthorize(Claims.Config.DocumentTemplate.Configure)]
public virtual ActionResult UpdateScope(string id, string Scope = null, bool redirect = false)
{
return Update(id, pScope, Scope, redirect);
}
[DiscoAuthorize(Claims.Config.DocumentTemplate.Configure)]
public virtual ActionResult UpdateJobSubTypes(string id, List<string> JobSubTypes = null, bool redirect = false)
{
try
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
var package = DocumentTemplatePackages.GetPackage(id);
if (package == null)
throw new ArgumentException("Invalid Document Template Package Id", nameof(id));
UpdateJobSubTypes(package, JobSubTypes);
if (redirect)
return RedirectToAction(MVC.Config.DocumentTemplate.ShowPackage(package.Id));
else
return Json("OK", JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
if (redirect)
throw;
else
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
}
}
#endregion
#region Update Properties
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");
}
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 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 UpdateFilterExpression(DocumentTemplatePackage Package, string FilterExpression)
{
string expression;
if (string.IsNullOrWhiteSpace(FilterExpression))
expression = null;
else
expression = FilterExpression.Trim();
if (Package.FilterExpression != expression)
{
Package.FilterExpression = expression;
DocumentTemplatePackages.UpdatePackage(Package);
Package.FilterExpressionInvalidateCache();
}
}
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 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 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);
}
}
}
}
#endregion
#region Actions
[DiscoAuthorize(Claims.Config.DocumentTemplate.BulkGenerate)]
public virtual ActionResult BulkGenerate(string id, string DataIds = null, bool InsertBlankPage = false)
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(DataIds))
throw new ArgumentNullException(nameof(DataIds));
var package = DocumentTemplatePackages.GetPackage(id);
if (package == null)
throw new ArgumentException("Invalid Document Template Package Id", "id");
switch (package.Scope)
{
case AttachmentTypes.Device:
Authorization.Require(Claims.Device.Actions.GenerateDocuments);
break;
case AttachmentTypes.Job:
Authorization.Require(Claims.Job.Actions.GenerateDocuments);
break;
case AttachmentTypes.User:
Authorization.Require(Claims.User.Actions.GenerateDocuments);
break;
default:
throw new InvalidOperationException("Unknown DocumentType Scope");
}
var dataIds = DataIds.Split(new string[] { Environment.NewLine, ",", ";" }, StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()).Where(d => !string.IsNullOrEmpty(d)).ToList();
var timeStamp = DateTime.Now;
var pdf = package.GeneratePdfPackageBulk(Database, UserService.CurrentUser, timeStamp, InsertBlankPage, dataIds);
return File(pdf, "application/pdf", string.Format("{0}_Bulk_{1:yyyyMMdd-HHmmss}.pdf", package.Id, timeStamp));
}
[DiscoAuthorize(Claims.Config.DocumentTemplate.Delete)]
public virtual ActionResult Delete(string id, bool? redirect = false)
{
try
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
var package = DocumentTemplatePackages.GetPackage(id);
if (package == null)
throw new ArgumentException("Invalid Document Template Package Id", nameof(id));
if (package != null)
{
DocumentTemplatePackages.RemovePackage(package.Id);
if (redirect.HasValue && redirect.Value)
return RedirectToAction(MVC.Config.DocumentTemplate.Index(null));
else
return Json("OK", JsonRequestBehavior.AllowGet);
}
throw new Exception("Invalid Document Template Package Id");
}
catch (Exception ex)
{
if (redirect.HasValue && redirect.Value)
throw;
else
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
}
}
#endregion
}
}
@@ -5,6 +5,7 @@ using Disco.Models.Services.Job;
using Disco.Models.Services.Jobs.JobLists;
using Disco.Services;
using Disco.Services.Authorization;
using Disco.Services.Documents;
using Disco.Services.Interop;
using Disco.Services.Jobs.JobLists;
using Disco.Services.Jobs.Statistics;
@@ -2086,13 +2087,13 @@ namespace Disco.Web.Areas.API.Controllers
#endregion
[DiscoAuthorize(Claims.Job.Actions.GenerateDocuments)]
public virtual ActionResult GeneratePdf(string id, string DocumentTemplateId)
public virtual ActionResult GeneratePdf(int id, string DocumentTemplateId)
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
if (id <= 0)
throw new ArgumentOutOfRangeException(nameof(id));
if (string.IsNullOrEmpty(DocumentTemplateId))
throw new ArgumentNullException("AttachmentTypeId");
var job = Database.Jobs.Find(int.Parse(id));
throw new ArgumentNullException(nameof(DocumentTemplateId));
var job = Database.Jobs.Find(id);
if (job != null)
{
var documentTemplate = Database.DocumentTemplates.Find(DocumentTemplateId);
@@ -2118,6 +2119,44 @@ namespace Disco.Web.Areas.API.Controllers
}
}
[DiscoAuthorize(Claims.Job.Actions.GenerateDocuments)]
public virtual ActionResult GeneratePdfPackage(int id, string DocumentTemplatePackageId)
{
if (id <= 0)
throw new ArgumentOutOfRangeException(nameof(id));
if (string.IsNullOrEmpty(DocumentTemplatePackageId))
throw new ArgumentNullException(nameof(DocumentTemplatePackageId));
var job = Database.Jobs.Find(id);
if (job != null)
{
var package = DocumentTemplatePackages.GetPackage(DocumentTemplatePackageId);
if (package != null)
{
if (package.Scope != AttachmentTypes.Job)
throw new ArgumentException("This package cannot be generated from the Job Scope", nameof(DocumentTemplatePackageId));
var timeStamp = DateTime.Now;
Stream pdf;
using (var generationState = DocumentState.DefaultState())
{
pdf = package.GeneratePdfPackage(Database, job, UserService.CurrentUser, timeStamp, generationState);
}
Database.SaveChanges();
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", package.Id, job.Id, timeStamp));
}
else
{
throw new ArgumentException("Invalid Document Template Package Id", nameof(DocumentTemplatePackageId));
}
}
else
{
throw new ArgumentException("Invalid Job Id", nameof(id));
}
}
[DiscoAuthorize(Claims.Job.Properties.DeviceHeldLocation)]
public virtual ActionResult DeviceHeldLocations()
{
@@ -1,7 +1,9 @@
using Disco.BI.Extensions;
using Disco.Models.Repository;
using Disco.Models.Services.Documents;
using Disco.Services;
using Disco.Services.Authorization;
using Disco.Services.Documents;
using Disco.Services.Interop;
using Disco.Services.Interop.ActiveDirectory;
using Disco.Services.Users;
@@ -160,9 +162,9 @@ namespace Disco.Web.Areas.API.Controllers
public virtual ActionResult GeneratePdf(string id, string Domain, string DocumentTemplateId)
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(DocumentTemplateId))
throw new ArgumentNullException("AttachmentTypeId");
throw new ArgumentNullException(nameof(DocumentTemplateId));
id = ActiveDirectory.ParseDomainAccountId(id, Domain);
@@ -191,6 +193,44 @@ namespace Disco.Web.Areas.API.Controllers
throw new ArgumentException("Invalid User Id", "id");
}
}
[DiscoAuthorize(Claims.User.Actions.GenerateDocuments)]
public virtual ActionResult GeneratePdfPackage(string id, string Domain, string DocumentTemplatePackageId)
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(DocumentTemplatePackageId))
throw new ArgumentNullException(nameof(DocumentTemplatePackageId));
id = ActiveDirectory.ParseDomainAccountId(id, Domain);
var user = Database.Users.Find(id);
if (user != null)
{
var package = DocumentTemplatePackages.GetPackage(DocumentTemplatePackageId);
if (package != null)
{
if (package.Scope != AttachmentTypes.User)
throw new ArgumentException("This package cannot be generated from the User Scope", nameof(DocumentTemplatePackageId));
var timeStamp = DateTime.Now;
Stream pdf;
using (var generationState = DocumentState.DefaultState())
{
pdf = package.GeneratePdfPackage(Database, user, UserService.CurrentUser, timeStamp, generationState);
}
Database.SaveChanges();
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", package.Id, user.UserId, timeStamp));
}
else
{
throw new ArgumentException("Invalid Document Template Package Id", nameof(DocumentTemplatePackageId));
}
}
else
{
throw new ArgumentException("Invalid User Id", nameof(id));
}
}
}
}