Search in sources :

Example 1 with WorkspaceMaterialFileFieldAnswerFile

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

the class WorkspaceFileFieldIOHandler method retrieve.

@Override
public String retrieve(WorkspaceMaterialField field, WorkspaceMaterialReply reply) throws WorkspaceFieldIOException {
    List<File> result = new ArrayList<>();
    WorkspaceMaterialFileFieldAnswer fieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerByFieldAndReply(field, reply);
    if (fieldAnswer != null) {
        List<WorkspaceMaterialFileFieldAnswerFile> answerFiles = workspaceMaterialFieldAnswerController.listWorkspaceMaterialFileFieldAnswerFilesByFieldAnswer(fieldAnswer);
        for (WorkspaceMaterialFileFieldAnswerFile answerFile : answerFiles) {
            result.add(new File(answerFile.getFileId(), answerFile.getFileName(), answerFile.getContentType()));
        }
    }
    try {
        return new ObjectMapper().writeValueAsString(result);
    } catch (IOException e) {
        throw new WorkspaceFieldIOException("Could not marshal file file response", e);
    }
}
Also used : WorkspaceMaterialFileFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 2 with WorkspaceMaterialFileFieldAnswerFile

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

the class WorkspaceMaterialFileFieldAnswerFileDAO method findByFileId.

public WorkspaceMaterialFileFieldAnswerFile findByFileId(String fileId) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialFileFieldAnswerFile> criteria = criteriaBuilder.createQuery(WorkspaceMaterialFileFieldAnswerFile.class);
    Root<WorkspaceMaterialFileFieldAnswerFile> root = criteria.from(WorkspaceMaterialFileFieldAnswerFile.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(WorkspaceMaterialFileFieldAnswerFile_.fileId), fileId));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile)

Example 3 with WorkspaceMaterialFileFieldAnswerFile

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

the class WorkspaceMaterialFileFieldAnswerFileDAO method listByFieldAnswer.

public List<WorkspaceMaterialFileFieldAnswerFile> listByFieldAnswer(WorkspaceMaterialFileFieldAnswer fieldAnswer) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialFileFieldAnswerFile> criteria = criteriaBuilder.createQuery(WorkspaceMaterialFileFieldAnswerFile.class);
    Root<WorkspaceMaterialFileFieldAnswerFile> root = criteria.from(WorkspaceMaterialFileFieldAnswerFile.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(WorkspaceMaterialFileFieldAnswerFile_.fieldAnswer), fieldAnswer));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile)

Example 4 with WorkspaceMaterialFileFieldAnswerFile

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

the class WorkspaceRESTService method getAllFileAnswers.

