Skip to content
Snippets Groups Projects
Commit 727f9c6e authored by yvesn's avatar yvesn
Browse files

SSDM-7428: added new core plugin for storing and retrieving V3 API searches

parent 6bd4425f
No related branches found
No related tags found
No related merge requests found
'''
@copyright:
2016 ETH Zuerich, SIS
@license:
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import ch.systemsx.cisd.openbis.generic.server.jython.api.v1.DataType as DataType
SAMPLE_TYPE_SEARCH_QUERY = "SEARCH_QUERY"
# we use XML to store the properties to avoid indexing of the fields
PROPERTY_TYPES = [
{
'code': 'NAME',
'dataType': DataType.VARCHAR,
'label': 'Name',
'mandatory': True,
'description': 'Human readable name'
},
{
'code': 'SEARCH_CRITERIA',
'dataType': DataType.XML,
'label': 'Search criteria',
'mandatory': True,
'description': 'V3 API search criteria'
},
{
'code': "FETCH_OPTIONS",
'dataType': DataType.XML,
'label': 'Fetch options',
'mandatory': False,
'description': 'V3 API fetch options'
},
{
'code': "CUSTOM_DATA",
'dataType': DataType.XML,
'label': 'Custom data',
'mandatory': False,
'description': 'Additional data in custom format'
}
]
tr = service.transaction()
sample_type = tr.getOrCreateNewSampleType(SAMPLE_TYPE_SEARCH_QUERY)
sample_type.setGeneratedCodePrefix("Q")
for propert_type_def in PROPERTY_TYPES:
property_type = tr.getOrCreateNewPropertyType(propert_type_def['code'], propert_type_def['dataType'])
property_type.setLabel(propert_type_def['label'])
property_type.setDescription(propert_type_def['description'])
assignment = tr.assignPropertyType(sample_type, property_type)
assignment.setMandatory(propert_type_def['mandatory'])
assignment.setShownEdit(True)
# openBIS search store
## Description
This plugin provides an API for storing searches in the form of V3 API
search criteria and fetch options.
## Masterdata
The SEARCH_QUERY sample type is created to store the search criteria,
fetch options, name and custom data. To avoid indexing, the search
criteria, fetch options and custom data are stored as XML fields.
## Service
The service provices methods to save, update, load and delete the
searches.
class = ch.ethz.sis.openbis.generic.server.asapi.v3.helper.service.JythonBasedCustomASServiceExecutor
script-path = search-store.py
#
# Copyright 2014 ETH Zuerich, Scientific IT Services
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
from ch.ethz.sis.openbis.generic.server.sharedapi.v3.json import GenericObjectMapper
from ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.create import SampleCreation
from ch.ethz.sis.openbis.generic.asapi.v3.dto.entitytype.id import EntityTypePermId
from ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.id import ExperimentPermId
from ch.ethz.sis.openbis.generic.asapi.v3.dto.space.id import SpacePermId
from ch.ethz.sis.openbis.generic.asapi.v3.dto.project.id import ProjectPermId
from ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.search import SampleSearchCriteria
from ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.fetchoptions import SampleFetchOptions
from ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.id import SamplePermId
from ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.update import SampleUpdate
from ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.delete import SampleDeletionOptions
SAMPLE_TYPE = 'SEARCH_QUERY'
PROP_NAME = 'NAME'
PROP_SEARCH_CRITERIA = 'SEARCH_CRITERIA'
PROP_FETCH_OPTIONS = 'FETCH_OPTIONS'
PROP_CUSTOM_DATA = 'CUSTOM_DATA'
PREFIX_XML = '<xml><![CDATA['
POSTFIX_XML = ']]></xml>'
def process(context, parameters):
""" Entry point for all methods.
:param parameters['method']: SAVE, UPDATE, LOAD or DELETE
see specific functions for more parameters.
"""
method = parameters['method']
if method == 'SAVE':
return save(context, parameters)
elif method == 'UPDATE':
return update(context, parameters)
elif method == 'LOAD':
return load(context, parameters)
elif method == 'DELETE':
return delete(context, parameters)
return None
def save(context, parameters):
""" Saves a search query in a sample.
:param parameters['spacePermId']: (optional) permId of the space in which to save the search
:param parameters['projectPermId']: (optional) permId of the project in which to save the search
:param parameters['experimentPermId']: (optional) permId of the experiment in which to save the search
:param parameters['name']: human readable name of the search
:param parameters['searchCriteria']: V3 search criteria
:param parameters['fetchOptions']: (optional) V3 fetch options
:param parameters['customData']: (optional) additional data in JSON format
"""
result = _save(context, parameters)
if len(result) > 0 and result[0].permId is not None:
permId = SamplePermId(result[0].permId)
fetchOptions = _getSampleFetchOptions()
result = context.applicationService.getSamples(context.sessionToken, [permId], fetchOptions)
return result[permId]
else:
return result
def update(context, parameters):
""" Updates an existing search.
:param parameters['permId']: permId of the search sample to update
:param parameters['name']: (optional) human readable name of the search
:param parameters['searchCriteria']: (optional) V3 search crieria
:param parameters['fetchOptions']: (optional) V3 fetch options
:param parameters['customData']: (optional) additional data in JSON format
"""
sampleUpdate = SampleUpdate()
sampleUpdate.setSampleId(SamplePermId(parameters['permId']))
if 'name' in parameters:
sampleUpdate.setProperty(PROP_NAME, _serialize(parameters['name']))
if 'searchCriteria' in parameters:
sampleUpdate.setProperty(PROP_SEARCH_CRITERIA, _serialize(parameters['searchCriteria']))
if 'fetchOptions' in parameters:
sampleUpdate.setProperty(PROP_FETCH_OPTIONS, _serialize(parameters['fetchOptions']))
if 'customData' in parameters:
sampleUpdate.setProperty(PROP_CUSTOM_DATA, _serialize(parameters['customData']))
context.applicationService.updateSamples(context.sessionToken, [sampleUpdate])
return True
def load(context, parameters):
""" Loads all stores query samples.
"""
searchCriteria = SampleSearchCriteria()
searchCriteria.withType().withCode().thatEquals(SAMPLE_TYPE)
fetchOptions = _getSampleFetchOptions()
result = context.applicationService.searchSamples(context.sessionToken, searchCriteria, fetchOptions)
return result
def delete(context, parameters):
""" Deletes a stored search.
:param parameters['permId']: permId of the search to delete
:param parameters['reason']: reason for deletion
"""
permId = SamplePermId(parameters['permId'])
reason = parameters['reason']
deletionOptions = SampleDeletionOptions()
deletionOptions.setReason(reason)
result = context.applicationService.deleteSamples(context.sessionToken, [permId], deletionOptions)
return result
def _save(context, parameters):
sampleCreation = SampleCreation()
sampleCreation.setAutoGeneratedCode(True)
sampleCreation.setTypeId(EntityTypePermId(SAMPLE_TYPE))
if 'experimentPermId' in parameters:
sampleCreation.setExperimentId(ExperimentPermId(parameters['experimentPermId']))
if 'spacePermId' in parameters:
sampleCreation.setSpaceId(SpacePermId(parameters['spacePermId']))
if 'projectPermId' in parameters:
sampleCreation.setProjectId(ProjectPermId(parameters['projectPermId']))
sampleCreation.setProperty(PROP_NAME, parameters['name'])
searchCriteriaValue = _serialize(parameters['searchCriteria'])
sampleCreation.setProperty(PROP_SEARCH_CRITERIA, searchCriteriaValue)
if 'fetchOptions' in parameters:
fetchOptionsValue = _serialize(parameters['fetchOptions'])
sampleCreation.setProperty(PROP_FETCH_OPTIONS, fetchOptionsValue)
if 'customData' in parameters:
customDataValue = _serialize(parameters['customData'])
sampleCreation.setProperty(PROP_CUSTOM_DATA, customDataValue)
return context.applicationService.createSamples(context.sessionToken, [sampleCreation])
def _getSampleFetchOptions():
fetchOptions = SampleFetchOptions()
fetchOptions.withProperties()
fetchOptions.withExperiment().withProject().withSpace()
fetchOptions.withRegistrator()
fetchOptions.sortBy().modificationDate().desc()
return fetchOptions
def _serialize(obj):
jsonString = GenericObjectMapper().writer().writeValueAsString(obj)
return PREFIX_XML + jsonString + POSTFIX_XML
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment