Exemplo n.º 1
0
        /// <summary>
        /// Adds new document to system.
        /// </summary>
        /// <param name="documentDto">The document</param>
        /// <param name="ownerId">The owner id = logged user id.</param>
        /// <returns></returns>
        public DocumentDTO SaveNewDocument(DocumentDTO documentDto, int ownerId)
        {
            if (documentDto == null)
            {
                throw new ArgumentNullException("documentDto");
            }

            documentDto.CreatedAt  = DateTime.Now;
            documentDto.IsArchived = false;
            documentDto.IsLocked   = false;
            documentDto.IsValid    = true;

            tDocument result = Mapper.Map <tDocument>(documentDto);

            result.Version   = 1;
            result.Guid      = Guid.NewGuid();
            result.Extension = result.Extension.TrimStart('.');

            result.tKeywords = Mapper.Map <List <tKeyword> >(documentDto.Keywords.ToList());

            foreach (DocumentShareDTO share in documentDto.DocumentShares)
            {
                tUserDocument ud = new tUserDocument();
                ud.IsReading             = false;
                ud.IsWriting             = false;
                ud.UserId                = share.UserId;
                ud.DocumentId            = documentDto.Id;
                ud.DocumentAccessRightId = (int)share.AccessRight;

                result.tUserDocuments.Add(ud);
            }

            tUserDocument owner = new tUserDocument()
            {
                IsReading             = false,
                IsWriting             = false,
                DocumentAccessRightId = (int)DocumentAccessRightCodes.Owner,
                UserId = ownerId
            };

            using (DMSDataBaseEntities context = new DMSDataBaseEntities())
            {
                result = context.tDocuments.Add(result);
                context.SaveChanges();

                owner.DocumentId = result.Id;
                result.tUserDocuments.Add(owner);
                context.SaveChanges();
            }

            AddAdministratorsToDocument(result.Id, ownerId);
            documentDto.Id = result.Id;
            return(documentDto);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Opens document for writing for current user if document is not locked and no one currently reads it.
        /// </summary>
        /// <param name="documentId">The document id.</param>
        /// <param name="loggedUserId">The logged user id.</param>
        /// <returns></returns>
        public bool TryOpenDocumentForWriting(int documentId, int loggedUserId)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (DMSDataBaseEntities context = new DMSDataBaseEntities())
                {
                    tDocument document = context.tDocuments.Find(documentId);
                    if (document.IsLocked)
                    {
                        scope.Complete();
                        return(false);
                    }
                    int readers = context.tUserDocuments.Where(u => u.DocumentId == documentId && u.IsReading).Count();
                    if (readers > 0)
                    {
                        scope.Complete();
                        return(false);
                    }

                    tUserDocument ud = context.tUserDocuments.Where(u => u.DocumentId == documentId && u.UserId == loggedUserId).Single();
                    ud.IsWriting      = true;
                    document.IsLocked = true;
                    context.SaveChanges();
                    scope.Complete();
                    return(true);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Toggles value of archived flag for specified document.
 /// </summary>
 /// <param name="documentDto">The document dto.</param>
 /// <returns>The modified document dto.</returns>
 public DocumentDTO ToggleArchivedFlag(DocumentDTO documentDto)
 {
     documentDto.IsArchived = !documentDto.IsArchived;
     using (DMSDataBaseEntities context = new DMSDataBaseEntities())
     {
         tDocument document = context.tDocuments.Find(documentDto.Id);
         Mapper.Map(documentDto, document);
         document.Version++;
         context.SaveChanges();
         return(documentDto);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Saves the new comment.
        /// </summary>
        /// <param name="commentDto">The comment dto.</param>
        /// <returns></returns>
        public CommentDTO SaveNewComment(CommentDTO commentDto)
        {
            commentDto.CreatedAt = DateTime.Now;
            tComment comment = Mapper.Map <tComment>(commentDto);

            using (DMSDataBaseEntities context = new DMSDataBaseEntities())
            {
                context.tComments.Add(comment);
                context.SaveChanges();
            }

            return(commentDto);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds new document version to document, which takes next hierarchy id.
        /// </summary>
        /// <param name="document">The document id.</param>
        /// <returns>Returns the filepath of the new document version.</returns>
        public string AddNewDocumentVersion(DocumentDTO document)
        {
            using (DMSDataBaseEntities context = new DMSDataBaseEntities())
            {
                tDocumentVersion lastVersion = context.tDocumentVersions.Where(u => u.DocumentId == document.Id).OrderByDescending(u => u.HierarchyId).FirstOrDefault();

                tDocumentVersion documentVersion = new tDocumentVersion();
                documentVersion.Guid        = Guid.NewGuid();
                documentVersion.CreatedAt   = DateTime.Now;
                documentVersion.HierarchyId = (lastVersion == null) ? 1 : lastVersion.HierarchyId + 1;
                documentVersion.DocumentId  = document.Id;
                documentVersion.Version     = 1;
                documentVersion.FilePath    = documentVersion.Guid + document.Extension;
                context.tDocumentVersions.Add(documentVersion);
                context.SaveChanges();
                return(documentVersion.FilePath);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Updates document title, keywords and sharing with users.
        /// </summary>
        /// <param name="documentDto">The document dto.</param>
        /// <param name="loggedUserId">The logged user id.</param>
        public void UpdateDocumentData(DocumentDTO documentDto, long loggedUserId)
        {
            if (documentDto == null)
            {
                throw new ArgumentNullException("documentDto");
            }

            // operators, not owner, not logged user
            Expression <Func <tUserDocument, bool> > oldShareRestrictions = x => x.DocumentId == documentDto.Id &&
                                                                            x.DocumentAccessRightId != (int)DocumentAccessRightCodes.Owner && x.UserId != loggedUserId && x.tUser.AuthorizationRoleId == (int)AuthorizationRoleCodes.Operator;

            using (DMSDataBaseEntities context = new DMSDataBaseEntities())
            {
                tDocument document = context.tDocuments.Find(documentDto.Id);
                document.tKeywords.Clear();
                document.tKeywords = Mapper.Map <List <tKeyword> >(documentDto.Keywords.ToList());

                document.LastModifiedAt = DateTime.Now;
                document.Version++;
                document.Title = documentDto.Title;

                IEnumerable <tUserDocument> oldShared = context.tUserDocuments.Where(oldShareRestrictions);
                foreach (tUserDocument ud in oldShared)
                {
                    context.tUserDocuments.Remove(ud);
                }

                foreach (DocumentShareDTO share in documentDto.DocumentShares)
                {
                    tUserDocument ud = new tUserDocument();
                    ud.IsReading             = false;
                    ud.IsWriting             = false;
                    ud.UserId                = share.UserId;
                    ud.DocumentId            = documentDto.Id;
                    ud.DocumentAccessRightId = (int)share.AccessRight;

                    document.tUserDocuments.Add(ud);
                }

                context.SaveChanges();
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gives modify access right to all administrators of system except owner, to the document.
        /// </summary>
        /// <param name="documentId">The document id</param>
        private void AddAdministratorsToDocument(int documentId, int ownerId)
        {
            using (DMSDataBaseEntities context = new DMSDataBaseEntities())
            {
                IEnumerable <tUser> users = context.tUsers.Where(u => u.AuthorizationRoleId == (int)AuthorizationRoleCodes.Administrator && u.Id != ownerId);
                foreach (tUser user in users)
                {
                    tUserDocument ud = new tUserDocument()
                    {
                        DocumentId            = documentId,
                        IsReading             = false,
                        IsWriting             = false,
                        DocumentAccessRightId = (int)DocumentAccessRightCodes.Modify,
                        UserId = user.Id
                    };

                    context.tUserDocuments.Add(ud);
                }
                context.SaveChanges();
            }
        }