public bool UserHasEditPermission(User user, Document document)
        {
            if (document.State != DocumentState.Working) return false;

            if (document.WorkMode == DocumentWorkMode.Private.ID)
            {
                if (!document.AuthorReference.IsLoaded) document.AuthorReference.Load();
                if (document.Author.UserID == user.UserID) return true;
                return false;
            }
            if (document.WorkMode == DocumentWorkMode.Collaborative.ID)
            {
                if (!document.AuthorReference.IsLoaded) document.AuthorReference.Load();

                var authorTeam = _userService.GetUserTeam(document.Author);
                var userTeam = _userService.GetUserTeam(user);

                if (authorTeam == null) return false;
                if (userTeam == null) return false;

                if (authorTeam.TeamID == userTeam.TeamID) return true;
            }

            return false;
        }
 public void AddFilesToHeadVersion(Document document, IEnumerable<File> uploadedFiles)
 {
     foreach (var file in uploadedFiles)
     {
         document.HeadVersion.Files.Add(file);
     }
     _documentRepository.Save();
 }
        public void MapToModelEditMode(DocumentEntityViewModel entityViewModel, Document model, Core.Models.Version version)
        {
            model.DistributionMode = entityViewModel.DistributionMode;
            model.WorkMode = entityViewModel.WorkMode;

            version.Classification = entityViewModel.Classification;
            version.Description = entityViewModel.Description;
            version.DocumentNumber = entityViewModel.DocumentNumber;
            version.DocumentTitle = entityViewModel.Title;
            version.Type = entityViewModel.Type;
        }
        public WorkflowRelatedStatus GetUserWorkflowRelatedStatus(User user, Document document)
        {
            //check if document was previously approved
            if (document.State == DocumentState.Finalized) return WorkflowRelatedStatus.NotAllowed;

            if (user.UserLevel == (int)UserLevels.Unassigned || user.UserLevel == (int)UserLevels.Administrator) return WorkflowRelatedStatus.NotAllowed;

            if (user.UserLevel == (int)UserLevels.Director)
            {
                if (!document.DocumentReviewQueueItems.IsLoaded) document.DocumentReviewQueueItems.Load();

                foreach (var item in document.DocumentReviewQueueItems)
                {
                    if (item.ForDirector) return WorkflowRelatedStatus.HighestDecisionMaker;
                }
            }

            //if user is part of management team or is a director, must check the fields ForManagement or ForUser
            //in DocumentReviewQueueItems collection
            if (user.UserLevel == (int)UserLevels.ManagementTeamMember)
            {
                if (!document.DocumentReviewQueueItems.IsLoaded) document.DocumentReviewQueueItems.Load();

                if (document.DocumentReviewQueueItems.Any(item => item.ForManagement))
                {
                    return WorkflowRelatedStatus.DecisionMaker;
                }
            }

            if (!user.DocumentReviewQueueItems.IsLoaded) user.DocumentReviewQueueItems.Load();
            foreach (var item in user.DocumentReviewQueueItems)
            {
                if (!item.DocumentReference.IsLoaded) item.DocumentReference.Load();
                if (item.Document.DocumentID == document.DocumentID) return WorkflowRelatedStatus.DecisionMaker;
            }

            //check if current user is author
            //first check if the document is not already on a workflow
            if (!document.DocumentReviewQueueItems.IsLoaded) document.DocumentReviewQueueItems.Load();
            if (document.DocumentReviewQueueItems.Count == 0)
            {
                if (document.Author.UserID == user.UserID)
                    return (user.UserLevel == (int)UserLevels.Director
                                ? WorkflowRelatedStatus.DirectorAuthor
                                : WorkflowRelatedStatus.Author);
            }

            return WorkflowRelatedStatus.NotAllowed;
        }
        public DocumentEntityViewModel MapToViewModel(Document model)
        {
            var viewModel = new DocumentEntityViewModel();
            viewModel.Id = model.DocumentID;
            viewModel.AuthorID = model.Author.UserID;
            viewModel.AuthorName = model.Author.Fullname;
            viewModel.AuthorLevel = model.Author.UserLevel;
            viewModel.Class = model.Class;
            viewModel.ClassName = DocumentClass.FromId(model.Class).Name;
            viewModel.Classification = model.HeadVersion.Classification;
            viewModel.Department = model.Author.Department;
            viewModel.Description = model.HeadVersion.Description;
            viewModel.DistributionMode = model.DistributionMode;
            viewModel.DistributionModeName = DocumentDitributionMode.FromId(model.DistributionMode).Name;
            viewModel.DocumentNumber = model.HeadVersion.DocumentNumber;
            viewModel.State = model.State;
            viewModel.StateName = DocumentState.FromId(model.State).Name;
            viewModel.Title = model.HeadVersion.DocumentTitle;
            viewModel.Type = model.HeadVersion.Type;
            viewModel.TypeName = DocumentType.FromId(model.HeadVersion.Type).Name;
            viewModel.WorkMode = model.WorkMode;
            viewModel.WorkModeName = DocumentWorkMode.FromId(model.WorkMode).Name;

            if(!model.HeadVersion.Files.IsLoaded) model.HeadVersion.Files.Load();

            if (model.HeadVersion.Files.Count>0)
            {
                var fileList = new List<FileEntityViewModel>();
                foreach (var file in model.HeadVersion.Files)
                {
                    var fileViewModel = new FileEntityViewModel();
                    fileViewModel.Id = file.FileID;
                    fileViewModel.FileName = file.FileName;
                    fileViewModel.FileLocation = file.FileLocation;
                    fileList.Add(fileViewModel);
                }

                viewModel.Files = fileList;
            }

            return viewModel;
        }
        public void ClearDocumentReviewQueue(Document document)
        {
            //using classic ADO.NET to call stored procedure
            //entity framework has problems calling stored procedures that don't return a current Entity
            var connection = new SqlConnection(((EntityConnection)Context.Connection).StoreConnection.ConnectionString);
            var command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "ClearDocumentReviewQueue";
            command.Parameters.AddWithValue("DocumentID", document.DocumentID);

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                connection.Close();
                command.Dispose();
                connection.Dispose();
            }
        }
        public void AddVersion(Document document, Models.Version version,
            IList<int> deletedFilesIds, IDictionary<int, File> changedFiles, IList<File> insertedFiles)
        {
            var headVersion = document.HeadVersion;
            headVersion.IsHeadVersion = false;
            version.IsHeadVersion = true;
            version.VersionNumber = headVersion.VersionNumber + 1;

            headVersion.Files.Load();

            foreach (var file in headVersion.Files)
            {
                if (!deletedFilesIds.Contains(file.FileID) && !changedFiles.Keys.Contains(file.FileID))
                    version.Files.Add(file);
            }

            foreach (var file in insertedFiles)
            {
                version.Files.Add(file);
            }

            SaveDocument(document);
            _fileService.CleanupUnusedFiles();
        }
 private void ClearDocumentReviewQueue(Document document)
 {
     _documentRepository.ClearDocumentReviewQueue(document);
 }
        private WorkflowItem AddWorkflowItem(Document document, User user, WorkflowDecisions workflowDecision, string message)
        {
            WorkflowItem workflowItem = new WorkflowItem();
            workflowItem.Decision = (int)workflowDecision;
            workflowItem.DecisionDate = DateTime.Now;
            workflowItem.Document = document;
            workflowItem.Message = message;
            workflowItem.User = user;

            return workflowItem;
        }
        private void AddDocumentReviewQueueItem(User user, Document document, WorkflowItem workflowItem)
        {
            DocumentReviewQueueItem reviewQueueItem = new DocumentReviewQueueItem();

            if (user.UserLevel == (int)UserLevels.TeamManager)
            {
                reviewQueueItem.ForManagement = true;
                reviewQueueItem.ForDirector = false;
                reviewQueueItem.User = null;
            }
            else if (user.UserLevel == (int)UserLevels.ManagementTeamMember)
            {
                reviewQueueItem.ForManagement = false;
                reviewQueueItem.ForDirector = true;
                reviewQueueItem.User = null;
            }
            else
            {
                reviewQueueItem.ForManagement = false;
                reviewQueueItem.ForDirector = false;
                reviewQueueItem.User = _userService.GetUserSuperior(user);
            }

            reviewQueueItem.WorkflowItem = workflowItem;
            document.DocumentReviewQueueItems.Add(reviewQueueItem);
        }
 public bool UserHasWorkflowDecissionPermission(User user, Document document)
 {
     WorkflowRelatedStatus workflowRelatedStatus = GetUserWorkflowRelatedStatus(user, document);
     return UserHasWorkflowDecissionPermission(user, document, workflowRelatedStatus);
 }
 public void MakeWorkflowDecision(User user, Document document, WorkflowDecisions decision, string message)
 {
     var workflowItem = AddWorkflowItem(document, user, decision, message);
     ClearDocumentReviewQueue(document);
     switch (decision)
     {
         case WorkflowDecisions.Accept:
             document.ApprovedBy = user;
             document.ApprovalDate = DateTime.Now;
             document.State = DocumentState.Finalized;
             break;
         case WorkflowDecisions.Reject:
             document.State = DocumentState.Working;
             break;
         case WorkflowDecisions.SendToSuperior:
             document.State = DocumentState.InReview;
             AddDocumentReviewQueueItem(user, document, workflowItem);
             break;
     }
     SaveDocument(document);
 }
 public void Delete(Document item)
 {
     throw new NotImplementedException();
 }
 public void Add(Document item)
 {
     Context.AddToDocuments(item);
 }
        public bool UserHasWorkflowDecissionPermission(User user, Document document, WorkflowRelatedStatus workflowRelatedStatus)
        {
            if (workflowRelatedStatus == WorkflowRelatedStatus.NotAllowed)
                return false;

            return true;
        }
 public void MapToModel(DocumentEntityViewModel entityViewModel, Document model)
 {
     throw new NotImplementedException();
 }
        public void OverwriteHeadVersion(Document document, Models.Version version,
            IList<int> deletedFilesIds, IDictionary<int, File> changedFiles, IList<File> insertedFiles)
        {
            var headVersion = document.HeadVersion;
            headVersion.Classification = version.Classification;
            headVersion.Description = version.Description;
            headVersion.DocumentNumber = version.DocumentNumber;
            headVersion.DocumentTitle = version.DocumentTitle;
            headVersion.Type = version.Type;
            headVersion.UpdatedAt = DateTime.Now;
            headVersion.VersionNumber = version.VersionNumber;

            foreach (var deletedFileId in deletedFilesIds)
            {
                var file = headVersion.Files.Where(f => f.FileID == deletedFileId).FirstOrDefault();
                headVersion.Files.Remove(file);
            }

            foreach (var changedFileId in changedFiles.Keys)
            {
                var file = headVersion.Files.Where(f => f.FileID == changedFileId).FirstOrDefault();
                headVersion.Files.Remove(file);
                headVersion.Files.Add(changedFiles[changedFileId]);
            }

            foreach (var insertedFile in insertedFiles)
            {
                headVersion.Files.Add(insertedFile);
            }

            SaveDocument(document);
            _fileService.CleanupUnusedFiles();
        }
 public void SaveDocument(Document document)
 {
     _documentRepository.Save();
     document.ReloadHeadVersion();
 }
        public DocumentEntityViewModel[] MapToViewModelCollection(Document[] documents)
        {
            var result = new List<DocumentEntityViewModel>();

            foreach (var document in documents)
            {
                var documentViewModel = MapToViewModel(document);
                result.Add(documentViewModel);
            }

            return result.ToArray();
        }
        public bool UserHasViewPermission(User user, Document document)
        {
            if (document.DistributionMode == DocumentDitributionMode.Public.ID) return true;

            if (UserHasWorkflowDecissionPermission(user, document))
                return true;

            if (!document.AuthorReference.IsLoaded) document.AuthorReference.Load();
            else if (document.DistributionMode == DocumentDitributionMode.Team.ID)
            {
                if (!document.AuthorReference.IsLoaded) document.AuthorReference.Load();
                switch (document.Author.UserLevel)
                {
                    case (int)UserLevels.ManagementTeamMember:
                        if (user.UserLevel == (int)UserLevels.ManagementTeamMember)
                            return true;
                        else
                        {
                            return false;
                        }
                        break;
                    case (int)UserLevels.TeamManager:
                    case (int)UserLevels.WorkshopMember:
                    case (int)UserLevels.WorkshopManager:
                        if (!document.Author.ManagesTeamReference.IsLoaded)
                            document.Author.ManagesTeamReference.Load();
                        Team userTeam = _userService.GetUserTeam(user);
                        Team authorTeam = _userService.GetUserTeam(document.Author);
                        if (userTeam == null) return false;

                        if (userTeam.TeamID == authorTeam.TeamID) return true;

                        return false;
                        break;
                    default:
                        return false;
                }
            }
            else if (document.DistributionMode == DocumentDitributionMode.Protected.ID)
            {
                if (user.UserLevel == (int)UserLevels.ManagementTeamMember &&
                    document.Author.UserLevel == (int)UserLevels.ManagementTeamMember)
                    return true;

                Workshop userWorkshop = _userService.GetUserWorkshop(user);
                Workshop authorWorkshop = _userService.GetUserWorkshop(user);

                if (userWorkshop == null) return false;
                if (authorWorkshop == null) return false;

                if (userWorkshop.WorkshopID == authorWorkshop.WorkshopID) return true;

                return false;
            }

            if (document.Author.UserID == user.UserID) return true;
            return false;
        }