From dc90a8b5d12cda1f6dcafe426ff047179bf57e94 Mon Sep 17 00:00:00 2001
From: pkupczyk <piotr.kupczyk@id.ethz.ch>
Date: Thu, 28 Mar 2024 16:08:09 +0100
Subject: [PATCH] SSDM-13578 : 2PT : Database and V3 Implementation - js facade

---
 .../src/js/api/server-data-store-facade.js    |  2 +-
 api-openbis-javascript/src/v3/openbis.js      | 38 +++++++++++++------
 .../dto/OpenBISJavaScriptFacade.java          |  4 +-
 .../openbis-v3-api-test/html/test/test-afs.ts |  7 ++--
 .../html/test/types/openbis.esm.d.ts          |  4 +-
 5 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/api-data-store-server-javascript/src/js/api/server-data-store-facade.js b/api-data-store-server-javascript/src/js/api/server-data-store-facade.js
index b2b5f5d6301..5a1824aa3c5 100644
--- a/api-data-store-server-javascript/src/js/api/server-data-store-facade.js
+++ b/api-data-store-server-javascript/src/js/api/server-data-store-facade.js
@@ -37,7 +37,7 @@ _DataStoreServerInternal.prototype.normalizeUrl = function(openbisUrlOrNull, htt
 		}
 	}
 	
