Skip to content

Commit

Permalink
#666 List fields (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiostalla committed Oct 13, 2023
1 parent cb559b6 commit 0c0584a
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 45 deletions.
138 changes: 138 additions & 0 deletions elements/src/main/java/com/manydesigns/elements/fields/ListField.java
Original file line number Diff line number Diff line change
@@ -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<Object> {

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -53,7 +52,7 @@ public class TableForm implements Element {
protected String selectInputName = "select";

protected final Column[] columns;
protected final Row[] rows;
protected final List<Row> rows = new ArrayList<>();

protected String prefix;

Expand All @@ -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]);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -221,15 +227,15 @@ 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
Collection collection = (Collection)obj;

int i = 0;
for (Object currentObj : collection) {
rows[i].writeToObject(currentObj);
rows.get(i).writeToObject(currentObj);
i++;
}
}
Expand Down Expand Up @@ -267,8 +273,8 @@ public Column[] getColumns() {
return columns;
}

public Row[] getRows() {
return rows;
public List<Row> getRows() {
return Collections.unmodifiableList(rows);
}

public String getPrefix() {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -58,8 +58,6 @@ public class TableFormBuilder extends AbstractFormBuilder {
protected final Map<String, TextFormat> titleTextFormats;

protected List<PropertyAccessor> propertyAccessors;
protected int nRows = DEFAULT_N_ROWS;
protected final List<Map<String[], SelectionProvider>> rowSelectionProviders;

public static final Logger logger =
LoggerFactory.getLogger(TableFormBuilder.class);
Expand All @@ -76,10 +74,9 @@ public TableFormBuilder(Class aClass) {
public TableFormBuilder(ClassAccessor classAccessor) {
super(classAccessor);

headerTextFormats = new HashMap<String, TextFormat>();
hrefTextFormats = new HashMap<String, TextFormat>();
titleTextFormats = new HashMap<String, TextFormat>();
rowSelectionProviders = new ArrayList<Map<String[], SelectionProvider>>(nRows);
headerTextFormats = new HashMap<>();
hrefTextFormats = new HashMap<>();
titleTextFormats = new HashMap<>();
}


Expand All @@ -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<String[], SelectionProvider>());
}
return this;
}

public TableFormBuilder configMode(Mode mode) {
this.mode = mode;
return this;
Expand All @@ -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<PropertyAccessor>();
propertyAccessors = new ArrayList<>();
for (PropertyAccessor current : classAccessor.getProperties()) {
if (!isPropertyVisible(current)) {
continue;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -241,11 +224,6 @@ protected void setupRows(TableForm tableForm) {
setupSelectionProvidersForRow(tableForm, row, current);
}

for (Map.Entry<String[], SelectionProvider> current :
rowSelectionProviders.get(index).entrySet()) {
setupSelectionProvidersForRow(tableForm, row, current);
}

index++;
}
}
Expand Down

0 comments on commit 0c0584a

Please sign in to comment.