Search in sources :

Example 51 with SourceDocument

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

the class RemoteApiController method sourceDocumentDelete.

/**
 * Delete the source document in project if user has an ADMIN permission
 *
 * To test when running in Eclipse, use the Linux "curl" command.
 *
 * curl -v -X DELETE
 * 'http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs/
 * {aSourceDocumentId}'
 *
 * @param aProjectId
 *            {@link Project} ID.
 * @param aSourceDocumentId
 *            {@link SourceDocument} ID.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}", method = RequestMethod.DELETE)
public ResponseEntity<String> sourceDocumentDelete(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        return ResponseEntity.badRequest().body("User [" + username + "] not found.");
    }
    // Get project
    Project project;
    try {
        project = projectRepository.getProject(aProjectId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project [" + aProjectId + "] not found.");
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to access project [" + aProjectId + "]");
    }
    LOG.info("Deleting document [" + project.getName() + "]");
    // Get source document
    SourceDocument srcDocument;
    try {
        srcDocument = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Source document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "] not found.");
    }
    documentRepository.removeSourceDocument(srcDocument);
    LOG.info("Successfully deleted project : [" + aProjectId + "]");
    return ResponseEntity.ok("Source document [" + aSourceDocumentId + "] in project [" + aProjectId + "] deleted.");
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) NoResultException(javax.persistence.NoResultException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 52 with SourceDocument

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

the class RemoteApiController method annotationDocumentList.

/**
 * List annotation documents for a source document in a projects where user is ADMIN
 *
 * Test when running in Eclipse: Open your browser, paste following URL with appropriate values:
 *
 * http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs/{
 * aSourceDocumentId}/annos
 *
 * @param aProjectId
 *            {@link Project} ID
 * @param aSourceDocumentId
 *            {@link SourceDocument} ID
 * @return JSON string of all the annotation documents with their projects.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS, method = RequestMethod.GET)
public ResponseEntity<String> annotationDocumentList(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        return ResponseEntity.badRequest().body("User [" + username + "] not found.");
    }
    // Get project
    Project project;
    try {
        project = projectRepository.getProject(aProjectId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project [" + aProjectId + "] not found.");
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to access project [" + aProjectId + "]");
    }
    // Get source document
    SourceDocument srcDocument;
    try {
        srcDocument = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Source document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "] not found.");
    }
    List<AnnotationDocument> annList = documentRepository.listAllAnnotationDocuments(srcDocument);
    JSONArray annDocArr = new JSONArray();
    for (AnnotationDocument annDoc : annList) {
        if (annDoc.getState().equals(AnnotationDocumentState.FINISHED) || annDoc.getState().equals(AnnotationDocumentState.IN_PROGRESS)) {
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ssZ");
            JSONObject annDocObj = new JSONObject();
            annDocObj.put("user", annDoc.getUser());
            annDocObj.put("state", annDoc.getState().getId());
            if (annDoc.getTimestamp() != null) {
                annDocObj.put("timestamp", sdf.format(annDoc.getTimestamp()));
            }
            annDocArr.put(annDocObj);
        }
    }
    JSONObject returnJSON = new JSONObject();
    returnJSON.put(srcDocument.getName(), annDocArr);
    return ResponseEntity.ok(returnJSON.toString());
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) JSONObject(org.apache.wicket.ajax.json.JSONObject) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JSONArray(org.apache.wicket.ajax.json.JSONArray) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) NoResultException(javax.persistence.NoResultException) SimpleDateFormat(java.text.SimpleDateFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 53 with SourceDocument

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

the class RemoteApiController method sourceDocumentList.

/**
 * Show source documents in given project where user has ADMIN access
 *
 * http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs
 *
 * @param aProjectId the project ID
 * @return JSON with {@link SourceDocument} : id
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS, method = RequestMethod.GET)
public ResponseEntity<String> sourceDocumentList(@PathVariable(PARAM_PROJECT_ID) long aProjectId) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        return ResponseEntity.badRequest().body("User [" + username + "] not found.");
    }
    // Get project
    Project project;
    try {
        project = projectRepository.getProject(aProjectId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project [" + aProjectId + "] not found.");
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to access project [" + aProjectId + "]");
    }
    List<SourceDocument> srcDocumentList = documentRepository.listSourceDocuments(project);
    JSONArray sourceDocumentJSONArr = new JSONArray();
    for (SourceDocument s : srcDocumentList) {
        JSONObject sourceDocumentJSONObj = new JSONObject();
        sourceDocumentJSONObj.put("id", s.getId());
        sourceDocumentJSONObj.put("name", s.getName());
        sourceDocumentJSONObj.put("state", s.getState());
        sourceDocumentJSONArr.put(sourceDocumentJSONObj);
    }
    return ResponseEntity.ok(sourceDocumentJSONArr.toString());
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) JSONObject(org.apache.wicket.ajax.json.JSONObject) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JSONArray(org.apache.wicket.ajax.json.JSONArray) NoResultException(javax.persistence.NoResultException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 54 with SourceDocument

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

the class RemoteApiController method annotationDocumentRead.

/**
 * Download annotation 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}/sourcedocs/{
 * aSourceDocumentId}/annos/{annotatorName}?format="text"
 *
 * @param response
 *            HttpServletResponse.
 * @param aProjectId
 *            {@link Project} ID.
 * @param aSourceDocumentId
 *            {@link SourceDocument} ID.
 * @param annotatorName
 *            {@link User} name.
 * @param format
 *            Export format.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS + "/{" + PARAM_USERNAME + "}", method = RequestMethod.GET)
public void annotationDocumentRead(HttpServletResponse response, @PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId, @PathVariable(PARAM_USERNAME) String annotatorName, @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 annotator user
    User annotator = userRepository.get(annotatorName);
    if (annotator == null) {
        response.sendError(HttpStatus.BAD_REQUEST.value(), "Annotator user [" + annotatorName + "] not found.");
        return;
    }
    // Get source document
    SourceDocument srcDoc;
    try {
        srcDoc = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
    } catch (NoResultException e) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "].");
        return;
    }
    // Get annotation document
    AnnotationDocument annDoc;
    try {
        annDoc = documentRepository.getAnnotationDocument(srcDoc, annotator);
    } catch (NoResultException e) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Annotations for user [" + annotatorName + "] not found on document [" + aSourceDocumentId + "] in project [" + aProjectId + "].");
        return;
    }
    String formatId;
    if (format == null) {
        formatId = srcDoc.getFormat();
    } else {
        formatId = format;
    }
    Class<?> writer = importExportService.getWritableFormats().get(formatId);
    if (writer == null) {
        String msg = "[" + srcDoc.getName() + "] No writer found for format [" + formatId + "] - exporting as WebAnno TSV instead.";
        LOG.info(msg);
        writer = WebannoTsv3XWriter.class;
    }
    // Temporary file of annotation document
    File downloadableFile = importExportService.exportAnnotationDocument(srcDoc, annotatorName, writer, annDoc.getName(), Mode.ANNOTATION);
    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 : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) NoResultException(javax.persistence.NoResultException) FileInputStream(java.io.FileInputStream) NoResultException(javax.persistence.NoResultException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) BufferedInputStream(java.io.BufferedInputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 55 with SourceDocument

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

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