DocumentAddCommand.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using BO.AppServer.Metadata.Dto;
  8. using BO.Console.Commands.Base;
  9. using Quadarax.Foundation.Core.IO.MimeTypes;
  10. using Quadarax.Foundation.Core.QConsole;
  11. using Quadarax.Foundation.Core.QConsole.Argument;
  12. using Quadarax.Foundation.Core.QConsole.Attributes;
  13. using Quadarax.Foundation.Core.QConsole.Value;
  14. using Quadarax.Foundation.Core.Value;
  15. using Quadarax.Foundation.Core.Value.Extensions;
  16. // ReSharper disable InconsistentNaming
  17. namespace BO.Console.Commands.Document
  18. {
  19. [CommandDefinition]
  20. internal class DocumentAddCommand : AbstractWorkspaceCommand
  21. {
  22. #region *** Constants ***
  23. private const string CS_CMD_DOCADD_NAME = "document_add";
  24. private const string CS_CMD_DOCADD_DESCR = "Add new metadocument with artifact to workspace or append artifact to existing document.";
  25. private const string CS_ARG_DESC_USER = "User name context. If not specified use default user context (who is logged in console).";
  26. private const string CS_ARG_NAME_NAME = "name";
  27. private const string CS_ARG_HINT_NAME = "<document_name>";
  28. private const string CS_ARG_DESC_NAME = "Metadata document name. If not specified use first artifact name. If document already exists, appends artifact to existing metadocument.";
  29. private const string CS_ARG_NAME_ARTS = "arts";
  30. private const string CS_ARG_HINT_ARTS = "<artifact_files>";
  31. private const string CS_ARG_DESC_ARTS = "Artifact file names (separated by ';') or single file name.";
  32. private const string CS_ARG_DESC_FORCE = "Flag if set, then appends artifact to existing metadocument, otherwise cancel operation.";
  33. private const string CS_ARG_NAME_OUTPUT = "setout";
  34. private const string CS_ARG_HINT_OUTPUT = "<is_art_output>";
  35. private const string CS_ARG_DESC_OUTPUT = "Flag if set, then appends artifact as output (output from PS) otherwise set as input (input from user).";
  36. #endregion
  37. #region *** Properties ***
  38. public override string Name => CS_CMD_DOCADD_NAME;
  39. public override string Description => CS_CMD_DOCADD_DESCR;
  40. protected string DocumentName { get; set; }
  41. protected IList<string> ArtifactFiles { get; set; }
  42. #endregion
  43. #region *** Constructor ***
  44. public DocumentAddCommand(Engine engine) : base(engine)
  45. {
  46. }
  47. #endregion
  48. #region *** EXECUTE ***
  49. protected override Result OnExecute()
  50. {
  51. // 1. Check if document exists
  52. WriteDebugInfo($"Checking existence of document '{DocumentName}' in workspace...");
  53. var document = Client.GetWorkspaceDocumentByNameAsync(ApiKey, ApiKeyPassword, DocumentName, UserName).Result;
  54. if (document!=null)
  55. {
  56. WriteDebugInfo($"Document '{DocumentName}' exists [{document.Id}] in workspace...");
  57. }
  58. else
  59. {
  60. // 2. Create new document
  61. WriteDebugInfo($"Document '{DocumentName}' not exists in workspace - creating...");
  62. document = Client.CreateWorkspaceDocumentAsync(ApiKey, ApiKeyPassword, new DocumentCDto(DocumentName), UserName).Result;
  63. WriteInfo($"Document '{document.Name}' [{document.Id}] created.");
  64. }
  65. // 3. Append artifacts
  66. WriteInfo($"Appending {ArtifactFiles.Count} artifacts...");
  67. //var appendArtTasks = new List<Task>();
  68. foreach (var artFile in ArtifactFiles)
  69. {
  70. //appendArtTasks.Add(Task.Factory.StartNew(async () =>
  71. //{
  72. var dtStart = DateTime.Now;
  73. ArtifactRDto art;
  74. //await using (var fs = FileSystem.File.OpenRead(artFile))
  75. using (var fs = FileSystem.File.OpenRead(artFile))
  76. {
  77. art = Client.AppendWorkspaceDocumentArtifactAsync(ApiKey, ApiKeyPassword, document.Id,
  78. FileSystem.Path.GetFileName(artFile), MimeTypeUtil.GetMimeType(FileSystem.Path.GetExtension(artFile)), fs, UserName).Result;
  79. }
  80. WriteInfo($"Artifact '{art.Name}' [{art.Id}] appended to document '{document.Name}' as mime '{art.MimeType}' @ {(DateTime.Now - dtStart).ToReadableString()}");
  81. //}));
  82. }
  83. // 4. Sync all
  84. //Task.WaitAll(appendArtTasks.ToArray());
  85. return new Result();
  86. }
  87. #endregion
  88. #region *** Protected overrides ***
  89. protected override void OnValidateArguments()
  90. {
  91. base.OnValidateArguments();
  92. DocumentName = GetArgumentValueOrDefault<string>(CS_ARG_NAME_NAME);
  93. var artifactFiles = GetArgumentValueOrDefault<string>(CS_ARG_NAME_ARTS);
  94. ArtifactFiles = artifactFiles.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
  95. var sb = new StringBuilder();
  96. foreach (var artFile in ArtifactFiles)
  97. {
  98. if (!FileSystem.File.Exists(artFile))
  99. sb.Append("'").Append(artFile).Append("',");
  100. }
  101. if (sb.Length > 0)
  102. {
  103. sb.Remove(sb.Length - 1, 1);
  104. throw new FileNotFoundException($"Artifact files: {sb} not found!");
  105. }
  106. if (string.IsNullOrEmpty(DocumentName))
  107. DocumentName = FileSystem.Path.GetFileNameWithoutExtension(ArtifactFiles.First());
  108. }
  109. #endregion
  110. #region *** Private operations ***
  111. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  112. {
  113. var args = base.OnSetupArguments().ToList();
  114. args.Add(new NamedArgument(CS_ARG_NAME_NAME, CS_ARG_DESC_NAME,CS_ARG_HINT_NAME, TypeValuesEnum.String, string.Empty, false));
  115. args.Add(new NamedArgument(CS_ARG_NAME_ARTS, CS_ARG_DESC_ARTS,CS_ARG_HINT_ARTS, TypeValuesEnum.String, string.Empty, true));
  116. AppendUserArgument(args, CS_ARG_DESC_USER);
  117. args.Add(new FlagArgument(CS_ARG_NAME_OUTPUT, CS_ARG_DESC_OUTPUT,CS_ARG_HINT_OUTPUT, false));
  118. AppendForceArgument(args, CS_ARG_DESC_FORCE);
  119. return args.ToArray();
  120. }
  121. #endregion
  122. }
  123. }