DocumentService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Principal;
  6. using System.Threading.Tasks;
  7. using BO.AppServer.Business.Exceptions;
  8. using BO.AppServer.Business.Mapper;
  9. using BO.AppServer.Business.Services.Base;
  10. using BO.AppServer.Data.Entity;
  11. using BO.AppServer.Data.Repository;
  12. using BO.AppServer.Metadata.Dto;
  13. using BO.AppServer.Metadata.Enums;
  14. using Microsoft.AspNetCore.Identity;
  15. using Microsoft.Extensions.Logging;
  16. using Quadarax.Foundation.Core.Data;
  17. using Quadarax.Foundation.Core.Data.Interface;
  18. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  19. namespace BO.AppServer.Business.Services
  20. {
  21. public class DocumentService : UserContextService
  22. {
  23. #region *** Private fields ***
  24. private WorkspaceRepo _repoWorkspace;
  25. private MetadocumentRepo _repoDocument;
  26. private MimeTypeRepo _repoMType;
  27. private ArtifactRepo _repoArtifact;
  28. private StorageService _srvStorage;
  29. #endregion
  30. #region *** Constructors ***
  31. public DocumentService(
  32. WorkspaceRepo repoWorkspace,
  33. MetadocumentRepo repoDocument,
  34. MimeTypeRepo repoMType,
  35. ArtifactRepo repoArtifact,
  36. UserRepo repoUser,
  37. UserManager<IdentityUser> userManager,
  38. IPrincipal currentPrincipal,
  39. ILoggerFactory logger) : base(repoUser, userManager, currentPrincipal, logger)
  40. {
  41. _repoWorkspace = repoWorkspace ?? throw new ArgumentNullException(nameof(repoWorkspace));
  42. _repoDocument = repoDocument ?? throw new ArgumentNullException(nameof(repoDocument));
  43. _repoMType = repoMType ?? throw new ArgumentNullException(nameof(repoMType));
  44. _repoArtifact = repoArtifact ?? throw new ArgumentNullException(nameof(repoArtifact));
  45. }
  46. #endregion
  47. #region *** Public Operations ***
  48. public async Task<ResultValueDto<DocumentRDto>> GetDocumentAsync(long workspaceId, long documentId)
  49. {
  50. var document = CheckAndGetDocument(workspaceId, documentId);
  51. return new ResultValueDto<DocumentRDto>(document.Map<Metadocument, DocumentRDto>());
  52. }
  53. public async Task<ResultsValueDto<DocumentRDto>> GetAllDocumentsAsync(long workspaceId, DocumentsScopeEnums scope, PagingDto paging)
  54. {
  55. CheckAndGetWorkspace(workspaceId);
  56. IList<Metadocument> documents = new List<Metadocument>();
  57. switch (scope)
  58. {
  59. case DocumentsScopeEnums.All:
  60. documents = _repoDocument.Query(paging).Where(x => x.Structure.WorkspaceId == workspaceId).OrderBy(x => x.Name).ToList();
  61. break;
  62. case DocumentsScopeEnums.Pending:
  63. documents = _repoDocument.Query(paging).Where(x => x.Structure.WorkspaceId == workspaceId && x.Status == (int)DocumentStatusEnum.Pending).OrderBy(x => x.Name).ToList();
  64. break;
  65. case DocumentsScopeEnums.Done:
  66. documents = _repoDocument.Query(paging).Where(x => x.Structure.WorkspaceId == workspaceId && x.Status == (int)DocumentStatusEnum.DoneOk).OrderBy(x => x.Name).ToList();
  67. break;
  68. case DocumentsScopeEnums.Working:
  69. documents = _repoDocument.Query(paging).Where(x => x.Structure.WorkspaceId == workspaceId && x.Status == (int)DocumentStatusEnum.Processing).OrderBy(x => x.Name).ToList();
  70. break;
  71. case DocumentsScopeEnums.Failed:
  72. documents = _repoDocument.Query(paging).Where(x => x.Structure.WorkspaceId == workspaceId && x.Status == (int)DocumentStatusEnum.DoneFail).OrderBy(x => x.Name).ToList();
  73. break;
  74. }
  75. return new ResultsValueDto<DocumentRDto>(documents.MapList<Metadocument, DocumentRDto>());
  76. }
  77. public async Task<ResultsValueDto<DocumentRDto>> GetAllDocumentsAsync(DocumentsScopeEnums scope, PagingDto paging)
  78. {
  79. IList<Metadocument> documents = new List<Metadocument>();
  80. switch (scope)
  81. {
  82. case DocumentsScopeEnums.All:
  83. documents = _repoDocument.Query(paging).OrderBy(x => x.Name).ToList();
  84. break;
  85. case DocumentsScopeEnums.Pending:
  86. documents = _repoDocument.Query(paging).Where(x => x.Status == (int)DocumentStatusEnum.Pending).OrderBy(x => x.Name).ToList();
  87. break;
  88. case DocumentsScopeEnums.Done:
  89. documents = _repoDocument.Query(paging).Where(x => x.Status == (int)DocumentStatusEnum.DoneOk).OrderBy(x => x.Name).ToList();
  90. break;
  91. case DocumentsScopeEnums.Working:
  92. documents = _repoDocument.Query(paging).Where(x => x.Status == (int)DocumentStatusEnum.Processing).OrderBy(x => x.Name).ToList();
  93. break;
  94. case DocumentsScopeEnums.Failed:
  95. documents = _repoDocument.Query(paging).Where(x => x.Status == (int)DocumentStatusEnum.DoneFail).OrderBy(x => x.Name).ToList();
  96. break;
  97. }
  98. return new ResultsValueDto<DocumentRDto>(documents.MapList<Metadocument, DocumentRDto>());
  99. }
  100. public async Task<ResultValueDto<DocumentRDto>> CreateDocumentAsync(long workspaceId, DocumentCDto document)
  101. {
  102. var workspace = CheckAndGetWorkspace(workspaceId);
  103. var exists = _repoDocument.Query(new Paging())
  104. .Any(x => x.Structure.WorkspaceId == workspaceId && x.Name == document.Name);
  105. if (exists)
  106. {
  107. var exc = new EntityAlreadyExistsException(typeof(Metadocument), document.Name);
  108. Log.LogWarning(exc.Message);
  109. throw exc;
  110. }
  111. var newDocument = _repoDocument.New();
  112. document.CopyToDto(newDocument);
  113. newDocument.Created = DateTime.Now;
  114. newDocument.CreatedByUser = GetCurrentUser();
  115. newDocument.Status = (int)DocumentStatusEnum.New;
  116. newDocument.StructureId = workspace.Structures.First().Id;
  117. _repoDocument.Set(newDocument);
  118. _repoDocument.Commit();
  119. return new ResultValueDto<DocumentRDto>(newDocument.Map<Metadocument, DocumentRDto>());
  120. }
  121. public async Task<ResultValueDto<ArtifactRDto>> AppendArtifactAsync(long workspaceId, long documentId,
  122. ArtifactCDto artifact, ArtifactTypeEnum type, Stream content)
  123. {
  124. //TODO: Rewrite better way - mainly rollback scenario
  125. var document = CheckAndGetDocument(workspaceId, documentId);
  126. var exists = document.Artifacts.Any(x =>
  127. x.Name.ToLower() == artifact.Name.ToLower() && x.Extension.ToLower() == artifact.Extension.ToLower());
  128. if (exists)
  129. {
  130. var exc = new EntityAlreadyExistsException(typeof(Artifact), artifact.Name + "." + artifact.Extension);
  131. Log.LogWarning(exc.Message);
  132. throw exc;
  133. }
  134. var mtype = _repoMType.GetByMimeType(artifact.MimeType);
  135. if (mtype == null)
  136. {
  137. var exc = new EntityNotExistsException(typeof(MimeType), artifact.MimeType, 0);
  138. Log.LogWarning(exc.Message);
  139. throw exc;
  140. }
  141. var newArtifact = _repoArtifact.New();
  142. newArtifact.Name = artifact.Name;
  143. newArtifact.Extension = artifact.Extension;
  144. newArtifact.Length = content.Length;
  145. newArtifact.Metadocument = document;
  146. newArtifact.MimeType = mtype;
  147. newArtifact.Created = DateTime.Now;
  148. newArtifact.CreatedByUser = GetCurrentUser();
  149. newArtifact.Type = (int)type;
  150. _repoArtifact.Set(newArtifact);
  151. document = _repoDocument.Get(documentId);
  152. document.Modified = DateTime.Now;
  153. document.ModifiedByUser = GetCurrentUser();
  154. _repoDocument.Set(document);
  155. _repoArtifact.Commit();
  156. _repoDocument.Commit();
  157. await _srvStorage.PushContentAsync(newArtifact.Id, content);
  158. return new ResultValueDto<ArtifactRDto>(newArtifact.Map<Artifact, ArtifactRDto>());
  159. }
  160. public async Task<Tuple<Stream, string>> GetArtifactAsync(long workspaceId, long documentId, long artifactId)
  161. {
  162. var artifact = CheckAndGetArtifact(workspaceId, documentId, artifactId);
  163. var result = await _srvStorage.PopContentAsync(artifact.Id);
  164. return new Tuple<Stream, string>(result.Item1, artifact.MimeType.Mimetype1);
  165. }
  166. #endregion
  167. #region *** Private Operations ***
  168. private Workspace CheckAndGetWorkspace(long workspaceId)
  169. {
  170. var workspace = _repoWorkspace.Get(workspaceId);
  171. if (workspace == null)
  172. {
  173. var exc = new EntityNotExistsException(typeof(Workspace), nameof(Workspace.Id), workspaceId);
  174. Log.LogWarning(exc.Message);
  175. throw exc;
  176. }
  177. return workspace;
  178. }
  179. private Metadocument CheckAndGetDocument(long workspaceId, long documentId)
  180. {
  181. CheckAndGetWorkspace(workspaceId);
  182. var document = _repoDocument.Get(workspaceId ,documentId);
  183. if (document == null)
  184. {
  185. var exc = new EntityNotExistsException(typeof(Metadocument), nameof(Metadocument.Id), documentId);
  186. Log.LogWarning(exc.Message);
  187. throw exc;
  188. }
  189. return document;
  190. }
  191. private Artifact CheckAndGetArtifact(long workspaceId, long documentId, long artifactId)
  192. {
  193. CheckAndGetWorkspace(workspaceId);
  194. CheckAndGetDocument(workspaceId, documentId);
  195. var artifact = _repoDocument.GetArtifact(documentId, artifactId);
  196. if (artifact == null)
  197. {
  198. var exc = new EntityNotExistsException(typeof(Artifact), nameof(Artifact.Id), artifactId);
  199. Log.LogWarning(exc.Message);
  200. throw exc;
  201. }
  202. return artifact;
  203. }
  204. #endregion
  205. }
  206. }