use of net.idea.restnet.i.task.ITask in project ambit-mirror by ideaconsult.
the class CatalogResource method post.
@Override
protected Representation post(Representation entity, Variant variant) throws ResourceException {
synchronized (this) {
Form form = entity.isAvailable() ? new Form(entity) : new Form();
// models
Iterator<T> query = queryObject;
if (query == null)
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
ArrayList<UUID> tasks = new ArrayList<UUID>();
while (query.hasNext()) try {
T model = query.next();
Reference reference = getSourceReference(form, model);
ICallableTask callable = createCallable(form, model);
ITask<ITaskResult, String> task = ((ITaskApplication) getApplication()).addTask(String.format("Apply %s %s %s", model.toString(), reference == null ? "" : "to", reference == null ? "" : reference), callable, getRequest().getRootRef(), callable instanceof CallablePOST, getToken());
task.update();
setStatus(task.isDone() ? Status.SUCCESS_OK : Status.SUCCESS_ACCEPTED);
tasks.add(task.getUuid());
} catch (ResourceException x) {
throw x;
} catch (Exception x) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getMessage(), x);
}
if (tasks.size() == 0)
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
else {
ITaskStorage storage = ((ITaskApplication) getApplication()).getTaskStorage();
FactoryTaskConvertor<Object> tc = new FactoryTaskConvertor<Object>(storage);
if (tasks.size() == 1)
return tc.createTaskRepresentation(tasks.get(0), variant, getRequest(), getResponse(), null);
else
return tc.createTaskRepresentation(tasks.iterator(), variant, getRequest(), getResponse(), null);
}
}
}
use of net.idea.restnet.i.task.ITask in project ambit-mirror by ideaconsult.
the class SubstanceResource method post.
@Override
protected Representation post(Representation entity, Variant variant) throws ResourceException {
if ((entity == null) || !entity.isAvailable())
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Empty content");
if (entity.getMediaType() != null)
if (MediaType.MULTIPART_FORM_DATA.getName().equals(entity.getMediaType().getName())) {
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()) {
if ("qaenabled".equals(file.getFieldName()))
try {
if ("on".equals(file.getString()))
qa.setEnabled(true);
if ("yes".equals(file.getString()))
qa.setEnabled(true);
if ("checked".equals(file.getString()))
qa.setEnabled(true);
} catch (Exception x) {
qa.setEnabled(true);
}
else if ("clearMeasurements".equals(file.getFieldName())) {
try {
clearMeasurements = false;
String cm = file.getString();
if ("on".equals(cm))
clearMeasurements = true;
else if ("yes".equals(cm))
clearMeasurements = true;
else if ("checked".equals(cm))
clearMeasurements = true;
} catch (Exception x) {
clearMeasurements = false;
}
} else if ("clearComposition".equals(file.getFieldName())) {
try {
clearComposition = false;
String cm = file.getString();
if ("on".equals(cm))
clearComposition = true;
else if ("yes".equals(cm))
clearComposition = true;
else if ("checked".equals(cm))
clearComposition = true;
} catch (Exception x) {
clearComposition = false;
}
} else
for (IQASettings.qa_field f : IQASettings.qa_field.values()) if (f.name().equals(file.getFieldName()))
try {
String value = file.getString("UTF-8");
f.addOption(qa, "null".equals(value) ? null : value == null ? null : value.toString());
} catch (Exception x) {
}
} else {
String ext = file.getName().toLowerCase().trim();
if ("".equals(ext) || ext.endsWith(".i5z") || ext.endsWith(".i6z") || ext.endsWith(".csv") || ext.endsWith(".rdf") || ext.endsWith(".ttl") || ext.endsWith(".json") || ext.endsWith(".xlsx") || ext.endsWith(".xls")) {
} else
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unsupported format " + ext);
}
}
CallableSubstanceImporter<String> callable = new CallableSubstanceImporter<String>(items, CallableFileUpload.field_files, CallableFileUpload.field_config, getRootRef(), getContext(), new SubstanceURIReporter(getRequest().getRootRef()), new DatasetURIReporter(getRequest().getRootRef()), token, getRequest().getResourceRef().toString(), getClientInfo());
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);
}
} else if (MediaType.APPLICATION_WWW_FORM.getName().equals(entity.getMediaType().getName())) {
/*
* web form to update substances from IUCLID5 server Expected web form fields :
* substance: UUID or query URL type : UUID or query or URL (ambit substance
* URL) qa options query : one of {@link QueryToolClient.PredefinedQuery} query
* parameters: depend on the query type iuclid5 server; credentials - optional,
* use preconfigured if not submitted [(option,UUID), (uuid,ZZZZZZZZZZ),
* (extidtype,CompTox), (extidvalue,Ambit Transfer), (i5server,null),
* (i5user,null), (i5pass,null)]
*/
Form form = new Form(entity);
String token = getToken();
CallableSubstanceI5Query<String> callable = new CallableSubstanceI5Query<String>(getRootRef(), form, getContext(), new SubstanceURIReporter(getRequest().getRootRef()), new DatasetURIReporter(getRequest().getRootRef()), token, getRequest().getResourceRef().toString(), getClientInfo());
ITask<Reference, Object> task = ((ITaskApplication) getApplication()).addTask("Retrieve substance from IUCLID server", 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);
}
throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE);
}
use of net.idea.restnet.i.task.ITask 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 net.idea.restnet.i.task.ITask in project ambit-mirror by ideaconsult.
the class FileUpload method upload.
public Representation upload(Representation entity, Variant variant, boolean newEntry, boolean propertyOnly, USERID token, String referer, ClientInfo clientInfo) throws ResourceException {
if ((entity == null) || !entity.isAvailable())
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Empty content");
if (MediaType.APPLICATION_WWW_FORM.equals(entity.getMediaType())) {
// propertyOnly POST:
boolean clearPreviousContent = !propertyOnly;
// false, PUT : true
return copyDatasetToQueryResultsTable(new Form(entity), token, clearPreviousContent, referer, clientInfo);
} else if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// factory.setSizeThreshold(100);
RestletFileUpload upload = new RestletFileUpload(factory);
Connection connection = null;
try {
DBConnection dbc = new DBConnection(getApplication().getContext());
connection = dbc.getConnection(30, true, 8);
List<FileItem> items = upload.parseRequest(getRequest());
DatasetURIReporter<IQueryRetrieval<ISourceDataset>, ISourceDataset> reporter = new DatasetURIReporter<IQueryRetrieval<ISourceDataset>, ISourceDataset>(getRequest());
ConformerURIReporter<IQueryRetrieval<IStructureRecord>> compoundReporter = new ConformerURIReporter<IQueryRetrieval<IStructureRecord>>(getRequest());
CallableFileImport callable = new CallableFileImport(getRequest().getClientInfo(), dataset, items, fileUploadField, connection, reporter, compoundReporter, firstCompoundOnly, token);
callable.setPropertyOnly(propertyOnly);
ITask<Reference, Object> task = ((ITaskApplication) getApplication()).addTask("File 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) {
try {
connection.close();
} catch (Exception xx) {
xx.printStackTrace();
}
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x);
}
} else if (isAllowedMediaType(entity.getMediaType())) {
Connection connection = null;
try {
DBConnection dbc = new DBConnection(getApplication().getContext());
connection = dbc.getConnection(30, true, 8);
DatasetURIReporter<IQueryRetrieval<ISourceDataset>, ISourceDataset> reporter = new DatasetURIReporter<IQueryRetrieval<ISourceDataset>, ISourceDataset>(getRequest());
ConformerURIReporter<IQueryRetrieval<IStructureRecord>> compoundReporter = new ConformerURIReporter<IQueryRetrieval<IStructureRecord>>(getRequest());
CallableFileImport callable = new CallableFileImport(getRequest().getClientInfo(), dataset, entity, connection, reporter, compoundReporter, firstCompoundOnly, token);
callable.setPropertyOnly(propertyOnly);
ITask<ITaskResult, Object> task = ((AmbitApplication) getApplication()).addTask(String.format("File import %s [%d]", entity.getDownloadName() == null ? entity.getMediaType() : entity.getDownloadName(), entity.getSize()), callable, getRequest().getRootRef(), token == null ? null : token.toString());
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) {
try {
connection.close();
} catch (Exception xx) {
xx.printStackTrace();
}
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x);
}
} else
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, String.format("Unsupported Content-type=%s", entity.getMediaType()));
}
Aggregations