Skip to content

Commit

Permalink
#666 Work towards ListField
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiostalla committed Oct 20, 2023
1 parent 0c0584a commit 9f7a92f
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 122 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Thumbs.db
oneclick/local.*
oneclick/build/*
#Created by portofino-spring-boot
/portofino-application/
/portofino-application/
/.vscode/
24 changes: 0 additions & 24 deletions .hgignore

This file was deleted.

65 changes: 0 additions & 65 deletions .hgtags

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface Element extends XhtmlFragment {
"Copyright (C) 2005-2020 ManyDesigns srl";

void readFromRequest(HttpServletRequest req);
void readFrom(KeyValueAccessor keyValueAccessor);
void readFrom(KeyValueAccessor keyValueAccessor);
boolean validate();
boolean isValid();
void readFromObject(Object obj);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.manydesigns.elements;

import java.util.List;

/**
* @author Angelo Lupo - [email protected]
* @author Giampiero Granatella - [email protected]
Expand All @@ -14,5 +16,6 @@ public interface KeyValueAccessor {
void set(String name, Object value);
boolean has(String name);

KeyValueAccessor inner(Object value);
KeyValueAccessor object(String name);
List<KeyValueAccessor> list(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public boolean has(String name) {
}

@Override
public KeyValueAccessor inner(Object value) {
return new MapKeyValueAccessor((Map<String, Object>) value);
public KeyValueAccessor object(String name) {
Map<String, Object> inner = (Map<String, Object>) get(name);
return inner != null ? new MapKeyValueAccessor(inner) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public void readFromRequest(HttpServletRequest req) {
}
}

@Override
public void readFrom(KeyValueAccessor keyValueAccessor) {
for (T current : this) {
current.readFrom(keyValueAccessor);
}
}

public boolean validate() {
boolean result = true;
for (T current : this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.manydesigns.elements.fields.search;

import com.manydesigns.elements.ElementsThreadLocals;
import com.manydesigns.elements.KeyValueAccessor;
import com.manydesigns.elements.annotations.Id;
import com.manydesigns.elements.annotations.InputName;
import com.manydesigns.elements.annotations.Required;
Expand Down Expand Up @@ -106,6 +107,11 @@ public void readFromObject(Object obj) {
public void writeToObject(Object obj) {
}

@Override
public void readFrom(KeyValueAccessor keyValueAccessor) {
throw new UnsupportedOperationException("TODO");
}

//**************************************************************************
// Getters/setters
//**************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,4 @@ public SearchField findSearchFieldByPropertyName(String propertyName) {
}
return null;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.lang.reflect.Array;
import java.util.*;

/*
/** A form with multiple rows, where a row is a collection of fields. Every row has the same fields.
* @author Paolo Predonzani - [email protected]
* @author Angelo Lupo - [email protected]
* @author Giampiero Granatella - [email protected]
Expand All @@ -52,7 +52,7 @@ public class TableForm implements Element {
protected String selectInputName = "select";

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

protected String prefix;

Expand Down Expand Up @@ -187,34 +187,26 @@ public boolean isValid() {

public void readFromObject(Object obj) {
Class clazz = obj.getClass();
if (clazz.isArray()) { // Tratta obj come un array
// Scorre tutti gli ellementi dell'array obj,
// indipendentemente da quante righe ci sono nell table form.
// Eventualmente lancia Eccezione.
if (clazz.isArray()) {
final int arrayLength = Array.getLength(obj);
rows = new ArrayList<>(arrayLength);
for (int i = 0; i < arrayLength; i++) {
Object currentObj = Array.get(obj, i);
rows[i].readFromObject(currentObj);
}

// Scorre le rimanenti righe del table form,
// passano null come ottetto di bind.
for (int i = arrayLength; i < rows.length; i++) {
rows[i].readFromObject(null);
Row row = new Row(i);
rows.add(row);
row.readFromObject(currentObj);
}
} else if (Collection.class.isAssignableFrom(clazz)) {
// Tratta obj come collection
Collection collection = (Collection)obj;

Collection collection = (Collection) obj;
rows = new ArrayList<>(collection.size());
int i = 0;
for (Object currentObj : collection) {
rows[i].readFromObject(currentObj);
Row row = new Row(i);
rows.add(row);
row.readFromObject(currentObj);
i++;
}

for (; i < rows.length; i++) {
rows[i].readFromObject(null);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public boolean has(String name) {
}

@Override
public KeyValueAccessor inner(Object value) {
return new JsonKeyValueAccessor((JSONObject) value);
public KeyValueAccessor object(String name) {
if (jsonObject.isNull(name)) {
return null;
}
return new JsonKeyValueAccessor(jsonObject.getJSONObject(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ protected double[] setupColumnSizes() {
int i = 0;
for (Field field : row) {
int size = StringUtils.length(field.getStringValue());
double relativeSize = ((double) size) / form.getRows().length;
double relativeSize = ((double) size) / form.getRows().size();
columnSizes[i++] += relativeSize;
}
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@
<configuration>
<rules>
<enforceBytecodeVersion>
<maxJdkVersion>1.8</maxJdkVersion>
<maxJdkVersion>11</maxJdkVersion>
</enforceBytecodeVersion>
<!--<dependencyConvergence/>-->
</rules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean has(String name) {
}

@Override
public KeyValueAccessor inner(Object value) {
public KeyValueAccessor object(String name) {
throw new UnsupportedOperationException();
}
}

0 comments on commit 9f7a92f

Please sign in to comment.