From 3b05e12161d55fbfc342f0b78a7d3d0ee7bf6d2b Mon Sep 17 00:00:00 2001
From: pkupczyk <pkupczyk>
Date: Tue, 21 Jan 2014 10:14:58 +0000
Subject: [PATCH] SP-1118 / BIS-628 : iPad App for Aebersold Lab

SVN: 30400
---
 .../ipad-tag-browser-service/ipad_read.py     | 182 ++++++++++++++----
 1 file changed, 146 insertions(+), 36 deletions(-)

diff --git a/openbis-ipad/source/core-plugins/ipad-ui-tag-browser/1/dss/reporting-plugins/ipad-tag-browser-service/ipad_read.py b/openbis-ipad/source/core-plugins/ipad-ui-tag-browser/1/dss/reporting-plugins/ipad-tag-browser-service/ipad_read.py
index 04b7f3ebda0..edacba5e33a 100644
--- a/openbis-ipad/source/core-plugins/ipad-ui-tag-browser/1/dss/reporting-plugins/ipad-tag-browser-service/ipad_read.py
+++ b/openbis-ipad/source/core-plugins/ipad-ui-tag-browser/1/dss/reporting-plugins/ipad-tag-browser-service/ipad_read.py
@@ -7,16 +7,30 @@ from ch.systemsx.cisd.openbis.generic.shared.basic.dto import MaterialIdentifier
 from com.fasterxml.jackson.databind import ObjectMapper
 from ch.systemsx.cisd.openbis.generic.shared.api.v1.dto import SearchCriteria, SearchSubCriteria
 from java.util import Date
+from java.util import HashMap
 
 from datetime import datetime
 
+##########
+# Request
+##########
+
+def getEntitiesParameter(handler):
+	entities = handler.getEntitiesParameter();
+	for entity in entities:
+		refcon = entity['REFCON']
+		if type(refcon) == str or type(refcon) == unicode:
+			refconMap = ObjectMapper().readValue(refcon, HashMap().getClass())
+			entity['REFCON'] = refconMap
+	return entities
+
 ###############
 # Dictionaries
 ###############
 
 def createTagDictionary(name, children):
 	dictionary = {}
-	dictionary['PERM_ID'] = 'TAG#' + name.upper()
+	dictionary['PERM_ID'] = 'TAG.' + name.upper()
 	dictionary['CATEGORY'] = 'Navigation'
 	dictionary['SUMMARY_HEADER'] = name
 	dictionary['SUMMARY'] = None
@@ -29,41 +43,41 @@ def createTagDictionary(name, children):
 	
 	return dictionary
 
-def createMaterialDictionary(material):
+def createExperimentDictionary(experiment):
 	dictionary = {}
-	dictionary['PERM_ID'] = 'MATERIAL#' + material.getMaterialIdentifier()
-	dictionary['CATEGORY'] = 'Material ' + material.getMaterialType()
-	dictionary['SUMMARY_HEADER'] = material.getCode()
-	dictionary['SUMMARY'] = material.getPropertyValue("DESC")
-	dictionary['IDENTIFIER'] = material.getMaterialIdentifier()
-	dictionary['IMAGES'] = []
-	dictionary['CHILDREN'] = []
+	dictionary['PERM_ID'] = 'EXPERIMENT.' + experiment.getPermId()
+	dictionary['CATEGORY'] = 'Experiment ' + experiment.getExperimentType()
+	dictionary['SUMMARY_HEADER'] = experiment.getExperimentIdentifier()
+	dictionary['SUMMARY'] = None
+	dictionary['IDENTIFIER'] = experiment.getExperimentIdentifier()
+	dictionary['IMAGES'] = IpadServiceUtilities.jsonEncodedValue([])
+	dictionary['CHILDREN'] = IpadServiceUtilities.jsonEncodedValue([])
 	dictionary['ROOT_LEVEL'] = None
 	
 	refcon = {}
-	refcon['ENTITY_TYPE'] =  'MATERIAL'
-	refcon['CODE'] = material.getCode()
-	refcon['TYPE_CODE'] = material.getMaterialType();
+	refcon['ENTITY_TYPE'] =  'EXPERIMENT'
+	# there is no way to get an experiment by a permanent id
+	refcon['IDENTIFIER'] = experiment.getExperimentIdentifier()
 	dictionary['REFCON'] = IpadServiceUtilities.jsonEncodedValue(refcon)
 	
 	return dictionary
-
-def createMaterialDetailedDictionary(material):
-	dictionary = createMaterialDictionary(material)
-	propertyDefinitions = getPropertyDefinitions(material.getMaterialType(), searchService.listPropertiesDefinitionsForMaterialType)
-	properties = getProperties(material, propertyDefinitions)
+	
+def createExperimentDetailedDictionary(experiment):
+	dictionary = createExperimentDictionary(experiment)
+	propertyDefinitions = getPropertyDefinitions(experiment.getExperimentType(), searchService.listPropertiesDefinitionsForExperimentType)
+	properties = getProperties(experiment, propertyDefinitions)
 	dictionary['PROPERTIES'] = IpadServiceUtilities.jsonEncodedValue(properties) 
 	return dictionary
 
 def createSampleDictionary(sample):
 	dictionary = {}
