Search in sources :

Example 1 with WorkspaceMaterialMultiSelectFieldAnswer

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

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer 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 3 with WorkspaceMaterialMultiSelectFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer in project muikku by otavanopisto.

the class WorkspaceMaterialMultiSelectFieldAnswerDAO method create.

public WorkspaceMaterialMultiSelectFieldAnswer create(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
    WorkspaceMaterialMultiSelectFieldAnswer workspaceMaterialMultiSelectFieldAnswer = new WorkspaceMaterialMultiSelectFieldAnswer();
    workspaceMaterialMultiSelectFieldAnswer.setField(field);
    workspaceMaterialMultiSelectFieldAnswer.setReply(reply);
    return persist(workspaceMaterialMultiSelectFieldAnswer);
}
Also used : WorkspaceMaterialMultiSelectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer)

Example 4 with WorkspaceMaterialMultiSelectFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer in project muikku by otavanopisto.

the class WorkspaceMultiSelectFieldIOHandler method retrieve.

@Override
public String retrieve(WorkspaceMaterialField field, WorkspaceMaterialReply reply) throws WorkspaceFieldIOException {
    String answer = "[]";
    WorkspaceMaterialMultiSelectFieldAnswer fieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialMultiSelectFieldAnswerByFieldAndReply(field, reply);
    if (fieldAnswer != null) {
        List<WorkspaceMaterialMultiSelectFieldAnswerOption> options = workspaceMaterialFieldAnswerController.listWorkspaceMaterialMultiSelectFieldAnswerOptions(fieldAnswer);
        int i = 0;
        String[] values = new String[options.size()];
        for (WorkspaceMaterialMultiSelectFieldAnswerOption option : options) {
            values[i++] = option.getOption().getName();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            answer = objectMapper.writeValueAsString(values);
        } catch (Exception e) {
            throw new WorkspaceFieldIOException("Could not marshal field response", e);
        }
    }
    return answer;
}
Also used : WorkspaceMaterialMultiSelectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer) WorkspaceMaterialMultiSelectFieldAnswerOption(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 5 with WorkspaceMaterialMultiSelectFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer in project muikku by otavanopisto.

the class WorkspaceMaterialMultiSelectFieldAnswerDAO method findByQueryFieldAndReply.

public WorkspaceMaterialMultiSelectFieldAnswer findByQueryFieldAndReply(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialMultiSelectFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialMultiSelectFieldAnswer.class);
    Root<WorkspaceMaterialMultiSelectFieldAnswer> root = criteria.from(WorkspaceMaterialMultiSelectFieldAnswer.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswer_.field), field), criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswer_.reply), reply)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialMultiSelectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer)

Aggregations

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