use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer 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.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer 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.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer in project muikku by otavanopisto.
the class WorkspaceMaterialMultiSelectFieldAnswerDAO method create.
public WorkspaceMaterialMultiSelectFieldAnswer create(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
WorkspaceMaterialMultiSelectFieldAnswer workspaceMaterialMultiSelectFieldAnswer = new WorkspaceMaterialMultiSelectFieldAnswer();
workspaceMaterialMultiSelectFieldAnswer.setField(field);
workspaceMaterialMultiSelectFieldAnswer.setReply(reply);
return persist(workspaceMaterialMultiSelectFieldAnswer);
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer 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;
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswer in project muikku by otavanopisto.
the class WorkspaceMaterialMultiSelectFieldAnswerDAO method findByQueryFieldAndReply.
public WorkspaceMaterialMultiSelectFieldAnswer findByQueryFieldAndReply(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<WorkspaceMaterialMultiSelectFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialMultiSelectFieldAnswer.class);
Root<WorkspaceMaterialMultiSelectFieldAnswer> root = criteria.from(WorkspaceMaterialMultiSelectFieldAnswer.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswer_.field), field), criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswer_.reply), reply)));
return getSingleResult(entityManager.createQuery(criteria));
}
Aggregations