Search in sources :

Example 1 with WorkspaceMaterialAudioFieldAnswerClip

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

the class WorkspaceMaterialAudioFieldAnswerClipDAO method create.

public WorkspaceMaterialAudioFieldAnswerClip create(WorkspaceMaterialAudioFieldAnswer fieldAnswer, byte[] content, String contentType, String clipId, String fileName) {
    WorkspaceMaterialAudioFieldAnswerClip workspaceMaterialAudioFieldAnswerClip = new WorkspaceMaterialAudioFieldAnswerClip();
    workspaceMaterialAudioFieldAnswerClip.setFieldAnswer(fieldAnswer);
    workspaceMaterialAudioFieldAnswerClip.setContent(content);
    workspaceMaterialAudioFieldAnswerClip.setContentType(contentType);
    workspaceMaterialAudioFieldAnswerClip.setClipId(clipId);
    workspaceMaterialAudioFieldAnswerClip.setFileName(fileName);
    return persist(workspaceMaterialAudioFieldAnswerClip);
}
Also used : WorkspaceMaterialAudioFieldAnswerClip(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip)

Example 2 with WorkspaceMaterialAudioFieldAnswerClip

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

the class WorkspaceAudioFieldIOHandler method store.

@Override
public void store(WorkspaceMaterialField field, WorkspaceMaterialReply reply, String value) throws WorkspaceFieldIOException {
    Clip[] clips = null;
    try {
        clips = new ObjectMapper().readValue(value, Clip[].class);
    } catch (IOException e) {
        throw new WorkspaceFieldIOException("Could not unmarshal field reply", e);
    }
    WorkspaceMaterialAudioFieldAnswer fieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialAudioFieldAnswerByFieldAndReply(field, reply);
    if (fieldAnswer == null) {
        fieldAnswer = workspaceMaterialFieldAnswerController.createWorkspaceMaterialAudioFieldAnswer(field, reply);
    }
    List<WorkspaceMaterialAudioFieldAnswerClip> existingClips = workspaceMaterialFieldAnswerController.listWorkspaceMaterialAudioFieldAnswerClipsByFieldAnswer(fieldAnswer);
    List<String> existingClipIds = new ArrayList<>(existingClips.size());
    for (WorkspaceMaterialAudioFieldAnswerClip existingClip : existingClips) {
        existingClipIds.add(existingClip.getClipId());
    }
    for (Clip clip : clips) {
        if (existingClipIds.contains(clip.getId())) {
            // Already existing clip
            existingClipIds.remove(clip.getId());
        } else {
            // New clip
            try {
                byte[] audioData = TempFileUtils.getTempFileData(clip.getId());
                if (audioData == null) {
                    throw new WorkspaceFieldIOException("Temp audio does not exist");
                }
                workspaceMaterialFieldAnswerController.createWorkspaceMaterialAudioFieldAnswerClip(fieldAnswer, audioData, clip.getContentType(), clip.getId(), clip.getName());
            } catch (IOException e) {
                throw new WorkspaceFieldIOException("Failed to retrieve clip data", e);
            }
        }
    }
    for (String existingClipId : existingClipIds) {
        WorkspaceMaterialAudioFieldAnswerClip workspaceMaterialAudioFieldAnswerClip = workspaceMaterialFieldAnswerController.findWorkspaceMaterialAudioFieldAnswerClipByClipId(existingClipId);
        if (workspaceMaterialAudioFieldAnswerClip != null) {
            workspaceMaterialFieldAnswerController.deleteWorkspaceMaterialAudioFieldAnswerClip(workspaceMaterialAudioFieldAnswerClip);
        }
    }
}
Also used : WorkspaceMaterialAudioFieldAnswerClip(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip) WorkspaceMaterialAudioFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswer) WorkspaceMaterialAudioFieldAnswerClip(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 3 with WorkspaceMaterialAudioFieldAnswerClip

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

the class WorkspaceMaterialAudioFieldAnswerClipDAO method findByClipId.

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

Example 4 with WorkspaceMaterialAudioFieldAnswerClip

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

the class WorkspaceMaterialAudioFieldAnswerClipDAO method listByFieldAnswer.

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

Example 5 with WorkspaceMaterialAudioFieldAnswerClip

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

the class WorkspaceRESTService method getAudioAnswer.

@GET
@Path("/audioanswer/{CLIPID}")
@RESTPermit(handling = Handling.INLINE)
public Response getAudioAnswer(@PathParam("CLIPID") String clipId) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    WorkspaceMaterialAudioFieldAnswerClip answerClip = workspaceMaterialFieldAnswerController.findWorkspaceMaterialAudioFieldAnswerClipByClipId(clipId);
    if (answerClip != null) {
        fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply workspaceMaterialReply = answerClip.getFieldAnswer().getReply();
        if (workspaceMaterialReply == null) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Could not find reply from answer audio %d", answerClip.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(answerClip.getContent()).type(answerClip.getContentType()).header("Content-Disposition", "attachment; filename=\"" + answerClip.getFileName().replaceAll("\"", "\\\"") + "\"").build();
    }
    return Response.status(Status.NOT_FOUND).build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterialAudioFieldAnswerClip(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip) 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

WorkspaceMaterialAudioFieldAnswerClip (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip)6 WorkspaceMaterialAudioFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswer)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)1 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)1 WorkspaceRootFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)1 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1