diff --git a/elements/src/main/java/com/manydesigns/elements/fields/ListField.java b/elements/src/main/java/com/manydesigns/elements/fields/ListField.java new file mode 100644 index 000000000..6a5662798 --- /dev/null +++ b/elements/src/main/java/com/manydesigns/elements/fields/ListField.java @@ -0,0 +1,138 @@ +package com.manydesigns.elements.fields; + +import com.manydesigns.elements.KeyValueAccessor; +import com.manydesigns.elements.Mode; +import com.manydesigns.elements.forms.Form; +import com.manydesigns.elements.forms.FormBuilder; +import com.manydesigns.elements.forms.TableForm; +import com.manydesigns.elements.forms.TableFormBuilder; +import com.manydesigns.elements.reflection.ClassAccessor; +import com.manydesigns.elements.reflection.PropertyAccessor; +import com.manydesigns.elements.reflection.factories.ClassAccessorFactories; +import com.manydesigns.elements.xml.XhtmlBuffer; +import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.servlet.http.HttpServletRequest; + +public class ListField extends AbstractField { + + protected final ClassAccessor classAccessor; + protected final String prefix; + protected Object value; + private TableForm form; + + public ListField(@NotNull PropertyAccessor accessor, @NotNull Mode mode, @Nullable String prefix) { + super(accessor, mode, prefix); + this.prefix = prefix; + classAccessor = ClassAccessorFactories.get(accessor.getType()); + } + + protected TableForm ensureForm() { + if (form == null) { + form = new TableFormBuilder(classAccessor) + .configPrefix(StringUtils.defaultString(prefix) + accessor.getName() + ".") + .build(); + } + return form; + } + + public ListField(@NotNull PropertyAccessor accessor, @NotNull Mode mode) { + this(accessor, mode, null); + } + + @Override + public boolean validate() { + errors.clear(); + if (isRequired() && value == null) { + errors.add(getText("elements.error.field.required")); + return false; + } + return ensureForm().validate(); + } + + @Override + public boolean isValid() { + return super.isValid() && ensureForm().isValid(); + } + + @Override + public void writeToObject(Object obj) { + ensureForm().writeToObject(accessor.get(obj)); + } + + @Override + public void valueToXhtml(XhtmlBuffer xb) { + ensureForm().toXhtml(xb); + } + + @Override + public String getStringValue() { + return null; + } + + @Override + public void setStringValue(String stringValue) { + throw new UnsupportedOperationException(); + } + + @Override + public Object getValue() { + return value; + } + + @Override + public void setValue(Object value) { + this.value = value; + ensureForm().readFromObject(value); + } + + @Override + public void readFromRequest(HttpServletRequest req) { + super.readFromRequest(req); + if (mode.isView(insertable, updatable)) { + return; + } + ensureForm().readFromRequest(req); + setValueFromForm(); + } + + @Override + public void readFromObject(Object obj) { + super.readFromObject(obj); + if (obj == null) { + setValue(null); + } else { + setValue(accessor.get(obj)); + } + } + + @Override + public void readFrom(KeyValueAccessor keyValueAccessor) { + if (isReadOnly()) { + return; + } + if(!keyValueAccessor.has(accessor.getName())) { + setValue(null); + return; + } + bulkChecked = true; + Object value = keyValueAccessor.get(accessor.getName()); + if (value == null) { + setValue(null); + } else { + ensureForm().readFrom(keyValueAccessor.inner(value)); + setValueFromForm(); + } + } + + protected void setValueFromForm() { + Object object = classAccessor.newInstance(); + if (object == null) { + throw new IllegalStateException("Could not create an instance using " + classAccessor); + } + ensureForm().writeToObject(object); + this.value = object; + } +} diff --git a/elements/src/main/java/com/manydesigns/elements/forms/TableForm.java b/elements/src/main/java/com/manydesigns/elements/forms/TableForm.java index e85686ec7..2cfbce47c 100644 --- a/elements/src/main/java/com/manydesigns/elements/forms/TableForm.java +++ b/elements/src/main/java/com/manydesigns/elements/forms/TableForm.java @@ -22,6 +22,7 @@ import com.manydesigns.elements.Element; import com.manydesigns.elements.ElementsProperties; +import com.manydesigns.elements.KeyValueAccessor; import com.manydesigns.elements.annotations.Help; import com.manydesigns.elements.composites.AbstractCompositeElement; import com.manydesigns.elements.fields.Field; @@ -36,9 +37,7 @@ import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Array; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; /* * @author Paolo Predonzani - paolo.predonzani@manydesigns.com @@ -53,7 +52,7 @@ public class TableForm implements Element { protected String selectInputName = "select"; protected final Column[] columns; - protected final Row[] rows; + protected final List rows = new ArrayList<>(); protected String prefix; @@ -70,14 +69,8 @@ public class TableForm implements Element { // Costruttori //************************************************************************** - public TableForm(int nRows, PropertyAccessor... propertyAccessors) { - rows = new Row[nRows]; + public TableForm(PropertyAccessor... propertyAccessors) { columns = new Column[propertyAccessors.length]; - - for (int i = 0; i < nRows; i++) { - rows[i] = new Row(i); - } - for (int i = 0; i < columns.length; i++) { columns[i] = new Column(propertyAccessors[i]); } @@ -144,7 +137,7 @@ public void toXhtml(@NotNull XhtmlBuffer xb) { xb.closeElement("tr"); xb.closeElement("thead"); - if(rows.length > 0) { + if(!rows.isEmpty()) { xb.openElement("tbody"); for (Row row : rows) { row.toXhtml(xb); @@ -161,6 +154,19 @@ public void readFromRequest(HttpServletRequest req) { } } + @Override + public void readFrom(KeyValueAccessor keyValueAccessor) { + Object object = keyValueAccessor.get(); + if (object instanceof Iterable) { + Iterator iterator = ((Iterable) object).iterator(); + for (Row row : rows) { + if (iterator.hasNext()) { + row.readFrom(keyValueAccessor.inner(iterator.next())); + } + } + } + } + public boolean validate() { boolean result = true; for (Row row : rows) { @@ -221,7 +227,7 @@ public void writeToObject(Object obj) { final int arrayLength = Array.getLength(obj); for (int i = 0; i < arrayLength; i++) { Object currentObj = Array.get(obj, i); - rows[i].writeToObject(currentObj); + rows.get(i).writeToObject(currentObj); } } else if (Collection.class.isAssignableFrom(clazz)) { // Tratta obj come collection @@ -229,7 +235,7 @@ public void writeToObject(Object obj) { int i = 0; for (Object currentObj : collection) { - rows[i].writeToObject(currentObj); + rows.get(i).writeToObject(currentObj); i++; } } @@ -267,8 +273,8 @@ public Column[] getColumns() { return columns; } - public Row[] getRows() { - return rows; + public List getRows() { + return Collections.unmodifiableList(rows); } public String getPrefix() { @@ -357,6 +363,13 @@ public void readFromRequest(HttpServletRequest req) { } } + @Override + public void readFrom(KeyValueAccessor keyValueAccessor) { + for (Field current : this) { + current.readFrom(keyValueAccessor); + } + } + public boolean validate() { boolean result = true; for (Field current : this) { diff --git a/elements/src/main/java/com/manydesigns/elements/forms/TableFormBuilder.java b/elements/src/main/java/com/manydesigns/elements/forms/TableFormBuilder.java index 76305666a..e4b65cfd3 100644 --- a/elements/src/main/java/com/manydesigns/elements/forms/TableFormBuilder.java +++ b/elements/src/main/java/com/manydesigns/elements/forms/TableFormBuilder.java @@ -30,8 +30,8 @@ import com.manydesigns.elements.reflection.JavaClassAccessor; import com.manydesigns.elements.reflection.PropertyAccessor; import com.manydesigns.elements.text.TextFormat; -import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,8 +58,6 @@ public class TableFormBuilder extends AbstractFormBuilder { protected final Map titleTextFormats; protected List propertyAccessors; - protected int nRows = DEFAULT_N_ROWS; - protected final List> rowSelectionProviders; public static final Logger logger = LoggerFactory.getLogger(TableFormBuilder.class); @@ -76,10 +74,9 @@ public TableFormBuilder(Class aClass) { public TableFormBuilder(ClassAccessor classAccessor) { super(classAccessor); - headerTextFormats = new HashMap(); - hrefTextFormats = new HashMap(); - titleTextFormats = new HashMap(); - rowSelectionProviders = new ArrayList>(nRows); + headerTextFormats = new HashMap<>(); + hrefTextFormats = new HashMap<>(); + titleTextFormats = new HashMap<>(); } @@ -106,14 +103,6 @@ public TableFormBuilder configPrefix(String prefix) { return this; } - public TableFormBuilder configNRows(int nRows) { - this.nRows = nRows; - while(rowSelectionProviders.size() < nRows) { - rowSelectionProviders.add(new HashMap()); - } - return this; - } - public TableFormBuilder configMode(Mode mode) { this.mode = mode; return this; @@ -125,14 +114,8 @@ public TableFormBuilder configSelectionProvider(SelectionProvider selectionProvi return this; } - public TableFormBuilder configSelectionProvider(int row, SelectionProvider selectionProvider, - String... fieldNames) { - rowSelectionProviders.get(row).put(fieldNames, selectionProvider); - return this; - } - public void configReflectiveFields() { - propertyAccessors = new ArrayList(); + propertyAccessors = new ArrayList<>(); for (PropertyAccessor current : classAccessor.getProperties()) { if (!isPropertyVisible(current)) { continue; @@ -193,9 +176,9 @@ public TableForm build() { new PropertyAccessor[propertyAccessors.size()]; propertyAccessors.toArray(propertyAccessorsArray); - TableForm tableForm = new TableForm(nRows, propertyAccessorsArray); + TableForm tableForm = new TableForm(propertyAccessorsArray); - if (null!=prefix && prefix.length()>0) { + if (StringUtils.isNotBlank(prefix)) { tableForm.setPrefix(prefix); } @@ -241,11 +224,6 @@ protected void setupRows(TableForm tableForm) { setupSelectionProvidersForRow(tableForm, row, current); } - for (Map.Entry current : - rowSelectionProviders.get(index).entrySet()) { - setupSelectionProvidersForRow(tableForm, row, current); - } - index++; } }