use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile 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);
}
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile in project muikku by otavanopisto.
the class WorkspaceMaterialFileFieldAnswerFileDAO method create.
public WorkspaceMaterialFileFieldAnswerFile create(WorkspaceMaterialFileFieldAnswer fieldAnswer, byte[] content, String contentType, String fileId, String fileName) {
WorkspaceMaterialFileFieldAnswerFile workspaceMaterialFileFieldAnswerFile = new WorkspaceMaterialFileFieldAnswerFile();
workspaceMaterialFileFieldAnswerFile.setFieldAnswer(fieldAnswer);
workspaceMaterialFileFieldAnswerFile.setContent(content);
workspaceMaterialFileFieldAnswerFile.setContentType(contentType);
workspaceMaterialFileFieldAnswerFile.setFileId(fileId);
workspaceMaterialFileFieldAnswerFile.setFileName(fileName);
return persist(workspaceMaterialFileFieldAnswerFile);
}
Aggregations