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