Search in sources :

Example 1 with MaterialFieldMetaParsingExeption

use of fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption in project muikku by otavanopisto.

the class WorkspaceMaterialFieldChangeListener method onWorkspaceMaterialMultiSelectFieldUpdate.

// Multi-select field
public void onWorkspaceMaterialMultiSelectFieldUpdate(@Observes WorkspaceMaterialFieldUpdateEvent event) throws MaterialFieldMetaParsingExeption, WorkspaceMaterialContainsAnswersExeption {
    if (event.getMaterialField().getType().equals("application/vnd.muikku.field.multiselect")) {
        // Field JSON to metadata object
        ObjectMapper objectMapper = new ObjectMapper();
        MultiSelectFieldMeta multiSelectFieldMeta;
        try {
            multiSelectFieldMeta = objectMapper.readValue(event.getMaterialField().getContent(), MultiSelectFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse multi-select field meta", e);
        }
        QueryMultiSelectField queryField = (QueryMultiSelectField) event.getWorkspaceMaterialField().getQueryField();
        // Ensure that if there are options being removed, they haven't been used as answers
        List<WorkspaceMaterialMultiSelectFieldAnswer> deprecatedAnswers = new ArrayList<WorkspaceMaterialMultiSelectFieldAnswer>();
        List<QueryMultiSelectFieldOption> oldOptions = queryMultiSelectFieldOptionDAO.listByField(queryField);
        List<MultiSelectFieldOptionMeta> newOptions = multiSelectFieldMeta.getOptions();
        for (MultiSelectFieldOptionMeta newOption : newOptions) {
            QueryMultiSelectFieldOption correspondingOption = findMultiSelectOptionByName(oldOptions, newOption.getName());
            if (correspondingOption != null) {
                oldOptions.remove(correspondingOption);
            }
        }
        for (QueryMultiSelectFieldOption removedOption : oldOptions) {
            List<WorkspaceMaterialMultiSelectFieldAnswer> answers = workspaceMaterialMultiSelectFieldAnswerDAO.listByQueryMultiSelectFieldOption(removedOption);
            deprecatedAnswers.addAll(answers);
        }
        if (!deprecatedAnswers.isEmpty() && !event.getRemoveAnswers()) {
            throw new WorkspaceMaterialContainsAnswersExeption("Could not remove workspace material field because it contains answers");
        } else if (!deprecatedAnswers.isEmpty()) {
            for (WorkspaceMaterialMultiSelectFieldAnswer deprecatedAnswer : deprecatedAnswers) {
                deleteFieldAnswer(deprecatedAnswer);
            }
        }
    }
}
Also used : QueryMultiSelectField(fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MultiSelectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta) WorkspaceMaterialMultiSelectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer) MaterialFieldMetaParsingExeption(fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) MultiSelectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta) QueryMultiSelectFieldOption(fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption)

Example 2 with MaterialFieldMetaParsingExeption

use of fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption in project muikku by otavanopisto.

the class WorkspaceMaterialFieldChangeListener method onWorkspaceMaterialSelectFieldUpdate.

// Select field
public void onWorkspaceMaterialSelectFieldUpdate(@Observes WorkspaceMaterialFieldUpdateEvent event) throws MaterialFieldMetaParsingExeption, WorkspaceMaterialContainsAnswersExeption {
    if (event.getMaterialField().getType().equals("application/vnd.muikku.field.select")) {
        // Field JSON to metadata object
        ObjectMapper objectMapper = new ObjectMapper();
        SelectFieldMeta selectFieldMeta;
        try {
            selectFieldMeta = objectMapper.readValue(event.getMaterialField().getContent(), SelectFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
        }
        QuerySelectField queryField = (QuerySelectField) event.getWorkspaceMaterialField().getQueryField();
        // Ensure that if there are options being removed, they haven't been used as answers
        List<WorkspaceMaterialSelectFieldAnswer> deprecatedAnswers = new ArrayList<WorkspaceMaterialSelectFieldAnswer>();
        List<QuerySelectFieldOption> oldOptions = querySelectFieldOptionDAO.listByField(queryField);
        List<SelectFieldOptionMeta> newOptions = selectFieldMeta.getOptions();
        for (SelectFieldOptionMeta newOption : newOptions) {
            QuerySelectFieldOption correspondingOption = findSelectOptionByName(oldOptions, newOption.getName());
            if (correspondingOption != null) {
                oldOptions.remove(correspondingOption);
            }
        }
        for (QuerySelectFieldOption removedOption : oldOptions) {
            List<WorkspaceMaterialSelectFieldAnswer> answers = workspaceMaterialSelectFieldAnswerDAO.listByQuerySelectFieldOption(removedOption);
            deprecatedAnswers.addAll(answers);
        }
        if (!deprecatedAnswers.isEmpty() && !event.getRemoveAnswers()) {
            throw new WorkspaceMaterialContainsAnswersExeption("Could not remove workspace material field because it contains answers");
        } else if (!deprecatedAnswers.isEmpty()) {
            for (WorkspaceMaterialSelectFieldAnswer deprecatedAnswer : deprecatedAnswers) {
                deleteFieldAnswer(deprecatedAnswer);
            }
        }
    }
}
Also used : WorkspaceMaterialSelectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSelectFieldAnswer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) QuerySelectField(fi.otavanopisto.muikku.plugins.material.model.QuerySelectField) QuerySelectFieldOption(fi.otavanopisto.muikku.plugins.material.model.QuerySelectFieldOption) SelectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldMeta) MultiSelectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta) MaterialFieldMetaParsingExeption(fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) SelectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta) MultiSelectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta)

Example 3 with MaterialFieldMetaParsingExeption

use of fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption 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 4 with MaterialFieldMetaParsingExeption

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

MaterialFieldMetaParsingExeption (fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption)4 IOException (java.io.IOException)4 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)4 ArrayList (java.util.ArrayList)3 MultiSelectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta)2 MultiSelectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta)2 WorkspaceMaterialField (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)2 OrganizerFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta)1 SelectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldMeta)1 SelectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta)1 SorterFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta)1 QueryMultiSelectField (fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField)1 QueryMultiSelectFieldOption (fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption)1 QuerySelectField (fi.otavanopisto.muikku.plugins.material.model.QuerySelectField)1 QuerySelectFieldOption (fi.otavanopisto.muikku.plugins.material.model.QuerySelectFieldOption)1 WorkspaceMaterialMultiSelectFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer)1 WorkspaceMaterialOrganizerFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer)1 WorkspaceMaterialSelectFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSelectFieldAnswer)1 WorkspaceMaterialSorterFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer)1 HashMap (java.util.HashMap)1