maintenance unify document generation ui
This commit is contained in:
@@ -386,30 +386,8 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
if (string.IsNullOrEmpty(DocumentTemplateId))
|
||||
throw new ArgumentNullException(nameof(DocumentTemplateId));
|
||||
|
||||
var device = Database.Devices.Find(id);
|
||||
if (device != null)
|
||||
{
|
||||
var documentTemplate = Database.DocumentTemplates.Find(DocumentTemplateId);
|
||||
if (documentTemplate != null)
|
||||
{
|
||||
var timeStamp = DateTime.Now;
|
||||
Stream pdf;
|
||||
using (var generationState = DocumentState.DefaultState())
|
||||
{
|
||||
pdf = documentTemplate.GeneratePdf(Database, device, UserService.CurrentUser, timeStamp, generationState);
|
||||
}
|
||||
Database.SaveChanges();
|
||||
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, device.SerialNumber, timeStamp));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Document Template Id", nameof(DocumentTemplateId));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Serial Number", nameof(id));
|
||||
}
|
||||
// Obsolete: Use API\DocumentTemplate\Generate instead
|
||||
return RedirectToAction(MVC.API.DocumentTemplate.Generate(DocumentTemplateId, id));
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Device.Actions.GenerateDocuments)]
|
||||
@@ -420,33 +398,8 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
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));
|
||||
}
|
||||
// Obsolete: Use API\DocumentTemplatePackage\Generate instead
|
||||
return RedirectToAction(MVC.API.DocumentTemplatePackage.Generate(DocumentTemplatePackageId, id));
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Device.Show)]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Documents;
|
||||
using Disco.Services;
|
||||
using Disco.Services.Authorization;
|
||||
using Disco.Services.Documents;
|
||||
@@ -691,6 +692,51 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
return File(pdf, "application/pdf", string.Format("{0}_Bulk_{1:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, timeStamp));
|
||||
}
|
||||
|
||||
public virtual ActionResult Generate(string id, string TargetId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
if (string.IsNullOrWhiteSpace(TargetId))
|
||||
throw new ArgumentNullException(nameof(TargetId));
|
||||
|
||||
// get template
|
||||
var template = Database.DocumentTemplates.Find(id);
|
||||
if (template == null)
|
||||
throw new ArgumentException("Invalid document template id", nameof(id));
|
||||
|
||||
// validate authorization
|
||||
switch (template.Scope)
|
||||
{
|
||||
case DocumentTemplate.DocumentTemplateScopes.Device:
|
||||
Authorization.Require(Claims.Device.Actions.GenerateDocuments);
|
||||
break;
|
||||
case DocumentTemplate.DocumentTemplateScopes.Job:
|
||||
Authorization.Require(Claims.Job.Actions.GenerateDocuments);
|
||||
break;
|
||||
case DocumentTemplate.DocumentTemplateScopes.User:
|
||||
Authorization.Require(Claims.User.Actions.GenerateDocuments);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException("Unknown DocumentType Scope");
|
||||
}
|
||||
|
||||
// resolve target
|
||||
var target = template.ResolveScopeTarget(Database, TargetId);
|
||||
if (target == null)
|
||||
throw new ArgumentException("Target not found", nameof(TargetId));
|
||||
|
||||
// generate document
|
||||
var timestamp = DateTime.Now;
|
||||
var document = default(Stream);
|
||||
using (var state = DocumentState.DefaultState())
|
||||
{
|
||||
document = template.GeneratePdf(Database, target, UserService.CurrentUser, timestamp, state);
|
||||
}
|
||||
Database.SaveChanges();
|
||||
|
||||
return File(document, "application/pdf", $"{template.Id}_{target.AttachmentReferenceId.Replace('\\', '_')}_{timestamp:yyyyMMdd-HHmmss}.pdf");
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Config.DocumentTemplate.Delete)]
|
||||
public virtual ActionResult Delete(string id, bool? redirect = false)
|
||||
{
|
||||
|
||||
@@ -8,6 +8,8 @@ using Disco.Services.Users;
|
||||
using Disco.Services.Web;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
@@ -384,6 +386,48 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
return File(pdf, "application/pdf", string.Format("{0}_Bulk_{1:yyyyMMdd-HHmmss}.pdf", package.Id, timeStamp));
|
||||
}
|
||||
|
||||
public virtual ActionResult Generate(string id, string TargetId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
if (string.IsNullOrWhiteSpace(TargetId))
|
||||
throw new ArgumentNullException(nameof(TargetId));
|
||||
|
||||
var package = DocumentTemplatePackages.GetPackage(id);
|
||||
if (package == null)
|
||||
throw new ArgumentException("Invalid document template package id", nameof(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 document type scope");
|
||||
}
|
||||
|
||||
// resolve target
|
||||
var target = package.ResolveScopeTarget(Database, TargetId);
|
||||
if (target == null)
|
||||
throw new ArgumentException("Target not found", nameof(TargetId));
|
||||
|
||||
var timestamp = DateTime.Now;
|
||||
var document = default(Stream);
|
||||
using (var state = DocumentState.DefaultState())
|
||||
{
|
||||
document = package.GeneratePdfPackage(Database, target, UserService.CurrentUser, timestamp, state);
|
||||
}
|
||||
Database.SaveChanges();
|
||||
|
||||
return File(document, "application/pdf", $"{package.Id}_{target.AttachmentReferenceId.Replace('\\', '_')}_{timestamp:yyyyMMdd-HHmmss}.pdf");
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Config.DocumentTemplate.Delete)]
|
||||
public virtual ActionResult Delete(string id, bool? redirect = false)
|
||||
{
|
||||
|
||||
@@ -2093,30 +2093,9 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
throw new ArgumentOutOfRangeException(nameof(id));
|
||||
if (string.IsNullOrEmpty(DocumentTemplateId))
|
||||
throw new ArgumentNullException(nameof(DocumentTemplateId));
|
||||
var job = Database.Jobs.Find(id);
|
||||
if (job != null)
|
||||
{
|
||||
var documentTemplate = Database.DocumentTemplates.Find(DocumentTemplateId);
|
||||
if (documentTemplate != null)
|
||||
{
|
||||
var timeStamp = DateTime.Now;
|
||||
Stream pdf;
|
||||
using (var generationState = DocumentState.DefaultState())
|
||||
{
|
||||
pdf = documentTemplate.GeneratePdf(Database, job, CurrentUser, timeStamp, generationState);
|
||||
}
|
||||
Database.SaveChanges();
|
||||
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, job.Id, timeStamp));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Job Id", "id");
|
||||
}
|
||||
|
||||
// Obsolete: Use API\DocumentTemplate\Generate instead
|
||||
return RedirectToAction(MVC.API.DocumentTemplate.Generate(DocumentTemplateId, id.ToString()));
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Job.Actions.GenerateDocuments)]
|
||||
@@ -2127,34 +2106,8 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
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));
|
||||
}
|
||||
// Obsolete: Use API\DocumentTemplatePackage\Generate instead
|
||||
return RedirectToAction(MVC.API.DocumentTemplatePackage.Generate(DocumentTemplatePackageId, id.ToString()));
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Job.Properties.DeviceHeldLocation)]
|
||||
|
||||
@@ -166,40 +166,12 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
if (string.IsNullOrEmpty(DocumentTemplateId))
|
||||
throw new ArgumentNullException(nameof(DocumentTemplateId));
|
||||
|
||||
id = ActiveDirectory.ParseDomainAccountId(id, Domain);
|
||||
var userId = ActiveDirectory.ParseDomainAccountId(id, Domain);
|
||||
|
||||
var user = Database.Users.Find(id);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
// Try importing the user
|
||||
user = UserService.GetUser(id, Database, true);
|
||||
}
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
var documentTemplate = Database.DocumentTemplates.Find(DocumentTemplateId);
|
||||
if (documentTemplate != null)
|
||||
{
|
||||
var timeStamp = DateTime.Now;
|
||||
Stream pdf;
|
||||
using (var generationState = DocumentState.DefaultState())
|
||||
{
|
||||
pdf = documentTemplate.GeneratePdf(Database, user, UserService.CurrentUser, timeStamp, generationState);
|
||||
}
|
||||
Database.SaveChanges();
|
||||
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, user.UserId, timeStamp));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid User Id", "id");
|
||||
}
|
||||
// Obsolete: Use API\DocumentTemplate\Generate instead
|
||||
return RedirectToAction(MVC.API.DocumentTemplate.Generate(DocumentTemplateId, userId));
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.User.Actions.GenerateDocuments)]
|
||||
public virtual ActionResult GeneratePdfPackage(string id, string Domain, string DocumentTemplatePackageId)
|
||||
{
|
||||
@@ -208,35 +180,10 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
if (string.IsNullOrEmpty(DocumentTemplatePackageId))
|
||||
throw new ArgumentNullException(nameof(DocumentTemplatePackageId));
|
||||
|
||||
id = ActiveDirectory.ParseDomainAccountId(id, Domain);
|
||||
var userId = 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));
|
||||
}
|
||||
// Obsolete: Use API\DocumentTemplatePackage\Generate instead
|
||||
return RedirectToAction(MVC.API.DocumentTemplatePackage.Generate(DocumentTemplatePackageId, userId));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user