From fc728674dbb577bdb5f732ada37f004d0147685e Mon Sep 17 00:00:00 2001
From: pkupczyk <piotr.kupczyk@id.ethz.ch>
Date: Tue, 8 Nov 2022 19:48:19 +0100
Subject: [PATCH] SSDM-13152 : Exports for master data and metadata UI - async
 execute service for Admin UI

---
 openbis_ng_ui/src/js/services/openbis/api.js | 41 +++++++++++++++++++-
 openbis_ng_ui/src/js/services/openbis/dto.js |  3 ++
 2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/openbis_ng_ui/src/js/services/openbis/api.js b/openbis_ng_ui/src/js/services/openbis/api.js
index 921597a3261..d269fc65341 100644
--- a/openbis_ng_ui/src/js/services/openbis/api.js
+++ b/openbis_ng_ui/src/js/services/openbis/api.js
@@ -1,4 +1,5 @@
 import autoBind from 'auto-bind'
+import dto from '@src/js/services/openbis/dto.js'
 
 class Facade {
   constructor() {
@@ -178,6 +179,10 @@ class Facade {
     return this.promise(this.v3.getVocabularies(ids, fo))
   }
 
+  getOperationExecutions(ids, fo) {
+    return this.promise(this.v3.getOperationExecutions(ids, fo))
+  }
+
   updateSampleTypes(updates) {
     return this.promise(this.v3.updateSampleTypes(updates))
   }
@@ -230,8 +235,40 @@ class Facade {
     return this.promise(this.v3.evaluatePlugin(options))
   }
 
-  executeService(id, options) {
-    return this.promise(this.v3.executeCustomASService(id, options))
+  async executeService(id, options) {
+    const scheduleResult = await this.executeOperations(
+      [new dto.ExecuteCustomASServiceOperation(id, options)],
+      new dto.AsynchronousOperationExecutionOptions()
+    )
+
+    const executionId = scheduleResult.executionId
+    const executionFetchOptions = new dto.OperationExecutionFetchOptions()
+    executionFetchOptions.withDetails().withResults()
+    executionFetchOptions.withDetails().withError()
+
+    for (;;) {
+      const executions = await this.getOperationExecutions(
+        [executionId],
+        executionFetchOptions
+      )
+
+      const execution = executions[executionId]
+
+      if (!execution) {
+        throw Error('Execution id: ' + executionId + ' not found.')
+      } else if (execution.details.error !== null) {
+        throw Error(execution.details.error.message)
+      } else if (
+        execution.details.results !== null &&
+        execution.details.results.length > 0
+      ) {
+        return execution.details.results[0].result
+      } else {
+        await new Promise(resolve => {
+          setTimeout(resolve, 1000)
+        })
+      }
+    }
   }
 
   executeQuery(id, options) {
diff --git a/openbis_ng_ui/src/js/services/openbis/dto.js b/openbis_ng_ui/src/js/services/openbis/dto.js
index b481f6dec2e..2145a63bc70 100644
--- a/openbis_ng_ui/src/js/services/openbis/dto.js
+++ b/openbis_ng_ui/src/js/services/openbis/dto.js
@@ -54,6 +54,8 @@ const CLASS_FULL_NAMES = [
   'as/dto/material/update/MaterialTypeUpdate',
   'as/dto/material/update/UpdateMaterialTypesOperation',
   'as/dto/operation/SynchronousOperationExecutionOptions',
+  'as/dto/operation/AsynchronousOperationExecutionOptions',
+  'as/dto/operation/fetchoptions/OperationExecutionFetchOptions',
   'as/dto/pat/create/CreatePersonalAccessTokensOperation',
   'as/dto/pat/create/PersonalAccessTokenCreation',
   'as/dto/pat/delete/DeletePersonalAccessTokensOperation',
@@ -146,6 +148,7 @@ const CLASS_FULL_NAMES = [
   'as/dto/sample/update/SampleTypeUpdate',
   'as/dto/sample/update/UpdateSampleTypesOperation',
   'as/dto/service/CustomASServiceExecutionOptions',
+  'as/dto/service/execute/ExecuteCustomASServiceOperation',
   'as/dto/service/id/CustomASServiceCode',
   'as/dto/space/Space',
   'as/dto/space/fetchoptions/SpaceFetchOptions',
-- 
GitLab