Search in sources :

Example 1 with RDocument

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RDocument in project webanno by webanno.

the class RemoteApiController2 method documentCreate.

@ApiOperation(value = "Create a new document in a project")
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS, method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<RDocument>> documentCreate(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @RequestParam(value = PARAM_CONTENT) MultipartFile aFile, @RequestParam(value = PARAM_NAME) String aName, @RequestParam(value = PARAM_FORMAT) String aFormat, @RequestParam(value = PARAM_STATE) Optional<String> aState, UriComponentsBuilder aUcb) throws Exception {
    // Get project (this also ensures that it exists and that the current user can access it
    Project project = getProject(aProjectId);
    // Check if the format is supported
    Map<String, Class<CollectionReader>> readableFormats = importExportService.getReadableFormats();
    if (readableFormats.get(aFormat) == null) {
        throw new UnsupportedFormatException("Format [%s] not supported. Acceptable formats are %s.", aFormat, readableFormats.keySet());
    }
    // Meta data entry to the database
    SourceDocument document = new SourceDocument();
    document.setProject(project);
    document.setName(aName);
    document.setFormat(aFormat);
    // Set state if one was provided
    if (aState.isPresent()) {
        SourceDocumentState state = parseSourceDocumentState(aState.get());
        switch(state) {
            // fallthrough
            case NEW:
            // fallthrough
            case ANNOTATION_IN_PROGRESS:
            case // fallthrough
            ANNOTATION_FINISHED:
                document.setState(state);
                documentService.createSourceDocument(document);
                break;
            // fallthrough
            case CURATION_IN_PROGRESS:
            case CURATION_FINISHED:
            default:
                throw new IllegalObjectStateException("State [%s] not valid when uploading a document.", aState.get());
        }
    }
    // Import source document to the project repository folder
    try (InputStream is = aFile.getInputStream()) {
        documentService.uploadSourceDocument(is, document);
    }
    RResponse<RDocument> rDocument = new RResponse<>(new RDocument(document));
    if (aState.isPresent()) {
        rDocument.addMessage(INFO, "State of document [" + document.getId() + "] set to [" + aState.get() + "]");
    }
    return ResponseEntity.created(aUcb.path(API_BASE + "/" + PROJECTS + "/{pid}/" + DOCUMENTS + "/{did}").buildAndExpand(project.getId(), document.getId()).toUri()).body(rDocument);
}
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) UnsupportedFormatException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException) SourceDocumentState(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) RResponse(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RResponse) RDocument(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RDocument) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with RDocument

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RDocument in project webanno by webanno.

the class RemoteApiController2 method documentList.

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

Aggregations

Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)2 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)2 RDocument (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RDocument)2 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)2 ApiOperation (io.swagger.annotations.ApiOperation)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 SourceDocumentState (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState)1 IllegalObjectStateException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.IllegalObjectStateException)1 UnsupportedFormatException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException)1 RResponse (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RResponse)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1