Example #1
0
        public IHttpActionResult UpdateDoc(int id, DocDO doc)
        {
            using (var transaction = this.unitOfWork.BeginTransaction())
            {
                UnitUser unitUser = this.unitOfWork.DbContext.Set<UnitUser>().FirstOrDefault(e => e.UserId == this.userContext.UserId);

                ClassificationPermission editPermission = this.classificationRepository.GetByAlias("Edit");
                ClassificationPermission readPermission = this.classificationRepository.GetByAlias("Read");

                bool hasEditPermission =
                    this.classificationRepository.HasPermission(unitUser.UnitId, id, editPermission.ClassificationPermissionId) &&
                    this.classificationRepository.HasPermission(unitUser.UnitId, id, readPermission.ClassificationPermissionId);

                if (!hasEditPermission)
                {
                    return BadRequest("Not enough permissions!");
                }

                DateTime currentDate = DateTime.Now;
                var oldDoc = this.docRepository.Find(id,
                        e => e.DocCorrespondents,
                        e => e.DocFiles.Select(df => df.DocFileOriginType),
                        e => e.DocFiles.Select(df => df.DocFileType),
                        e => e.DocUnits);

                if (oldDoc == null)
                {
                    return NotFound();
                }

                oldDoc.EnsureForProperVersion(doc.Version);

                oldDoc.DocSourceTypeId = doc.DocSourceTypeId;
                oldDoc.DocSubject = doc.DocSubject;
                oldDoc.DocBody = doc.DocBody;
                oldDoc.CorrRegNumber = doc.CorrRegNumber;
                oldDoc.CorrRegDate = doc.CorrRegDate;
                oldDoc.AssignmentTypeId = doc.AssignmentTypeId;
                oldDoc.AssignmentDate = doc.AssignmentDate;
                oldDoc.AssignmentDeadline = doc.AssignmentDeadline;
                oldDoc.ModifyDate = DateTime.Now;
                oldDoc.ModifyUserId = userContext.UserId;

                #region DocUnits

                List<DocUnitRole> docUnitRoles = this.unitOfWork.DbContext.Set<DocUnitRole>().ToList();

                doc.DocUnitsFrom.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "From").DocUnitRoleId; });
                doc.DocUnitsTo.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "To").DocUnitRoleId; });
                doc.DocUnitsImportedBy.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "ImportedBy").DocUnitRoleId; });
                doc.DocUnitsMadeBy.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "MadeBy").DocUnitRoleId; });
                doc.DocUnitsCCopy.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "CCopy").DocUnitRoleId; });
                doc.DocUnitsInCharge.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "InCharge").DocUnitRoleId; });
                doc.DocUnitsControlling.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "Controlling").DocUnitRoleId; });
                doc.DocUnitsReaders.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "Readers").DocUnitRoleId; });
                doc.DocUnitsEditors.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "Editors").DocUnitRoleId; });
                doc.DocUnitsRegistrators.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "Registrators").DocUnitRoleId; });

                var allDocUnits = doc.DocUnitsTo.Union(
                    doc.DocUnitsFrom.Union(
                        doc.DocUnitsImportedBy.Union(
                            doc.DocUnitsMadeBy.Union(
                                doc.DocUnitsCCopy.Union(
                                    doc.DocUnitsInCharge.Union(
                                        doc.DocUnitsControlling.Union(
                                            doc.DocUnitsReaders.Union(
                                                doc.DocUnitsEditors.Union(
                                                    doc.DocUnitsRegistrators)))))))));

                var listDocUnits = oldDoc.DocUnits.ToList();

                for (var i = 0; i < listDocUnits.Count; i++)
                {
                    var matching = allDocUnits
                        .Where(e => e.NomValueId == listDocUnits[i].UnitId && e.ForeignKeyId == listDocUnits[i].DocUnitRoleId)
                        .ToList();

                    if (matching.Any())
                    {
                        matching.ForEach(e => { e.IsProcessed = true; });
                    }
                    else
                    {
                        oldDoc.DeleteDocUnit(listDocUnits[i], this.userContext);
                    }
                }
                foreach (var du in allDocUnits.Where(e => !e.IsProcessed))
                {
                    oldDoc.CreateDocUnit(du.NomValueId, du.ForeignKeyId, this.userContext);
                }

                #endregion

                #region DocCorrespondents

                var listDocCorrespondents = oldDoc.DocCorrespondents.ToList();

                for (var i = 0; i < listDocCorrespondents.Count; i++)
                {
                    var matching = doc.DocCorrespondents
                        .Where(e => e.NomValueId == listDocCorrespondents[i].CorrespondentId)
                        .ToList();

                    if (matching.Any())
                    {
                        matching.ForEach(e => { e.IsProcessed = true; });
                    }
                    else
                    {
                        oldDoc.DeleteDocCorrespondent(listDocCorrespondents[i], this.userContext);
                    }
                }
                foreach (var du in doc.DocCorrespondents.Where(e => !e.IsProcessed))
                {
                    oldDoc.CreateDocCorrespondent(du.NomValueId, this.userContext);
                }

                #endregion

                #region DocFiles

                DocFile editable = oldDoc.DocFiles.FirstOrDefault(e => e.DocFileOriginTypeId.HasValue && e.DocFileOriginType.Alias == "EditableFile");

                if (editable != null)
                {
                    byte[] content = null;

                    if (editable.DocFileType.DocTypeUri == "Checklist")
                    {
                        string contentStr = JsonConvert.SerializeObject(doc.JObject);
                        content = System.Text.Encoding.UTF8.GetBytes(contentStr);
                    }
                    else //doc is rio object
                    {
                        content = RioObjectUtils.GetBytesFromJObject(editable.DocFileType.DocTypeUri, doc.JObject);
                    }

                    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbContext"].ConnectionString))
                    {
                        connection.Open();
                        using (var blobWriter = new BlobWriter(connection))
                        using (var stream = blobWriter.OpenStream())
                        {
                            stream.Write(content, 0, content.Length);
                            editable.DocFileContentId = blobWriter.GetBlobKey();
                        }
                    }
                }

                List<DocFileDO> allDocFiles = doc.DocFiles;
                List<DocFileType> docFileTypes = this.unitOfWork.DbContext.Set<DocFileType>().ToList();

                foreach (var file in allDocFiles.Where(e => !e.IsNew && e.IsDeleted && e.DocFileId.HasValue))
                {
                    oldDoc.DeleteDocFile(file.DocFileId.Value, this.userContext);
                }

                foreach (var file in allDocFiles.Where(e => !e.IsNew && !e.IsDeleted && e.IsDirty && e.DocFileId.HasValue))
                {
                    var docFileType = docFileTypes.FirstOrDefault(e => e.Extention == Path.GetExtension(file.File.Name));

                    if (docFileType == null)
                    {
                        docFileType = docFileTypes.FirstOrDefault(e => e.Alias == "UnknownBinary");
                    }

                    oldDoc.UpdateDocFile(file.DocFileId.Value, file.DocFileKindId, docFileType.DocFileTypeId, file.Name, file.File.Name, "", file.File.Key, userContext);
                }

                foreach (var file in allDocFiles.Where(e => e.IsNew && !e.IsDeleted))
                {
                    var docFileType = docFileTypes.FirstOrDefault(e => e.Extention == Path.GetExtension(file.File.Name));

                    if (docFileType == null)
                    {
                        docFileType = docFileTypes.FirstOrDefault(e => e.Alias == "UnknownBinary");
                    }

                    oldDoc.CreateDocFile(file.DocFileKindId, docFileType.DocFileTypeId, file.Name, file.File.Name, String.Empty, file.File.Key, userContext);
                }

                #endregion

                this.unitOfWork.Save();

                this.docRepository.ExecSpSetDocTokens(docId: oldDoc.DocId);
                this.docRepository.ExecSpSetDocUnitTokens(docId: oldDoc.DocId);

                transaction.Commit();

                return Ok();
            }
        }
