Search in sources :

Example 1 with WorkspaceMaterialAudioFieldAnswer

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

the class WorkspaceMaterialAudioFieldAnswerDAO method findByQueryFieldAndReply.

public WorkspaceMaterialAudioFieldAnswer findByQueryFieldAndReply(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialAudioFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialAudioFieldAnswer.class);
    Root<WorkspaceMaterialAudioFieldAnswer> root = criteria.from(WorkspaceMaterialAudioFieldAnswer.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceMaterialAudioFieldAnswer_.field), field), criteriaBuilder.equal(root.get(WorkspaceMaterialAudioFieldAnswer_.reply), reply)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) WorkspaceMaterialAudioFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswer) EntityManager(javax.persistence.EntityManager)

Example 2 with WorkspaceMaterialAudioFieldAnswer

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

the class WorkspaceMaterialAudioFieldAnswerDAO method create.

public WorkspaceMaterialAudioFieldAnswer create(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
    WorkspaceMaterialAudioFieldAnswer workspaceMaterialAudioFieldAnswer = new WorkspaceMaterialAudioFieldAnswer();
    workspaceMaterialAudioFieldAnswer.setField(field);
    workspaceMaterialAudioFieldAnswer.setReply(reply);
    return persist(workspaceMaterialAudioFieldAnswer);
}
Also used : WorkspaceMaterialAudioFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswer)

Example 3 with WorkspaceMaterialAudioFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswer 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 4 with WorkspaceMaterialAudioFieldAnswer

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

the class WorkspaceAudioFieldIOHandler method retrieve.

@Override
public String retrieve(WorkspaceMaterialField field, WorkspaceMaterialReply reply) throws WorkspaceFieldIOException {
    List<Clip> result = new ArrayList<>();
    WorkspaceMaterialAudioFieldAnswer fieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialAudioFieldAnswerByFieldAndReply(field, reply);
    if (fieldAnswer != null) {
        List<WorkspaceMaterialAudioFieldAnswerClip> answerClips = workspaceMaterialFieldAnswerController.listWorkspaceMaterialAudioFieldAnswerClipsByFieldAnswer(fieldAnswer);
        for (WorkspaceMaterialAudioFieldAnswerClip answerClip : answerClips) {
            result.add(new Clip(answerClip.getClipId(), answerClip.getFileName(), answerClip.getContentType()));
        }
    }
    try {
        return new ObjectMapper().writeValueAsString(result);
    } catch (IOException e) {
        throw new WorkspaceFieldIOException("Could not marshal audio audio response", e);
    }
}
Also used : WorkspaceMaterialAudioFieldAnswerClip(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip) WorkspaceMaterialAudioFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswer) ArrayList(java.util.ArrayList) WorkspaceMaterialAudioFieldAnswerClip(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

WorkspaceMaterialAudioFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswer)4 WorkspaceMaterialAudioFieldAnswerClip (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1