DocumentService.cs 11 KB

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