Search in sources :

Example 1 with SorterFieldMeta

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

the class HtmlMaterialFieldChangeListener method onHtmlMaterialSorterFieldCreated.

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

Example 2 with SorterFieldMeta

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

the class QuerySorterFieldController method updateQuerySorterField.

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

Example 3 with SorterFieldMeta

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

Aggregations

SorterFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta)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 QuerySorterField (fi.otavanopisto.muikku.plugins.material.model.QuerySorterField)1 WorkspaceMaterialField (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)1 WorkspaceMaterialSorterFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer)1 ArrayList (java.util.ArrayList)1