Skip to content
Snippets Groups Projects
Commit df536983 authored by juanf's avatar juanf
Browse files

SSDM-3092 : Export functionality, ongoing work

SVN: 36609
parent 17e3e7f5
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
from collections import deque from collections import deque
import time import time
import jarray import jarray
import threading
#Java Core #Java Core
from java.io import ByteArrayInputStream from java.io import ByteArrayInputStream
...@@ -62,6 +63,9 @@ from ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.download import DataS ...@@ -62,6 +63,9 @@ from ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.download import DataS
from ch.ethz.sis.openbis.generic.server.sharedapi.v3.json import GenericObjectMapper; from ch.ethz.sis.openbis.generic.server.sharedapi.v3.json import GenericObjectMapper;
from com.fasterxml.jackson.databind import SerializationFeature from com.fasterxml.jackson.databind import SerializationFeature
#Session Workspace
from ch.systemsx.cisd.openbis.dss.client.api.v1 import DssComponentFactory
def process(tr, params, tableBuilder): def process(tr, params, tableBuilder):
method = params.get("method"); method = params.get("method");
isOk = False; isOk = False;
...@@ -152,7 +156,12 @@ def exportAll(tr, params): ...@@ -152,7 +156,12 @@ def exportAll(tr, params):
entitiesToExpand.append(entityFound); entitiesToExpand.append(entityFound);
print "Found " + str(len(entitiesToExport)) + " entities to export."; print "Found " + str(len(entitiesToExport)) + " entities to export.";
params.put("entities", entitiesToExport); params.put("entities", entitiesToExport);
return export(tr, params);
thread = threading.Thread(target=export, args=(sessionToken, entitiesToExport));
thread.daemon = True;
thread.start();
return True;
def getFileName(spaceCode, projCode, expCode, sampCode, dataCode): def getFileName(spaceCode, projCode, expCode, sampCode, dataCode):
fileName = ""; fileName = "";
...@@ -182,25 +191,28 @@ def addToZipFile(path, file, zos): ...@@ -182,25 +191,28 @@ def addToZipFile(path, file, zos):
zos.closeEntry(); zos.closeEntry();
fis.close(); fis.close();
def export(tr, params): def export(sessionToken, entities):
sessionToken = params.get("sessionToken");
v3 = HttpInvokerUtils.createServiceStub(IApplicationServerApi, OPENBISURL + IApplicationServerApi.SERVICE_URL, 30 * 1000); v3 = HttpInvokerUtils.createServiceStub(IApplicationServerApi, OPENBISURL + IApplicationServerApi.SERVICE_URL, 30 * 1000);
v3d = ServiceProvider.getApplicationContext().getBean(V3_DSS_BEAN); v3d = ServiceProvider.getApplicationContext().getBean(V3_DSS_BEAN);
objectCache = {}; objectCache = {};
objectMapper = GenericObjectMapper(); objectMapper = GenericObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT); objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
#Create temporal folder #Create temporal folder
rootDirFile = File.createTempFile(str(time.time()), None); tempDirName = "export_" + str(time.time());
rootDirFile.delete(); tempDirPathFile = File.createTempFile(tempDirName, None);
rootDirFile.mkdir(); tempDirPathFile.delete();
rootDir = rootDirFile.getCanonicalPath(); tempDirPathFile.mkdir();
tempDirPath = tempDirPathFile.getCanonicalPath();
tempZipFileName = tempDirName + ".zip";
tempZipFilePath = tempDirPath + ".zip";
#Create Zip File #Create Zip File
fos = FileOutputStream(rootDir + ".zip"); fos = FileOutputStream(tempZipFilePath);
zos = ZipOutputStream(fos); zos = ZipOutputStream(fos);
for entity in params.get("entities"): for entity in entities:
type = entity["type"]; type = entity["type"];
permId = entity["permId"]; permId = entity["permId"];
print "exporting type: " + str(type) + " permId: " + str(permId); print "exporting type: " + str(type) + " permId: " + str(permId);
...@@ -263,7 +275,7 @@ def export(tr, params): ...@@ -263,7 +275,7 @@ def export(tr, params):
datasetEntityFilePath = getFileName(datasetEntityObj.getSample().getExperiment().getProject().getSpace().getCode(), datasetEntityObj.getSample().getExperiment().getProject().getCode(), datasetEntityObj.getSample().getExperiment().getCode(), datasetEntityObj.getSample().getCode(), datasetEntityObj.getCode()); datasetEntityFilePath = getFileName(datasetEntityObj.getSample().getExperiment().getProject().getSpace().getCode(), datasetEntityObj.getSample().getExperiment().getProject().getCode(), datasetEntityObj.getSample().getExperiment().getCode(), datasetEntityObj.getSample().getCode(), datasetEntityObj.getCode());
filePath = datasetEntityFilePath + "/" + entity["path"]; filePath = datasetEntityFilePath + "/" + entity["path"];
rawFileInputStream = v3d.downloadFiles(sessionToken, [DataSetFilePermId(DataSetPermId(permId), entity["path"])], DataSetFileDownloadOptions()); rawFileInputStream = v3d.downloadFiles(sessionToken, [DataSetFilePermId(DataSetPermId(permId), entity["path"])], DataSetFileDownloadOptions());
rawFile = File(rootDir + filePath + ".json"); rawFile = File(tempDirPath + filePath + ".json");
rawFile.getParentFile().mkdirs(); rawFile.getParentFile().mkdirs();
IOUtils.copyLarge(rawFileInputStream, FileOutputStream(rawFile)); IOUtils.copyLarge(rawFileInputStream, FileOutputStream(rawFile));
addToZipFile(filePath, rawFile, zos); addToZipFile(filePath, rawFile, zos);
...@@ -273,12 +285,16 @@ def export(tr, params): ...@@ -273,12 +285,16 @@ def export(tr, params):
if entityObj is not None and entityFilePath is not None: if entityObj is not None and entityFilePath is not None:
entityJson = String(objectMapper.writeValueAsString(entityObj)); entityJson = String(objectMapper.writeValueAsString(entityObj));
jsonEntityFile = File(rootDir + entityFilePath + ".json"); jsonEntityFile = File(tempDirPath + entityFilePath + ".json");
jsonEntityFile.getParentFile().mkdirs(); jsonEntityFile.getParentFile().mkdirs();
IOUtils.write(entityJson.getBytes(), FileOutputStream(jsonEntityFile)); IOUtils.write(entityJson.getBytes(), FileOutputStream(jsonEntityFile));
addToZipFile(entityFilePath + ".json", jsonEntityFile, zos); addToZipFile(entityFilePath + ".json", jsonEntityFile, zos);
zos.close(); zos.close();
fos.close(); fos.close();
print "Zip file found at: " + rootDir + ".zip"
#Store on workspace to be able to generate a download link
dssComponent = DssComponentFactory.tryCreate(sessionToken, OPENBISURL);
dssComponent.putFileToSessionWorkspace(tempZipFileName, FileInputStream(File(tempZipFilePath)));
print "Zip file found at: " + tempZipFilePath;
return True return True
\ No newline at end of file
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