use of ambit2.rest.substance.CallableStudyBundleImporter._mode in project ambit-mirror by ideaconsult.
the class BundleMatrixResource method put.
@Override
protected Representation put(Representation entity, Variant variant) throws ResourceException {
_matrix matrix = getList();
if ((entity == null) || !entity.isAvailable())
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Empty content");
if (entity.getMediaType() != null) {
if (MediaType.APPLICATION_JSON.getName().equals(entity.getMediaType().getName())) {
_mode importmode = _mode.studyimport;
switch(matrix) {
case deleted_values:
{
importmode = _mode.matrixvaluedelete;
break;
}
case matrix_working:
{
importmode = _mode.studyimport;
break;
}
default:
throw new ResourceException(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
}
try {
File file = File.createTempFile("_matrix_", ".json");
file.deleteOnExit();
DownloadTool.download(entity.getStream(), file);
String token = getToken();
QASettings qa = new QASettings();
// sets enabled to false and clears all flags
qa.clear();
CallableStudyBundleImporter<String> callable = new CallableStudyBundleImporter<String>(importmode, file, getRootRef(), getContext(), new SubstanceURIReporter(getRequest().getRootRef()), new DatasetURIReporter(getRequest().getRootRef()), token, getRequest().getResourceRef().toString(), getClientInfo());
callable.setBundle(bundle);
callable.setClearComposition(false);
callable.setClearMeasurements(false);
callable.setQASettings(qa);
ITask<Reference, Object> task = ((ITaskApplication) getApplication()).addTask("Substance import", callable, getRequest().getRootRef(), token);
ITaskStorage storage = ((ITaskApplication) getApplication()).getTaskStorage();
FactoryTaskConvertor<Object> tc = new FactoryTaskConvertor<Object>(storage);
task.update();
getResponse().setStatus(task.isDone() ? Status.SUCCESS_OK : Status.SUCCESS_ACCEPTED);
return tc.createTaskRepresentation(task.getUuid(), variant, getRequest(), getResponse(), null);
} catch (Exception x) {
x.printStackTrace();
} finally {
try {
entity.getStream().close();
} catch (Exception xx) {
}
}
} else if (MediaType.MULTIPART_FORM_DATA.getName().equals(entity.getMediaType().getName())) {
switch(matrix) {
case matrix_working:
{
break;
}
default:
throw new ResourceException(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
}
DiskFileItemFactory factory = new DiskFileItemFactory();
RestletFileUpload upload = new RestletFileUpload(factory);
try {
List<FileItem> items = upload.parseRequest(getRequest());
String token = getToken();
QASettings qa = new QASettings();
// sets enabled to false and clears all flags
qa.clear();
boolean clearMeasurements = false;
boolean clearComposition = false;
for (FileItem file : items) {
if (file.isFormField()) {
// ignore
} else {
String ext = file.getName().toLowerCase();
if (ext.endsWith(".json")) {
} else
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unsupported format " + ext);
}
}
CallableStudyBundleImporter<String> callable = new CallableStudyBundleImporter<String>(items, CallableFileUpload.field_files, CallableFileUpload.field_config, getRootRef(), getContext(), new SubstanceURIReporter(getRequest().getRootRef()), new DatasetURIReporter(getRequest().getRootRef()), token, getRequest().getResourceRef().toString(), getClientInfo());
callable.setBundle(bundle);
callable.setClearComposition(clearComposition);
callable.setClearMeasurements(clearMeasurements);
callable.setQASettings(qa);
ITask<Reference, Object> task = ((ITaskApplication) getApplication()).addTask("Substance import", callable, getRequest().getRootRef(), token);
ITaskStorage storage = ((ITaskApplication) getApplication()).getTaskStorage();
FactoryTaskConvertor<Object> tc = new FactoryTaskConvertor<Object>(storage);
task.update();
getResponse().setStatus(task.isDone() ? Status.SUCCESS_OK : Status.SUCCESS_ACCEPTED);
return tc.createTaskRepresentation(task.getUuid(), variant, getRequest(), getResponse(), null);
} catch (ResourceException x) {
throw x;
} catch (Exception x) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x);
}
}
}
throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE);
}
use of ambit2.rest.substance.CallableStudyBundleImporter._mode in project ambit-mirror by ideaconsult.
the class CallableStudyBundleImporterTest method test.
@Test
public void test() throws Exception {
setUpDatabase(dbFile);
IDatabaseConnection c = getConnection();
IDatabaseConnection c1 = getConnection();
Form form = new Form();
form.add("substance_uri", "http://localhost:8081/ambit2/substance/IUC4-efdb21bb-e79f-3286-a988-b6f6944d3734");
form.add("tag", "CM");
form.add("remarks", "remark");
try {
String ref = "http://localhost:8081/ambit2";
Reference rootref = new Reference(ref);
SubstanceEndpointsBundle bundle = new SubstanceEndpointsBundle(1);
SubstanceURIReporter<IQueryRetrieval<SubstanceRecord>> reporter = new SubstanceURIReporter<IQueryRetrieval<SubstanceRecord>>(rootref);
DatasetURIReporter dreporter = new DatasetURIReporter(rootref);
File file = new File(getClass().getClassLoader().getResource("matrixvalue_delete.json").getFile());
CallableStudyBundleImporter callable = new CallableStudyBundleImporter(_mode.matrixvaluedelete, file, rootref, null, reporter, dreporter, null, ref, null);
TaskResult task = callable.call();
ITable table = c1.createQueryTable("EXPECTED", String.format("SELECT idbundle,idsubstance,tag,remarks from bundle_substance"));
Assert.assertEquals(1, table.getRowCount());
Assert.assertEquals("CM", table.getValue(0, "tag"));
Assert.assertEquals("remark", table.getValue(0, "remarks"));
} catch (Exception x) {
throw x;
} finally {
try {
c.close();
} catch (Exception x) {
}
try {
c1.close();
} catch (Exception x) {
}
}
}
Aggregations