From 5a2195d9b0f13af9dca730f30a60f679791363d7 Mon Sep 17 00:00:00 2001
From: felmer <franz-josef.elmer@id.ethz.ch>
Date: Fri, 17 Mar 2023 18:44:19 +0100
Subject: [PATCH] SSDM-12929: refactor code duplication in the plugins by
 introducing function getDataSetsOfSampleAndItsChildren() in the main script

---
 .../eln-tree/resolver-plugins/flow.py         | 25 ++-----------------
 .../eln-tree/resolver-plugins/microscopy.py   | 14 +----------
 .../file-system-plugins/eln-tree/script.py    | 12 +++++++++
 3 files changed, 15 insertions(+), 36 deletions(-)

diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/resolver-plugins/flow.py b/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/resolver-plugins/flow.py
index 40f7509e0fe..5e1141655e9 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/resolver-plugins/flow.py
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/dss/file-system-plugins/eln-tree/resolver-plugins/flow.py
@@ -1,19 +1,7 @@
-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
 
 def addSampleChildNodes(path, samplePermId, sampleType, response, acceptor, context):
-    dataSetSearchCriteria = DataSetSearchCriteria()
-    dataSetSearchCriteria.withOrOperator()
-    dataSetSearchCriteria.withSample().withPermId().thatEquals(samplePermId)
-    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()
+    dataSets = script.getDataSetsOfSampleAndItsChildren(samplePermId, context)
     for dataSet in dataSets:
         if acceptor.acceptDataSet(dataSet):
             dataSetCode = dataSet.getCode()
@@ -22,16 +10,7 @@ def addSampleChildNodes(path, samplePermId, sampleType, response, acceptor, cont
             script.addDataSetFileNodes(path, dataSetCode, contentNode, response, acceptor, context)
 
 def addSampleChildNodesWithPlates(path, samplePermId, sampleType, response, acceptor, context):
-    dataSetSearchCriteria = DataSetSearchCriteria()
-    dataSetSearchCriteria.withOrOperator()
-    dataSetSearchCriteria.withSample().withPermId().thatEquals(samplePermId)
-    parentsSearchCriteria = dataSetSearchCriteria.withSample().withParents()
-    parentsSearchCriteria.withPermId().thatEquals(samplePermId)
-    fetchOptions = DataSetFetchOptions()
-    fetchOptions.withType()
-    fetchOptions.withProperties()
-    fetchOptions.withSample().withType()
-    dataSets = context.getApi().searchDataSets(context.getSessionToken(), dataSetSearchCriteria, fetchOptions).getObjects()
+    dataSets = script.getDataSetsOfSampleAndItsChildren(samplePermId, context)
     for dataSet in dataSets:
         sampleTypeCode = dataSet.getSample().getType().getCode()
         if not sampleTypeCode.endswith("_WELL"):
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 68885e4499b..7429a9d6a6d 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,6 +1,3 @@
-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")
@@ -9,16 +6,7 @@ acceptor.hideDataSetType("MICROSCOPY_IMG_THUMBNAIL")
 #acceptor.hideSampleType("MICROSCOPY_SAMPLE_TYPE")
 
 def addSampleChildNodes(path, samplePermId, sampleType, response, acceptor, context):
-    dataSetSearchCriteria = DataSetSearchCriteria()
-    dataSetSearchCriteria.withOrOperator()
-    dataSetSearchCriteria.withSample().withPermId().thatEquals(samplePermId)
-    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()
+    dataSets = script.getDataSetsOfSampleAndItsChildren(samplePermId, context)
     for dataSet in dataSets:
         if acceptor.acceptDataSet(dataSet):
             dataSetCode = dataSet.getCode()
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 8909b6834b1..37f6c5a8737 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
@@ -359,6 +359,18 @@ def getContentNode(permId, context):
     contentNode = content.tryGetNode(splittedId[1]) if len(splittedId) > 1 else content.getRootNode()
     return dataSetCode, contentNode, content
 
+def getDataSetsOfSampleAndItsChildren(samplePermId, context):
+    dataSetSearchCriteria = DataSetSearchCriteria()
+    dataSetSearchCriteria.withOrOperator()
+    dataSetSearchCriteria.withSample().withPermId().thatEquals(samplePermId)
+    parentsSearchCriteria = dataSetSearchCriteria.withSample().withParents()
+    parentsSearchCriteria.withPermId().thatEquals(samplePermId)
+    fetchOptions = DataSetFetchOptions()
+    fetchOptions.withType()
+    fetchOptions.withProperties()
+    fetchOptions.withSample().withType()
+    return context.getApi().searchDataSets(context.getSessionToken(), dataSetSearchCriteria, fetchOptions).getObjects()
+
 def listDataSets(path, dataSetSearchCriteria, assignedToSample, response, acceptor, context):
     fetchOptions = DataSetFetchOptions()
     fetchOptions.withType()
-- 
GitLab