From ebf0091c0de0c2425edb99e832fb95f7cb6ab780 Mon Sep 17 00:00:00 2001
From: juanf <juanf@bsse-bs-dock-128-101.ethz.ch>
Date: Fri, 20 Apr 2018 16:39:16 +0200
Subject: [PATCH] SSDM-6099 : Show only last version of a dataset belonging to
 a repository_id or history_id

---
 .../widgets/DatasetViewerController.js        | 74 ++++++++++++++++---
 .../DataSetForm/widgets/DatasetViewerModel.js | 30 ++++++--
 2 files changed, 89 insertions(+), 15 deletions(-)

diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/DatasetViewerController.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/DatasetViewerController.js
index 47318b737c0..00e6c09869c 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/DatasetViewerController.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/DatasetViewerController.js
@@ -75,12 +75,8 @@ function DataSetViewerController(containerId, profile, entity, serverFacade, dat
 		var _this = this;
 		var datasetPermIds = [];
 		
-		_this._datasetViewerModel.datasets = datasets; //In case they are loaded after the model is already created.
-		
 		for(var i = 0; i < datasets.length; i++) { //DataSets for entity
-			var dataset = datasets[i];
-			this._datasetViewerModel.entityDataSets[dataset.code] = dataset;
-			datasetPermIds.push(dataset.code);
+			datasetPermIds.push(datasets[i].code);
 		}
 		
 		require([ "as/dto/dataset/id/DataSetPermId", "as/dto/dataset/fetchoptions/DataSetFetchOptions" ],
@@ -92,11 +88,71 @@ function DataSetViewerController(containerId, profile, entity, serverFacade, dat
 					}
 		            var fetchOptions = new DataSetFetchOptions();
 		            fetchOptions.withLinkedData().withExternalDms();
-		            mainController.openbisV3.getDataSets(ids, fetchOptions).done(function(map) {
-		            	for(var dIdx = 0; dIdx < datasetPermIds.length; dIdx++) {
-							_this._datasetViewerModel.v3Datasets.push(map[datasetPermIds[dIdx]]);
+		            fetchOptions.withProperties();
+		            var parentFetchOptions = fetchOptions.withParents();
+		            parentFetchOptions.withLinkedData().withExternalDms();
+		            parentFetchOptions.withProperties();
+		            
+					mainController.openbisV3.getDataSets(ids, fetchOptions).done(function(map) {
+					
+						var toShow = {
+									standard : {},
+									lastVersionOfLinked : {},
+									lastVersionOfHistory: {}
+						}
+						
+			            	for(var dIdx = 0; dIdx < datasetPermIds.length; dIdx++) {
+			            			var dataset = map[datasetPermIds[dIdx]];
+								
+								if(dataset.linkedData && dataset.linkedData.contentCopies && dataset.linkedData.contentCopies[0]) {
+									var repositoryId = dataset.linkedData.contentCopies[0].gitRepositoryId;
+									//console.log("dataset: " + dataset.permId.permId + " repository: " + repositoryId);
+									var lastInToShow = toShow.lastVersionOfLinked[repositoryId];
+									if(!lastInToShow || dataset.registrationDate > lastInToShow.registrationDate) {
+										toShow.lastVersionOfLinked[repositoryId] = dataset;
+									}
+								} else if(dataset.properties && dataset.properties["HISTORY_ID"]) {
+									var historyId = dataset.properties["HISTORY_ID"];
+									//console.log("dataset: " + dataset.permId.permId + " history: " + historyId);
+									var lastInToShow = toShow.lastVersionOfHistory[historyId];
+									if(!lastInToShow || dataset.registrationDate > lastInToShow.registrationDate) {
+										toShow.lastVersionOfHistory[historyId] = dataset;
+									}
+								} else {
+									//console.log("dataset: " + dataset.permId.permId);
+									toShow.standard[dataset.permId.permId] = dataset;
+								}
+						}
+						
+						// Rebuild other lists
+						for(repositoryId in toShow.lastVersionOfLinked) {
+							var dataset = toShow.lastVersionOfLinked[repositoryId];
+							toShow.standard[dataset.permId.permId] = dataset;
+						}
+						for(historyId in toShow.lastVersionOfHistory) {
+							var dataset = toShow.lastVersionOfHistory[historyId];
+							toShow.standard[dataset.permId.permId] = dataset;
 						}
-		            	_this._datasetViewerView.repaintDatasets();
+						
+						// V3 List
+						for(datasetId in toShow.standard) {
+							_this._datasetViewerModel.v3Datasets.push(toShow.standard[datasetId]);
+						}
+						
+						// TODO : Legacy list to be removed
+						var v1Standard = [];
+						for(var i = 0; i < datasets.length; i++) { //DataSets for entity
+							var dataset = datasets[i];
+							if(toShow.standard[dataset.code]) {
+								_this._datasetViewerModel.entityDataSets[dataset.code] = dataset;
+								v1Standard.push(dataset);
+							}
+						}
+						
+						// TODO : Legacy list to be removed
+						_this._datasetViewerModel.datasets = v1Standard; //In case they are loaded after the model is already created.
+					
+		            		_this._datasetViewerView.repaintDatasets();
 		            });
 		});
 		
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/DatasetViewerModel.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/DatasetViewerModel.js
index 5970347b0f1..2395ca44039 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/DatasetViewerModel.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/widgets/DatasetViewerModel.js
@@ -100,8 +100,17 @@ function DataSetViewerModel(containerId, profile, entity, serverFacade, datastor
 	}
 
 	this.isLinkDataset = function(datasetCode) {
-		for(var idx = 0; idx < this.datasets.length; idx++) {
-			if(this.datasets[idx].code === datasetCode && this.datasets[idx].linkDataSet) {
+		for(var idx = 0; idx < this.v3Datasets.length; idx++) {
+			if(this.v3Datasets[idx].code === datasetCode && this.v3Datasets[idx].linkedData) {
+				return true;
+			}
+		}
+		return false;
+	}
+	
+	this.isHistoryDataset = function(datasetCode) {
+		for(var idx = 0; idx < this.v3Datasets.length; idx++) {
+			if(this.v3Datasets[idx].code === datasetCode && this.v3Datasets[idx].properties["HISTORY_ID"]) {
 				return true;
 			}
 		}
@@ -125,10 +134,19 @@ function DataSetViewerModel(containerId, profile, entity, serverFacade, datastor
 	}
 	
 	this.getDirectDirectoryLink = function(datasetCode, pathInDataSet) {
-		var directLinkComponent = null;
+		var directLinkComponent = "";
+		var isLinked = false;
+		
 		if(this.isLinkDataset(datasetCode)) {
 			directLinkComponent = "<span class='glyphicon glyphicon-link'></span>";
-		} else if(profile.directLinkEnabled && (profile.cifsFileServer || profile.sftpFileServer)) {
+			isLinked = true;
+		}
+		
+		if(this.isHistoryDataset(datasetCode)) {
+			directLinkComponent = "<span class='glyphicon glyphicon-time'></span>";
+		}
+		
+		if(!isLinked && profile.directLinkEnabled && (profile.cifsFileServer || profile.sftpFileServer)) {
 			var path = null;
 			
 			if(this.isExperiment()) {
@@ -136,8 +154,8 @@ function DataSetViewerModel(containerId, profile, entity, serverFacade, datastor
 			} else {
 				path = this.entity.experimentIdentifierOrNull.substring(1) + "/" + datasetCode + "/" + pathInDataSet + "/";
 			}
-			
-			directLinkComponent = "<span onclick=\"" + "Util.showDirectLink('" + path + "')" + "\" class='glyphicon glyphicon-hdd'></span>";
+				
+			directLinkComponent += "<span onclick=\"" + "Util.showDirectLink('" + path + "')" + "\" class='glyphicon glyphicon-hdd'></span>";
 		}
 		return directLinkComponent;
 	}
-- 
GitLab