use of fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta in project muikku by otavanopisto.
the class QuerySelectFieldController method updateQuerySelectField.
public QuerySelectField updateQuerySelectField(Material material, MaterialField field, boolean removeAnswers) throws MaterialFieldMetaParsingExeption {
// Field JSON to metadata object
ObjectMapper objectMapper = new ObjectMapper();
SelectFieldMeta selectFieldMeta;
try {
selectFieldMeta = objectMapper.readValue(field.getContent(), SelectFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse select field meta", e);
}
QuerySelectField queryField = querySelectFieldDAO.findByMaterialAndName(material, selectFieldMeta.getName());
// -> fi.otavanopisto.muikku.plugins.workspace.QueryFieldChangeListener
queryFieldUpdateEvent.fire(new QueryFieldUpdateEvent(queryField, field, removeAnswers));
List<QuerySelectFieldOption> oldOptions = querySelectFieldOptionDAO.listByField(queryField);
List<SelectFieldOptionMeta> newOptions = selectFieldMeta.getOptions();
for (SelectFieldOptionMeta newOption : newOptions) {
QuerySelectFieldOption correspondingOption = findOptionByName(oldOptions, newOption.getName());
if (correspondingOption == null) {
// New options
createQuerySelectFieldOption(queryField, newOption.getName(), newOption.getText());
} else {
// Modified options
if (!StringUtils.equals(correspondingOption.getText(), newOption.getText())) {
updateQuerySelectFieldOptionText(correspondingOption, newOption.getText());
}
oldOptions.remove(correspondingOption);
}
}
// Removed options
for (QuerySelectFieldOption removedOption : oldOptions) {
deleteQuerySelectFieldOption(removedOption);
}
return queryField;
}
use of fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta 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.material.fieldmeta.SelectFieldOptionMeta in project muikku by otavanopisto.
the class FieldElementsHandler method handleRadioHorzSelectField.
private Node handleRadioHorzSelectField(Document ownerDocument, SelectFieldMeta selectFieldMeta) {
List<Element> elements = new ArrayList<>();
for (SelectFieldOptionMeta option : selectFieldMeta.getOptions()) {
Element inputElement = ownerDocument.createElement("input");
inputElement.setAttribute("type", "radio");
inputElement.setAttribute("value", option.getName());
inputElement.setAttribute("name", selectFieldMeta.getName());
// TODO: Label For ...
Element labelElement = ownerDocument.createElement("label");
labelElement.setTextContent(option.getText());
elements.add(inputElement);
elements.add(labelElement);
}
return wrapWithObjectElement(ownerDocument, elements, selectFieldMeta);
}
use of fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta in project muikku by otavanopisto.
the class FieldElementsHandler method handleRadioSelectField.
private Node handleRadioSelectField(Document ownerDocument, SelectFieldMeta selectFieldMeta) {
List<Element> elements = new ArrayList<>();
for (SelectFieldOptionMeta option : selectFieldMeta.getOptions()) {
Element inputElement = ownerDocument.createElement("input");
inputElement.setAttribute("type", "radio");
inputElement.setAttribute("value", option.getName());
inputElement.setAttribute("name", selectFieldMeta.getName());
// TODO: Label For ...
Element labelElement = ownerDocument.createElement("label");
labelElement.setTextContent(option.getText());
elements.add(inputElement);
elements.add(labelElement);
elements.add(ownerDocument.createElement("br"));
}
return wrapWithObjectElement(ownerDocument, elements, selectFieldMeta);
}
use of fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta in project muikku by otavanopisto.
the class FieldElementsHandler method handleDropdownSelectField.
private Node handleDropdownSelectField(Document ownerDocument, SelectFieldMeta selectFieldMeta) {
Element selectElement = ownerDocument.createElement("select");
selectElement.setAttribute("name", selectFieldMeta.getName());
for (SelectFieldOptionMeta option : selectFieldMeta.getOptions()) {
Element optionElement = ownerDocument.createElement("option");
optionElement.setAttribute("value", option.getName());
optionElement.setTextContent(option.getText());
selectElement.appendChild(optionElement);
}
return wrapWithObjectElement(ownerDocument, selectElement, selectFieldMeta);
}
Aggregations