Search in sources :

Example 1 with QueryMultiSelectFieldOption

use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption in project muikku by otavanopisto.

the class QueryMultiSelectFieldController method deleteQueryMultiSelectField.

public void deleteQueryMultiSelectField(QueryMultiSelectField queryField, boolean removeAnswers) {
    queryFieldDeleteEvent.fire(new QueryFieldDeleteEvent(queryField, removeAnswers));
    for (QueryMultiSelectFieldOption option : listQueryMultiSelectFieldOptionsByField(queryField)) {
        deleteQueryMultiSelectFieldOption(option);
    }
    queryMultiSelectFieldDAO.delete(queryField);
}
Also used : QueryFieldDeleteEvent(fi.otavanopisto.muikku.plugins.material.events.QueryFieldDeleteEvent) QueryMultiSelectFieldOption(fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption)

Example 2 with QueryMultiSelectFieldOption

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

use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption in project muikku by otavanopisto.

the class QueryMultiSelectFieldOptionDAO method listByField.

public List<QueryMultiSelectFieldOption> listByField(QueryMultiSelectField field) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<QueryMultiSelectFieldOption> criteria = criteriaBuilder.createQuery(QueryMultiSelectFieldOption.class);
    Root<QueryMultiSelectFieldOption> root = criteria.from(QueryMultiSelectFieldOption.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(QueryMultiSelectFieldOption_.field), field));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) QueryMultiSelectFieldOption(fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption)

Example 4 with QueryMultiSelectFieldOption

use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption in project muikku by otavanopisto.

the class WorkspaceMultiSelectFieldIOHandler method store.

@Override
public void store(WorkspaceMaterialField field, WorkspaceMaterialReply reply, String value) throws WorkspaceFieldIOException {
    String[] postedValues = new String[0];
    WorkspaceMaterialMultiSelectFieldAnswer fieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialMultiSelectFieldAnswerByFieldAndReply(field, reply);
    if (fieldAnswer == null && StringUtils.isNotBlank(value)) {
        fieldAnswer = workspaceMaterialFieldAnswerController.createWorkspaceMaterialMultiSelectFieldAnswer(field, reply);
    }
    if (StringUtils.isNotBlank(value)) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            postedValues = objectMapper.readValue(value, String[].class);
        } catch (Exception e) {
            throw new WorkspaceFieldIOException("Could not marshal field response", e);
        }
    }
    List<QueryMultiSelectFieldOption> options = workspaceMaterialFieldAnswerController.listMultiSelectFieldOptions((QueryMultiSelectField) field.getQueryField());
    List<WorkspaceMaterialMultiSelectFieldAnswerOption> currentAnswers = workspaceMaterialFieldAnswerController.listWorkspaceMaterialMultiSelectFieldAnswerOptions(fieldAnswer);
    for (String postedValue : postedValues) {
        WorkspaceMaterialMultiSelectFieldAnswerOption answerOption = getAnswerOption(currentAnswers, postedValue);
        if (answerOption == null) {
            // From unchecked to checked
            QueryMultiSelectFieldOption fieldOption = getFieldOption(options, postedValue);
            if (fieldOption == null) {
                throw new WorkspaceFieldIOException("Multiselect lacks posted option " + postedValue);
            }
            workspaceMaterialFieldAnswerController.createWorkspaceMaterialMultiSelectFieldAnswerOption(fieldAnswer, fieldOption);
        } else {
            // From checked to checked
            currentAnswers.remove(answerOption);
        }
    }
    // From checked to unchecked
    for (WorkspaceMaterialMultiSelectFieldAnswerOption currentAnswer : currentAnswers) {
        workspaceMaterialFieldAnswerController.deleteWorkspaceMaterialMultiSelectFieldAnswerOption(currentAnswer);
    }
}
Also used : WorkspaceMaterialMultiSelectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer) WorkspaceMaterialMultiSelectFieldAnswerOption(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) QueryMultiSelectFieldOption(fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption)

Example 5 with QueryMultiSelectFieldOption

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

QueryMultiSelectFieldOption (fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption)7 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 MultiSelectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta)2 MultiSelectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta)2 QueryMultiSelectField (fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField)2 WorkspaceMaterialMultiSelectFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer)2 IOException (java.io.IOException)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 MaterialFieldMetaParsingExeption (fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption)1 QueryFieldDeleteEvent (fi.otavanopisto.muikku.plugins.material.events.QueryFieldDeleteEvent)1 QueryFieldUpdateEvent (fi.otavanopisto.muikku.plugins.material.events.QueryFieldUpdateEvent)1 WorkspaceMaterialMultiSelectFieldAnswerOption (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption)1 ArrayList (java.util.ArrayList)1