Search in sources :

Example 1 with NextprotMediaType

use of org.nextprot.api.core.service.export.format.NextprotMediaType in project nextprot-api by calipho-sib.

the class ChromosomeReportController method exportChromosomeEntriesReportFile.

@ApiMethod(path = "/chromosome-report/export/{chromosome}", verb = ApiVerb.GET, description = "Export informations of neXtProt entries located on a given chromosome", produces = { MediaType.TEXT_PLAIN_VALUE, NextprotMediaType.TSV_MEDIATYPE_VALUE })
@RequestMapping(value = "/chromosome-report/export/{chromosome}", method = { RequestMethod.GET })
public void exportChromosomeEntriesReportFile(@ApiPathParam(name = "chromosome", description = "The chromosome number or name (X,Y..)", allowedvalues = { "Y" }) @PathVariable("chromosome") String chromosome, HttpServletRequest request, HttpServletResponse response) {
    NextprotMediaType mediaType = NextprotMediaType.valueOf(request);
    try (OutputStream os = response.getOutputStream()) {
        String filename = "nextprot_chromosome_" + chromosome + "." + mediaType.getExtension();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        chromosomeReportExportService.exportChromosomeEntryReport(chromosome, NextprotMediaType.valueOf(request), os);
    } catch (IOException e) {
        throw new NextProtException(e.getMessage() + ": cannot export chromosome " + chromosome + " as " + mediaType);
    }
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) OutputStream(java.io.OutputStream) NextprotMediaType(org.nextprot.api.core.service.export.format.NextprotMediaType) IOException(java.io.IOException) ApiMethod(org.jsondoc.core.annotation.ApiMethod) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with NextprotMediaType

use of org.nextprot.api.core.service.export.format.NextprotMediaType in project nextprot-api by calipho-sib.

the class EntryController method getListOfIsoformAcMd5Sequence.

@ApiMethod(path = "/isoforms", verb = ApiVerb.GET, description = "Retrieves all isoforms", produces = { MediaType.APPLICATION_JSON_VALUE, NextprotMediaType.TSV_MEDIATYPE_VALUE })
@RequestMapping(value = "/isoforms", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE, NextprotMediaType.TSV_MEDIATYPE_VALUE })
public void getListOfIsoformAcMd5Sequence(HttpServletRequest request, HttpServletResponse response) {
    NextprotMediaType mediaType = NextprotMediaType.valueOf(request);
    try {
        List<SlimIsoform> isoforms = isoformService.findListOfIsoformAcMd5Sequence();
        if (mediaType == NextprotMediaType.JSON) {
            JSONObjectsWriter<SlimIsoform> writer = new JSONObjectsWriter<>(response.getOutputStream());
            writer.write(isoforms);
        } else if (mediaType == NextprotMediaType.TSV) {
            SlimIsoformTSVWriter writer = new SlimIsoformTSVWriter(response.getOutputStream());
            writer.write(isoforms);
            writer.close();
        }
    } catch (IOException e) {
        throw new NextProtException("cannot get isoforms in " + mediaType.getExtension() + " format", e);
    }
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) SlimIsoformTSVWriter(org.nextprot.api.core.service.export.io.SlimIsoformTSVWriter) NextprotMediaType(org.nextprot.api.core.service.export.format.NextprotMediaType) IOException(java.io.IOException) JSONObjectsWriter(org.nextprot.api.web.service.impl.writer.JSONObjectsWriter) ApiMethod(org.jsondoc.core.annotation.ApiMethod)

Example 3 with NextprotMediaType

use of org.nextprot.api.core.service.export.format.NextprotMediaType in project nextprot-api by calipho-sib.

the class EntryAccessionController method writeEntries.

private void writeEntries(Collection<String> entries, NextprotMediaType mediaType, HttpServletResponse response) throws IOException {
    if (mediaType == NextprotMediaType.JSON) {
        JSONObjectsWriter<String> writer = new JSONObjectsWriter<>(response.getOutputStream());
        writer.write(entries);
    } else if (mediaType == NextprotMediaType.TXT) {
        PrintWriter writer = new PrintWriter(response.getOutputStream());
        entries.forEach(entryAccession -> {
            writer.write(entryAccession);
            writer.write("\n");
        });
        writer.close();
    }
}
Also used : PrintWriter(java.io.PrintWriter) ApiVerb(org.jsondoc.core.pojo.ApiVerb) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) MediaType(org.springframework.http.MediaType) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) NextProtException(org.nextprot.api.commons.exception.NextProtException) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) Controller(org.springframework.stereotype.Controller) NextprotMediaType(org.nextprot.api.core.service.export.format.NextprotMediaType) ApiQueryParam(org.jsondoc.core.annotation.ApiQueryParam) ProteinExistence(org.nextprot.api.core.domain.ProteinExistence) HttpServletRequest(javax.servlet.http.HttpServletRequest) ApiMethod(org.jsondoc.core.annotation.ApiMethod) JSONObjectsWriter(org.nextprot.api.web.service.impl.writer.JSONObjectsWriter) Api(org.jsondoc.core.annotation.Api) MasterIdentifierService(org.nextprot.api.core.service.MasterIdentifierService) ApiPathParam(org.jsondoc.core.annotation.ApiPathParam) JSONObjectsWriter(org.nextprot.api.web.service.impl.writer.JSONObjectsWriter) PrintWriter(java.io.PrintWriter)

