Search in sources :

Example 16 with Project

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

the class RemoteApiController method curationDocumentRead.

/**
 * Download curated document with requested parameters
 *
 * Test when running in Eclipse: Open your browser, paste following URL with appropriate values:
 *
 * http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/curationdoc/
 * {aSourceDocumentId}?format=xmi
 *
 * @param response
 *            HttpServletResponse.
 * @param aProjectId
 *            {@link Project} ID.
 * @param aSourceDocumentId
 *            {@link SourceDocument} ID.
 * @param format
 *            Export format.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + CURATION + "/{" + PARAM_DOCUMENT_ID + "}", method = RequestMethod.GET)
public void curationDocumentRead(HttpServletResponse response, @PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId, @RequestParam(value = PARAM_FORMAT, required = false) String format) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        response.sendError(HttpStatus.BAD_REQUEST.value(), "User [" + username + "] not found.");
        return;
    }
    // Get project
    Project project;
    try {
        project = projectRepository.getProject(aProjectId);
    } catch (NoResultException e) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Project" + aProjectId + "] not found.");
        return;
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        response.sendError(HttpStatus.FORBIDDEN.value(), "User [" + username + "] is not allowed to access project [" + aProjectId + "]");
        return;
    }
    // Get source document
    SourceDocument srcDocument;
    try {
        srcDocument = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
    } catch (NoResultException e) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Source document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "] not found.");
        return;
    }
    // Check if curation is complete
    if (!SourceDocumentState.CURATION_FINISHED.equals(srcDocument.getState())) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Curation of source document [" + aSourceDocumentId + "] not yet complete.");
        return;
    }
    String formatId;
    if (format == null) {
        formatId = srcDocument.getFormat();
    } else {
        formatId = format;
    }
    Class<?> writer = importExportService.getWritableFormats().get(formatId);
    if (writer == null) {
        LOG.info("[" + srcDocument.getName() + "] No writer found for format [" + formatId + "] - exporting as WebAnno TSV instead.");
        writer = WebannoTsv3XWriter.class;
    }
    // Temporary file of annotation document
    File downloadableFile = importExportService.exportAnnotationDocument(srcDocument, WebAnnoConst.CURATION_USER, writer, srcDocument.getName(), Mode.CURATION);
    try {
        // Set mime type
        String mimeType = URLConnection.guessContentTypeFromName(downloadableFile.getName());
        if (mimeType == null) {
            LOG.info("mimetype is not detectable, will take default");
            mimeType = "application/octet-stream";
        }
        // Set response
        response.setContentType(mimeType);
        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition", "inline; filename=\"" + downloadableFile.getName() + "\"");
        response.setContentLength((int) downloadableFile.length());
        InputStream inputStream = new BufferedInputStream(new FileInputStream(downloadableFile));
        FileCopyUtils.copy(inputStream, response.getOutputStream());
    } catch (Exception e) {
        LOG.info("Exception occured" + e.getMessage());
    } finally {
        if (downloadableFile.exists()) {
            downloadableFile.delete();
        }
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) NoResultException(javax.persistence.NoResultException) ZipFile(java.util.zip.ZipFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInputStream(java.io.FileInputStream) NoResultException(javax.persistence.NoResultException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with Project

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

the class RemoteApiController2 method curationDelete.

@ApiOperation(value = "Delete a user's annotations of one document from a project")
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + CURATION, method = RequestMethod.DELETE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<Void>> curationDelete(@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);
    curationService.deleteCurationCas(doc);
    // guess is that we set the state back to annotation-in-progress.
    switch(doc.getState()) {
        // Fall-through
        case CURATION_IN_PROGRESS:
        case CURATION_FINISHED:
            doc.setState(SourceDocumentState.ANNOTATION_IN_PROGRESS);
            documentService.createSourceDocument(doc);
            break;
        default:
    }
    return ResponseEntity.ok(new RResponse<>(INFO, "Curated annotations for document [" + aDocumentId + "] deleted from project [" + aProjectId + "]."));
}
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) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with Project

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

the class RemoteApiController2 method projectList.

@ApiOperation(value = "List the projects accessible by the authenticated user")
@RequestMapping(value = ("/" + PROJECTS), method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<List<RProject>>> projectList() throws Exception {
    // Get current user - this will throw an exception if the current user does not exit
    User user = getCurrentUser();
    // Get projects with permission
    List<Project> accessibleProjects = projectService.listAccessibleProjects(user);
    // Collect all the projects
    List<RProject> projectList = new ArrayList<>();
    for (Project project : accessibleProjects) {
        projectList.add(new RProject(project));
    }
    return ResponseEntity.ok(new RResponse<>(projectList));
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) ArrayList(java.util.ArrayList) RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with Project

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

the class RemoteApiController2 method documentDelete.

@ApiOperation(value = "Delete a document from a project")
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}", method = RequestMethod.DELETE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<Void>> documentDelete(@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);
    documentService.removeSourceDocument(doc);
    return ResponseEntity.ok(new RResponse<>(INFO, "Document [" + aDocumentId + "] deleted from project [" + aProjectId + "]."));
}
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) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with Project

use of de.tudarmstadt.ukp.clarin.webanno.model.Project 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)

Aggregations

Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)68 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)22 File (java.io.File)22 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)17 IOException (java.io.IOException)15 ApiOperation (io.swagger.annotations.ApiOperation)14 NoResultException (javax.persistence.NoResultException)13 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)12 ArrayList (java.util.ArrayList)12 MDC (org.slf4j.MDC)12 InputStream (java.io.InputStream)11 JCas (org.apache.uima.jcas.JCas)10 UIMAException (org.apache.uima.UIMAException)8 MultipartFile (org.springframework.web.multipart.MultipartFile)8 BufferedInputStream (java.io.BufferedInputStream)7 FileInputStream (java.io.FileInputStream)7 Transactional (org.springframework.transaction.annotation.Transactional)7 ZipFile (java.util.zip.ZipFile)6