Search in sources :

Example 1 with WorkspaceMaterialMultiSelectFieldAnswerOption

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

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

the class WorkspaceMaterialMultiSelectFieldAnswerOptionDAO method listByFieldAnswer.

public List<WorkspaceMaterialMultiSelectFieldAnswerOption> listByFieldAnswer(WorkspaceMaterialMultiSelectFieldAnswer fieldAnswer) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialMultiSelectFieldAnswerOption> criteria = criteriaBuilder.createQuery(WorkspaceMaterialMultiSelectFieldAnswerOption.class);
    Root<WorkspaceMaterialMultiSelectFieldAnswerOption> root = criteria.from(WorkspaceMaterialMultiSelectFieldAnswerOption.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswerOption_.fieldAnswer), fieldAnswer));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialMultiSelectFieldAnswerOption(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption)

Example 3 with WorkspaceMaterialMultiSelectFieldAnswerOption

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

the class WorkspaceMaterialMultiSelectFieldAnswerOptionDAO method findByFieldAnswerAndOption.

public WorkspaceMaterialMultiSelectFieldAnswerOption findByFieldAnswerAndOption(WorkspaceMaterialMultiSelectFieldAnswer fieldAnswer, QueryMultiSelectFieldOption option) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialMultiSelectFieldAnswerOption> criteria = criteriaBuilder.createQuery(WorkspaceMaterialMultiSelectFieldAnswerOption.class);
    Root<WorkspaceMaterialMultiSelectFieldAnswerOption> root = criteria.from(WorkspaceMaterialMultiSelectFieldAnswerOption.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswerOption_.fieldAnswer), fieldAnswer), criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswerOption_.option), option)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialMultiSelectFieldAnswerOption(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption)

Example 4 with WorkspaceMaterialMultiSelectFieldAnswerOption

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

the class WorkspaceMaterialMultiSelectFieldAnswerOptionDAO method create.

public WorkspaceMaterialMultiSelectFieldAnswerOption create(WorkspaceMaterialMultiSelectFieldAnswer fieldAnswer, QueryMultiSelectFieldOption option) {
    WorkspaceMaterialMultiSelectFieldAnswerOption workspaceMaterialMultiSelectFieldAnswerOption = new WorkspaceMaterialMultiSelectFieldAnswerOption();
    workspaceMaterialMultiSelectFieldAnswerOption.setFieldAnswer(fieldAnswer);
    workspaceMaterialMultiSelectFieldAnswerOption.setOption(option);
    return persist(workspaceMaterialMultiSelectFieldAnswerOption);
}
Also used : WorkspaceMaterialMultiSelectFieldAnswerOption(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption)

Example 5 with WorkspaceMaterialMultiSelectFieldAnswerOption

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

Aggregations

WorkspaceMaterialMultiSelectFieldAnswerOption (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption)6 WorkspaceMaterialMultiSelectFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer)3 EntityManager (javax.persistence.EntityManager)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 QueryMultiSelectFieldOption (fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption)1