Search in sources :

Example 1 with WorkspaceMaterialSorterFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer in project muikku by otavanopisto.

the class WorkspaceMaterialSorterFieldAnswerDAO method create.

public WorkspaceMaterialSorterFieldAnswer create(WorkspaceMaterialField field, WorkspaceMaterialReply reply, String value) {
    WorkspaceMaterialSorterFieldAnswer workspaceMaterialSorterFieldAnswer = new WorkspaceMaterialSorterFieldAnswer();
    workspaceMaterialSorterFieldAnswer.setField(field);
    workspaceMaterialSorterFieldAnswer.setReply(reply);
    workspaceMaterialSorterFieldAnswer.setValue(value);
    return persist(workspaceMaterialSorterFieldAnswer);
}
Also used : WorkspaceMaterialSorterFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer)

Example 2 with WorkspaceMaterialSorterFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer in project muikku by otavanopisto.

the class WorkspaceMaterialSorterFieldAnswerDAO method findByFieldAndReply.

public WorkspaceMaterialSorterFieldAnswer findByFieldAndReply(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialSorterFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialSorterFieldAnswer.class);
    Root<WorkspaceMaterialSorterFieldAnswer> root = criteria.from(WorkspaceMaterialSorterFieldAnswer.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceMaterialSorterFieldAnswer_.field), field), criteriaBuilder.equal(root.get(WorkspaceMaterialSorterFieldAnswer_.reply), reply)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialSorterFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer)

Example 3 with WorkspaceMaterialSorterFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer in project muikku by otavanopisto.

the class WorkspaceMaterialFieldChangeListener method onWorkspaceMaterialSorterFieldUpdate.

// Sorter field
public void onWorkspaceMaterialSorterFieldUpdate(@Observes WorkspaceMaterialFieldUpdateEvent event) throws MaterialFieldMetaParsingExeption, WorkspaceMaterialContainsAnswersExeption {
    if (event.getMaterialField().getType().equals("application/vnd.muikku.field.sorter")) {
        ObjectMapper objectMapper = new ObjectMapper();
        SorterFieldMeta sorterFieldMeta;
        try {
            sorterFieldMeta = objectMapper.readValue(event.getMaterialField().getContent(), SorterFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse sorter field meta", e);
        }
        List<WorkspaceMaterialField> fields = workspaceMaterialFieldDAO.listByQueryField(event.getWorkspaceMaterialField().getQueryField());
        for (WorkspaceMaterialField field : fields) {
            List<WorkspaceMaterialSorterFieldAnswer> answers = workspaceMaterialSorterFieldAnswerDAO.listByWorkspaceMaterialField(field);
            for (WorkspaceMaterialSorterFieldAnswer answer : answers) {
                try {
                    boolean answerModified = false;
                    List<String> answerItems = objectMapper.readValue(answer.getValue(), new TypeReference<ArrayList<String>>() {
                    });
                    for (int i = answerItems.size() - 1; i >= 0; i--) {
                        String answerItem = answerItems.get(i);
                        if (!sorterFieldMeta.hasItemWithId(answerItem)) {
                            if (!event.getRemoveAnswers()) {
                                throw new WorkspaceMaterialContainsAnswersExeption("Could not update sorter field because it contains answers");
                            } else {
                                answerItems.remove(answerItem);
                                answerModified = true;
                            }
                        }
                    }
                    if (answerModified) {
                        workspaceMaterialSorterFieldAnswerDAO.updateValue(answer, objectMapper.writeValueAsString(answerItems));
                    }
                } catch (IOException e) {
                    throw new MaterialFieldMetaParsingExeption("Could not parse sorter field answer meta", e);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) WorkspaceMaterialSorterFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer) SorterFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta) MaterialFieldMetaParsingExeption(fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 4 with WorkspaceMaterialSorterFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer in project muikku by otavanopisto.

the class WorkspaceMaterialSorterFieldAnswerDAO method listByWorkspaceMaterialField.

public List<WorkspaceMaterialSorterFieldAnswer> listByWorkspaceMaterialField(WorkspaceMaterialField field) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialSorterFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialSorterFieldAnswer.class);
    Root<WorkspaceMaterialSorterFieldAnswer> root = criteria.from(WorkspaceMaterialSorterFieldAnswer.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(WorkspaceMaterialOrganizerFieldAnswer_.field), field));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialSorterFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer)

Aggregations

WorkspaceMaterialSorterFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer)4 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 MaterialFieldMetaParsingExeption (fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption)1 SorterFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta)1 WorkspaceMaterialField (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1