Search in sources :

Example 1 with WorkspaceMaterialOrganizerFieldAnswer

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

the class WorkspaceMaterialOrganizerFieldAnswerDAO method listByWorkspaceMaterialField.

public List<WorkspaceMaterialOrganizerFieldAnswer> listByWorkspaceMaterialField(WorkspaceMaterialField field) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialOrganizerFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialOrganizerFieldAnswer.class);
    Root<WorkspaceMaterialOrganizerFieldAnswer> root = criteria.from(WorkspaceMaterialOrganizerFieldAnswer.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) WorkspaceMaterialOrganizerFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer)

Example 2 with WorkspaceMaterialOrganizerFieldAnswer

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

the class WorkspaceMaterialFieldChangeListener method onWorkspaceMaterialOrganizerFieldUpdate.

// Create
// Update
// Organizer field
public void onWorkspaceMaterialOrganizerFieldUpdate(@Observes WorkspaceMaterialFieldUpdateEvent event) throws MaterialFieldMetaParsingExeption, WorkspaceMaterialContainsAnswersExeption {
    if (event.getMaterialField().getType().equals("application/vnd.muikku.field.organizer")) {
        ObjectMapper objectMapper = new ObjectMapper();
        OrganizerFieldMeta organizerFieldMeta;
        try {
            organizerFieldMeta = objectMapper.readValue(event.getMaterialField().getContent(), OrganizerFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse organizer field meta", e);
        }
        List<WorkspaceMaterialField> fields = workspaceMaterialFieldDAO.listByQueryField(event.getWorkspaceMaterialField().getQueryField());
        for (WorkspaceMaterialField field : fields) {
            List<WorkspaceMaterialOrganizerFieldAnswer> answers = workspaceMaterialOrganizerFieldAnswerDAO.listByWorkspaceMaterialField(field);
            for (WorkspaceMaterialOrganizerFieldAnswer answer : answers) {
                try {
                    boolean answerModified = false;
                    HashMap<String, Set<String>> answerObject = objectMapper.readValue(answer.getValue(), new TypeReference<HashMap<String, Set<String>>>() {
                    });
                    Set<String> terms = new HashSet<String>();
                    Set<String> categories = answerObject.keySet();
                    for (String category : categories) {
                        terms.addAll(answerObject.get(category));
                        if (!organizerFieldMeta.hasCategoryWithId(category)) {
                            if (!event.getRemoveAnswers()) {
                                throw new WorkspaceMaterialContainsAnswersExeption("Could not update organizer field because it contains answers");
                            } else {
                                answerObject.remove(category);
                                answerModified = true;
                            }
                        }
                    }
                    for (String term : terms) {
                        if (!organizerFieldMeta.hasTermWithId(term)) {
                            if (!event.getRemoveAnswers()) {
                                throw new WorkspaceMaterialContainsAnswersExeption("Could not update organizer field because it contains answers");
                            } else {
                                Iterator<String> categoryIterator = answerObject.keySet().iterator();
                                while (categoryIterator.hasNext()) {
                                    Set<String> categoryTerms = answerObject.get(categoryIterator.next());
                                    if (categoryTerms.contains(term)) {
                                        categoryTerms.remove(term);
                                        answerModified = true;
                                    }
                                }
                            }
                        }
                    }
                    if (answerModified) {
                        workspaceMaterialOrganizerFieldAnswerDAO.updateValue(answer, objectMapper.writeValueAsString(answerObject));
                    }
                } catch (IOException e) {
                    throw new MaterialFieldMetaParsingExeption("Could not parse organizer field answer meta", e);
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) OrganizerFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta) WorkspaceMaterialOrganizerFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer) IOException(java.io.IOException) MaterialFieldMetaParsingExeption(fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) HashSet(java.util.HashSet)

Example 3 with WorkspaceMaterialOrganizerFieldAnswer

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

the class WorkspaceMaterialOrganizerFieldAnswerDAO method create.

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

Example 4 with WorkspaceMaterialOrganizerFieldAnswer

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

the class WorkspaceMaterialOrganizerFieldAnswerDAO method findByFieldAndReply.

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

Aggregations

WorkspaceMaterialOrganizerFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer)4 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 MaterialFieldMetaParsingExeption (fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption)1 OrganizerFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta)1 WorkspaceMaterialField (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1