Search in sources :

Example 1 with FactoryTaskConvertor

use of ambit2.rest.task.FactoryTaskConvertor in project ambit-mirror by ideaconsult.

the class CompoundResource method put.

/**
 * POST as in the dataset resource
 */
@Override
protected Representation put(Representation entity, Variant variant) 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())) {
        Object token = getToken();
        CallableStructureEntry callable = new CallableStructureEntry(new Form(entity), getRootRef(), getRecord(), getContext(), null, token, getRequest().getResourceRef().toString(), getClientInfo());
        callable.setPropertyOnly(true);
        ITask<Reference, Object> task = ((ITaskApplication) getApplication()).addTask("Properties from web form", 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);
    } else {
        if (upload == null)
            upload = createFileUpload();
        upload.setDataset(new SourceDataset("User uploaded", LiteratureEntry.getInstance("User uploaded", getResourceRef(getRequest()).toString())));
        return upload.upload(entity, variant, true, true, getToken(), getRequest().getResourceRef().toString(), getClientInfo());
    }
}
Also used : Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) ITaskStorage(net.idea.restnet.i.task.ITaskStorage) FactoryTaskConvertor(ambit2.rest.task.FactoryTaskConvertor) CallableStructureEntry(ambit2.rest.task.CallableStructureEntry) ITaskApplication(net.idea.restnet.i.task.ITaskApplication) ResourceException(org.restlet.resource.ResourceException) SourceDataset(ambit2.base.data.SourceDataset)

Example 2 with FactoryTaskConvertor

use of ambit2.rest.task.FactoryTaskConvertor 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);
        }
    }
}
Also used : ITask(net.idea.restnet.i.task.ITask) Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) ITaskStorage(net.idea.restnet.i.task.ITaskStorage) ArrayList(java.util.ArrayList) ITaskApplication(net.idea.restnet.i.task.ITaskApplication) CallablePOST(ambit2.rest.task.CallablePOST) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) ICallableTask(net.idea.restnet.i.task.ICallableTask) CallablePOST(ambit2.rest.task.CallablePOST) FactoryTaskConvertor(ambit2.rest.task.FactoryTaskConvertor) ResourceException(org.restlet.resource.ResourceException) UUID(java.util.UUID)

Example 3 with FactoryTaskConvertor

use of ambit2.rest.task.FactoryTaskConvertor in project ambit-mirror by ideaconsult.

the class OpenSSOPoliciesResource method post.

@Override
protected Representation post(Representation entity, Variant variant) throws ResourceException {
    synchronized (this) {
        ArrayList<UUID> tasks = new ArrayList<UUID>();
        try {
            Form form = entity.isAvailable() ? new Form(entity) : new Form();
            ICallableTask callable = createCallable(form, null);
            ITask<ITaskResult, String> task = ((ITaskApplication) getApplication()).addTask(String.format("Create policy"), 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);
        }
    }
}
Also used : Form(org.restlet.data.Form) ITaskStorage(net.idea.restnet.i.task.ITaskStorage) ArrayList(java.util.ArrayList) ITaskApplication(net.idea.restnet.i.task.ITaskApplication) CallablePOST(ambit2.rest.task.CallablePOST) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) ICallableTask(net.idea.restnet.i.task.ICallableTask) FactoryTaskConvertor(ambit2.rest.task.FactoryTaskConvertor) ITaskResult(net.idea.restnet.i.task.ITaskResult) ResourceException(org.restlet.resource.ResourceException) UUID(java.util.UUID)

Example 4 with FactoryTaskConvertor

use of ambit2.rest.task.FactoryTaskConvertor 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);
}
Also used : ITask(net.idea.restnet.i.task.ITask) RestletFileUpload(org.restlet.ext.fileupload.RestletFileUpload) Form(org.restlet.data.Form) ITaskApplication(net.idea.restnet.i.task.ITaskApplication) FactoryTaskConvertor(ambit2.rest.task.FactoryTaskConvertor) ResourceException(org.restlet.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) ITaskStorage(net.idea.restnet.i.task.ITaskStorage) Reference(org.restlet.data.Reference) QASettings(net.idea.i5.io.QASettings) IQASettings(net.idea.i5.io.IQASettings) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) IQASettings(net.idea.i5.io.IQASettings) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) PatternSyntaxException(java.util.regex.PatternSyntaxException) SQLException(java.sql.SQLException) ProcessorException(ambit2.base.processors.ProcessorException) FileItem(org.apache.commons.fileupload.FileItem) CallableSubstanceI5Query(ambit2.db.processors.CallableSubstanceI5Query) DatasetURIReporter(ambit2.rest.dataset.DatasetURIReporter)

Example 5 with FactoryTaskConvertor

use of ambit2.rest.task.FactoryTaskConvertor in project ambit-mirror by ideaconsult.

the class CompoundResource method post.

/**
 * POST as in the dataset resource
 */
