Search in sources :

Example 1 with QuerySelectField

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

the class QuerySelectFieldController method updateQuerySelectField.

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

Example 2 with QuerySelectField

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

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

the class QuerySelectFieldDAO method findByMaterialAndName.

public QuerySelectField findByMaterialAndName(Material material, String name) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<QuerySelectField> criteria = criteriaBuilder.createQuery(QuerySelectField.class);
    Root<QuerySelectField> root = criteria.from(QuerySelectField.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(QuerySelectField_.material), material), criteriaBuilder.equal(root.get(QuerySelectField_.name), name)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) QuerySelectField(fi.otavanopisto.muikku.plugins.material.model.QuerySelectField)

Example 4 with QuerySelectField

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

the class QuerySelectFieldDAO method create.

public QuerySelectField create(Material material, String name) {
    QuerySelectField querySelectField = new QuerySelectField();
    querySelectField.setMaterial(material);
    querySelectField.setName(name);
    return persist(querySelectField);
}
Also used : QuerySelectField(fi.otavanopisto.muikku.plugins.material.model.QuerySelectField)

Example 5 with QuerySelectField

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

the class HtmlMaterialFieldChangeListener method onHtmlMaterialSelectFieldCreated.

// Select field
public void onHtmlMaterialSelectFieldCreated(@Observes HtmlMaterialFieldCreateEvent event) throws MaterialQueryIntegrityExeption, MaterialFieldMetaParsingExeption {
    if (event.getField().getType().equals("application/vnd.muikku.field.select")) {
        ObjectMapper objectMapper = new ObjectMapper();
        SelectFieldMeta selectFieldMeta;
        try {
            selectFieldMeta = objectMapper.readValue(event.getField().getContent(), SelectFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
        }
        QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(event.getMaterial(), selectFieldMeta.getName());
        if (queryField != null) {
            throw new MaterialQueryIntegrityExeption("Field with same name already exists in the database");
        }
        QuerySelectField querySelectField = querySelectFieldController.createQuerySelectField(event.getMaterial(), selectFieldMeta.getName());
        for (SelectFieldOptionMeta selectFieldOptionMeta : selectFieldMeta.getOptions()) {
            querySelectFieldController.createQuerySelectFieldOption(querySelectField, selectFieldOptionMeta.getName(), selectFieldOptionMeta.getText());
        }
    }
}
Also used : SelectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldMeta) MultiSelectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta) QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField) IOException(java.io.IOException) QuerySelectField(fi.otavanopisto.muikku.plugins.material.model.QuerySelectField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) SelectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta) MultiSelectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta)

Aggregations

QuerySelectField (fi.otavanopisto.muikku.plugins.material.model.QuerySelectField)5 SelectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldMeta)3 SelectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta)3 IOException (java.io.IOException)3 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 QuerySelectFieldOption (fi.otavanopisto.muikku.plugins.material.model.QuerySelectFieldOption)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 WorkspaceMaterialSelectFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSelectFieldAnswer)1 ArrayList (java.util.ArrayList)1 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1