Exemplo n.º 1
0
        public JsonResult GetFlatDocument(string document_id)
        {
            Guid id;

            if (Guid.TryParse(document_id, out id))
            {
                //Grab the desired file
                Marine_Permit_Palace.Models.Document document = _DocumentService.Get(id);
                if (document != null)
                {
                    MemoryStream file = new MemoryStream(System.IO.File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist", "documents", document.TemplateName)));
                    return(Json(new
                    {
                        file = Convert.ToBase64String(file.ToArray())
                    }));
                }
                else
                {
                    return(Json(new Result()
                    {
                        status_code = 404,
                        result = "Failure",
                        reason = "No document found"
                    }));
                }
            }
            else
            {
                return(Json(new Result()
                {
                    reason = "Invalid guid format",
                    result = "Failure",
                    status_code = 400
                }));
            }
        }
Exemplo n.º 2
0
        public JsonResult SaveFile([FromBody] SaveDocumentObject document)
        {
            if (document != null && !string.IsNullOrEmpty(document.submitted_file_id))
            {
                Guid SubmittedDocId;
                if (!Guid.TryParse(document.submitted_file_id, out SubmittedDocId))
                {
                    return(Json(new { result = "Failure", reason = "Incorrect GUID format" }));
                }

                var  user                      = _UserManager.GetUserAsync(User).Result;
                Guid sub_file_guid             = Guid.Empty;
                SubmittedDocument SubmittedDoc = null;
                if (Guid.TryParse(document.submitted_file_id, out sub_file_guid))
                {
                    SubmittedDoc = _SubmittedDocumentService.Get(sub_file_guid);


                    SubmittedDoc.Name = document.name;
                    if (SubmittedDoc.IsCompleted)
                    {
                        return(Json(new Result()
                        {
                            reason = "Document is no longer editable. Document was completed.",
                            result = "Failure",
                            status_code = 400
                        }));
                    }
                    if (SubmittedDoc.IsEditLocked)
                    {
                        return(Json(new Result()
                        {
                            reason = "Document is not editable.",
                            result = "Failure",
                            status_code = 400
                        }));
                    }
                }
                else
                {
                    return(Json(new { result = "Failure", reason = "Incorrect GUID format", status_code = 400 }));
                }


                Marine_Permit_Palace.Models.Document RefDocument = _DocumentSerivce.Get(SubmittedDoc.DocumentId);
                if (RefDocument == null)
                {
                    return(Json(new { result = "Failure", reason = "No Document with that ID exists" }));
                }


                var AssignedUsers = _DocumentAsigneeService.GetByDocument(SubmittedDoc.IdSubmittedDocument);

                var UserPermissions = AssignedUsers.FirstOrDefault(e => e.IdAssigneeId == user.Id);
                if (UserPermissions == null || !UserPermissions.IsAllowedEdit)
                {
                    return(Json(new Result()
                    {
                        reason = "User Does not have permission to edit this document.",
                        result = "Failure",
                        status_code = 401
                    }));
                }
                List <DocumentCheckBoxField> CBFields = document.document_meta
                                                        .Where(e => e.field_type == "Checkbox")
                                                        .Select(e => new DocumentCheckBoxField()
                {
                    FormValue             = (e.value == "On"),
                    IdFormName            = e.field_name,
                    IsCompleted           = (e.value == "On"),
                    IdSubmittedDocumentId = SubmittedDoc.IdSubmittedDocument
                }).ToList();

                List <DocumentFormField> FMFields = document.document_meta
                                                    .Where(e => e.field_type == "Text")
                                                    .Select(e => new DocumentFormField()
                {
                    FormValue             = e.value,
                    IdFormName            = e.field_name,
                    IsCompleted           = !string.IsNullOrEmpty(e.value),
                    IdSubmittedDocumentId = SubmittedDoc.IdSubmittedDocument
                }).ToList();

                List <DocumentSignatureField> SigFields = document.document_meta
                                                          .Where(e => e.field_type == "Signature")
                                                          .Select(e => new DocumentSignatureField()
                {
                    IdSubmittedDocumentId = SubmittedDoc.IdSubmittedDocument,
                    IdFormName            = e.field_name,
                    IsCompleted           = !string.IsNullOrEmpty(e.value),
                    SignatureData         = !string.IsNullOrEmpty(e.value) ? new DataStorage()
                    {
                        Data = Convert.FromBase64String(e.value),
                        Type = AppDataType.SIGNATURE
                    } : null
                }).ToList();

                try { _DocumentSignatureService.SaveWithDocumentSignatureData(SigFields); } catch (Exception ex)
                {
                    return(Json(new Result()
                    {
                        reason = ex.Message,
                        result = "Failure",
                        status_code = 500
                    }));
                }
                try { _DocumentCheckBoxService.SaveAllCheckBoxFields(CBFields); }catch (Exception ex)
                {
                    return(Json(new Result()
                    {
                        reason = ex.Message,
                        result = "Failure",
                        status_code = 500
                    }));
                }
                try { _DocumentFormFieldService.SaveAllFormFields(FMFields); }catch (Exception ex)
                {
                    return(Json(new Result()
                    {
                        reason = ex.Message,
                        result = "Failure",
                        status_code = 500
                    }));
                }
                if (document.is_completed)
                {
                    if (UserPermissions.IsAllowedSubmit)
                    {
                        SubmittedDoc.IsCompleted      = true;
                        SubmittedDoc.DateCompletedUtc = DateTime.UtcNow;
                        SubmittedDoc.IsEditLocked     = true;
                    }
                    else
                    {
                        return(Json(new Result()
                        {
                            reason = "Document saved, but user does not have permission to submit this document.",
                            result = "Failure",
                            status_code = 401
                        }));
                    }
                }
                SubmittedDoc = _SubmittedDocumentService.Update(SubmittedDoc);
                return(Json(new Result()
                {
                    result = "Success", reason = SubmittedDoc.IdSubmittedDocument.ToString()
                }));
            }

            else
            {
                return(Json(new Result()
                {
                    result = "Failure", status_code = 400
                }));
            }
        }
Exemplo n.º 3
0
        public async Task <JsonResult> GetSubmittedDocumentMeta(string submitted_document_id)
        {
            try
            {
                Guid id;
                if (Guid.TryParse(submitted_document_id, out id))
                {
                    //Grab the desired file
                    SubmittedDocument SubmittedDocument = _SubmittedDocumentService.GetPopulated(id);

                    ApplicationUser user = await _UserManager.GetUserAsync(User);

                    var DocAssignees    = _DocumentAsigneeService.GetByDocument(SubmittedDocument.IdSubmittedDocument);
                    var UserPermissions = DocAssignees.FirstOrDefault(e => e.IdAssigneeId == user.Id);
                    if (UserPermissions == null || !UserPermissions.IsActive)
                    {
                        return(Json(new Result()
                        {
                            reason = "User does not have permission to view this document",
                            result = "Failure",
                            status_code = 401
                        }));
                    }


                    Marine_Permit_Palace.Models.Document document = _DocumentSerivce.Get(SubmittedDocument.DocumentId);
                    MemoryStream PDF_Mem = new MemoryStream();
                    MemoryStream file    = new MemoryStream(System.IO.File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist", "documents", document.TemplateName)));
                    file.CopyTo(PDF_Mem);
                    using (PdfReader reader = new PdfReader(System.IO.File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist", "documents", document.TemplateName))))
                        using (PdfStamper stamper = new PdfStamper(reader, PDF_Mem, '\0', false))
                        {
                            stamper.FormFlattening = false;
                            AcroFields pdfFormFields = stamper.AcroFields;

                            if (user != null)
                            {
                                //populate all the known fields based on user information
                                AutoFillManager.AutoFillBasedOnUser(user, pdfFormFields);
                            }

                            string RequestingUserId = user.Id;

                            List <string> FieldNames = pdfFormFields.Fields.Select(e => e.Key).ToList();
                            //List<DocumentMeta> JsonDocument = new List<DocumentMeta>();
                            List <PDFPage> pages = new List <PDFPage>();
                            foreach (string field in FieldNames)
                            {
                                var Position = pdfFormFields.GetFieldPositions(field).FirstOrDefault();
                                if (Position == null)
                                {
                                    continue;
                                }

                                if (pages.FirstOrDefault(e => e.page_number == Position.page) == null)
                                {
                                    pages.Add(new PDFPage()
                                    {
                                        page_number = Position.page, page = reader.GetPageSize(Position.page), document_meta = new List <DocumentMeta>()
                                    });
                                }

                                int indexOfPage = pages.FindIndex(e => e.page_number == Position.page);


                                string field_type;
                                switch (reader.AcroFields.GetFieldType(field))
                                {
                                case AcroFields.FIELD_TYPE_CHECKBOX:
                                    field_type = ("Checkbox");
                                    break;

                                case AcroFields.FIELD_TYPE_COMBO:
                                    field_type = ("Combobox");
                                    break;

                                case AcroFields.FIELD_TYPE_LIST:
                                    field_type = ("List");
                                    break;

                                case AcroFields.FIELD_TYPE_NONE:
                                    field_type = ("None");
                                    break;

                                case AcroFields.FIELD_TYPE_PUSHBUTTON:
                                    field_type = ("Pushbutton");
                                    break;

                                case AcroFields.FIELD_TYPE_RADIOBUTTON:
                                    field_type = ("Radiobutton");
                                    break;

                                case AcroFields.FIELD_TYPE_SIGNATURE:
                                    field_type = ("Signature");
                                    break;

                                case AcroFields.FIELD_TYPE_TEXT:
                                    field_type = ("Text");
                                    break;

                                default:
                                    field_type = ("?");
                                    break;
                                }
                                string value, disabled_message = null;
                                bool   IsAllowedToEdit = true;

                                FieldData field_data    = SubmittedDocument[field];
                                var       AssigneeDodID = "";
                                if (field_data == null)
                                {
                                    value = pdfFormFields.GetField(field);
                                }
                                else
                                {
                                    value = field_data.value;
                                    if (!string.IsNullOrEmpty(field_data.user_assigned))
                                    {
                                        var OtherUser = await _UserManager.FindByIdAsync(field_data.user_assigned);

                                        if (OtherUser != null)
                                        {
                                            AssigneeDodID   = OtherUser.UserName;
                                            IsAllowedToEdit = RequestingUserId == field_data.user_assigned;
                                            if (!IsAllowedToEdit)
                                            {
                                                disabled_message = $"This field is assigned to {OtherUser.Rank}. {OtherUser.LastName}, {OtherUser.FirstName}";
                                            }
                                        }
                                    }
                                }

                                pages[indexOfPage].document_meta.Add(new DocumentMeta()
                                {
                                    field_name       = field,
                                    field_position   = Position,
                                    value            = value,
                                    field_type       = field_type,
                                    assigned_to      = AssigneeDodID,
                                    disabled_message = disabled_message,
                                    is_disabled      = !IsAllowedToEdit
                                });
                            }


                            return(Json(new
                            {
                                result = "Success",
                                status_code = 200,
                                pages
                            }));
                        }
                }
                else
                {
                    return(Json(new Result("Failure", "Incorrect Guid Format", 406)));
                }
            }
            catch (Exception ex)
            {
                return(Json(new Result()
                {
                    reason = $"Something went wrong while reading the file: See Exception...  {ex.Message}",
                    result = "Failure",
                    status_code = 500
                }));
            }
        }
Exemplo n.º 4
0
        public async Task <JsonResult> GetDocumentMeta(string document_id) // Save the
        {
            try
            {
                Guid id;
                if (Guid.TryParse(document_id, out id))
                {
                    //Grab the desired file
                    Marine_Permit_Palace.Models.Document document = _DocumentSerivce.Get(id);
                    MemoryStream PDF_Mem = new MemoryStream();
                    MemoryStream file    = new MemoryStream(System.IO.File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist", "documents", document.TemplateName)));
                    file.CopyTo(PDF_Mem);
                    using (PdfReader reader = new PdfReader(System.IO.File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist", "documents", document.TemplateName))))
                        using (PdfStamper stamper = new PdfStamper(reader, PDF_Mem, '\0', false))
                        {
                            stamper.FormFlattening = false;
                            AcroFields pdfFormFields = stamper.AcroFields;
                            //AcroFields.FieldPosition

                            ApplicationUser user = await _UserManager.GetUserAsync(User);

                            if (user != null)
                            {
                                //populate all the known fields based on user information
                                //AutoFillManager.AutoFillBasedOnUser(user, pdfFormFields);
                            }

                            List <PDFPage> pages      = new List <PDFPage>();
                            List <string>  FieldNames = pdfFormFields.Fields.Select(e => e.Key).ToList();

                            foreach (string field in FieldNames)
                            {
                                var Position = pdfFormFields.GetFieldPositions(field).FirstOrDefault();
                                if (Position == null)
                                {
                                    continue;
                                }

                                if (pages.FirstOrDefault(e => e.page_number == Position.page) == null)
                                {
                                    pages.Add(new PDFPage()
                                    {
                                        page_number = Position.page, page = reader.GetPageSize(Position.page), document_meta = new List <DocumentMeta>()
                                    });
                                }

                                int indexOfPage = pages.FindIndex(e => e.page_number == Position.page);

                                string value = pdfFormFields.GetField(field);

                                string field_type;
                                switch (reader.AcroFields.GetFieldType(field))
                                {
                                case AcroFields.FIELD_TYPE_CHECKBOX:
                                    field_type = ("Checkbox");
                                    break;

                                case AcroFields.FIELD_TYPE_COMBO:
                                    field_type = ("Combobox");
                                    break;

                                case AcroFields.FIELD_TYPE_LIST:
                                    field_type = ("List");
                                    break;

                                case AcroFields.FIELD_TYPE_NONE:
                                    field_type = ("None");
                                    break;

                                case AcroFields.FIELD_TYPE_PUSHBUTTON:
                                    field_type = ("Pushbutton");
                                    break;

                                case AcroFields.FIELD_TYPE_RADIOBUTTON:
                                    field_type = ("Radiobutton");
                                    break;

                                case AcroFields.FIELD_TYPE_SIGNATURE:
                                    field_type = ("Signature");
                                    break;

                                case AcroFields.FIELD_TYPE_TEXT:
                                    field_type = ("Text");
                                    break;

                                default:
                                    field_type = ("?");
                                    break;
                                }
                                pages[indexOfPage].document_meta.Add(new DocumentMeta()
                                {
                                    field_name     = field,
                                    field_position = Position,
                                    value          = value,
                                    field_type     = field_type
                                });
                            }

                            return(Json(new
                            {
                                result = "Success",
                                status_code = 200,
                                pages
                            }));
                        }
                }
                else
                {
                    return(Json(new Result("Failure", "Incorrect Guid Format", 406)));
                }
            }
            catch (Exception ex)
            {
                return(Json(new Result()
                {
                    reason = $"Something went wrong while reading the file: See Exception...  {ex.Message}",
                    result = "Failure",
                    status_code = 500
                }));
            }
        }
Exemplo n.º 5
0
        public async Task <JsonResult> AssignDocument([FromBody] AssignDocumentData data)
        {
            if (data != null)
            {
                //Assign the document to the specified users. With the specified field values.

                var assigning_user = await _UserManager.GetUserAsync(User);

                Marine_Permit_Palace.Models.Document RefDocument = _DocumentService.Get(data.document_id);
                if (RefDocument == null)
                {
                    return(Json(new { result = "Failure", reason = "No Document with that ID exists" }));
                }
                SubmittedDocument SubmittedDoc = null;

                SubmittedDoc = new SubmittedDocument()
                {
                    Document   = RefDocument,
                    DocumentId = data.document_id,
                    Name       = data.document_name,
                    AssignerId = assigning_user.Id
                };
                SubmittedDoc = _SubmittedDocumentService.Add(SubmittedDoc);

                if (SubmittedDoc != null)
                {
                    Dictionary <string, string> AssignedUsers = new Dictionary <string, string>();
                    foreach (var user in data.assignees)
                    {
                        ApplicationUser assignee = await _UserManager.FindByNameAsync(user.dod_id.ToString());

                        if (assignee == null)
                        {
                            continue;
                        }
                        AssignedUsers.Add(assignee.UserName, assignee.Id);
                        DocumentAssigneeIntermediate documentAssigneeIntermediate = new DocumentAssigneeIntermediate()
                        {
                            IdActiveDocumentId    = SubmittedDoc.IdSubmittedDocument,
                            IdAssigneeId          = assignee.Id,
                            IsAllowedApprove      = user.is_allowed_approve,
                            IsAllowedAssignFields = user.is_allowed_assign,
                            IsAllowedEdit         = user.is_allowed_edit,
                            IsAllowedSubmit       = user.is_allowed_submit
                        };
                        try { _AssigneeIntermediate.Add(documentAssigneeIntermediate, false); }
                        catch (Exception ex)
                        {
                            return(Json(new Result()
                            {
                                reason = "Database save failure. Please try again later.",
                                result = "Failure",
                                status_code = 500
                            }));
                        }
                    }



                    List <DocumentCheckBoxField> CBFields = data.document_meta
                                                            .Where(e => e.field_type == "Checkbox")
                                                            .Select(e => new DocumentCheckBoxField()
                    {
                        FormValue             = (e.value == "On"),
                        IdFormName            = e.field_name,
                        IdSubmittedDocumentId = SubmittedDoc.IdSubmittedDocument,
                        AssigneeId            = string.IsNullOrEmpty(e.assigned_to) ? null : AssignedUsers[e.assigned_to]
                    }).ToList();

                    List <DocumentFormField> FMFields = data.document_meta
                                                        .Where(e => e.field_type == "Text")
                                                        .Select(e => new DocumentFormField()
                    {
                        FormValue             = e.value,
                        IdFormName            = e.field_name,
                        IdSubmittedDocumentId = SubmittedDoc.IdSubmittedDocument,
                        AssigneeId            = string.IsNullOrEmpty(e.assigned_to) ? null : AssignedUsers[e.assigned_to]
                    }).ToList();

                    List <DocumentSignatureField> SigFields = data.document_meta
                                                              .Where(e => e.field_type == "Signature")
                                                              .Select(e => new DocumentSignatureField()
                    {
                        IdSubmittedDocumentId = SubmittedDoc.IdSubmittedDocument,
                        IdFormName            = e.field_name,
                        AssigneeId            = string.IsNullOrEmpty(e.assigned_to) ? null : AssignedUsers[e.assigned_to]
                    }).ToList();

                    try { _DocSig.Add(SigFields, false); }
                    catch (Exception ex)
                    {
                        return(Json(new Result()
                        {
                            reason = ex.Message,
                            result = "Failure",
                            status_code = 500
                        }));
                    }
                    try { _DocCheck.Add(CBFields); }
                    catch (Exception ex)
                    {
                        return(Json(new Result()
                        {
                            reason = ex.Message,
                            result = "Failure",
                            status_code = 500
                        }));
                    }
                    try { _DocForm.Add(FMFields); }
                    catch (Exception ex)
                    {
                        return(Json(new Result()
                        {
                            reason = ex.Message,
                            result = "Failure",
                            status_code = 500
                        }));
                    }

                    try { _DatabaseService.SaveChanges(); }
                    catch (Exception ex)
                    {
                        return(Json(new Result()
                        {
                            reason = "Database save failure. Please try again later.",
                            result = "Failure",
                            status_code = 500
                        }));
                    }

                    return(Json(new Result()
                    {
                        reason = "Document Created Successfully.",
                        result = "Success",
                        status_code = 200
                    }));
                }
                else
                {
                    return(Json(new Result()
                    {
                        reason = "No document was found",
                        result = "Failure",
                        status_code = 404
                    }));
                }
            }
            else
            {
                return(Json(new Result()
                {
                    reason = "No data was passed", result = "Failure", status_code = 400
                }));
            }
        }