use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer 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);
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer in project muikku by otavanopisto.
the class WorkspaceMaterialFileFieldAnswerDAO method findByQueryFieldAndReply.
public WorkspaceMaterialFileFieldAnswer findByQueryFieldAndReply(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<WorkspaceMaterialFileFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialFileFieldAnswer.class);
Root<WorkspaceMaterialFileFieldAnswer> root = criteria.from(WorkspaceMaterialFileFieldAnswer.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceMaterialFileFieldAnswer_.field), field), criteriaBuilder.equal(root.get(WorkspaceMaterialFileFieldAnswer_.reply), reply)));
return getSingleResult(entityManager.createQuery(criteria));
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer in project muikku by otavanopisto.
the class WorkspaceMaterialFileFieldAnswerDAO method create.
public WorkspaceMaterialFileFieldAnswer create(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
WorkspaceMaterialFileFieldAnswer workspaceMaterialFileFieldAnswer = new WorkspaceMaterialFileFieldAnswer();
workspaceMaterialFileFieldAnswer.setField(field);
workspaceMaterialFileFieldAnswer.setReply(reply);
return persist(workspaceMaterialFileFieldAnswer);
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer in project muikku by otavanopisto.
the class WorkspaceFileFieldIOHandler method store.
@Override
public void store(WorkspaceMaterialField field, WorkspaceMaterialReply reply, String value) throws WorkspaceFieldIOException {
File[] files = null;
try {
files = new ObjectMapper().readValue(value, File[].class);
} catch (IOException e) {
throw new WorkspaceFieldIOException("Could not unmarshal field reply", e);
}
WorkspaceMaterialFileFieldAnswer fieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerByFieldAndReply(field, reply);
if (fieldAnswer == null) {
fieldAnswer = workspaceMaterialFieldAnswerController.createWorkspaceMaterialFileFieldAnswer(field, reply);
}
List<WorkspaceMaterialFileFieldAnswerFile> currentFiles = workspaceMaterialFieldAnswerController.listWorkspaceMaterialFileFieldAnswerFilesByFieldAnswer(fieldAnswer);
Set<String> currentFileIds = new HashSet<String>();
for (WorkspaceMaterialFileFieldAnswerFile currentFile : currentFiles) {
currentFileIds.add(currentFile.getFileId());
}
for (File file : files) {
try {
String fileId = file.getFileId();
if (StringUtils.isBlank(fileId)) {
throw new WorkspaceFieldIOException("Blank fileId");
}
if (currentFileIds.contains(fileId)) {
// Existing file
currentFileIds.remove(fileId);
} else {
// New file
byte[] fileData = TempFileUtils.getTempFileData(fileId);
if (fileData == null) {
throw new WorkspaceFieldIOException("Temp file does not exist");
}
logger.info(String.format("Creating new file answer %s (%s)", fileId, file.getName()));
workspaceMaterialFieldAnswerController.createWorkspaceMaterialFileFieldAnswerFile(fieldAnswer, fileData, file.getContentType(), fileId, file.getName());
}
} catch (IOException e) {
throw new WorkspaceFieldIOException("Failed to store file data", e);
}
}
// Removed files
for (String removedId : currentFileIds) {
WorkspaceMaterialFileFieldAnswerFile fieldAnswerFile = workspaceMaterialFieldAnswerController.findWorkspaceMaterialFileFieldAnswerFileByFileId(removedId);
if (fieldAnswerFile != null) {
logger.info(String.format("Removing existing file answer %s (%s)", removedId, fieldAnswerFile.getFileName()));
workspaceMaterialFieldAnswerController.deleteWorkspaceMaterialFileFieldAnswerFile(fieldAnswerFile);
}
}
}
Aggregations