@GET
@Produces("application/zip")
@Path("/allfileanswers/{FILEID}")
@RESTPermit(handling = Handling.INLINE)
public Response getAllFileAnswers(@PathParam("FILEID") String fileId, @QueryParam("archiveName") String archiveName) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    // Find the initial file
    WorkspaceMaterialFileFieldAnswerFile answerFile = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerFileByFileId(fileId);
    if (answerFile == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    // Access check
    fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply workspaceMaterialReply = answerFile.getFieldAnswer().getReply();
    if (workspaceMaterialReply == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find reply from answer file %d", answerFile.getId())).build();
    }
    WorkspaceMaterial workspaceMaterial = workspaceMaterialReply.getWorkspaceMaterial();
    if (workspaceMaterial == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace material from reply %d", workspaceMaterialReply.getId())).build();
    }
    WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
    if (workspaceRootFolder == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace root folder for material %d", workspaceMaterial.getId())).build();
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceRootFolder.getWorkspaceEntityId());
    if (workspaceEntity == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace entity for root folder %d", workspaceRootFolder.getId())).build();
    }
    if (!workspaceMaterialReply.getUserEntityId().equals(sessionController.getLoggedUserEntity().getId())) {
        if (!sessionController.hasWorkspacePermission(MuikkuPermissions.ACCESS_STUDENT_ANSWERS, workspaceEntity)) {
            return Response.status(Status.FORBIDDEN).build();
        }
    }
    // Fetch all files belonging to the same answer as the initial file
    List<WorkspaceMaterialFileFieldAnswerFile> answerFiles = workspaceMaterialFieldAnswerController.listWorkspaceMaterialFileFieldAnswerFilesByFieldAnswer(answerFile.getFieldAnswer());
    if (CollectionUtils.isEmpty(answerFiles)) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("answerFiles not found")).build();
    }
    try {
        Set<String> fileNames = new HashSet<String>();
        StreamingOutput output = new StreamingOutput() {

            @Override
            public void write(OutputStream out) throws IOException {
                ZipOutputStream zout = new ZipOutputStream(out);
                for (WorkspaceMaterialFileFieldAnswerFile file : answerFiles) {
                    // Prevent duplicate file names
                    String fileName = file.getFileName();
                    if (fileNames.contains(fileName)) {
                        int counter = 1;
                        String name = file.getFileName();
                        String prefix = "";
                        if (StringUtils.contains(name, ".")) {
                            prefix = StringUtils.substringAfterLast(name, ".");
                            name = StringUtils.substringBeforeLast(name, ".");
                        }
                        if (!StringUtils.isEmpty(prefix)) {
                            prefix = String.format(".%s", prefix);
                        }
                        while (fileNames.contains(fileName)) {
                            fileName = String.format("%s (%s)%s", name, counter++, prefix);
                        }
                    }
                    fileNames.add(fileName);
                    // Zip file
                    ZipEntry ze = new ZipEntry(fileName);
                    zout.putNextEntry(ze);
                    InputStream inputStream = new ByteArrayInputStream(file.getContent());
                    IOUtils.copy(inputStream, zout);
                    zout.closeEntry();
                }
                zout.flush();
                zout.close();
            }
        };
        archiveName = StringUtils.defaultIfEmpty(archiveName, "files.zip");
        return Response.ok(output).header("Content-Disposition", "attachment; filename=\"" + archiveName.replaceAll("\"", "\\\"") + "\"").build();
    } catch (Exception e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) StreamingOutput(javax.ws.rs.core.StreamingOutput) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) IOException(java.io.IOException) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile) ZipOutputStream(java.util.zip.ZipOutputStream) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with WorkspaceMaterialFileFieldAnswerFile

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

the class WorkspaceRESTService method getFileAnswer.

@GET
@Path("/fileanswer/{FILEID}")
@RESTPermit(handling = Handling.INLINE)
public Response getFileAnswer(@PathParam("FILEID") String fileId) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    WorkspaceMaterialFileFieldAnswerFile answerFile = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerFileByFileId(fileId);
    if (answerFile == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply workspaceMaterialReply = answerFile.getFieldAnswer().getReply();
    if (workspaceMaterialReply == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find reply from answer file %d", answerFile.getId())).build();
    }
    WorkspaceMaterial workspaceMaterial = workspaceMaterialReply.getWorkspaceMaterial();
    if (workspaceMaterial == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace material from reply %d", workspaceMaterialReply.getId())).build();
    }
    WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
    if (workspaceRootFolder == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace root folder for material %d", workspaceMaterial.getId())).build();
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceRootFolder.getWorkspaceEntityId());
    if (workspaceEntity == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find workspace entity for root folder %d", workspaceRootFolder.getId())).build();
    }
    if (!workspaceMaterialReply.getUserEntityId().equals(sessionController.getLoggedUserEntity().getId())) {
        if (!sessionController.hasWorkspacePermission(MuikkuPermissions.ACCESS_STUDENT_ANSWERS, workspaceEntity)) {
            return Response.status(Status.FORBIDDEN).build();
        }
    }
    return Response.ok(answerFile.getContent()).type(answerFile.getContentType()).header("Content-Disposition", "attachment; filename=\"" + answerFile.getFileName().replaceAll("\"", "\\\"") + "\"").build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

WorkspaceMaterialFileFieldAnswerFile (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile)7 IOException (java.io.IOException)3 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)2 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)2 WorkspaceMaterialFileFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer)2 WorkspaceRootFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)2 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)2 HashSet (java.util.HashSet)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 WorkspaceFieldIOException (fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1