Example #2
0
        public IHttpActionResult UpdateTechDoc(int id, string docVersion, DocDO doc)
        {
            using (var transaction = this.unitOfWork.BeginTransaction())
            {
                UnitUser unitUser = this.unitOfWork.DbContext.Set<UnitUser>().FirstOrDefault(e => e.UserId == this.userContext.UserId);

                ClassificationPermission editTechPermission = this.classificationRepository.GetByAlias("EditTech");
                ClassificationPermission readPermission = this.classificationRepository.GetByAlias("Read");

                bool hasEditTechPermission =
                    this.classificationRepository.HasPermission(unitUser.UnitId, id, editTechPermission.ClassificationPermissionId) &&
                    this.classificationRepository.HasPermission(unitUser.UnitId, id, readPermission.ClassificationPermissionId);

                if (!hasEditTechPermission)
                {
                    return BadRequest("Not enough permissions!");
                }

                Doc oldDoc = this.docRepository.Find(id,
                    e => e.DocType,
                    e => e.DocUnits);

                if (oldDoc == null)
                {
                    return NotFound();
                }

                if (doc.UnregisterDoc)
                {
                    oldDoc.Unregister(this.userContext);
                }

                if (doc.PrimaryRegisterIndexId.HasValue)
                {
                    DocRegister docRegister = this.unitOfWork.DbContext.Set<DocRegister>()
                        .FirstOrDefault(e => e.RegisterIndexId == doc.PrimaryRegisterIndexId.Value);

                    if (docRegister != null)
                    {
                        oldDoc.DocRegisterId = docRegister.DocRegisterId;
                    }
                    else
                    {
                        oldDoc.DocRegisterId = this.docRepository.spGetDocRegisterIdByRegisterIndexId(doc.PrimaryRegisterIndexId.Value);
                    }
                }

                if (oldDoc.DocTypeId != doc.DocTypeId || oldDoc.DocDirectionId != doc.DocDirectionId)
                {
                    #region DocUnits

                    List<DocUnitRole> docUnitRoles = this.unitOfWork.DbContext.Set<DocUnitRole>().ToList();

                    doc.DocUnitsFrom.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "From").DocUnitRoleId; });
                    doc.DocUnitsTo.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "To").DocUnitRoleId; });
                    doc.DocUnitsImportedBy.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "ImportedBy").DocUnitRoleId; });
                    doc.DocUnitsMadeBy.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "MadeBy").DocUnitRoleId; });
                    doc.DocUnitsCCopy.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "CCopy").DocUnitRoleId; });
                    doc.DocUnitsInCharge.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "InCharge").DocUnitRoleId; });
                    doc.DocUnitsControlling.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "Controlling").DocUnitRoleId; });
                    doc.DocUnitsReaders.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "Readers").DocUnitRoleId; });
                    doc.DocUnitsEditors.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "Editors").DocUnitRoleId; });
                    doc.DocUnitsRegistrators.ForEach(e => { e.ForeignKeyId = docUnitRoles.Single(p => p.Alias == "Registrators").DocUnitRoleId; });

                    var allDocUnits = doc.DocUnitsTo.Union(
                        doc.DocUnitsFrom.Union(
                            doc.DocUnitsImportedBy.Union(
                                doc.DocUnitsMadeBy.Union(
                                    doc.DocUnitsCCopy.Union(
                                        doc.DocUnitsInCharge.Union(
                                            doc.DocUnitsControlling.Union(
                                                doc.DocUnitsReaders.Union(
                                                    doc.DocUnitsEditors.Union(
                                                        doc.DocUnitsRegistrators)))))))));

                    var listDocUnits = oldDoc.DocUnits.ToList();

                    for (var i = 0; i < listDocUnits.Count; i++)
                    {
                        var matching = allDocUnits
                            .Where(e => e.NomValueId == listDocUnits[i].UnitId && e.ForeignKeyId == listDocUnits[i].DocUnitRoleId)
                            .ToList();

                        if (matching.Any())
                        {
                            matching.ForEach(e => { e.IsProcessed = true; });
                        }
                        else
                        {
                            oldDoc.DeleteDocUnit(listDocUnits[i], this.userContext);
                        }
                    }
                    foreach (var du in allDocUnits.Where(e => !e.IsProcessed))
                    {
                        oldDoc.CreateDocUnit(du.NomValueId, du.ForeignKeyId, this.userContext);
                    }

                    #endregion

                    oldDoc.DocTypeId = doc.DocTypeId;
                    oldDoc.DocDirectionId = doc.DocDirectionId;
                }

                oldDoc.ReceiptOrder = doc.ReceiptOrder;

                this.unitOfWork.Save();

                this.docRepository.ExecSpSetDocTokens(docId: oldDoc.DocId);
                this.docRepository.ExecSpSetDocUnitTokens(docId: oldDoc.DocId);

                transaction.Commit();

                return Ok();
            }
        }
