Skip to content
Snippets Groups Projects
Commit 2851507b authored by vkovtun's avatar vkovtun
Browse files

SSDM-8778 Added metadata import support.

parent e5984c82
No related branches found
No related tags found
No related merge requests found
from ch.systemsx.cisd.openbis.generic.client.web.client.exception import UserFailureException from ch.systemsx.cisd.openbis.generic.client.web.client.exception import UserFailureException
from ch.systemsx.cisd.openbis.dss.generic.shared import ServiceProvider 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>"; 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"; FAILED_TO_PARSE_SAMPLE_ERROR_MESSAGE = "Failed to parse sample";
...@@ -7,11 +9,12 @@ FAILED_TO_PARSE_EXPERIMENT_ERROR_MESSAGE = "Failed to parse experiment"; ...@@ -7,11 +9,12 @@ FAILED_TO_PARSE_EXPERIMENT_ERROR_MESSAGE = "Failed to parse experiment";
SAMPLE_MISSING_ERROR_MESSAGE = "Sample not found"; SAMPLE_MISSING_ERROR_MESSAGE = "Sample not found";
EXPERIMENT_MISSING_ERROR_MESSAGE = "Experiment not found"; EXPERIMENT_MISSING_ERROR_MESSAGE = "Experiment not found";
MORE_THAN_ONE_FOLDER_ERROR_MESSAGE = "More than one folder 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): def process(transaction):
incoming = transaction.getIncoming(); incoming = transaction.getIncoming();
folderName = incoming.getName(); folderName = incoming.getName();
if not folderName.startswith('.'): if not folderName.startswith('.'):
datasetInfo = folderName.split("+"); datasetInfo = folderName.split("+");
entityKind = None; entityKind = None;
...@@ -19,7 +22,7 @@ def process(transaction): ...@@ -19,7 +22,7 @@ def process(transaction):
experiment = None; experiment = None;
datasetType = None; datasetType = None;
name = None; name = None;
# Parse entity Kind # Parse entity Kind
if len(datasetInfo) >= 1: if len(datasetInfo) >= 1:
entityKind = datasetInfo[0]; entityKind = datasetInfo[0];
...@@ -76,34 +79,45 @@ def process(transaction): ...@@ -76,34 +79,45 @@ def process(transaction):
raise UserFailureException(INVALID_FORMAT_ERROR_MESSAGE + ":" + FAILED_TO_PARSE_EXPERIMENT_ERROR_MESSAGE); raise UserFailureException(INVALID_FORMAT_ERROR_MESSAGE + ":" + FAILED_TO_PARSE_EXPERIMENT_ERROR_MESSAGE);
else: else:
raise UserFailureException(INVALID_FORMAT_ERROR_MESSAGE + ":" + FAILED_TO_PARSE_EXPERIMENT_ERROR_MESSAGE); raise UserFailureException(INVALID_FORMAT_ERROR_MESSAGE + ":" + FAILED_TO_PARSE_EXPERIMENT_ERROR_MESSAGE);
# Create dataset # Create dataset
dataSet = None; dataSet = None;
if datasetType is not None: #Set type if found if datasetType is not None: #Set type if found
dataSet = transaction.createNewDataSet(datasetType); dataSet = transaction.createNewDataSet(datasetType);
else: else:
dataSet = transaction.createNewDataSet(); dataSet = transaction.createNewDataSet();
if name is not None: if name is not None:
dataSet.setPropertyValue("$NAME", name); #Set name if found dataSet.setPropertyValue("$NAME", name); #Set name if found
# Set sample or experiment # Set sample or experiment
if sample is not None: if sample is not None:
dataSet.setSample(sample); dataSet.setSample(sample);
else: else:
dataSet.setExperiment(experiment); dataSet.setExperiment(experiment);
# Move folder to dataset # Move folder to dataset
filesInFolder = incoming.listFiles(); filesInFolder = incoming.listFiles();
# Discard folders started with a . (hidden files) # Discard folders started with a . (hidden files)
itemsInFolder = 0; itemsInFolder = 0;
datasetItem = None; datasetItem = None;
for item in filesInFolder: for item in filesInFolder:
# Exclude files starting with . fileName = item.getName()
# Exclude Mac .DS_Store if fileName == "metadata.json":
# Exclude Windows Thumbs.db root = JSONObject(FileUtils.readFileToString(item, "UTF-8"))
if (not item.getName().startswith('.')) and (not item.getName() == ".DS_Store") and (not item.getName() == "Thumbs.db"): 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; itemsInFolder = itemsInFolder + 1;
datasetItem = item; datasetItem = item;
......
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