DocumentClearCommand.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using BO.Console.Commands.Base;
  5. using Quadarax.Foundation.Core.QConsole;
  6. using Quadarax.Foundation.Core.QConsole.Argument;
  7. using Quadarax.Foundation.Core.QConsole.Attributes;
  8. using Quadarax.Foundation.Core.QConsole.Value;
  9. using Quadarax.Foundation.Core.Value;
  10. namespace BO.Console.Commands.Document
  11. {
  12. [CommandDefinition]
  13. internal class DocumentClearCommand: AbstractWorkspaceCommand
  14. {
  15. #region *** Constants ***
  16. private const string CS_CMD_DOCCLR_NAME = "document_clear";
  17. private const string CS_CMD_DOCCLR_DESCR = "Add new metadocument with artifact to workspace or append artifact to existing document.";
  18. private const string CS_ARG_DESC_USER = "User name context. If not specified use default user context (who is logged in console).";
  19. private const string CS_ARG_NAME_NAME = "name";
  20. private const string CS_ARG_HINT_NAME = "<document_name>";
  21. 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.";
  22. private const string CS_ARG_DESC_FORCE = "Flag if set, then clears artifacts and ignores metadocument state, otherwise cancel operation.";
  23. #endregion
  24. #region *** Properties ***
  25. public override string Name => CS_CMD_DOCCLR_NAME;
  26. public override string Description => CS_CMD_DOCCLR_DESCR;
  27. protected string DocumentName { get; set; }
  28. #endregion
  29. #region *** Constructor ***
  30. public DocumentClearCommand(Engine engine) : base(engine)
  31. {
  32. }
  33. #endregion
  34. #region *** EXECUTE ***
  35. protected override Result OnExecute()
  36. {
  37. WriteDebugInfo($"Getting metadocument '{DocumentName}' in workspace...");
  38. var document = Client.GetWorkspaceDocumentByNameAsync(ApiKey, ApiKeyPassword, DocumentName, UserName).Result;
  39. if (document!=null)
  40. {
  41. WriteDebugInfo($"Document '{DocumentName}' exists [{document.Id}] in workspace. Deleting artifacts...");
  42. var deletedArtifacts = Client.ClearWorkspaceDocumentArtifactAsync(ApiKey, ApiKeyPassword, document.Id,
  43. IsForce.GetValueOrDefault(), UserName).Result;
  44. int cnt = 0;
  45. foreach (var art in deletedArtifacts)
  46. {
  47. WriteInfo($"Artifact '{art.Name + art.Extension}' [{art.Id}] (Created:{art.Audit.Created}) DELETED.");
  48. cnt++;
  49. }
  50. WriteCaption($"Deleted {cnt} artifacts.");
  51. }
  52. else
  53. return new Result(new Exception($"Document '{DocumentName}' not found."));
  54. return new Result();
  55. }
  56. #endregion
  57. #region *** Protected overrides ***
  58. protected override void OnValidateArguments()
  59. {
  60. base.OnValidateArguments();
  61. DocumentName = GetArgumentValueOrDefault<string>(CS_ARG_NAME_NAME);
  62. }
  63. #endregion
  64. #region *** Private operations ***
  65. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  66. {
  67. var args = base.OnSetupArguments().ToList();
  68. args.Add(new NamedArgument(CS_ARG_NAME_NAME, CS_ARG_DESC_NAME,CS_ARG_HINT_NAME, TypeValuesEnum.String, string.Empty, false));
  69. AppendUserArgument(args, CS_ARG_DESC_USER);
  70. AppendForceArgument(args, CS_ARG_DESC_FORCE);
  71. return args.ToArray();
  72. }
  73. #endregion
  74. }
  75. }