use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption 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.WorkspaceMaterialMultiSelectFieldAnswerOption in project muikku by otavanopisto.
the class WorkspaceMaterialMultiSelectFieldAnswerOptionDAO method listByFieldAnswer.
public List<WorkspaceMaterialMultiSelectFieldAnswerOption> listByFieldAnswer(WorkspaceMaterialMultiSelectFieldAnswer fieldAnswer) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<WorkspaceMaterialMultiSelectFieldAnswerOption> criteria = criteriaBuilder.createQuery(WorkspaceMaterialMultiSelectFieldAnswerOption.class);
Root<WorkspaceMaterialMultiSelectFieldAnswerOption> root = criteria.from(WorkspaceMaterialMultiSelectFieldAnswerOption.class);
criteria.select(root);
criteria.where(criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswerOption_.fieldAnswer), fieldAnswer));
return entityManager.createQuery(criteria).getResultList();
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption in project muikku by otavanopisto.
the class WorkspaceMaterialMultiSelectFieldAnswerOptionDAO method findByFieldAnswerAndOption.
public WorkspaceMaterialMultiSelectFieldAnswerOption findByFieldAnswerAndOption(WorkspaceMaterialMultiSelectFieldAnswer fieldAnswer, QueryMultiSelectFieldOption option) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<WorkspaceMaterialMultiSelectFieldAnswerOption> criteria = criteriaBuilder.createQuery(WorkspaceMaterialMultiSelectFieldAnswerOption.class);
Root<WorkspaceMaterialMultiSelectFieldAnswerOption> root = criteria.from(WorkspaceMaterialMultiSelectFieldAnswerOption.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswerOption_.fieldAnswer), fieldAnswer), criteriaBuilder.equal(root.get(WorkspaceMaterialMultiSelectFieldAnswerOption_.option), option)));
return getSingleResult(entityManager.createQuery(criteria));
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption in project muikku by otavanopisto.
the class WorkspaceMaterialMultiSelectFieldAnswerOptionDAO method create.
public WorkspaceMaterialMultiSelectFieldAnswerOption create(WorkspaceMaterialMultiSelectFieldAnswer fieldAnswer, QueryMultiSelectFieldOption option) {
WorkspaceMaterialMultiSelectFieldAnswerOption workspaceMaterialMultiSelectFieldAnswerOption = new WorkspaceMaterialMultiSelectFieldAnswerOption();
workspaceMaterialMultiSelectFieldAnswerOption.setFieldAnswer(fieldAnswer);
workspaceMaterialMultiSelectFieldAnswerOption.setOption(option);
return persist(workspaceMaterialMultiSelectFieldAnswerOption);
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialMultiSelectFieldAnswerOption 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;
}
Aggregations