Search in sources :

Example 1 with WorkspaceMaterialSelectFieldAnswer

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

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

the class WorkspaceMaterialSelectFieldAnswerDAO method findByQueryFieldAndReply.

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

Example 3 with WorkspaceMaterialSelectFieldAnswer

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

the class WorkspaceMaterialSelectFieldAnswerDAO method listByQuerySelectFieldOption.

public List<WorkspaceMaterialSelectFieldAnswer> listByQuerySelectFieldOption(QuerySelectFieldOption option) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialSelectFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialSelectFieldAnswer.class);
    Root<WorkspaceMaterialSelectFieldAnswer> root = criteria.from(WorkspaceMaterialSelectFieldAnswer.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(WorkspaceMaterialSelectFieldAnswer_.value), option));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialSelectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSelectFieldAnswer)

Example 4 with WorkspaceMaterialSelectFieldAnswer

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

the class WorkspaceMaterialSelectFieldAnswerDAO method create.

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

Aggregations

WorkspaceMaterialSelectFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSelectFieldAnswer)4 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 SelectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldMeta)1 SelectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta)1 QuerySelectField (fi.otavanopisto.muikku.plugins.material.model.QuerySelectField)1 QuerySelectFieldOption (fi.otavanopisto.muikku.plugins.material.model.QuerySelectFieldOption)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1