Example #3
0
        public IHttpActionResult GetDoc(int id)
        {
            DateTime currentDate = DateTime.Now;

            UnitUser unitUser = this.unitOfWork.DbContext.Set<UnitUser>()
                .Include(e => e.User)
                .Include(e => e.Unit)
                .FirstOrDefault(e => e.UserId == this.userContext.UserId);

            List<vwDocUser> vwDocUsers = this.docRepository.GetvwDocUsersForDocByUnitId(id, unitUser);

            ClassificationPermission readPermission = this.unitOfWork.DbContext.Set<ClassificationPermission>()
                .SingleOrDefault(e => e.Alias == "Read");

            if (!vwDocUsers.Any(e => e.ClassificationPermissionId == readPermission.ClassificationPermissionId))
            {
                return BadRequest("Not enough permissions!");
            }

            var doc = this.docRepository.Find(id,
                e => e.DocStatus,
                e => e.DocEntryType,
                e => e.DocCasePartType,
                e => e.DocDirection,
                e => e.DocType,
                e => e.DocSourceType);

            if (doc == null)
            {
                return NotFound();
            }

            #region Load

            int caseId = this.docRepository.GetCaseId(id);

            this.unitOfWork.DbContext.Set<DocHasRead>()
            .Where(e => e.DocId == id)
            .ToList();

            this.unitOfWork.DbContext.Set<DocFile>()
             .Include(e => e.DocFileOriginType)
             .Include(e => e.DocFileType)
             .Include(e => e.DocFileKind)
             .Where(e => e.DocId == id)
             .ToList();

            this.unitOfWork.DbContext.Set<DocUnit>()
             .Include(e => e.Unit.UnitRelations.Select(er => er.ParentUnit))
             .Include(e => e.DocUnitRole)
             .Where(e => e.Doc.DocRelations.Any(dr => dr.RootDocId == caseId))
             .ToList();

            this.unitOfWork.DbContext.Set<DocCorrespondent>()
             .Include(e => e.Correspondent.CorrespondentType)
             .Where(e => e.DocId == id)
             .ToList();

            this.unitOfWork.DbContext.Set<DocCorrespondentContact>()
             .Include(e => e.CorrespondentContact.Correspondent)
             .Where(e => e.DocId == id)
             .ToList();

            this.unitOfWork.DbContext.Set<DocWorkflow>()
                .Include(e => e.DocWorkflowAction)
                .Include(e => e.ToUnit)
                .Include(e => e.PrincipalUnit)
                .Where(e => e.DocId == id)
                .ToList();

            this.unitOfWork.DbContext.Set<DocClassification>()
             .Include(e => e.Classification)
             .Where(e => e.DocId == id)
             .ToList();

            this.unitOfWork.DbContext.Set<DocIncomingDoc>()
             .Where(e => e.DocId == id)
             .ToList();

            #endregion

            var returnValue = new DocDO(doc, unitUser);

            #region DocCorrespondents

            foreach (var dc in doc.DocCorrespondents)
            {
                returnValue.DocCorrespondents.Add(new NomDo(dc));
            }

            #endregion

            #region DocWorkflows

            foreach (var dw in doc.DocWorkflows.OrderBy(e => e.DocWorkflowId))
            {
                returnValue.DocWorkflows.Add(new DocWorkflowDO(dw));
            }

            #endregion

            #region DocUnits

            List<DocUnitRole> docUnitRoles = this.unitOfWork.DbContext.Set<DocUnitRole>().ToList();
            foreach (var du in doc.DocUnits)
            {
                switch (du.DocUnitRole.Alias)
                {
                    case "From":
                        returnValue.DocUnitsFrom.Add(new NomDo(du));
                        break;
                    case "To":
                        returnValue.DocUnitsTo.Add(new NomDo(du));
                        break;
                    case "ImportedBy":
                        returnValue.DocUnitsImportedBy.Add(new NomDo(du));
                        break;
                    case "MadeBy":
                        returnValue.DocUnitsMadeBy.Add(new NomDo(du));
                        break;
                    case "CCopy":
                        returnValue.DocUnitsCCopy.Add(new NomDo(du));
                        break;
                    case "InCharge":
                        returnValue.DocUnitsInCharge.Add(new NomDo(du));
                        break;
                    case "Controlling":
                        returnValue.DocUnitsControlling.Add(new NomDo(du));
                        break;
                    case "Readers":
                        returnValue.DocUnitsReaders.Add(new NomDo(du));
                        break;
                    case "Editors":
                        returnValue.DocUnitsEditors.Add(new NomDo(du));
                        break;
                    case "Registrators":
                        returnValue.DocUnitsRegistrators.Add(new NomDo(du));
                        break;
                };
            }

            #endregion

            #region DocClassifications

            foreach (var dc in doc.DocClassifications)
            {
                returnValue.DocClassifications.Add(new DocClassificationDO(dc));
            }

            #endregion

            #region DocFiles

            DocFile editable = doc.DocFiles.FirstOrDefault(e => e.DocFileOriginTypeId.HasValue && e.DocFileOriginType.Alias == "EditableFile");
            if (editable != null)
            {
                returnValue.JObjectForm = editable.DocFileType.DocTypeUri;
            }

            foreach (var df in doc.DocFiles.Where(e => !e.DocFileOriginTypeId.HasValue || e.DocFileOriginType.Alias != "EditableFile"))
            {
                if (df.DocFileKind.Alias.ToLower() == "PrivateAttachedFile".ToLower())
                {
                    returnValue.PrivateDocFiles.Add(new DocFileDO(df));
                }
                else if (df.DocFileKind.Alias.ToLower() == "PublicAttachedFile".ToLower())
                {
                    returnValue.PublicDocFiles.Add(new DocFileDO(df));
                }
            }

            #endregion

            #region DocRelations

            returnValue.DocRelations.AddRange(
                this.docRepository.GetCaseRelationsByDocId(id,
                    e => e.Doc.DocCasePartType,
                    e => e.Doc.DocCasePartMovements.Select(dc => dc.User),
                    e => e.Doc.DocDirection,
                    e => e.Doc.DocType,
                    e => e.Doc.DocStatus,
                    e => e.Doc.DocSourceType,
                    e => e.Doc.DocEntryType)
                   .Select(e => new DocRelationDO(e))
                );

            #endregion

            #region DocElectronicServiceStages

            returnValue.DocElectronicServiceStages.AddRange(
                this.docRepository.GetCaseElectronicServiceStagesByDocId(id,
                    e => e.ElectronicServiceStage.ElectronicServiceStageExecutors.Select(ee => ee.Unit))
                    .Select(e => new DocElectronicServiceStageDO(e))
                );

            #endregion

            #region Set permissions

            returnValue.CanRead = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "Read");

            returnValue.CanEdit = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "Edit");

            returnValue.CanRegister = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "Register");

            returnValue.CanPublicFileManagement = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "PublicFileManagement");

            returnValue.CanManagement = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "Management");

            returnValue.CanDocWorkflowSign = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "DocWorkflowSign");

            returnValue.CanDocWorkflowDiscuss = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "DocWorkflowDiscuss");

            returnValue.CanSubstituteManagement = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "SubstituteManagement");

            returnValue.CanDocWorkflowManagement = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "DocWorkflowManagement");

            returnValue.CanESign = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "ESign");

            returnValue.CanFinish = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "Finish");

            returnValue.CanReverse = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "Reverse");

            returnValue.CanEditTech = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "EditTechElectronicServiceStage");

            returnValue.CanEditTechElectronicServiceStage = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "EditTech");

            returnValue.CanDocCasePartManagement = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "DocCasePartManagement");

            returnValue.CanDocMovement = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "DocMovement");

            returnValue.CanSendMail = vwDocUsers.Any(e => e.DocId == doc.DocId && e.ClassificationPermission.Alias == "SendMail");

            #endregion

            returnValue.Set();

            return Ok(returnValue);
        }