Search in sources :

Example 1 with WorkspaceMaterialFileFieldAnswer

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

Example 2 with WorkspaceMaterialFileFieldAnswer

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));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialFileFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer)

Example 3 with WorkspaceMaterialFileFieldAnswer

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

Example 4 with 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);
        }
    }
}
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)

Aggregations

WorkspaceMaterialFileFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswer)4 WorkspaceMaterialFileFieldAnswerFile (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile)2 IOException (java.io.IOException)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1