From 78b69e99a47867c4eb352ed4ed4eea9bb386f94f Mon Sep 17 00:00:00 2001
From: felmer <franz-josef.elmer@id.ethz.ch>
Date: Thu, 21 Nov 2019 09:06:15 +0100
Subject: [PATCH] SSDM-8929: Unarchiving Helper: first implementation, not yet
 fully functional.

---
 .../1/as/webapps/eln-lims/html/index.html     |  4 +
 .../eln-lims/html/js/config/Profile.js        |  1 +
 .../html/js/controllers/MainController.js     | 13 ++++
 .../as/webapps/eln-lims/html/js/util/Util.js  | 28 +++++++
 .../widgets/AdvancedEntitySearchDropdown.js   | 26 +------
 .../widgets/AdvancedEntitySearchDropdown2.js  | 69 +++++++++++++++++
 .../js/views/SideMenu/SideMenuWidgetView.js   |  5 ++
 .../UnarchivingHelperController.js            | 49 ++++++++++++
 .../UnarchivingHelperModel.js                 |  3 +
 .../UnarchivingHelperView.js                  | 76 +++++++++++++++++++
 .../archiving-api/plugin.properties           |  2 +
 11 files changed, 251 insertions(+), 25 deletions(-)
 create mode 100644 openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown2.js
 create mode 100644 openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperController.js
 create mode 100644 openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperModel.js
 create mode 100644 openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperView.js
 create mode 100644 openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/archiving-api/plugin.properties

diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/index.html b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/index.html
index 24f2a40f357..035dce26036 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/index.html
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/index.html
@@ -183,6 +183,9 @@
 	<script type="text/javascript" src="./js/views/ExperimentForm/ExperimentFormModel.js"></script>
 	<script type="text/javascript" src="./js/views/ExperimentForm/ExperimentFormView.js"></script>
 	
+	<script type="text/javascript" src="./js/views/UnarchivingHelper/UnarchivingHelperController.js"></script>
+	<script type="text/javascript" src="./js/views/UnarchivingHelper/UnarchivingHelperModel.js"></script>
+	<script type="text/javascript" src="./js/views/UnarchivingHelper/UnarchivingHelperView.js"></script>
 	<script type="text/javascript" src="./js/views/UserManager/UserManagerController.js"></script>
 	<script type="text/javascript" src="./js/views/UserManager/UserManagerModel.js"></script>
 	<script type="text/javascript" src="./js/views/UserManager/UserManagerView.js"></script>
@@ -198,6 +201,7 @@
 	<script type="text/javascript" src="./js/views/DataSetForm/DataSetFormView.js"></script>
 	
 	<script type="text/javascript" src="./js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown.js"></script>