-	dictionary['PERM_ID'] = 'SAMPLE#' + sample.getPermId()
+	dictionary['PERM_ID'] = 'SAMPLE.' + sample.getPermId()
 	dictionary['CATEGORY'] = 'Sample ' + sample.getSampleType()	
-	dictionary['SUMMARY_HEADER'] = sample.getCode()
-	dictionary['SUMMARY'] = sample.getPropertyValue("DESC")
+	dictionary['SUMMARY_HEADER'] = sample.getSampleIdentifier()
+	dictionary['SUMMARY'] = None
 	dictionary['IDENTIFIER'] = sample.getSampleIdentifier()
-	dictionary['IMAGES'] = []
-	dictionary['CHILDREN'] = []
+	dictionary['IMAGES'] = IpadServiceUtilities.jsonEncodedValue([])
+	dictionary['CHILDREN'] = IpadServiceUtilities.jsonEncodedValue([])
 	dictionary['ROOT_LEVEL'] = None
 	
 	refcon = {}
@@ -79,14 +93,67 @@ def createSampleDetailedDictionary(sample):
 	properties = getProperties(sample, propertyDefinitions)
 	dictionary['PROPERTIES'] = IpadServiceUtilities.jsonEncodedValue(properties) 
 	return dictionary
+
+def createDataSetDictionary(dataSet):
+	dictionary = {}
+	dictionary['PERM_ID'] = 'DATA_SET.' + dataSet.getDataSetCode()
+	dictionary['CATEGORY'] = 'Data set ' + dataSet.getDataSetType()	
+	dictionary['SUMMARY_HEADER'] = dataSet.getDataSetCode()
+	dictionary['SUMMARY'] = None
+	dictionary['IDENTIFIER'] = dataSet.getDataSetCode()
+	dictionary['IMAGES'] = IpadServiceUtilities.jsonEncodedValue([])
+	dictionary['CHILDREN'] = IpadServiceUtilities.jsonEncodedValue([])
+	dictionary['ROOT_LEVEL'] = None
+	
+	refcon = {}
+	refcon['ENTITY_TYPE'] =  'DATA_SET'
+	refcon['CODE'] = dataSet.getDataSetCode()
+	dictionary['REFCON'] = IpadServiceUtilities.jsonEncodedValue(refcon)
+	
+	return dictionary
 	
+def createDataSetDetailedDictionary(dataSet):
+	dictionary = createDataSetDictionary(dataSet)
+	propertyDefinitions = getPropertyDefinitions(dataSet.getDataSetType(), searchService.listPropertiesDefinitionsForDataSetType)
+	properties = getProperties(dataSet, propertyDefinitions)
+	dictionary['PROPERTIES'] = IpadServiceUtilities.jsonEncodedValue(properties) 
+	return dictionary
+
+def createMaterialDictionary(material):
+	dictionary = {}
+	dictionary['PERM_ID'] = 'MATERIAL.' + material.getMaterialIdentifier()
+	dictionary['CATEGORY'] = 'Material ' + material.getMaterialType()
+	dictionary['SUMMARY_HEADER'] = material.getMaterialIdentifier()
+	dictionary['SUMMARY'] = None
+	dictionary['IDENTIFIER'] = material.getMaterialIdentifier()
+	dictionary['IMAGES'] = IpadServiceUtilities.jsonEncodedValue([])
+	dictionary['CHILDREN'] = IpadServiceUtilities.jsonEncodedValue([])
+	dictionary['ROOT_LEVEL'] = None
+	
+	refcon = {}
+	refcon['ENTITY_TYPE'] =  'MATERIAL'
+	refcon['CODE'] = material.getCode()
+	refcon['TYPE_CODE'] = material.getMaterialType();
+	dictionary['REFCON'] = IpadServiceUtilities.jsonEncodedValue(refcon)
+	
+	return dictionary
+
+def createMaterialDetailedDictionary(material):
+	dictionary = createMaterialDictionary(material)
+	propertyDefinitions = getPropertyDefinitions(material.getMaterialType(), searchService.listPropertiesDefinitionsForMaterialType)
+	properties = getProperties(material, propertyDefinitions)
+	dictionary['PROPERTIES'] = IpadServiceUtilities.jsonEncodedValue(properties) 
+	return dictionary
+
+
 #########
 # Entity
 #########
 
-def getExperiment(permId):
-	pass
-
+def getExperiment(identifier):
+	# there is no way to get an experiment by a permanent id
+	return searchService.getExperiment(identifier)
+	
 def getSample(permId):
 	criteria = SearchCriteria()
 	criteria.setOperator(criteria.SearchOperator.MATCH_ANY_CLAUSES)
@@ -98,10 +165,17 @@ def getSample(permId):
 		return None
 
 def getDataSet(code):
-	pass
+	criteria = SearchCriteria()
+	criteria.setOperator(criteria.SearchOperator.MATCH_ANY_CLAUSES)
+	criteria.addMatchClause(criteria.MatchClause.createAttributeMatch(criteria.MatchClauseAttribute.CODE, code))
+	datasets = searchService.searchForDataSets(criteria)
+	if datasets:
+		return datasets[0]
+	else:
+		return None
 
 def getMaterial(code, typeCode):
