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