Search in sources :

Example 1 with WorkspaceMaterialCompositeReply

use of fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialCompositeReply in project muikku by otavanopisto.

the class WorkspaceRESTService method getWorkspaceMaterialAnswers.

@GET
@Path("/workspaces/{WORKSPACEENTITYID}/materials/{WORKSPACEMATERIALID}/compositeMaterialReplies")
@RESTPermitUnimplemented
public Response getWorkspaceMaterialAnswers(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId, @QueryParam("userEntityId") Long userEntityId) {
    // TODO: Security
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).entity("Not logged in").build();
    }
    // TODO Return everyone's answers
    if (userEntityId == null) {
        return Response.status(Status.NOT_IMPLEMENTED).build();
    }
    UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
    WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
    if (workspaceMaterial == null) {
        return Response.status(Status.NOT_FOUND).entity("Workspace material could not be found").build();
    }
    List<WorkspaceMaterialFieldAnswer> answers = new ArrayList<>();
    try {
        fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply reply = workspaceMaterialReplyController.findWorkspaceMaterialReplyByWorkspaceMaterialAndUserEntity(workspaceMaterial, userEntity);
        if (reply != null) {
            List<WorkspaceMaterialField> fields = workspaceMaterialFieldController.listWorkspaceMaterialFieldsByWorkspaceMaterial(workspaceMaterial);
            for (WorkspaceMaterialField field : fields) {
                String value = workspaceMaterialFieldController.retrieveFieldValue(field, reply);
                Material material = field.getQueryField().getMaterial();
                WorkspaceMaterialFieldAnswer answer = new WorkspaceMaterialFieldAnswer(workspaceMaterial.getId(), material.getId(), field.getEmbedId(), field.getQueryField().getName(), value);
                answers.add(answer);
            }
        }
        WorkspaceMaterialCompositeReply result = new WorkspaceMaterialCompositeReply(answers, reply != null ? reply.getState() : null, reply != null ? reply.getCreated() : null, reply != null ? reply.getLastModified() : null, reply != null ? reply.getSubmitted() : null, reply != null ? reply.getWithdrawn() : null);
        return Response.ok(result).build();
    } catch (WorkspaceFieldIOException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Internal error occurred while retrieving field answers: " + e.getMessage()).build();
    }
}
Also used : WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) ArrayList(java.util.ArrayList) WorkspaceMaterialCompositeReply(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialCompositeReply) Material(fi.otavanopisto.muikku.plugins.material.model.Material) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceMaterialFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialFieldAnswer) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Aggregations

UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)1 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)1 Material (fi.otavanopisto.muikku.plugins.material.model.Material)1 WorkspaceFieldIOException (fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException)1 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)1 WorkspaceMaterialField (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)1 WorkspaceMaterialCompositeReply (fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialCompositeReply)1 WorkspaceMaterialFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialFieldAnswer)1 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1