From 82395b5a30893787564bdb700f96df3dc8419bae Mon Sep 17 00:00:00 2001
From: felmer <franz-josef.elmer@id.ethz.ch>
Date: Sat, 18 Feb 2023 08:08:35 +0100
Subject: [PATCH] SSDM-12929: SFTP resolver for microscopy

---
 .../eln-tree/resolver-plugins/microscopy.py   | 24 ++++++++++
 .../file-system-plugins/eln-tree/script.py    | 44 +++++++++++++------
 2 files changed, 55 insertions(+), 13 deletions(-)

diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/resolver-plugins/microscopy.py b/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/resolver-plugins/microscopy.py
index 36571d8a4e6..e2d4f56fde2 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/resolver-plugins/microscopy.py
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/resolver-plugins/microscopy.py
@@ -1,3 +1,27 @@
+from ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.search import DataSetSearchCriteria
+from ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.fetchoptions import DataSetFetchOptions
+
+import script
+
 acceptor.hideDataSetType("MICROSCOPY_IMG_CONTAINER")
 acceptor.hideDataSetType("MICROSCOPY_IMG_OVERVIEW")
 acceptor.hideDataSetType("MICROSCOPY_IMG_THUMBNAIL")
+#acceptor.hideSampleType("MICROSCOPY_SAMPLE_TYPE")
+
+def addSampleChildNodes(path, samplePermId, sampleType, response, acceptor, context):
+    dataSetSearchCriteria = DataSetSearchCriteria()
+    parentsSearchCriteria = dataSetSearchCriteria.withSample().withParents()
+    parentsSearchCriteria.withPermId().thatEquals(samplePermId)
+    fetchOptions = DataSetFetchOptions()
+    fetchOptions.withType()
+    fetchOptions.withProperties()
+    fetchOptions.withSample()
+    dataSets = context.getApi().searchDataSets(context.getSessionToken(), dataSetSearchCriteria, fetchOptions).getObjects()
+    for dataSet in dataSets:
+        if acceptor.acceptDataSet(dataSet):
+            dataSetCode = dataSet.getCode()
+            content = context.getContentProvider().asContent(dataSetCode)
+            contentNode = content.getRootNode()
+            script.addDataSetFileNodes(path, dataSetCode, contentNode, response, acceptor, context)
+
+acceptor.sampleChildrenHandlers["MICROSCOPY_EXPERIMENT"] = addSampleChildNodes
diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/script.py b/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/script.py
index c4d8881db88..f3eb85b9711 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/script.py
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/script.py
@@ -21,10 +21,15 @@ class NodeWithEntityType(Node):
     def __init__(self, type, permId, entityType):
         super(NodeWithEntityType, self).__init__(type, permId)
         self.entityType = entityType
+        self.permIds = set()
+        self.addPermId(permId)
 
     def getEntityType(self):
         return self.entityType
 
+    def addPermId(self, permId):
+        self.permIds.add(permId)
+
 class Acceptor(object):
     def __init__(self, settings):
         self.sections = ["Lab Notebook", "Inventory", "Stock"]
@@ -38,6 +43,7 @@ class Acceptor(object):
         self.hiddenExperimentTypes = {}
         self.hiddenSampleTypes = {}
         self.hiddenDataSetTypes = {}
+        self.sampleChildrenHandlers = {}
 
     def assertValidSection(self, section):
         if section not in self.sections:
@@ -234,13 +240,16 @@ def listChildren(subPath, acceptor, context):
     nodeType = node.getType()
     permId = node.getPermId()
     if nodeType == "DATASET":
-        dataSetCode, contentNode, content = getContentNode(permId, context)
-        if contentNode.isDirectory():
-            response = context.createDirectoryResponse()
-            addDataSetFileNodes(path, dataSetCode, contentNode, response, acceptor, context)
-            return response
-        else:
-            return context.createFileResponse(contentNode, content)
+        response = None
+        for permId in node.permIds:
+            dataSetCode, contentNode, content = getContentNode(permId, context)
+            if contentNode.isDirectory():
+                if response is None:
+                    response = context.createDirectoryResponse()
+                addDataSetFileNodes(path, dataSetCode, contentNode, response, acceptor, context)
+            else:
+                response = context.createFileResponse(contentNode, content)
+        return response
     elif nodeType == "SAMPLE":
         response = context.createDirectoryResponse()
         addSampleChildNodes(path, permId, node.getEntityType(), response, acceptor, context)
@@ -259,11 +268,13 @@ def getNode(subPath, acceptor, context):
             nodeType = parentNode.getType()
             entityType = parentNode.getEntityType()
             permId = parentNode.getPermId()
+            print(">>>>>>>>>> GET NODE: %s, %s, %s" % (path, nodeType, parentNode.permIds))
             if nodeType == "EXPERIMENT":
                 addExperimentChildNodes(parentPathString, permId, entityType, None, acceptor, context)
             elif nodeType == "SAMPLE":
                 addSampleChildNodes(parentPathString, permId, entityType, None, acceptor, context)
             elif nodeType == "DATASET":
+                print("====== GET DATASET NODE: %s, %s" % (path, parentNode.permIds))
                 dataSetCode, contentNode, _ = getContentNode(parentNode.getPermId(), context)
                 addDataSetFileNodes(parentPathString, dataSetCode, contentNode, None, acceptor, context)
             else:
@@ -307,6 +318,9 @@ def addExperimentChildNodes(path, experimentPermId, experimentType, response, ac
     addNodes("SAMPLE", entitiesByName, path, response, context)
 
 def addSampleChildNodes(path, samplePermId, sampleType, response, acceptor, context):
+    if sampleType in acceptor.sampleChildrenHandlers:
+        acceptor.sampleChildrenHandlers[sampleType](path, samplePermId, sampleType, response, acceptor, context)
+        return
     dataSetSearchCriteria = DataSetSearchCriteria()
     dataSetSearchCriteria.withSample().withPermId().thatEquals(samplePermId)
 
@@ -335,12 +349,16 @@ def addDataSetFileNodes(path, dataSetCode, contentNode, response, acceptor, cont
         nodeName = childNode.getName()
         filePath = "%s/%s" % (path, nodeName)
         filePermId = "%s::%s" % (dataSetCode, childNode.getRelativePath())
-        context.getCache().putNode(NodeWithEntityType("DATASET", filePermId, None), filePath)
-        if response is not None:
-            if childNode.isDirectory():
-                response.addDirectory(nodeName, childNode.getLastModified())
-            else:
-                response.addFile(nodeName, childNode)
+        cachedNode = context.getCache().getNode(filePath)
+        if cachedNode is not None:
+            cachedNode.addPermId(filePermId)
+        else:
+            context.getCache().putNode(NodeWithEntityType("DATASET", filePermId, None), filePath)
+            if response is not None:
+                if childNode.isDirectory():
+                    response.addDirectory(nodeName, childNode.getLastModified())
+                else:
+                    response.addFile(nodeName, childNode)
 
 def getContentNode(permId, context):
     splittedId = permId.split("::")
-- 
GitLab