Example 4 with NextprotMediaType

use of org.nextprot.api.core.service.export.format.NextprotMediaType in project nextprot-api by calipho-sib.

the class DbXrefController method getRequestedFormat.

private NextprotMediaType getRequestedFormat(HttpServletRequest request) {
    NextprotMediaType format;
    String uri = request.getRequestURI();
    if (uri.toLowerCase().endsWith(".ttl")) {
        format = NextprotMediaType.TURTLE;
    } else if (uri.toLowerCase().endsWith(".xml")) {
        format = NextprotMediaType.XML;
    } else if (uri.toLowerCase().endsWith(".json")) {
        format = NextprotMediaType.JSON;
    } else if (uri.toLowerCase().endsWith(".txt")) {
        format = NextprotMediaType.TXT;
    } else
        throw new NextProtException("Format not recognized");
    return format;
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) NextprotMediaType(org.nextprot.api.core.service.export.format.NextprotMediaType)

Example 5 with NextprotMediaType

use of org.nextprot.api.core.service.export.format.NextprotMediaType in project nextprot-api by calipho-sib.

the class DbXrefController method findAllXrefs.

@ApiMethod(path = "/rdf/xrefs", verb = ApiVerb.GET, description = "Exports list of xrefs", produces = { "text/turtle" })
@RequestMapping("/rdf/xrefs")
public void findAllXrefs(Model model, HttpServletResponse response, HttpServletRequest request) throws Exception {
    // too many data, memory errors... should stream rather than list...
    List<Long> ids = this.xrService.getAllDbXrefsIds();
    // for (Long id : ids) System.out.println("fulllist - id: " + id);
    int idx = 0;
    int bunchSize = 100000;
    int bunchCount = 0;
    NextprotMediaType format = getRequestedFormat(request);
    String fileName = "nextprot-xrefs" + "." + format.getExtension();
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    View v = viewResolver.resolveViewName("prefix", Locale.ENGLISH);
    v.render(model.asMap(), request, response);
    while (true) {
        bunchCount++;
        int idx2 = idx + bunchSize;
        if (idx2 > ids.size())
            idx2 = ids.size();
        System.out.println("bunch: " + bunchCount + " - indices: " + idx + " - " + idx2);
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // do the job
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        List<Long> someIds = ids.subList(idx, idx2);
        List<DbXref> refs = this.xrService.findDbXRefByIds(someIds);
        model.addAttribute("bunch", bunchCount);
        model.addAttribute("xrefIds", refs);
        model.addAttribute("StringUtils", StringUtils.class);
        v = viewResolver.resolveViewName("xref-all", Locale.ENGLISH);
        v.render(model.asMap(), request, response);
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        idx = idx2;
        if (idx == ids.size())
            break;
    }
}
Also used : DbXref(org.nextprot.api.core.domain.DbXref) NextprotMediaType(org.nextprot.api.core.service.export.format.NextprotMediaType) View(org.springframework.web.servlet.View) ApiMethod(org.jsondoc.core.annotation.ApiMethod) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

NextprotMediaType (org.nextprot.api.core.service.export.format.NextprotMediaType)6 ApiMethod (org.jsondoc.core.annotation.ApiMethod)5 NextProtException (org.nextprot.api.commons.exception.NextProtException)5 IOException (java.io.IOException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 OutputStream (java.io.OutputStream)2 JSONObjectsWriter (org.nextprot.api.web.service.impl.writer.JSONObjectsWriter)2 PrintWriter (java.io.PrintWriter)1 Collection (java.util.Collection)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Api (org.jsondoc.core.annotation.Api)1 ApiPathParam (org.jsondoc.core.annotation.ApiPathParam)1 ApiQueryParam (org.jsondoc.core.annotation.ApiQueryParam)1 ApiVerb (org.jsondoc.core.pojo.ApiVerb)1 DbXref (org.nextprot.api.core.domain.DbXref)1 ProteinExistence (org.nextprot.api.core.domain.ProteinExistence)1 MasterIdentifierService (org.nextprot.api.core.service.MasterIdentifierService)1 SlimIsoformTSVWriter (org.nextprot.api.core.service.export.io.SlimIsoformTSVWriter)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1