Search in sources :

Example 1 with RResponse

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RResponse in project webanno by webanno.

the class AeroRemoteApiController method documentCreate.

@ApiOperation(value = "Create a new document in a project")
@ApiImplicitParams({ @ApiImplicitParam(name = PARAM_NAME, paramType = "form", required = true), @ApiImplicitParam(name = PARAM_FORMAT, paramType = "form", required = true), @ApiImplicitParam(name = PARAM_STATE, paramType = "form", required = true) })
@// 
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(PARAM_CONTENT) MultipartFile aFile, @RequestParam(PARAM_NAME) String aName, @RequestParam(PARAM_FORMAT) String aFormat, @RequestParam(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
    if (!importExportService.getReadableFormatById(aFormat).isPresent()) {
        throw new UnsupportedFormatException("Format [%s] not supported. Acceptable formats are %s.", aFormat, importExportService.getReadableFormats().stream().map(FormatSupport::getId).sorted().collect(Collectors.toList()));
    }
    // 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.aero.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) IllegalObjectStateException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.IllegalObjectStateException) UnsupportedFormatException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.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.aero.model.RResponse) RDocument(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RDocument) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with RResponse

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RResponse in project webanno by webanno.

the class AeroRemoteApiController method annotationsCreate.

@ApiOperation(value = "Create annotations for a document in a project")
@ApiImplicitParams({ @ApiImplicitParam(name = PARAM_FORMAT, paramType = "form", required = true), @ApiImplicitParam(name = PARAM_STATE, paramType = "form", required = true) })
@// 
RequestMapping(value = // 
"/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS + "/{" + PARAM_ANNOTATOR_ID + // 
"}", // 
method = RequestMethod.POST, // 
consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<RAnnotation>> annotationsCreate(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aDocumentId, @PathVariable(PARAM_ANNOTATOR_ID) String aAnnotatorId, @RequestPart(PARAM_CONTENT) MultipartFile aFile, @RequestParam(PARAM_FORMAT) Optional<String> aFormat, @RequestParam(PARAM_STATE) Optional<String> aState, UriComponentsBuilder aUcb) throws Exception {
    User annotator = getUser(aAnnotatorId);
    Project project = getProject(aProjectId);
    SourceDocument document = getDocument(project, aDocumentId);
    AnnotationDocument anno = getAnnotation(document, aAnnotatorId, true);
    CAS annotationCas = createCompatibleCas(aProjectId, aDocumentId, aFile, aFormat);
    // If they are compatible, then we can store the new annotations
    documentService.writeAnnotationCas(annotationCas, document, annotator, false);
    // Set state if one was provided
    if (aState.isPresent()) {
        anno.setState(parseAnnotationDocumentState(aState.get()));
        documentService.createAnnotationDocument(anno);
    }
    RResponse<RAnnotation> response = new RResponse<>(new RAnnotation(anno));
    if (aState.isPresent()) {
        response.addMessage(INFO, "State of annotations of user [" + aAnnotatorId + "] on document [" + document.getId() + "] set to [" + aState.get() + "]");
    }
    return ResponseEntity.created(aUcb.path(API_BASE + "/" + PROJECTS + "/{pid}/" + DOCUMENTS + "/{did}/" + ANNOTATIONS + "/{aid}").buildAndExpand(project.getId(), document.getId(), annotator.getUsername()).toUri()).body(response);
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) RAnnotation(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RAnnotation) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) CAS(org.apache.uima.cas.CAS) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) RResponse(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RResponse) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with RResponse

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RResponse in project webanno by webanno.

the class AeroRemoteApiController method curationCreate.

@ApiOperation(value = "Create curation for a document in a project")
@ApiImplicitParams({ @ApiImplicitParam(name = PARAM_FORMAT, paramType = "form", required = true), @ApiImplicitParam(name = PARAM_STATE, paramType = "form", required = true) })
@// 
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, @RequestPart(value = PARAM_CONTENT) MultipartFile aFile, @RequestParam(PARAM_FORMAT) Optional<String> aFormat, @RequestParam(PARAM_STATE) Optional<String> aState, UriComponentsBuilder aUcb) throws Exception {
    Project project = getProject(aProjectId);
    SourceDocument document = getDocument(project, aDocumentId);
    CAS 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.aero.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) IllegalObjectStateException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.IllegalObjectStateException) RAnnotation(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RAnnotation) SourceDocumentState(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState) CAS(org.apache.uima.cas.CAS) AnnotationDocumentState(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocumentState) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) RResponse(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RResponse) Date(java.util.Date) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with RResponse

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RResponse in project webanno by webanno.

the class AeroRemoteApiController method projectCreate.

@ApiOperation(value = "Create a new project")
@ApiImplicitParams({ @ApiImplicitParam(name = PARAM_NAME, paramType = "form", required = true), @ApiImplicitParam(name = PARAM_CREATOR, paramType = "form") })
@// 
RequestMapping(// 
value = ("/" + PROJECTS), // 
method = RequestMethod.POST, // 
consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<RProject>> projectCreate(@RequestParam(PARAM_NAME) String aName, @RequestParam(PARAM_CREATOR) Optional<String> aCreator, UriComponentsBuilder aUcb) throws Exception {
    // Get current user - this will throw an exception if the current user does not exit
    User user = getCurrentUser();
    // Check for the access
    assertPermission("User [" + user.getUsername() + "] is not allowed to create projects", userRepository.isProjectCreator(user) || userRepository.isAdministrator(user));
    // Check if the user can create projects for another user
    assertPermission("User [" + user.getUsername() + "] is not allowed to create projects for user [" + aCreator.orElse("<unspecified>") + "]", userRepository.isAdministrator(user) || (aCreator.isPresent() && aCreator.get().equals(user.getUsername())));
    // Existing project
    if (projectService.existsProject(aName)) {
        throw new ObjectExistsException("A project with name [" + aName + "] already exists");
    }
    // Create the project and initialize tags
    LOG.info("Creating project [" + aName + "]");
    Project project = new Project();
    project.setName(aName);
    project.setMode(WebAnnoConst.PROJECT_TYPE_ANNOTATION);
    project.setScriptDirection(ScriptDirection.LTR);
    project.setState(ProjectState.NEW);
    projectService.createProject(project);
    projectService.initializeProject(project);
    // Create permission for the project creator
    String owner = aCreator.isPresent() ? aCreator.get() : user.getUsername();
    projectService.createProjectPermission(new ProjectPermission(project, owner, PermissionLevel.MANAGER));
    projectService.createProjectPermission(new ProjectPermission(project, owner, PermissionLevel.CURATOR));
    projectService.createProjectPermission(new ProjectPermission(project, owner, PermissionLevel.ANNOTATOR));
    RResponse<RProject> response = new RResponse<>(new RProject(project));
    return ResponseEntity.created(aUcb.path(API_BASE + "/" + PROJECTS + "/{id}").buildAndExpand(project.getId()).toUri()).body(response);
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) ObjectExistsException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.ObjectExistsException) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission) RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RProject) RResponse(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RResponse) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)4 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RProject)4 RResponse (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RResponse)4 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)4 ApiOperation (io.swagger.annotations.ApiOperation)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)3 SourceDocumentState (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState)2 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)2 IllegalObjectStateException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.IllegalObjectStateException)2 RAnnotation (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RAnnotation)2 CAS (org.apache.uima.cas.CAS)2 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)1 AnnotationDocumentState (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocumentState)1 ProjectPermission (de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission)1 ObjectExistsException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.ObjectExistsException)1 UnsupportedFormatException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.UnsupportedFormatException)1 RDocument (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RDocument)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1