diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/drop-boxes/eln-lims-dropbox/eln-lims-dropbox.py b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/drop-boxes/eln-lims-dropbox/eln-lims-dropbox.py index 8dca51e8d11823fed89532d75fe2ceeca267c4ca..3184fbd880bb1be0a2359fb0a3c3e8ac1f92d4b6 100644 --- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/drop-boxes/eln-lims-dropbox/eln-lims-dropbox.py +++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/drop-boxes/eln-lims-dropbox/eln-lims-dropbox.py @@ -1,5 +1,7 @@ from ch.systemsx.cisd.openbis.generic.client.web.client.exception import UserFailureException from ch.systemsx.cisd.openbis.dss.generic.shared import ServiceProvider +from org.json import JSONObject +from org.apache.commons.io import FileUtils INVALID_FORMAT_ERROR_MESSAGE = "Invalid format for the folder name, should follow the pattern <ENTITY_KIND>+<SPACE_CODE>+<PROJECT_CODE>+[<EXPERIMENT_CODE|<SAMPLE_CODE>]+<OPTIONAL_DATASET_TYPE>+<OPTIONAL_NAME>"; FAILED_TO_PARSE_SAMPLE_ERROR_MESSAGE = "Failed to parse sample"; @@ -7,11 +9,12 @@ FAILED_TO_PARSE_EXPERIMENT_ERROR_MESSAGE = "Failed to parse experiment"; SAMPLE_MISSING_ERROR_MESSAGE = "Sample not found"; EXPERIMENT_MISSING_ERROR_MESSAGE = "Experiment not found"; MORE_THAN_ONE_FOLDER_ERROR_MESSAGE = "More than one folder found"; +NAME_PROPERTY_IN_METADATA_ERROR_MESSAGE = "$NAME property should not be specified in metadata file" def process(transaction): incoming = transaction.getIncoming(); folderName = incoming.getName(); - + if not folderName.startswith('.'): datasetInfo = folderName.split("+"); entityKind = None; @@ -19,7 +22,7 @@ def process(transaction): experiment = None; datasetType = None; name = None; - + # Parse entity Kind if len(datasetInfo) >= 1: entityKind = datasetInfo[0]; @@ -76,34 +79,45 @@ def process(transaction): raise UserFailureException(INVALID_FORMAT_ERROR_MESSAGE + ":" + FAILED_TO_PARSE_EXPERIMENT_ERROR_MESSAGE); else: raise UserFailureException(INVALID_FORMAT_ERROR_MESSAGE + ":" + FAILED_TO_PARSE_EXPERIMENT_ERROR_MESSAGE); - + # Create dataset dataSet = None; if datasetType is not None: #Set type if found dataSet = transaction.createNewDataSet(datasetType); else: dataSet = transaction.createNewDataSet(); - + if name is not None: dataSet.setPropertyValue("$NAME", name); #Set name if found - + # Set sample or experiment if sample is not None: dataSet.setSample(sample); else: dataSet.setExperiment(experiment); - + # Move folder to dataset filesInFolder = incoming.listFiles(); - + # Discard folders started with a . (hidden files) itemsInFolder = 0; datasetItem = None; for item in filesInFolder: - # Exclude files starting with . - # Exclude Mac .DS_Store - # Exclude Windows Thumbs.db - if (not item.getName().startswith('.')) and (not item.getName() == ".DS_Store") and (not item.getName() == "Thumbs.db"): + fileName = item.getName() + if fileName == "metadata.json": + root = JSONObject(FileUtils.readFileToString(item, "UTF-8")) + properties = root.get("properties") + for propertyKey in properties.keys(): + if propertyKey == "$NAME": + raise UserFailureException(NAME_PROPERTY_IN_METADATA_ERROR_MESSAGE) + propertyValue = properties.get(propertyKey) + if propertyValue is not None: + propertyValueString = str(propertyValue) + dataSet.setPropertyValue(propertyKey, propertyValueString) + elif (not fileName.startswith('.')) and (not fileName == ".DS_Store") and (not fileName == "Thumbs.db"): + # Exclude files starting with . + # Exclude Mac .DS_Store + # Exclude Windows Thumbs.db itemsInFolder = itemsInFolder + 1; datasetItem = item;