Search in sources :

Example 1 with JSONArray

use of org.apache.wicket.ajax.json.JSONArray 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 2 with JSONArray

use of org.apache.wicket.ajax.json.JSONArray in project webanno by webanno.

the class RemoteApiController method projectList.

/**
 * List all the projects for a given user with their roles
 *
 * Test when running in Eclipse: Open your browser, paste following URL with appropriate values
 * for username and password:
 *
 * http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects
 *
 * @return JSON string of project where user has access to and respective roles in the project.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = ("/" + PROJECTS), method = RequestMethod.GET)
public ResponseEntity<String> projectList() 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 projects with permission
    List<Project> accessibleProjects = projectRepository.listAccessibleProjects(user);
    // Add permissions for each project into JSON array and store in JSON object
    JSONObject returnJSONObj = new JSONObject();
    for (Project project : accessibleProjects) {
        String projectId = Long.toString(project.getId());
        List<ProjectPermission> projectPermissions = projectRepository.listProjectPermissionLevel(user, project);
        JSONArray permissionArr = new JSONArray();
        JSONObject projectJSON = new JSONObject();
        for (ProjectPermission p : projectPermissions) {
            permissionArr.put(p.getLevel().getName());
        }
        projectJSON.put(project.getName(), permissionArr);
        returnJSONObj.put(projectId, projectJSON);
    }
    return ResponseEntity.ok(returnJSONObj.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) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission) JSONArray(org.apache.wicket.ajax.json.JSONArray) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with JSONArray

use of org.apache.wicket.ajax.json.JSONArray 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)

Aggregations

Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)3 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)3 JSONArray (org.apache.wicket.ajax.json.JSONArray)3 JSONObject (org.apache.wicket.ajax.json.JSONObject)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)2 NoResultException (javax.persistence.NoResultException)2 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)1 ProjectPermission (de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission)1 SimpleDateFormat (java.text.SimpleDateFormat)1