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));
}
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);
}
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);
}
}
}
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);
}
}
Aggregations