use of fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta in project muikku by otavanopisto.
the class HtmlMaterialFieldChangeListener method onHtmlMaterialMultiSelectFieldCreated.
// Multi-select field
public void onHtmlMaterialMultiSelectFieldCreated(@Observes HtmlMaterialFieldCreateEvent event) throws MaterialQueryIntegrityExeption, MaterialFieldMetaParsingExeption {
if (event.getField().getType().equals("application/vnd.muikku.field.multiselect")) {
ObjectMapper objectMapper = new ObjectMapper();
MultiSelectFieldMeta multiSelectFieldMeta;
try {
multiSelectFieldMeta = objectMapper.readValue(event.getField().getContent(), MultiSelectFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
}
QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(event.getMaterial(), multiSelectFieldMeta.getName());
if (queryField != null) {
throw new MaterialQueryIntegrityExeption("Field with same name already exists in the database");
}
QueryMultiSelectField queryMultiSelectField = queryMultiSelectFieldController.createQueryMultiSelectField(event.getMaterial(), multiSelectFieldMeta.getName());
for (MultiSelectFieldOptionMeta multiSelectFieldOptionMeta : multiSelectFieldMeta.getOptions()) {
queryMultiSelectFieldController.createQueryMultiSelectFieldOption(queryMultiSelectField, multiSelectFieldOptionMeta.getName(), multiSelectFieldOptionMeta.getText());
}
}
}
use of fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta in project muikku by otavanopisto.
the class WorkspaceMaterialFieldChangeListener method onWorkspaceMaterialMultiSelectFieldUpdate.
// Multi-select field
public void onWorkspaceMaterialMultiSelectFieldUpdate(@Observes WorkspaceMaterialFieldUpdateEvent event) throws MaterialFieldMetaParsingExeption, WorkspaceMaterialContainsAnswersExeption {
if (event.getMaterialField().getType().equals("application/vnd.muikku.field.multiselect")) {
// Field JSON to metadata object
ObjectMapper objectMapper = new ObjectMapper();
MultiSelectFieldMeta multiSelectFieldMeta;
try {
multiSelectFieldMeta = objectMapper.readValue(event.getMaterialField().getContent(), MultiSelectFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse multi-select field meta", e);
}
QueryMultiSelectField queryField = (QueryMultiSelectField) event.getWorkspaceMaterialField().getQueryField();
// Ensure that if there are options being removed, they haven't been used as answers
List<WorkspaceMaterialMultiSelectFieldAnswer> deprecatedAnswers = new ArrayList<WorkspaceMaterialMultiSelectFieldAnswer>();
List<QueryMultiSelectFieldOption> oldOptions = queryMultiSelectFieldOptionDAO.listByField(queryField);
List<MultiSelectFieldOptionMeta> newOptions = multiSelectFieldMeta.getOptions();
for (MultiSelectFieldOptionMeta newOption : newOptions) {
QueryMultiSelectFieldOption correspondingOption = findMultiSelectOptionByName(oldOptions, newOption.getName());
if (correspondingOption != null) {
oldOptions.remove(correspondingOption);
}
}
for (QueryMultiSelectFieldOption removedOption : oldOptions) {
List<WorkspaceMaterialMultiSelectFieldAnswer> answers = workspaceMaterialMultiSelectFieldAnswerDAO.listByQueryMultiSelectFieldOption(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 (WorkspaceMaterialMultiSelectFieldAnswer deprecatedAnswer : deprecatedAnswers) {
deleteFieldAnswer(deprecatedAnswer);
}
}
}
}
use of fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta in project muikku by otavanopisto.
the class FieldElementsHandler method handleChecklistField.
@Override
public Node handleChecklistField(Document ownerDocument, String paramName, List<MultiSelectFieldOptionMeta> options, String helpOf, String hintOf) {
MultiSelectFieldMeta checklistFieldMeta = fieldTranslator.translateChecklistField(paramName, options);
List<Element> elements = new ArrayList<>();
for (MultiSelectFieldOptionMeta option : checklistFieldMeta.getOptions()) {
Element inputElement = ownerDocument.createElement("input");
inputElement.setAttribute("type", "checkbox");
inputElement.setAttribute("value", option.getName());
inputElement.setAttribute("name", checklistFieldMeta.getName());
// TODO: Label For ...
Element labelElement = ownerDocument.createElement("label");
labelElement.setTextContent(option.getText());
elements.add(inputElement);
elements.add(labelElement);
}
return wrapWithObjectElement(ownerDocument, elements, checklistFieldMeta);
}
use of fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta in project muikku by otavanopisto.
the class QueryMultiSelectFieldController method updateQueryMultiSelectField.
public QueryMultiSelectField updateQueryMultiSelectField(Material material, MaterialField field, boolean removeAnswers) throws MaterialFieldMetaParsingExeption {
// Field JSON to metadata object
ObjectMapper objectMapper = new ObjectMapper();
MultiSelectFieldMeta multiSelectFieldMeta;
try {
multiSelectFieldMeta = objectMapper.readValue(field.getContent(), MultiSelectFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
}
QueryMultiSelectField queryField = queryMultiSelectFieldDAO.findByMaterialAndName(material, multiSelectFieldMeta.getName());
// -> fi.otavanopisto.muikku.plugins.workspace.QueryFieldChangeListener
queryFieldUpdateEvent.fire(new QueryFieldUpdateEvent(queryField, field, removeAnswers));
List<QueryMultiSelectFieldOption> oldOptions = queryMultiSelectFieldOptionDAO.listByField(queryField);
List<MultiSelectFieldOptionMeta> newOptions = multiSelectFieldMeta.getOptions();
for (MultiSelectFieldOptionMeta newOption : newOptions) {
QueryMultiSelectFieldOption correspondingOption = findOptionByName(oldOptions, newOption.getName());
if (correspondingOption == null) {
// New options
createQueryMultiSelectFieldOption(queryField, newOption.getName(), newOption.getText());
} else {
// Modified options
if (!StringUtils.equals(correspondingOption.getText(), newOption.getText())) {
updateQueryMultiSelectFieldOptionText(correspondingOption, newOption.getText());
}
oldOptions.remove(correspondingOption);
}
}
// Removed options
for (QueryMultiSelectFieldOption removedOption : oldOptions) {
deleteQueryMultiSelectFieldOption(removedOption);
}
return queryField;
}
Aggregations