Search in sources :

Example 56 with SourceDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.

the class RemoteApiController2 method readAnnotation.

private ResponseEntity<byte[]> readAnnotation(long aProjectId, long aDocumentId, String aAnnotatorId, Mode aMode, Optional<String> aFormat) throws RemoteApiException, ClassNotFoundException, IOException, UIMAException {
    // Get project (this also ensures that it exists and that the current user can access it
    Project project = getProject(aProjectId);
    SourceDocument doc = getDocument(project, aDocumentId);
    // Check format
    String format;
    if (aFormat.isPresent()) {
        if (VAL_ORIGINAL.equals(aFormat.get())) {
            format = doc.getFormat();
        } else {
            format = aFormat.get();
        }
    } else {
        format = doc.getFormat();
    }
    // Determine the format
    Class<?> writer = importExportService.getWritableFormats().get(format);
    if (writer == null) {
        String msg = "[" + doc.getName() + "] No writer found for format [" + format + "] - exporting as WebAnno TSV instead.";
        LOG.info(msg);
        writer = WebannoTsv3XWriter.class;
    }
    // annotation document entry is actually properly set up in the database.
    if (Mode.ANNOTATION.equals(aMode)) {
        getAnnotation(doc, aAnnotatorId, false);
    }
    // Create a temporary export file from the annotations
    File exportedAnnoFile = null;
    byte[] resource;
    try {
        exportedAnnoFile = importExportService.exportAnnotationDocument(doc, aAnnotatorId, writer, doc.getName(), Mode.ANNOTATION);
        resource = FileUtils.readFileToByteArray(exportedAnnoFile);
    } finally {
        if (exportedAnnoFile != null) {
            FileUtils.forceDelete(exportedAnnoFile);
        }
    }
    String filename = FilenameUtils.removeExtension(doc.getName());
    filename += "-" + aAnnotatorId;
    // Actually, exportedAnnoFile cannot be null here - the warning can be ignored.
    filename += "." + FilenameUtils.getExtension(exportedAnnoFile.getName());
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentLength(resource.length);
    httpHeaders.set("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    return new ResponseEntity<>(resource, httpHeaders, OK);
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 57 with SourceDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.

the class RemoteApiController2 method documentRead.

@ApiOperation(value = "Get a document from a project", response = byte[].class)
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}", method = RequestMethod.GET, produces = { APPLICATION_OCTET_STREAM_VALUE, APPLICATION_JSON_UTF8_VALUE })
public ResponseEntity documentRead(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aDocumentId, @RequestParam(value = PARAM_FORMAT) Optional<String> aFormat) throws Exception {
    // Get project (this also ensures that it exists and that the current user can access it
    Project project = getProject(aProjectId);
    SourceDocument doc = getDocument(project, aDocumentId);
    boolean originalFile;
    String format;
    if (aFormat.isPresent()) {
        if (VAL_ORIGINAL.equals(aFormat.get())) {
            format = doc.getFormat();
            originalFile = true;
        } else {
            format = aFormat.get();
            originalFile = doc.getFormat().equals(format);
        }
    } else {
        format = doc.getFormat();
        originalFile = true;
    }
    if (originalFile) {
        // Export the original file - no temporary file created here, we export directly from
        // the file system
        File docFile = documentService.getSourceDocumentFile(doc);
        FileSystemResource resource = new FileSystemResource(docFile);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentLength(resource.contentLength());
        httpHeaders.set("Content-Disposition", "attachment; filename=\"" + doc.getName() + "\"");
        return new ResponseEntity<org.springframework.core.io.Resource>(resource, httpHeaders, OK);
    } else {
        // Export a converted file - here we first export to a local temporary file and then
        // send that back to the client
        // Check if the format is supported
        Map<String, Class<JCasAnnotator_ImplBase>> writableFormats = importExportService.getWritableFormats();
        Class<JCasAnnotator_ImplBase> writer = writableFormats.get(format);
        if (writer == null) {
            throw new UnsupportedFormatException("Format [%s] cannot be exported. Exportable formats are %s.", aFormat, writableFormats.keySet());
        }
        // Create a temporary export file from the annotations
        JCas jcas = documentService.createOrReadInitialCas(doc);
        File exportedFile = null;
        try {
            // Load the converted file into memory
            exportedFile = importExportService.exportCasToFile(jcas.getCas(), doc, doc.getName(), writer, true);
            byte[] resource = FileUtils.readFileToByteArray(exportedFile);
            // Send it back to the client
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentLength(resource.length);
            httpHeaders.set("Content-Disposition", "attachment; filename=\"" + exportedFile.getName() + "\"");
            return new ResponseEntity<>(resource, httpHeaders, OK);
        } finally {
            if (exportedFile != null) {
                FileUtils.forceDelete(exportedFile);
            }
        }
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) JCasAnnotator_ImplBase(org.apache.uima.analysis_component.JCasAnnotator_ImplBase) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) FileSystemResource(org.springframework.core.io.FileSystemResource) RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ResponseEntity(org.springframework.http.ResponseEntity) UnsupportedFormatException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 58 with SourceDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.

the class RemoteApiController2 method curationCreate.

@ApiOperation(value = "Create curation for a document in a project")
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + CURATION, method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<RAnnotation>> curationCreate(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aDocumentId, @RequestParam(value = PARAM_CONTENT) MultipartFile aFile, @RequestParam(value = PARAM_FORMAT) Optional<String> aFormat, @RequestParam(value = PARAM_STATE) Optional<String> aState, UriComponentsBuilder aUcb) throws Exception {
    Project project = getProject(aProjectId);
    SourceDocument document = getDocument(project, aDocumentId);
    JCas annotationCas = createCompatibleCas(aProjectId, aDocumentId, aFile, aFormat);
    // If they are compatible, then we can store the new annotations
    curationService.writeCurationCas(annotationCas, document, false);
    AnnotationDocumentState resultState = AnnotationDocumentState.IN_PROGRESS;
    if (aState.isPresent()) {
        SourceDocumentState state = parseSourceDocumentState(aState.get());
        switch(state) {
            case CURATION_IN_PROGRESS:
                resultState = AnnotationDocumentState.IN_PROGRESS;
                document.setState(state);
                documentService.createSourceDocument(document);
                break;
            case CURATION_FINISHED:
                resultState = AnnotationDocumentState.FINISHED;
                document.setState(state);
                documentService.createSourceDocument(document);
                break;
            // fallthrough
            case NEW:
            // fallthrough
            case ANNOTATION_IN_PROGRESS:
            // fallthrough
            case ANNOTATION_FINISHED:
            default:
                throw new IllegalObjectStateException("State [%s] not valid when uploading a curation.", aState.get());
        }
    } else {
        document.setState(SourceDocumentState.CURATION_IN_PROGRESS);
        documentService.createSourceDocument(document);
    }
    RResponse<RAnnotation> response = new RResponse<>(new RAnnotation(WebAnnoConst.CURATION_USER, resultState, new Date()));
    return ResponseEntity.created(aUcb.path(API_BASE + "/" + PROJECTS + "/{pid}/" + DOCUMENTS + "/{did}/" + CURATION).buildAndExpand(project.getId(), document.getId()).toUri()).body(response);
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) IllegalObjectStateException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.IllegalObjectStateException) RAnnotation(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RAnnotation) SourceDocumentState(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState) AnnotationDocumentState(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocumentState) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) RResponse(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RResponse) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 59 with SourceDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.

the class RemoteApiController2 method annotationsList.

@ApiOperation(value = "List annotations of a document in a project")
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS, method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<List<RAnnotation>>> annotationsList(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aDocumentId) throws Exception {
    // Get project (this also ensures that it exists and that the current user can access it
    Project project = getProject(aProjectId);
    SourceDocument doc = getDocument(project, aDocumentId);
    List<AnnotationDocument> annotations = documentService.listAnnotationDocuments(doc);
    List<RAnnotation> annotationList = new ArrayList<>();
    for (AnnotationDocument annotation : annotations) {
        annotationList.add(new RAnnotation(annotation));
    }
    return ResponseEntity.ok(new RResponse<>(annotationList));
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) RAnnotation(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RAnnotation) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ArrayList(java.util.ArrayList) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)59 JCas (org.apache.uima.jcas.JCas)24 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)22 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)22 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 File (java.io.File)13 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 ApiOperation (io.swagger.annotations.ApiOperation)9 IOException (java.io.IOException)9 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)8 Map (java.util.Map)8 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)7 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)7 DiffResult (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult)6 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)6 NoResultException (javax.persistence.NoResultException)6