use of fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta in project muikku by otavanopisto.
the class HtmlMaterialFieldChangeListener method onHtmlMaterialSorterFieldCreated.
// Sorter field
public void onHtmlMaterialSorterFieldCreated(@Observes HtmlMaterialFieldCreateEvent event) throws MaterialQueryIntegrityExeption, MaterialFieldMetaParsingExeption {
if (event.getField().getType().equals("application/vnd.muikku.field.sorter")) {
ObjectMapper objectMapper = new ObjectMapper();
SorterFieldMeta sorterFieldMeta;
try {
sorterFieldMeta = objectMapper.readValue(event.getField().getContent(), SorterFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse sorter field meta", e);
}
QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(event.getMaterial(), sorterFieldMeta.getName());
if (queryField == null) {
querySorterFieldController.createQuerySorterField(event.getMaterial(), sorterFieldMeta.getName());
} else {
throw new MaterialQueryIntegrityExeption("Field with same name already exists in the database");
}
}
}
use of fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta in project muikku by otavanopisto.
the class QuerySorterFieldController method updateQuerySorterField.
public QuerySorterField updateQuerySorterField(Material material, MaterialField field, boolean removeAnswers) throws MaterialFieldMetaParsingExeption {
ObjectMapper objectMapper = new ObjectMapper();
SorterFieldMeta sorterFieldMeta;
try {
sorterFieldMeta = objectMapper.readValue(field.getContent(), SorterFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse sorter field meta", e);
}
QuerySorterField queryField = querySorterFieldDAO.findByMaterialAndName(material, sorterFieldMeta.getName());
// -> fi.otavanopisto.muikku.plugins.workspace.QueryFieldChangeListener
queryFieldUpdateEvent.fire(new QueryFieldUpdateEvent(queryField, field, removeAnswers));
return queryField;
}
use of fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta in project muikku by otavanopisto.
the class WorkspaceMaterialFieldChangeListener method onWorkspaceMaterialSorterFieldUpdate.
// Sorter field
public void onWorkspaceMaterialSorterFieldUpdate(@Observes WorkspaceMaterialFieldUpdateEvent event) throws MaterialFieldMetaParsingExeption, WorkspaceMaterialContainsAnswersExeption {
if (event.getMaterialField().getType().equals("application/vnd.muikku.field.sorter")) {
ObjectMapper objectMapper = new ObjectMapper();
SorterFieldMeta sorterFieldMeta;
try {
sorterFieldMeta = objectMapper.readValue(event.getMaterialField().getContent(), SorterFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse sorter field meta", e);
}
List<WorkspaceMaterialField> fields = workspaceMaterialFieldDAO.listByQueryField(event.getWorkspaceMaterialField().getQueryField());
for (WorkspaceMaterialField field : fields) {
List<WorkspaceMaterialSorterFieldAnswer> answers = workspaceMaterialSorterFieldAnswerDAO.listByWorkspaceMaterialField(field);
for (WorkspaceMaterialSorterFieldAnswer answer : answers) {
try {
boolean answerModified = false;
List<String> answerItems = objectMapper.readValue(answer.getValue(), new TypeReference<ArrayList<String>>() {
});
for (int i = answerItems.size() - 1; i >= 0; i--) {
String answerItem = answerItems.get(i);
if (!sorterFieldMeta.hasItemWithId(answerItem)) {
if (!event.getRemoveAnswers()) {
throw new WorkspaceMaterialContainsAnswersExeption("Could not update sorter field because it contains answers");
} else {
answerItems.remove(answerItem);
answerModified = true;
}
}
}
if (answerModified) {
workspaceMaterialSorterFieldAnswerDAO.updateValue(answer, objectMapper.writeValueAsString(answerItems));
}
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse sorter field answer meta", e);
}
}
}
}
}
Aggregations