use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption in project muikku by otavanopisto.
the class QueryMultiSelectFieldController method deleteQueryMultiSelectField.
public void deleteQueryMultiSelectField(QueryMultiSelectField queryField, boolean removeAnswers) {
queryFieldDeleteEvent.fire(new QueryFieldDeleteEvent(queryField, removeAnswers));
for (QueryMultiSelectFieldOption option : listQueryMultiSelectFieldOptionsByField(queryField)) {
deleteQueryMultiSelectFieldOption(option);
}
queryMultiSelectFieldDAO.delete(queryField);
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption 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.QueryMultiSelectFieldOption in project muikku by otavanopisto.
the class QueryMultiSelectFieldOptionDAO method listByField.
public List<QueryMultiSelectFieldOption> listByField(QueryMultiSelectField field) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<QueryMultiSelectFieldOption> criteria = criteriaBuilder.createQuery(QueryMultiSelectFieldOption.class);
Root<QueryMultiSelectFieldOption> root = criteria.from(QueryMultiSelectFieldOption.class);
criteria.select(root);
criteria.where(criteriaBuilder.equal(root.get(QueryMultiSelectFieldOption_.field), field));
return entityManager.createQuery(criteria).getResultList();
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption 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);
}
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryMultiSelectFieldOption 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;
}
Aggregations