Search in sources :

Example 1 with OrganizerFieldMeta

use of fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta in project muikku by otavanopisto.

the class HtmlMaterialFieldChangeListener method onHtmlMaterialOrganizerFieldCreated.

// Organizer field
public void onHtmlMaterialOrganizerFieldCreated(@Observes HtmlMaterialFieldCreateEvent event) throws MaterialQueryIntegrityExeption, MaterialFieldMetaParsingExeption {
    if (event.getField().getType().equals("application/vnd.muikku.field.organizer")) {
        ObjectMapper objectMapper = new ObjectMapper();
        OrganizerFieldMeta organizerFieldMeta;
        try {
            organizerFieldMeta = objectMapper.readValue(event.getField().getContent(), OrganizerFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse organizer field meta", e);
        }
        QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(event.getMaterial(), organizerFieldMeta.getName());
        if (queryField == null) {
            queryOrganizerFieldController.createQueryOrganizerField(event.getMaterial(), organizerFieldMeta.getName());
        } else {
            throw new MaterialQueryIntegrityExeption("Field with same name already exists in the database");
        }
    }
}
Also used : QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField) OrganizerFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 2 with OrganizerFieldMeta

use of fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta 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 OrganizerFieldMeta

use of fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta in project muikku by otavanopisto.

the class QueryOrganizerFieldController method updateQueryOrganizerField.

public QueryOrganizerField updateQueryOrganizerField(Material material, MaterialField field, boolean removeAnswers) throws MaterialFieldMetaParsingExeption {
    ObjectMapper objectMapper = new ObjectMapper();
    OrganizerFieldMeta organizerFieldMeta;
    try {
        organizerFieldMeta = objectMapper.readValue(field.getContent(), OrganizerFieldMeta.class);
    } catch (IOException e) {
        throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
    }
    QueryOrganizerField queryField = queryOrganizerFieldDAO.findByMaterialAndName(material, organizerFieldMeta.getName());
    // -> fi.otavanopisto.muikku.plugins.workspace.QueryFieldChangeListener
    queryFieldUpdateEvent.fire(new QueryFieldUpdateEvent(queryField, field, removeAnswers));
    return queryField;
}
Also used : OrganizerFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta) IOException(java.io.IOException) QueryOrganizerField(fi.otavanopisto.muikku.plugins.material.model.QueryOrganizerField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) QueryFieldUpdateEvent(fi.otavanopisto.muikku.plugins.material.events.QueryFieldUpdateEvent)

Aggregations

OrganizerFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta)3 IOException (java.io.IOException)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 MaterialFieldMetaParsingExeption (fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption)1 QueryFieldUpdateEvent (fi.otavanopisto.muikku.plugins.material.events.QueryFieldUpdateEvent)1 QueryField (fi.otavanopisto.muikku.plugins.material.model.QueryField)1 QueryOrganizerField (fi.otavanopisto.muikku.plugins.material.model.QueryOrganizerField)1 WorkspaceMaterialField (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)1 WorkspaceMaterialOrganizerFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1