use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer in project muikku by otavanopisto.
the class WorkspaceMaterialOrganizerFieldAnswerDAO method listByWorkspaceMaterialField.
public List<WorkspaceMaterialOrganizerFieldAnswer> listByWorkspaceMaterialField(WorkspaceMaterialField field) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<WorkspaceMaterialOrganizerFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialOrganizerFieldAnswer.class);
Root<WorkspaceMaterialOrganizerFieldAnswer> root = criteria.from(WorkspaceMaterialOrganizerFieldAnswer.class);
criteria.select(root);
criteria.where(criteriaBuilder.equal(root.get(WorkspaceMaterialOrganizerFieldAnswer_.field), field));
return entityManager.createQuery(criteria).getResultList();
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer in project muikku by otavanopisto.
the class WorkspaceMaterialFieldChangeListener method onWorkspaceMaterialOrganizerFieldUpdate.
// Create
// Update
// Organizer field
public void onWorkspaceMaterialOrganizerFieldUpdate(@Observes WorkspaceMaterialFieldUpdateEvent event) throws MaterialFieldMetaParsingExeption, WorkspaceMaterialContainsAnswersExeption {
if (event.getMaterialField().getType().equals("application/vnd.muikku.field.organizer")) {
ObjectMapper objectMapper = new ObjectMapper();
OrganizerFieldMeta organizerFieldMeta;
try {
organizerFieldMeta = objectMapper.readValue(event.getMaterialField().getContent(), OrganizerFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse organizer field meta", e);
}
List<WorkspaceMaterialField> fields = workspaceMaterialFieldDAO.listByQueryField(event.getWorkspaceMaterialField().getQueryField());
for (WorkspaceMaterialField field : fields) {
List<WorkspaceMaterialOrganizerFieldAnswer> answers = workspaceMaterialOrganizerFieldAnswerDAO.listByWorkspaceMaterialField(field);
for (WorkspaceMaterialOrganizerFieldAnswer answer : answers) {
try {
boolean answerModified = false;
HashMap<String, Set<String>> answerObject = objectMapper.readValue(answer.getValue(), new TypeReference<HashMap<String, Set<String>>>() {
});
Set<String> terms = new HashSet<String>();
Set<String> categories = answerObject.keySet();
for (String category : categories) {
terms.addAll(answerObject.get(category));
if (!organizerFieldMeta.hasCategoryWithId(category)) {
if (!event.getRemoveAnswers()) {
throw new WorkspaceMaterialContainsAnswersExeption("Could not update organizer field because it contains answers");
} else {
answerObject.remove(category);
answerModified = true;
}
}
}
for (String term : terms) {
if (!organizerFieldMeta.hasTermWithId(term)) {
if (!event.getRemoveAnswers()) {
throw new WorkspaceMaterialContainsAnswersExeption("Could not update organizer field because it contains answers");
} else {
Iterator<String> categoryIterator = answerObject.keySet().iterator();
while (categoryIterator.hasNext()) {
Set<String> categoryTerms = answerObject.get(categoryIterator.next());
if (categoryTerms.contains(term)) {
categoryTerms.remove(term);
answerModified = true;
}
}
}
}
}
if (answerModified) {
workspaceMaterialOrganizerFieldAnswerDAO.updateValue(answer, objectMapper.writeValueAsString(answerObject));
}
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse organizer field answer meta", e);
}
}
}
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer in project muikku by otavanopisto.
the class WorkspaceMaterialOrganizerFieldAnswerDAO method create.
public WorkspaceMaterialOrganizerFieldAnswer create(WorkspaceMaterialField field, WorkspaceMaterialReply reply, String value) {
WorkspaceMaterialOrganizerFieldAnswer workspaceMaterialOrganizerFieldAnswer = new WorkspaceMaterialOrganizerFieldAnswer();
workspaceMaterialOrganizerFieldAnswer.setField(field);
workspaceMaterialOrganizerFieldAnswer.setReply(reply);
workspaceMaterialOrganizerFieldAnswer.setValue(value);
return persist(workspaceMaterialOrganizerFieldAnswer);
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer in project muikku by otavanopisto.
the class WorkspaceMaterialOrganizerFieldAnswerDAO method findByFieldAndReply.
public WorkspaceMaterialOrganizerFieldAnswer findByFieldAndReply(WorkspaceMaterialField field, WorkspaceMaterialReply reply) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<WorkspaceMaterialOrganizerFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialOrganizerFieldAnswer.class);
Root<WorkspaceMaterialOrganizerFieldAnswer> root = criteria.from(WorkspaceMaterialOrganizerFieldAnswer.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceMaterialOrganizerFieldAnswer_.field), field), criteriaBuilder.equal(root.get(WorkspaceMaterialOrganizerFieldAnswer_.reply), reply)));
return getSingleResult(entityManager.createQuery(criteria));
}
Aggregations