-	pass
+	return searchService.getMaterial(code, typeCode)
 
 ####################
 # Entity Properties
@@ -115,11 +189,13 @@ def getPropertyDefinitions(typeCode, searchFunction):
 def getProperties(entity, propertyDefinitions):
 	properties = []
 	for propertyDefinition in propertyDefinitions:
-		properties.append({
-			'key' : propertyDefinition.getPropertyTypeCode(), 
-			'label' : propertyDefinition.getPropertyTypeLabel(), 
-			'value' : entity.getPropertyValue(propertyDefinition.getPropertyTypeCode()) 
-		})
+		propertyValue = entity.getPropertyValue(propertyDefinition.getPropertyTypeCode())
+		if propertyValue:
+			properties.append({
+				'key' : propertyDefinition.getPropertyTypeCode(), 
+				'label' : propertyDefinition.getPropertyTypeLabel(), 
+				'value' : propertyValue
+			})
 	return properties
 	
 ###################
@@ -137,13 +213,33 @@ class TagRootRequestHandler(RootRequestHandler):
 	"""Handler for the ROOT request."""
 	
 	def addDataRows(self):
-		tagsPermIdsAndRefcons = self.getEntitiesParameter()
+		tagsPermIdsAndRefcons = getEntitiesParameter(self)
 		
 		if tagsPermIdsAndRefcons:
 			tagName = tagsPermIdsAndRefcons[0]['REFCON']['NAME']
-			samples = self.searchService.getMetaprojectAssignments(tagName).getSamples();
-			self.addRows([createTagDictionary(tagName, [sample.getPermId() for sample in samples])])
+			tagAssignments = self.searchService.getMetaprojectAssignments(tagName)
+			
+			experiments = tagAssignments.getExperiments();
+			samples = tagAssignments.getSamples();
+			dataSets = tagAssignments.getDataSets();
+			materials = tagAssignments.getMaterials();
+			
+			tagChildren = []
+			
+			for experiment in experiments:
+				tagChildren.append('EXPERIMENT.' + experiment.getPermId())
+			for sample in samples:
+				tagChildren.append('SAMPLE.' + sample.getPermId())
+			for dataSet in dataSets:
+				tagChildren.append('DATA_SET.' + dataSet.getDataSetCode())
+			for material in materials:
+				tagChildren.append('MATERIAL.' + material.getMaterialIdentifier())
+			
+			self.addRows([createTagDictionary(tagName, tagChildren)])
+			self.addRows([createExperimentDictionary(experiment) for experiment in experiments])
 			self.addRows([createSampleDictionary(sample) for sample in samples])
+			self.addRows([createDataSetDictionary(dataSet) for dataSet in dataSets])
+			self.addRows([createMaterialDictionary(material) for material in materials])
 
 class TagDrillRequestHandler(DrillRequestHandler):
 	"""Handler for the DRILL request."""
@@ -152,15 +248,24 @@ class TagDetailRequestHandler(DetailRequestHandler):
 	"""Handler for the DETAIL request."""
 
 	def addDataRows(self):
-		entitiesPermIdsAndRefcons = self.getEntitiesParameter()
+		entitiesPermIdsAndRefcons = getEntitiesParameter(self)
 		
 		if entitiesPermIdsAndRefcons:
 			entityRefcon = entitiesPermIdsAndRefcons[0]['REFCON'] 
 			entityType = entityRefcon['ENTITY_TYPE'];
 			
+			if 'EXPERIMENT' == entityType:
+				experiment = getExperiment(entityRefcon['IDENTIFIER'])
+				self.addRows([createExperimentDetailedDictionary(experiment)])
 			if 'SAMPLE' == entityType:
 				sample = getSample(entityRefcon['PERM_ID'])
 				self.addRows([createSampleDetailedDictionary(sample)])
+			if 'DATA_SET' == entityType:
+				dataSet = getDataSet(entityRefcon['CODE'])
+				self.addRows([createDataSetDetailedDictionary(dataSet)])
+			if 'MATERIAL' == entityType:
+				material = getMaterial(entityRefcon['CODE'], entityRefcon['TYPE_CODE'])
+				self.addRows([createMaterialDetailedDictionary(material)])
 
 class TagSearchRequestHandler(SearchRequestHandler):
 	"""Handler for the SEARCH request"""
@@ -168,8 +273,13 @@ class TagSearchRequestHandler(SearchRequestHandler):
 	def addDataRows(self):
 		criteria = self.trySearchCriteria()
 		if criteria:
+			# there is no way to search for experiments and materials
+			
 			samples = self.searchService.searchForSamples(criteria)
 			self.addRows([createSampleDictionary(sample) for sample in samples])
+			
+			dataSets = self.searchService.searchForDataSets(criteria)
+			self.addRows([createDataSetDictionary(dataSet) for dataSet in dataSets])
 
 ####################
 # Request Factories
-- 
GitLab