+	<script type="text/javascript" src="./js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown2.js"></script>
 	<script type="text/javascript" src="./js/views/DataSetForm/widgets/DatasetViewerController.js"></script>
 	<script type="text/javascript" src="./js/views/DataSetForm/widgets/DatasetViewerModel.js"></script>
 	<script type="text/javascript" src="./js/views/DataSetForm/widgets/DatasetViewerView.js"></script>
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/Profile.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/Profile.js
index bc6f0493ec6..1d08a2ea8c0 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/Profile.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/Profile.js
@@ -56,6 +56,7 @@ $.extend(DefaultProfile.prototype, {
 				showExports : true,
 				showStorageManager : true,
 				showAdvancedSearch : true,
+				showUnarchivingHelper : true,
 				showTrashcan : true,
 				showSettings : true,
 				showVocabularyViewer : true,
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/MainController.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/MainController.js
index d650bf24b09..527e965714f 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/MainController.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/MainController.js
@@ -462,6 +462,11 @@ function MainController(profile) {
 					this._showAdvancedSearchPage(argToUse);
 					//window.scrollTo(0,0);
 					break;
+				case "showUnarchivingHelperPage":
+					document.title = "Unarchiving Helper";
+					this._showUnarchivingHelper();
+					//window.scrollTo(0,0);
+					break;
 				case "showUserManagerPage":
 					document.title = "User Manager";
 					this._showUserManager();
@@ -1019,6 +1024,14 @@ function MainController(profile) {
 		this.currentView = userManagerController;
 	}
 	
+	this._showUnarchivingHelper = function() {
+		var views = this._getNewViewModel(true, true, false);
+		
+		var unarchivingHelperController = new UnarchivingHelperController(this);
+		unarchivingHelperController.init(views);
+		this.currentView = unarchivingHelperController;
+	}
+	
 	this._showSamplesPage = function(experimentIdentifier) {
 		var views = this._getNewViewModel(true, true, false);
 		
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/Util.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/Util.js
index 8ed2fbcfc30..ca6ade82928 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/Util.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/Util.js
@@ -592,6 +592,34 @@ var Util = new function() {
 		return displayName;
 	}
 	
+	this.getDisplayNameForEntity2 = function(entity) {
+		var text = null;
+		if(entity["@type"] === "as.dto.dataset.DataSet") {
+			text = entity.permId.permId;
+			if(profile.propertyReplacingCode && entity.properties && entity.properties[profile.propertyReplacingCode]) {
+				text += " (" + entity.properties[profile.propertyReplacingCode] + ")";
+			}
+			if(entity.sample) {
+				text += " " + ELNDictionary.Sample + " [" + Util.getDisplayNameForEntity2(entity.sample) + "]";
+			}
+			
+			if(entity.experiment) {
+				text += " " + ELNDictionary.getExperimentDualName() + " [" + Util.getDisplayNameForEntity2(entity.experiment) + "]";
+			}
+		} else {
+			if(entity.identifier && entity.identifier.identifier) {
+				text = entity.identifier.identifier;
+			}
+			if(!entity.identifier && entity.code) {
+				text = Util.getDisplayNameFromCode(entity.code);
+			}
+			if(profile.propertyReplacingCode && entity.properties && entity.properties[profile.propertyReplacingCode]) {
+				text += " (" + entity.properties[profile.propertyReplacingCode] + ")";
+			}
+		}
+		return text;
+	}
+	
 	this.getDisplayNameFromCode = function(openBISCode) {
 		var normalizedCodeParts = openBISCode.toLowerCase().split('_');
 		var displayName = "";
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown.js
index 9d79a2a9355..6e8671454d9 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown.js
@@ -147,31 +147,7 @@ function AdvancedEntitySearchDropdown(isMultiple,
 	// Search Entity
 	//
 	var getDisplayName = function(entity) {
-		var text = null;
-		if(entity["@type"] === "as.dto.dataset.DataSet") {
-			text = entity.permId.permId;
-			if(profile.propertyReplacingCode && entity.properties && entity.properties[profile.propertyReplacingCode]) {
-				text += " (" + entity.properties[profile.propertyReplacingCode] + ")";
-			}
-			if(entity.sample) {
-				text += " " + ELNDictionary.Sample + " [" + getDisplayName(entity.sample) + "]";
-			}
-			
-			if(entity.experiment) {
-				text += " " + ELNDictionary.getExperimentDualName() + " [" + getDisplayName(entity.experiment) + "]";
-			}
-		} else {
-			if(entity.identifier && entity.identifier.identifier) {
-				text = entity.identifier.identifier;
-			}
-			if(!entity.identifier && entity.code) {
-				text = Util.getDisplayNameFromCode(entity.code);
-			}
-			if(profile.propertyReplacingCode && entity.properties && entity.properties[profile.propertyReplacingCode]) {
-				text += " (" + entity.properties[profile.propertyReplacingCode] + ")";
-			}
-		}
-		return text;
+		return Util.getDisplayNameForEntity2(entity);
 	}
 	
 	var searchProject = function(action) {
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown2.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown2.js
new file mode 100644
index 00000000000..8406b0d3d74
--- /dev/null
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/AdvancedEntitySearchDropdown2.js
@@ -0,0 +1,69 @@
+function AdvancedEntitySearchDropdown2(placeholder) {
+	var placeholder = placeholder;
+	this.isMultiple = true;
+	this.isRequired = false;
+	var onChangeCallback = null;
+	
+	this.onChange = function(onChangeCallbackGiven) {
+		onChangeCallback = onChangeCallbackGiven
+	}
+
+	this.search = function(query, callback) {
+		callback([]);
+	}
+	
+	this.renderResult = function(result) {
+		return [];
+	}
+	
+	var $select = FormUtil.getPlainDropdown({}, "");
+	
+	this.init = function($container) {
+		var _this = this;
+		$select.attr("multiple", "multiple");
+		if (this.isRequired) {
+			$select.attr("required", "required");
+		}
+		if (this.isMultiple) {
+			maximumSelectionLength = 9999;
+		} else {
+			maximumSelectionLength = 1;
+		}
+		$container.append($select);
+
+		$select.select2({
+			width: '100%', 
+			theme: "bootstrap",
+			maximumSelectionLength: maximumSelectionLength,
+			minimumInputLength: 2,
+			placeholder : placeholder,
+			ajax: {
+				delay: 1000,
+				processResults: function (data) {
+					return {
+						"results": _this.renderResult(data),
+						"pagination": {
+							"more": false
+						}
+					};
+				},
+				transport: function (params, success, failure) {
+					var query = params.data.q;
+					_this.search(query, success);
+					return {
+						abort : function() { /*Not implemented*/ }
+					}
+				}
+			}
+		});
+
+		if (onChangeCallback) {
+			var onSelectOrUnselect = function (e) {
+				onChangeCallback($select.select2('data'));
+			};
+			$select.on('select2:select', onSelectOrUnselect);
+			$select.on('select2:unselect', onSelectOrUnselect);
+		}
+	}
+
+}
\ No newline at end of file
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SideMenu/SideMenuWidgetView.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SideMenu/SideMenuWidgetView.js
index 65e1b63b9c5..e458b3bd72d 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SideMenu/SideMenuWidgetView.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SideMenu/SideMenuWidgetView.js
@@ -274,6 +274,11 @@ function SideMenuWidgetView(sideMenuWidgetController, sideMenuWidgetModel) {
             treeModelUtils.push({ displayName: "Advanced Search", title : advancedSearchLink, entityType: "ADVANCED_SEARCH", key : "ADVANCED_SEARCH", folder : false, lazy : false, view : "showAdvancedSearchPage", icon : "glyphicon glyphicon-search" });
         }
 
+        if(profile.mainMenu.showUnarchivingHelper) {
+        	var unarchivingHelperLink = _this.getLinkForNode("Unarchiving Helper", "UNARCHIVING_HELPER", "showUnarchivingHelperPage", null, null);
+        	treeModelUtils.push({ displayName: "Unarchiving Helper", title : unarchivingHelperLink, entityType: "UNARCHIVING_HELPER", key : "UNARCHIVING_HELPER", folder : false, lazy : false, view : "showUnarchivingHelperPage", icon : "glyphicon glyphicon-open" });
+        }
+        
         if (profile.mainMenu.showExports || profile.mainMenu.showResearchCollectionExportBuilder || profile.mainMenu.showZenodoExportBuilder) {
             var treeModelExports = [];
 
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperController.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperController.js
new file mode 100644
index 00000000000..b24d5e27347
--- /dev/null
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperController.js
@@ -0,0 +1,49 @@
+function UnarchivingHelperController(mainController) {
+	this._mainController = mainController;
+	this._unarchivingHelperModel = new UnarchivingHelperModel();
+	this._unarchivingHelperView = new UnarchivingHelperView(this, this._unarchivingHelperModel);
+	
+	this.init = function(views) {
+		var _this = this;
+		_this._unarchivingHelperView.repaint(views);
+	}
+
+	this.searchDataSets = function(query, callback) {
+		var _this = this;
+		require([ "as/dto/dataset/search/DataSetSearchCriteria", "as/dto/dataset/fetchoptions/DataSetFetchOptions" ],
+			function(DataSetSearchCriteria, DataSetFetchOptions) {
+				var searchCriteria = new DataSetSearchCriteria();
+				_populateSearchCriteria(searchCriteria, query);
+				var fetchOptions = new DataSetFetchOptions();
+				fetchOptions.withPhysicalData();
+				fetchOptions.withExperiment();
+				fetchOptions.withSample();
+				mainController.openbisV3.searchDataSets(searchCriteria, fetchOptions).done(function(results) {
+					var archivedDataSets = results.getObjects().filter(function (dataSet) {
+						return dataSet.getPhysicalData().getStatus() === "ARCHIVED";
+					});
+					callback(archivedDataSets);
+				});
+			});
+	}
+	
+	_populateSearchCriteria = function(searchCriteria, query) {
+		searchCriteria.withOrOperator();
+		searchCriteria.withProperty("$NAME").thatContains(query);
+		searchCriteria.withCode().thatContains(query);
+		searchCriteria.withSample().withProperty("$NAME").thatContains(query);
+		searchCriteria.withSample().withCode().thatContains(query);
+		searchCriteria.withExperiment().withProperty("$NAME").thatContains(query);
+		searchCriteria.withExperiment().withCode().thatContains(query);
+	}
+
+	this.getInfo = function(ids, callback) {
+		mainController.serverFacade.getArchivingInfo(ids, function(info) {
+			callback(info);
+		});
+	}
+	
+	this.unarchive = function(ids) {
+		alert(ids);
+	}
+}
\ No newline at end of file
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperModel.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperModel.js
new file mode 100644
index 00000000000..f42d645bea7
--- /dev/null
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperModel.js
@@ -0,0 +1,3 @@
+function UnarchivingHelperModel() {
+	this.dataSets = [];
+}
\ No newline at end of file
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperView.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperView.js
new file mode 100644
index 00000000000..1885ee056f3
--- /dev/null
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/UnarchivingHelper/UnarchivingHelperView.js
@@ -0,0 +1,76 @@
+function UnarchivingHelperView(unarchivingHelperController, unarchivingHelperModel) {
+	this._unarchivingHelperController = unarchivingHelperController;
+	this._unarchivingHelperModel = unarchivingHelperModel;
+	
+	this.repaint = function(views) {
+		var _this = this;
+		var $header = views.header;
+		$header.append($("<h1>").append("Unarchiving Helper"));
+		
+		var $container = views.content;
+		$container.empty();
+		$container.append($("<span>").text("Please, enter the names/codes of the archived datasets you want " 
+				+ "to unarchive, or the names/codes of the experiments/objects which contain those datasets."));
+		var $datasetsContainer = $("<div>", { style : "width: 100%;" });
+		$container.append(FormUtil.getFieldForComponentWithLabel($datasetsContainer, "Datasets"));
+		var datasetsSearchDropdown = new AdvancedEntitySearchDropdown2("Select as many datasets as you need");
+		datasetsSearchDropdown.search = this._unarchivingHelperController.searchDataSets;
+		datasetsSearchDropdown.renderResult = this.renderDataSets;
+		datasetsSearchDropdown.isRequired = true;
+		$container.append($("<span>").text("Archiving information:"));
+		var $infosContainer = $("<div>", { style : "width: 100%;" });
+		$container.append($infosContainer);
+		datasetsSearchDropdown.onChange(function(dataSets) {
+			var ids = dataSets.map(d => d.id);
+			_this._unarchivingHelperController.getInfo(ids, function(infos) {
+				$infosContainer.empty();
+				dataSets.forEach(function (dataSet) {
+					var info = infos[dataSet.id];
+					$infosContainer.append(renderInfo(dataSet.text, info));
+				});
+				var totalSize = infos["total size"];
+				var $totalSizeContainer = $("<div>", { style : "width: 100%;" });
+				$totalSizeContainer.append("Unarchiving all of them needs " + totalSize + " bytes free disk space.");
+				$infosContainer.append($totalSizeContainer);
+				var $btnUnarchive = $('<div>', { 'class' : 'btn btn-default', 'text' : 'Unarchive', 'id' : 'unarchive' });
+				$btnUnarchive.click(function() {
+					_this._unarchivingHelperController.unarchive(ids);
+				});
+				$infosContainer.append($btnUnarchive);
+			});
+		});
+		datasetsSearchDropdown.init($datasetsContainer);
+
+	}
+	
+	
+	this.renderDataSets = function(dataSets) {
+		var result = []
+		for (var i = 0; i < dataSets.length; i++) {
+			var dataSet = dataSets[i];
+			var label = Util.getDisplayNameForEntity2(dataSet);
+			result.push({id: dataSet.permId.permId,
+				text: label,
+				data: dataSet
+				});
+		}
+		return result;
+	}
+	
+	renderInfo = function(dataSetLabel, info)
+	{
+		var $infoContainer = $("<div>", { style : "width: 100%;" });
+		$infoContainer.append(dataSetLabel);
+		var $infoDetailsContainer = $("<div>", { style : "width: 100%;" });
+		var dataSetSize = info["size"];
+		var container = info["container"];
+		var containerSize = info["container size"];
+		$infoDetailsContainer.append("has " + dataSetSize + " bytes.");
+		if (containerSize > dataSetSize) {
+			$infoDetailsContainer.append(" It is part of a bundle of " + container.length 
+					+ " datasets. Total size: " + containerSize + " bytes.");
+		}
+		$infoContainer.append($infoDetailsContainer);
+		return $infoContainer;
+	}
+}
\ No newline at end of file
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/archiving-api/plugin.properties b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/archiving-api/plugin.properties
new file mode 100644
index 00000000000..879d4dd8018
--- /dev/null
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/archiving-api/plugin.properties
@@ -0,0 +1,2 @@
+label = Archiving API
+class = ch.systemsx.cisd.openbis.dss.generic.server.plugins.standard.ArchivingAggregationService
-- 
GitLab