public HttpResponseMessage DownloadChecklist(int id, string identifier) { int checklistId; AopApp app = this.unitOfWork.DbContext.Set<AopApp>().Find(id); if (identifier == "st") { checklistId = app.STChecklistId.Value; } else if (identifier == "nd") { checklistId = app.NDChecklistId.Value; } else { throw new Exception("Identifier missing."); } DocFile editable = this.unitOfWork.DbContext.Set<DocFile>().FirstOrDefault(e => e.DocId == checklistId && e.DocFileOriginType.Alias == "EditableFile"); byte[] content; using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbContext"].ConnectionString)) { connection.Open(); using (MemoryStream m1 = new MemoryStream()) using (var blobStream = new BlobReadStream(connection, "dbo", "Blobs", "Content", "Key", editable.DocFileContentId)) { blobStream.CopyTo(m1); content = m1.ToArray(); } string contentToString = System.Text.Encoding.UTF8.GetString(content); JObject checklistObj = JsonConvert.DeserializeObject<JObject>(contentToString); JArray checklistVersions = checklistObj["versions"] as JArray; JObject latest = checklistVersions.First() as JObject; string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App\word_templates\checklist.docx"); using (FileStream template = File.Open(templatePath, FileMode.Open, FileAccess.Read)) { var memoryStream = new MemoryStream(); template.CopyTo(memoryStream); WordTemplateTransformer tt = new WordTemplateTransformer(memoryStream); tt.Transform(latest); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); memoryStream.Position = 0; result.Content = new StreamContent(memoryStream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "checklist_for_print.docx" }; return result; } } }
public IHttpActionResult CreateChildReport(int id) { using (var transaction = this.unitOfWork.BeginTransaction()) { int docId, checklistId; AopApp app = this.unitOfWork.DbContext.Set<AopApp>().Find(id); if (app.NDDocId.HasValue && app.NDChecklistId.HasValue) { docId = app.NDDocId.Value; checklistId = app.NDChecklistId.Value; } else { throw new Exception("NDDocId OR NDChecklistId missing."); } UnitUser unitUser = this.unitOfWork.DbContext.Set<UnitUser>().FirstOrDefault(e => e.UserId == this.userContext.UserId); DocEntryType documentEntryType = this.unitOfWork.DbContext.Set<DocEntryType>().SingleOrDefault(e => e.Alias == "Document"); DocDirection outgoingDocDirection = this.unitOfWork.DbContext.Set<DocDirection>() .SingleOrDefault(e => e.Alias.ToLower() == "Outgoing".ToLower()); DocCasePartType internalDocCasePartType = this.unitOfWork.DbContext.Set<DocCasePartType>() .SingleOrDefault(e => e.Alias.ToLower() == "Internal".ToLower()); DocFormatType paperDocFormatType = this.unitOfWork.DbContext.Set<DocFormatType>() .SingleOrDefault(e => e.Alias.ToLower() == "Paper".ToLower()); DocStatus draftStatus = this.unitOfWork.DbContext.Set<DocStatus>().SingleOrDefault(e => e.Alias == "Draft"); DocType docType = this.unitOfWork.DbContext.Set<DocType>() .SingleOrDefault(e => e.Alias.ToLower() == "Report".ToLower()); string docSubject = "Генериран доклад"; Doc newDoc = this.docRepository.CreateDoc( outgoingDocDirection.DocDirectionId, documentEntryType.DocEntryTypeId, draftStatus.DocStatusId, docSubject, internalDocCasePartType.DocCasePartTypeId, null, null, docType.DocTypeId, paperDocFormatType.DocFormatTypeId, null, userContext); DocRelation parentDocRelation = this.unitOfWork.DbContext.Set<DocRelation>().FirstOrDefault(e => e.DocId == docId); ElectronicServiceStage electronicServiceStage = null; if (parentDocRelation == null) { electronicServiceStage = this.unitOfWork.DbContext.Set<ElectronicServiceStage>() .SingleOrDefault(e => e.DocTypeId == newDoc.DocTypeId && e.IsFirstByDefault); } List<DocUnitRole> docUnitRoles = this.unitOfWork.DbContext.Set<DocUnitRole>().ToList(); List<DocTypeUnitRole> docTypeUnitRoles = new List<DocTypeUnitRole>(); //from docTypeUnitRoles.Add(new DocTypeUnitRole { DocTypeId = docType.DocTypeId, DocDirectionId = outgoingDocDirection.DocDirectionId, DocUnitRoleId = docUnitRoles.SingleOrDefault(e => e.Alias == "From").DocUnitRoleId, UnitId = unitUser.UnitId, IsActive = true }); DocUnitRole importedBy = docUnitRoles.SingleOrDefault(e => e.Alias == "ImportedBy"); List<DocClassification> parentDocClassifications = this.unitOfWork.DbContext.Set<DocClassification>() .Where(e => e.DocId == parentDocRelation.DocId && e.IsActive && e.IsInherited) .ToList(); Doc caseDoc = this.docRepository.Find(this.docRepository.GetCaseId(docId), e => e.DocCorrespondents); List<int> correspondentIds = caseDoc.DocCorrespondents.Select(e => e.CorrespondentId).ToList(); newDoc.CreateDocProperties( parentDocRelation, null, null, parentDocClassifications, electronicServiceStage, docTypeUnitRoles, importedBy, unitUser, correspondentIds, null, this.userContext); this.unitOfWork.Save(); #region FileContent DocFile docFile = this.unitOfWork.DbContext.Set<DocFile>() .FirstOrDefault(e => e.DocId == checklistId && e.DocFileOriginTypeId.HasValue && e.DocFileOriginType.Alias == "EditableFile"); DocFileType docFileType = this.unitOfWork.DbContext.Set<DocFileType>().SingleOrDefault(e => e.Alias == "DOCX"); DocFileKind docFileKind = this.unitOfWork.DbContext.Set<DocFileKind>().Single(e => e.Alias == "PublicAttachedFile"); DocFileOriginType docFileOriginType = this.unitOfWork.DbContext.Set<DocFileOriginType>().Single(e => e.Alias == "AttachedFile"); Guid blobKey; byte[] content; using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbContext"].ConnectionString)) { connection.Open(); using (MemoryStream m1 = new MemoryStream()) using (var blobStream = new BlobReadStream(connection, "dbo", "Blobs", "Content", "Key", docFile.DocFileContentId)) { blobStream.CopyTo(m1); content = m1.ToArray(); } string contentToString = System.Text.Encoding.UTF8.GetString(content); JObject checklistObj = JsonConvert.DeserializeObject<JObject>(contentToString); JArray checklistVersions = checklistObj["versions"] as JArray; JObject lastest = checklistVersions.First() as JObject; DocUnit checklistMadeBy = this.unitOfWork.DbContext.Set<DocUnit>() .Include(e => e.Unit.UnitRelations.Select(u => u.ParentUnit)) .Include(e => e.DocUnitRole) .FirstOrDefault(e => e.DocId == checklistId && (e.DocUnitRole.Alias == "ImportedBy" || e.DocUnitRole.Alias == "MadeBy")); DocWorkflow checklistDiscuss = this.unitOfWork.DbContext.Set<DocWorkflow>() .Include(e => e.PrincipalUnit.UnitRelations.Select(u => u.ParentUnit)) .FirstOrDefault(e => e.DocId == checklistId && e.DocWorkflowAction.Alias == "Discuss"); JObject json = this.dataGenerator.GenerateReport(lastest, date: DateTime.Now.ToString("dd.MM.yyyy"), madeByName: checklistMadeBy != null ? checklistMadeBy.Unit.Name : string.Empty, madeByPosition: checklistMadeBy != null ? checklistMadeBy.Unit.UnitRelations.First().ParentUnit.Name : string.Empty, coordinatorName: checklistDiscuss != null ? checklistDiscuss.PrincipalUnit.Name : string.Empty, coordinatorPosition: checklistDiscuss != null ? checklistDiscuss.PrincipalUnit.UnitRelations.First().ParentUnit.Name : string.Empty ); string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App\word_templates\doklad_template.docx"); using (FileStream template = File.Open(templatePath, FileMode.Open, FileAccess.Read)) using (var memoryStream = new MemoryStream()) using (var blobWriter = new BlobWriter(connection)) using (var stream = blobWriter.OpenStream()) { template.CopyTo(memoryStream); WordTemplateTransformer tt = new WordTemplateTransformer(memoryStream); tt.Transform(json); memoryStream.Position = 0; memoryStream.CopyTo(stream); blobKey = blobWriter.GetBlobKey(); } } newDoc.CreateDocFile( docFileKind.DocFileKindId, docFileType.DocFileTypeId, docFileOriginType.DocFileOriginTypeId, "Report.docx", "Report.docx", "", blobKey, this.userContext); #endregion app.NDReportId = newDoc.DocId; this.docRepository.ExecSpSetDocTokens(docId: newDoc.DocId); this.docRepository.ExecSpSetDocUnitTokens(docId: newDoc.DocId); this.unitOfWork.Save(); transaction.Commit(); return Ok(new { docId = newDoc.DocId }); } }