Search in sources :

Example 6 with WorkspaceMaterialFileFieldAnswerFile

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);
        }
    }
}
Also used : WorkspaceMaterialFileFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile) IOException(java.io.IOException) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) HashSet(java.util.HashSet)

Example 7 with WorkspaceMaterialFileFieldAnswerFile

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);
}
Also used : WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile)

Aggregations

WorkspaceMaterialFileFieldAnswerFile (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile)7 IOException (java.io.IOException)3 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)2 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)2 WorkspaceMaterialFileFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer)2 WorkspaceRootFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)2 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)2 HashSet (java.util.HashSet)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 WorkspaceFieldIOException (fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1