-	return parts.protocol + "://" + parts.authority + httpServerUri;
+	return parts.protocol + "://" + parts.authority + (httpServerUri || parts.path);
 }
 
 _DataStoreServerInternal.prototype.getUrlForMethod = function(method) {
diff --git a/api-openbis-javascript/src/v3/openbis.js b/api-openbis-javascript/src/v3/openbis.js
index 408d1b875fb..35085226c72 100644
--- a/api-openbis-javascript/src/v3/openbis.js
+++ b/api-openbis-javascript/src/v3/openbis.js
@@ -435,42 +435,56 @@ define([ 'jquery', 'util/Json', 'as/dto/datastore/search/DataStoreSearchCriteria
 		}
 	}
 
-    var afsServerFacade = function(asFacade) {
+    var AfsServerFacade = function(asFacade, afsUrl) {
 
-        this.afsServer = new AfsServer("http://localhost:8085", "/data-store-server");
-        this.afsServer.useSession(asFacade._private.sessionToken)
+        if(!afsUrl){
+            throw Error("Please specify AFS server url");
+        }
+
+        var afsServer = new AfsServer(afsUrl);
 
 		this.list = function(owner, source, recursively) {
-            return this.afsServer.list(owner, source, recursively);
+		    useSession();
+            return afsServer.list(owner, source, recursively);
 		}
 
 		this.read = function(owner, source, offset, limit){
-		    return this.afsServer.read(owner, source, offset, limit);
+		    useSession();
+		    return afsServer.read(owner, source, offset, limit);
 		}
 
 		this.write = function(owner, source, offset, data){
-		    return this.afsServer.write(owner, source, offset, data);
+		    useSession();
+		    return afsServer.write(owner, source, offset, data);
 		}
 
 		this.delete = function(owner, source){
-		    return this.afsServer.delete(owner, source);
+		    useSession();
+		    return afsServer.delete(owner, source);
 		}
 
 		this.copy = function(sourceOwner, source, targetOwner, target){
-		    return this.afsServer.copy(sourceOwner, source, targetOwner, target);
+		    useSession();
+		    return afsServer.copy(sourceOwner, source, targetOwner, target);
 		}
 
 		this.move = function(sourceOwner, source, targetOwner, target){
-		    return this.afsServer.move(sourceOwner, source, targetOwner, target);
+		    useSession();
+		    return afsServer.move(sourceOwner, source, targetOwner, target);
 		}
 
 		this.create = function(owner, source, directory){
-		    return this.afsServer.create(owner, source, directory);
+		    useSession();
+		    return afsServer.create(owner, source, directory);
 		}
 
+		function useSession(){
+            afsServer.useSession(asFacade._private.sessionToken)
+        }
+
 	}
 
-	var facade = function(openbisUrl) {
+	var facade = function(openbisUrl, afsUrl) {
 
 		if (!openbisUrl) {
 			openbisUrl = "/openbis/openbis/rmi-application-server-v3.json";
@@ -2458,7 +2472,7 @@ define([ 'jquery', 'util/Json', 'as/dto/datastore/search/DataStoreSearchCriteria
 		}
 
 		this.getAfsServerFacade = function() {
-            return new afsServerFacade(this)
+            return new AfsServerFacade(this, afsUrl)
 		}
 
 		this.getMajorVersion = function() {
diff --git a/api-openbis-typescript/source/java/ch/ethz/sis/openbis/generic/typescript/dto/OpenBISJavaScriptFacade.java b/api-openbis-typescript/source/java/ch/ethz/sis/openbis/generic/typescript/dto/OpenBISJavaScriptFacade.java
index ca7f282643d..62bfcaa899e 100644
--- a/api-openbis-typescript/source/java/ch/ethz/sis/openbis/generic/typescript/dto/OpenBISJavaScriptFacade.java
+++ b/api-openbis-typescript/source/java/ch/ethz/sis/openbis/generic/typescript/dto/OpenBISJavaScriptFacade.java
@@ -259,7 +259,9 @@ public class OpenBISJavaScriptFacade implements IApplicationServerApi
 
     public OpenBISJavaScriptFacade(){}
 
-    public OpenBISJavaScriptFacade(String url){}
+    public OpenBISJavaScriptFacade(String openbisUrl){}
+
+    public OpenBISJavaScriptFacade(String openbisUrl, String afsUrl){}
 
     @TypeScriptMethod(sessionToken = false, async = false)
     public OpenBISJavaScriptDSSFacade getDataStoreFacade(){
diff --git a/test-api-openbis-javascript/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/test/test-afs.ts b/test-api-openbis-javascript/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/test/test-afs.ts
index ee639ad3212..7d8f02ecb69 100644
--- a/test-api-openbis-javascript/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/test/test-afs.ts
+++ b/test-api-openbis-javascript/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/test/test-afs.ts
@@ -55,9 +55,10 @@ exports.default = new Promise((resolve) => {
         }
 
         resolve(function () {
-            executeModule("Afs tests (RequireJS)", new openbisRequireJS(), dtos)
-            executeModule("Afs tests (module VAR)", new window.openbis.openbis(), window.openbis)
-            executeModule("Afs tests (module ESM)", new window.openbisESM.openbis(), window.openbisESM)
+            var afsServerUrl = "http://localhost:8085/data-store-server"
+            executeModule("Afs tests (RequireJS)", new openbisRequireJS(null, afsServerUrl), dtos)
+            executeModule("Afs tests (module VAR)", new window.openbis.openbis(null, afsServerUrl), window.openbis)
+            executeModule("Afs tests (module ESM)", new window.openbisESM.openbis(null, afsServerUrl), window.openbisESM)
         })
     })
 })
diff --git a/test-api-openbis-javascript/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/test/types/openbis.esm.d.ts b/test-api-openbis-javascript/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/test/types/openbis.esm.d.ts
index 68bfe530f7c..cc506ec215c 100644
--- a/test-api-openbis-javascript/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/test/types/openbis.esm.d.ts
+++ b/test-api-openbis-javascript/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/test/types/openbis.esm.d.ts
@@ -1,6 +1,6 @@
 /* tslint:disable */
 /* eslint-disable */
-// Generated using typescript-generator version 3.2.1263 on 2024-03-27 12:40:27.
+// Generated using typescript-generator version 3.2.1263 on 2024-03-28 16:04:27.
 
 export default openbis;
 
@@ -14024,6 +14024,8 @@ export namespace openbis {
         new (): OpenBISJavaScriptFacade;
 
         new (arg0: string): OpenBISJavaScriptFacade;
+
+        new (arg0: string, arg1: string): OpenBISJavaScriptFacade;
     }
 
     interface OperationExecution extends Serializable, ICodeHolder, IDescriptionHolder, IPermIdHolder {
-- 
GitLab