Search in sources :

Example 1 with MultiSelectFieldOptionMeta

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

the class HtmlMaterialFieldChangeListener method onHtmlMaterialMultiSelectFieldCreated.

// Multi-select field
public void onHtmlMaterialMultiSelectFieldCreated(@Observes HtmlMaterialFieldCreateEvent event) throws MaterialQueryIntegrityExeption, MaterialFieldMetaParsingExeption {
    if (event.getField().getType().equals("application/vnd.muikku.field.multiselect")) {
        ObjectMapper objectMapper = new ObjectMapper();
        MultiSelectFieldMeta multiSelectFieldMeta;
        try {
            multiSelectFieldMeta = objectMapper.readValue(event.getField().getContent(), MultiSelectFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
        }
        QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(event.getMaterial(), multiSelectFieldMeta.getName());
        if (queryField != null) {
            throw new MaterialQueryIntegrityExeption("Field with same name already exists in the database");
        }
        QueryMultiSelectField queryMultiSelectField = queryMultiSelectFieldController.createQueryMultiSelectField(event.getMaterial(), multiSelectFieldMeta.getName());
        for (MultiSelectFieldOptionMeta multiSelectFieldOptionMeta : multiSelectFieldMeta.getOptions()) {
            queryMultiSelectFieldController.createQueryMultiSelectFieldOption(queryMultiSelectField, multiSelectFieldOptionMeta.getName(), multiSelectFieldOptionMeta.getText());
        }
    }
}
Also used : QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField) QueryMultiSelectField(fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField) IOException(java.io.IOException) MultiSelectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) MultiSelectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta)

Example 2 with MultiSelectFieldOptionMeta

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

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

the class FieldElementsHandler method handleChecklistField.

@Override
public Node handleChecklistField(Document ownerDocument, String paramName, List<MultiSelectFieldOptionMeta> options, String helpOf, String hintOf) {
    MultiSelectFieldMeta checklistFieldMeta = fieldTranslator.translateChecklistField(paramName, options);
    List<Element> elements = new ArrayList<>();
    for (MultiSelectFieldOptionMeta option : checklistFieldMeta.getOptions()) {
        Element inputElement = ownerDocument.createElement("input");
        inputElement.setAttribute("type", "checkbox");
        inputElement.setAttribute("value", option.getName());
        inputElement.setAttribute("name", checklistFieldMeta.getName());
        // TODO: Label For ...
        Element labelElement = ownerDocument.createElement("label");
        labelElement.setTextContent(option.getText());
        elements.add(inputElement);
        elements.add(labelElement);
    }
    return wrapWithObjectElement(ownerDocument, elements, checklistFieldMeta);
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MultiSelectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta) MultiSelectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta)

Example 4 with MultiSelectFieldOptionMeta

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

the class DeusNexContentParser method handleCheckListField.

private Node handleCheckListField(Document ownerDocument, Element fieldElement, String paramName) throws XPathExpressionException {
    List<MultiSelectFieldOptionMeta> options = new ArrayList<>();
    List<Element> optionElements = DeusNexXmlUtils.getElementsByXPath(fieldElement, "option");
    for (Element optionElement : optionElements) {
        String optionName = optionElement.getAttribute("name");
        String optionText = optionElement.getTextContent();
        // Points to correct conversion
        String pointsStr = optionElement.getAttribute("points");
        Boolean correct = StringUtils.isNotBlank(pointsStr) ? NumberUtils.createDouble(pointsStr) > 0 : Boolean.FALSE;
        options.add(new MultiSelectFieldOptionMeta(optionName, optionText, correct));
    }
    if (fieldElementHandler != null) {
        return fieldElementHandler.handleChecklistField(ownerDocument, paramName, options, helpOf(fieldElement), hintOf(fieldElement));
    }
    return null;
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MultiSelectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta)

Example 5 with MultiSelectFieldOptionMeta

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

the class QueryMultiSelectFieldController method updateQueryMultiSelectField.

public QueryMultiSelectField updateQueryMultiSelectField(Material material, MaterialField field, boolean removeAnswers) throws MaterialFieldMetaParsingExeption {
    // Field JSON to metadata object
    ObjectMapper objectMapper = new ObjectMapper();
    MultiSelectFieldMeta multiSelectFieldMeta;
    try {
        multiSelectFieldMeta = objectMapper.readValue(field.getContent(), MultiSelectFieldMeta.class);
    } catch (IOException e) {
        throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
    }
    QueryMultiSelectField queryField = queryMultiSelectFieldDAO.findByMaterialAndName(material, multiSelectFieldMeta.getName());
    // -> fi.otavanopisto.muikku.plugins.workspace.QueryFieldChangeListener
    queryFieldUpdateEvent.fire(new QueryFieldUpdateEvent(queryField, field, removeAnswers));
    List<QueryMultiSelectFieldOption> oldOptions = queryMultiSelectFieldOptionDAO.listByField(queryField);
    List<MultiSelectFieldOptionMeta> newOptions = multiSelectFieldMeta.getOptions();
    for (MultiSelectFieldOptionMeta newOption : newOptions) {
        QueryMultiSelectFieldOption correspondingOption = findOptionByName(oldOptions, newOption.getName());
        if (correspondingOption == null) {
            // New options
            createQueryMultiSelectFieldOption(queryField, newOption.getName(), newOption.getText());
        } else {
            // Modified options
            if (!StringUtils.equals(correspondingOption.getText(), newOption.getText())) {
                updateQueryMultiSelectFieldOptionText(correspondingOption, newOption.getText());
            }
            oldOptions.remove(correspondingOption);
        }
    }
    // Removed options
    for (QueryMultiSelectFieldOption removedOption : oldOptions) {
        deleteQueryMultiSelectFieldOption(removedOption);
    }
    return queryField;
}
Also used : QueryMultiSelectField(fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField) IOException(java.io.IOException) MultiSelectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) QueryFieldUpdateEvent(fi.otavanopisto.muikku.plugins.material.events.QueryFieldUpdateEvent) MultiSelectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta) QueryMultiSelectFieldOption(fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption)

Aggregations

MultiSelectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta)5 MultiSelectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta)4 QueryMultiSelectField (fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 QueryMultiSelectFieldOption (fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption)2 Element (org.w3c.dom.Element)2 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 WorkspaceMaterialMultiSelectFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer)1