use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField in project muikku by otavanopisto.
the class HtmlMaterialFieldChangeListener method onHtmlMaterialMultiSelectFieldCreated.
// Multi-select field
public void onHtmlMaterialMultiSelectFieldCreated(@Observes HtmlMaterialFieldCreateEvent event) throws MaterialQueryIntegrityExeption, MaterialFieldMetaParsingExeption {
if (event.getField().getType().equals("application/vnd.muikku.field.multiselect")) {
ObjectMapper objectMapper = new ObjectMapper();
MultiSelectFieldMeta multiSelectFieldMeta;
try {
multiSelectFieldMeta = objectMapper.readValue(event.getField().getContent(), MultiSelectFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
}
QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(event.getMaterial(), multiSelectFieldMeta.getName());
if (queryField != null) {
throw new MaterialQueryIntegrityExeption("Field with same name already exists in the database");
}
QueryMultiSelectField queryMultiSelectField = queryMultiSelectFieldController.createQueryMultiSelectField(event.getMaterial(), multiSelectFieldMeta.getName());
for (MultiSelectFieldOptionMeta multiSelectFieldOptionMeta : multiSelectFieldMeta.getOptions()) {
queryMultiSelectFieldController.createQueryMultiSelectFieldOption(queryMultiSelectField, multiSelectFieldOptionMeta.getName(), multiSelectFieldOptionMeta.getText());
}
}
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField 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);
}
}
}
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField 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;
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField in project muikku by otavanopisto.
the class QueryMultiSelectFieldDAO method findByMaterialAndName.
public QueryMultiSelectField findByMaterialAndName(Material material, String name) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<QueryMultiSelectField> criteria = criteriaBuilder.createQuery(QueryMultiSelectField.class);
Root<QueryMultiSelectField> root = criteria.from(QueryMultiSelectField.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(QueryMultiSelectField_.material), material), criteriaBuilder.equal(root.get(QueryMultiSelectField_.name), name)));
return getSingleResult(entityManager.createQuery(criteria));
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectField in project muikku by otavanopisto.
the class QueryMultiSelectFieldDAO method create.
public QueryMultiSelectField create(Material material, String name) {
QueryMultiSelectField queryMultiSelectField = new QueryMultiSelectField();
queryMultiSelectField.setMaterial(material);
queryMultiSelectField.setName(name);
return persist(queryMultiSelectField);
}
Aggregations