use of fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta in project muikku by otavanopisto.
the class HtmlMaterialFieldChangeListener method onHtmlMaterialOrganizerFieldCreated.
// Organizer field
public void onHtmlMaterialOrganizerFieldCreated(@Observes HtmlMaterialFieldCreateEvent event) throws MaterialQueryIntegrityExeption, MaterialFieldMetaParsingExeption {
if (event.getField().getType().equals("application/vnd.muikku.field.organizer")) {
ObjectMapper objectMapper = new ObjectMapper();
OrganizerFieldMeta organizerFieldMeta;
try {
organizerFieldMeta = objectMapper.readValue(event.getField().getContent(), OrganizerFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse organizer field meta", e);
}
QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(event.getMaterial(), organizerFieldMeta.getName());
if (queryField == null) {
queryOrganizerFieldController.createQueryOrganizerField(event.getMaterial(), organizerFieldMeta.getName());
} else {
throw new MaterialQueryIntegrityExeption("Field with same name already exists in the database");
}
}
}
use of fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta 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.material.fieldmeta.OrganizerFieldMeta in project muikku by otavanopisto.
the class QueryOrganizerFieldController method updateQueryOrganizerField.
public QueryOrganizerField updateQueryOrganizerField(Material material, MaterialField field, boolean removeAnswers) throws MaterialFieldMetaParsingExeption {
ObjectMapper objectMapper = new ObjectMapper();
OrganizerFieldMeta organizerFieldMeta;
try {
organizerFieldMeta = objectMapper.readValue(field.getContent(), OrganizerFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
}
QueryOrganizerField queryField = queryOrganizerFieldDAO.findByMaterialAndName(material, organizerFieldMeta.getName());
// -> fi.otavanopisto.muikku.plugins.workspace.QueryFieldChangeListener
queryFieldUpdateEvent.fire(new QueryFieldUpdateEvent(queryField, field, removeAnswers));
return queryField;
}
Aggregations