@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 (MediaType.APPLICATION_WWW_FORM.equals(entity.getMediaType())) {
        Object token = getToken();
        CallableStructureEntry callable = new CallableStructureEntry(new Form(entity), getRootRef(), getRecord(), getContext(), null, token, getRequest().getResourceRef().toString(), getClientInfo());
        callable.setPropertyOnly(false);
        ITask<Reference, Object> task = ((ITaskApplication) getApplication()).addTask("New structure from web form", 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);
    /*
			 * Form form = new Form(entity); String cmpname =
			 * form.getFirstValue("identifier"); String uri =
			 * form.getFirstValue(OpenTox.params.compound_uri.toString());
			 * 
			 * if (cmpname != null) try { OTDatasets datasets =
			 * OTDatasets.datasets(); OTContainers<OTDataset> result =
			 * datasets.read
			 * (String.format("%s/query/compound/%s",getRequest().getRootRef
			 * (),Reference.encode(cmpname))); if (result.size()==0) { //not
			 * found uri = String.format("%s/query%s/%s",
			 * getRequest().getRootRef(), CSLSResource.resource,
			 * Reference.encode(cmpname)); } else return new
			 * StringRepresentation
			 * (result.getItem(0).getUri().toString(),MediaType.TEXT_URI_LIST);
			 * } catch (Exception x) {
			 * 
			 * }
			 * 
			 * 
			 * 
			 * if (uri == null) if ((entity == null) || !entity.isAvailable())
			 * throw new
			 * ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,OpenTox
			 * .params.compound_uris.toString() + " empty.");
			 * 
			 * String sdf = getSDFFromURI(uri); if (sdf == null) throw new
			 * ResourceException(Status.CLIENT_ERROR_NOT_FOUND); if (cmpname !=
			 * null) {
			 * 
			 * if (CASNumber.isValid(cmpname)) sdf =
			 * sdf.replace("$$$$",String.format
			 * ("\n> <CAS>\n%s\n\n$$$$",cmpname)).trim(); else if
			 * (cmpname.startsWith("InChI")) sdf =
			 * sdf.replace("$$$$",String.format
			 * ("\n> <InChI>\n%s\n\n$$$$",cmpname)).trim(); else sdf =
			 * sdf.replace
			 * ("$$$$",String.format("\n> <Name>\n%s\n\n$$$$",cmpname)).trim();
			 * } if(upload == null) upload = createFileUpload(); String source =
			 * uri; String name = "Copied from URL"; if
			 * (uri.startsWith(CSLSRequest.CSLS_URL)) { source =
			 * CSLSRequest.CSLS_URL; name = "CIR"; uri = name; }
			 * 
			 * if (uri.startsWith(String.format("%s/query%s",getRequest().
			 * getRootRef (),CSLSResource.resource))) { source =
			 * CSLSRequest.CSLS_URL;name = "Chemical Identifier Resolver (CIR)";
			 * uri = "CIR"; } if (uri.startsWith
			 * (String.format("%s/query/pubchem",getRequest().getRootRef()))) {
			 * source = "http://www.ncbi.nlm.nih.gov/entrez/eutils"; name =
			 * "PUBCHEM"; uri = "PUBCHEM"; }
			 * 
			 * SourceDataset dataset = new
			 * SourceDataset(uri,LiteratureEntry.getInstance(name,source));
			 * upload.setDataset(dataset); StringRepresentation representation =
			 * new StringRepresentation(sdf,ChemicalMediaType.CHEMICAL_MDLSDF);
			 * //representation.setDownloadName(dataset.getName());
			 * Representation r = upload.upload(representation,
			 * variant,true,false, getToken() ); return r; //return
			 * copyDatasetToQueryResultsTable(new Form(entity),true); //throw
			 * new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE,
			 * entity .getMediaType().toString());
			 */
    } else {
        if (upload == null)
            upload = createFileUpload();
        upload.setDataset(new SourceDataset("User uploaded", LiteratureEntry.getInstance("User uploaded", getResourceRef(getRequest()).toString())));
        return upload.upload(entity, variant, true, false, getToken(), getRequest().getResourceRef().toString(), getClientInfo());
    }
}
Also used : Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) ITaskStorage(net.idea.restnet.i.task.ITaskStorage) FactoryTaskConvertor(ambit2.rest.task.FactoryTaskConvertor) CallableStructureEntry(ambit2.rest.task.CallableStructureEntry) ITaskApplication(net.idea.restnet.i.task.ITaskApplication) ResourceException(org.restlet.resource.ResourceException) SourceDataset(ambit2.base.data.SourceDataset)

Aggregations

FactoryTaskConvertor (ambit2.rest.task.FactoryTaskConvertor)9 ITaskApplication (net.idea.restnet.i.task.ITaskApplication)9 ITaskStorage (net.idea.restnet.i.task.ITaskStorage)9 ResourceException (org.restlet.resource.ResourceException)9 Form (org.restlet.data.Form)7 Reference (org.restlet.data.Reference)7 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)6 ITask (net.idea.restnet.i.task.ITask)4 DatasetURIReporter (ambit2.rest.dataset.DatasetURIReporter)3 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 UUID (java.util.UUID)3 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)3 RestletFileUpload (org.restlet.ext.fileupload.RestletFileUpload)3 SourceDataset (ambit2.base.data.SourceDataset)2 ProcessorException (ambit2.base.processors.ProcessorException)2 ReadEffectRecordByBundleMatrix._matrix (ambit2.db.update.bundle.matrix.ReadEffectRecordByBundleMatrix._matrix)2 DBConnection (ambit2.rest.DBConnection)2 CallablePOST (ambit2.rest.task.CallablePOST)2