using BeWoPlanerMobil.Models; using BeWoPlanerMobil.Service; using BeWoPlanerMobil.Util; using BS.Shared; using BS.Shared.DataContracts; using System; using System.IO; using System.Linq; using System.Web.Mvc; namespace BeWoPlanerMobil.Controllers { public class PersonController : AbstractBaseController, IDocumentsController { [Authorize] public override string SaveCollapsibleStatus(bool isOpen, string identifier) { return SaveCollapsibleStatusToModel(Model, isOpen, identifier); } public PersonController() { } private PersonModel _Model; public PersonModel Model { get { if(!MobileSessionFacade.IsUserLoggedIn()) { Logout(); return null; } if(_Model is null) { _Model = new PersonModel(); InitModel(_Model); InitViewModel(); } return _Model; } } [Authorize] public ActionResult Person(long? id) { try { if(!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return Logout(); } if(id.HasValue) { LoadPersonToModel(id); } } catch(Exception exception) { Log.Info("Exception: " + exception.Message + "\n" + exception.StackTrace); if(exception.InnerException != null) { Log.Info("\n\nInnerException: " + exception.InnerException.Message + "\n" + exception.InnerException.StackTrace); } throw; } return View(Model); } [Authorize] [HttpPost] public ActionResult SelectPerson(FormCollection formCollection) { if(!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { TempData[TempDataConstants.DoLogoutKey] = true; return Logout(); } var selectedPersonOidString = formCollection["SelectedPersonOid"]; if(long.TryParse(selectedPersonOidString, out var selectedPersonOid)) { LoadPersonToModel(selectedPersonOid); } return RedirectToActionPermanent("Person", new { id = selectedPersonOid }); } private void InitViewModel() { if(!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { Logout(); return; } if(Model.PersonSessionModel.SelectedPersonOid.HasValue) { Model.SelectedPerson = CustomerService.LoadPerson(Model.PersonSessionModel.SelectedPersonOid.Value); Model.Umfeld = CustomerService.GetEnvironmentCustomerForPerson(Model.PersonSessionModel.SelectedPersonOid.Value); } Model.Persons = CustomerService.GetAllAuthorizedCompactPersons(); if(Model.SelectedPerson?.PersonOid != null) { Model.FolderTree = OperationsService.GetFolderTree(TableID.Person, Model.SelectedPerson.PersonOid.Value); } } private void LoadPersonToModel(long? personOid) { if(personOid is null) { return; } if(personOid.Value > 0 && false == Model.Persons.Any(p => p.PersonOid.Equals(personOid.Value))) { return; } if(personOid == -1) { Model.PersonSessionModel.ResetValues(); } else { if(Model.SelectedPersonOid?.Equals(personOid) ?? false) { Model.PersonSessionModel.ResetValues(); } Model.Umfeld = CustomerService.GetEnvironmentCustomerForPerson(personOid.Value); Model.SelectedPersonOid = personOid; } } #region Dokumente [Authorize] public ActionResult SaveDescriptionChange(string descriptionText, long? fileOid) { if(Model is null || !MobileSessionFacade.IsUserLoggedIn()) { TempData[TempDataConstants.DoLogoutKey] = true; return PartialView("DocumentPartial"); } if(Model.ObjectOid is null || fileOid is null) { TempData[TempDataConstants.ErrorAlertPopupMessage] = "Es ist leider ein unbekannter Fehler aufgetreten.

Bitte kontaktieren Sie einen Administrator."; return PartialView("DocumentPartial", Model); } var file = DownloadService.GetFileAttachmentForMobile(fileOid.Value, MobileSessionFacade.Tenant); if(file != null) { file.Description = descriptionText; OperationsService.UpdateFileAttachment(file); } Model.FolderTree = OperationsService.GetFolderTree(Model.ObjectTid, Model.ObjectOid.Value); var fileTemp = DownloadService.GetFileAttachmentForMobile(fileOid.Value, MobileSessionFacade.Tenant); return PartialView("DocumentPartial", Model); } [Authorize] public FileResult DownloadFile(long fileOid) { if(Model is null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { TempData[TempDataConstants.DoLogoutKey] = true; return null; } var fileAttachment = DownloadService.GetFileAttachmentForMobile(fileOid, MobileSessionFacade.Tenant); return File(fileAttachment.BinaryData, "application/octet-stream", fileAttachment.FileName); } [Authorize] public ActionResult DeleteFile(long? fileOid, long? fileVersion) { if(Model is null || !MobileSessionFacade.IsUserLoggedIn()) { TempData[TempDataConstants.DoLogoutKey] = true; return PartialView("DocumentPartial"); } if(fileOid is null || fileVersion is null || Model.ObjectOid is null) { return PartialView("DocumentPartial", Model); } OperationsService.DeleteFileAttachment(fileOid.Value, fileVersion.Value); Model.FolderTree = OperationsService.GetFolderTree(Model.ObjectTid, Model.ObjectOid.Value); return PartialView("DocumentPartial", Model); } [Authorize] [HttpPost] public ActionResult UploadFile(FormCollection formCollection) { if(Model is null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { TempData[TempDataConstants.DoLogoutKey] = true; return PartialView("DocumentPartial"); } if(Request.Files.Count > 0) { var folderOidAsString = formCollection["folder-oid-input"]; var description = formCollection["file-description-input"]; var folderOid = string.IsNullOrWhiteSpace(folderOidAsString) ? null : (long?)long.Parse(folderOidAsString); var file = Request.Files[0]; if(file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); byte[] fileData; using(var binaryReader = new BinaryReader(file.InputStream)) { fileData = binaryReader.ReadBytes(file.ContentLength); } var fileAttachment = new FileAttachmentDC { BeWoFolderOid = folderOid, Description = description, FileName = fileName, ObjectTid = Model.ObjectTid, ObjectOid = Model.ObjectOid, BinaryData = fileData, FileAttachmentType = FileAttachmentType.NotSpecified, InsertedBy = $"{Model.LoggedInEmployee.FirstName} {Model.LoggedInEmployee.LastName}", InsertedOn = DateTime.Now }; OperationsService.InsertFileAttachment(fileAttachment); } } return RedirectToActionPermanent("Person", new { id = Model.SelectedPersonOid, model = Model }); } [Authorize] public ActionResult DeleteFolder(long? folderOid) { if(Model is null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { TempData[TempDataConstants.DoLogoutKey] = true; return PartialView("DocumentPartial"); } if(folderOid is null) { return PartialView("DocumentPartial", Model); } var folder = OperationsService.GetFolderById(folderOid.Value); if( folder?.BeWoFolderOid != null && folder.BeWoFolderVersion.HasValue && Model.ObjectOid.HasValue && folder.ObjectOid.Equals(Model.ObjectOid.Value) && folder.ObjectTid == Model.ObjectTid ) { OperationsService.DeleteFolder(folder.BeWoFolderOid.Value, folder.BeWoFolderVersion.Value); Model.FolderTree = OperationsService.GetFolderTree(Model.ObjectTid, Model.ObjectOid.Value); } return PartialView("DocumentPartial", Model); } [Authorize] public ActionResult RenameFolder(string newName, long folderOid) { if(Model is null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { TempData[TempDataConstants.DoLogoutKey] = true; return PartialView("DocumentPartial"); } if(string.IsNullOrWhiteSpace(newName)) { TempData[TempDataConstants.ErrorMessageKey] = "Ungültiger Name"; return PartialView("DocumentPartial", Model); } var folder2Rename = OperationsService.GetFolderById(folderOid); if(folder2Rename != null && Model.ObjectOid.HasValue && folder2Rename.ObjectTid == Model.ObjectTid && folder2Rename.ObjectOid == Model.ObjectOid.Value) { folder2Rename.Name = newName; OperationsService.UpdateFolder(folder2Rename); Model.FolderTree = OperationsService.GetFolderTree(Model.ObjectTid, Model.ObjectOid.Value); } return PartialView("DocumentPartial", Model); } [Authorize] public ActionResult AddNewFolder(string folderName, long? parentFolderOid) { if(Model is null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { TempData[TempDataConstants.DoLogoutKey] = true; return PartialView("DocumentPartial"); } if(Model.ObjectOid is null) { return PartialView("DocumentPartial", Model); } if(string.IsNullOrWhiteSpace(folderName)) { folderName = "Neuer Ordner"; } var folder = new BeWoFolderDC { Name = folderName, ObjectTid = Model.ObjectTid, ObjectOid = Model.ObjectOid, ParentBeWoFolderOid = parentFolderOid }; OperationsService.InsertNewFolder(folder); Model.FolderTree = OperationsService.GetFolderTree(Model.ObjectTid, Model.ObjectOid.Value); return PartialView("DocumentPartial", Model); } #endregion } }