-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#666 The number of the Beast! Add the capability to handle nested obj…
…ects in Elements forms (needs more testing). Also, refactor Groovy support out of Elements.
- Loading branch information
1 parent
4d2e62f
commit 59cb6e8
Showing
27 changed files
with
482 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
elements/src/main/java/com/manydesigns/elements/fields/ObjectField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
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.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 ObjectField extends AbstractField<Object> { | ||
|
||
protected final ClassAccessor classAccessor; | ||
protected final Form form; | ||
protected Object value; | ||
|
||
public ObjectField(@NotNull PropertyAccessor accessor, @NotNull Mode mode, @Nullable String prefix) { | ||
super(accessor, mode, prefix); | ||
classAccessor = ClassAccessorFactories.get(accessor.getType()); | ||
form = new FormBuilder(classAccessor) | ||
.configPrefix(StringUtils.defaultString(prefix) + accessor.getName() + ".") | ||
.build(); | ||
} | ||
|
||
public ObjectField(@NotNull PropertyAccessor accessor, @NotNull Mode mode) { | ||
this(accessor, mode, null); | ||
} | ||
|
||
@Override | ||
public boolean validate() { | ||
return form.validate(); | ||
} | ||
|
||
@Override | ||
public void writeToObject(Object obj) { | ||
form.writeToObject(accessor.get(obj)); | ||
} | ||
|
||
@Override | ||
public void valueToXhtml(XhtmlBuffer xb) { | ||
form.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; | ||
form.readFromObject(value); | ||
} | ||
|
||
@Override | ||
public void readFromRequest(HttpServletRequest req) { | ||
super.readFromRequest(req); | ||
form.readFromRequest(req); | ||
} | ||
|
||
@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())) { | ||
return; | ||
} | ||
bulkChecked = true; | ||
Object value = keyValueAccessor.get(accessor.getName()); | ||
if (value == null) { | ||
setValue(null); | ||
} else { | ||
Object object = classAccessor.newInstance(); | ||
form.readFrom(keyValueAccessor.inner(value)); | ||
form.writeToObject(object); | ||
this.value = object; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,13 @@ | |
|
||
import java.lang.reflect.AnnotatedElement; | ||
|
||
/* | ||
* @author Paolo Predonzani - [email protected] | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
/** | ||
* Describes the structure of a homogeneous group of objects, and allows accessing their properties. | ||
* @author Paolo Predonzani - [email protected] | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public interface ClassAccessor extends AnnotatedElement { | ||
public static final String copyright = | ||
"Copyright (C) 2005-2020 ManyDesigns srl"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ | |
|
||
/** | ||
* A {@link ClassAccessor} targeting Java classes. Use {@link GroovyClassAccessor} for Groovy classes instead. | ||
* | ||
* | ||
* @author Paolo Predonzani - [email protected] | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
|
@@ -76,7 +76,7 @@ public class JavaClassAccessor implements ClassAccessor { | |
classAccessorCache = CacheBuilder.newBuilder().weakKeys().build(); | ||
} | ||
|
||
public static JavaClassAccessor getClassAccessor(Class javaClass) { | ||
public static JavaClassAccessor getClassAccessor(Class<?> javaClass) { | ||
JavaClassAccessor cachedResult = classAccessorCache.getIfPresent(javaClass); | ||
if (cachedResult == null) { | ||
logger.debug("Cache miss for: {}", javaClass); | ||
|
@@ -211,7 +211,7 @@ private boolean isPropertyPresent(List<PropertyAccessor> accessorList, | |
return false; | ||
} | ||
|
||
|
||
//************************************************************************** | ||
// ClassAccessor implementation | ||
//************************************************************************** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public class JavaFieldAccessor<T> implements PropertyAccessor<Object, T> { | ||
public class JavaFieldAccessor implements PropertyAccessor { | ||
public static final String copyright = | ||
"Copyright (C) 2005-2020 ManyDesigns srl"; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ | |
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public class JavaPropertyAccessor<T> extends AbstractAnnotatedAccessor implements PropertyAccessor<Object, T> { | ||
public class JavaPropertyAccessor extends AbstractAnnotatedAccessor implements PropertyAccessor { | ||
public static final String copyright = | ||
"Copyright (C) 2005-2020 ManyDesigns srl"; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,14 @@ | |
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/* | ||
* @author Paolo Predonzani - [email protected] | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public class MapEntryAccessor<T> implements PropertyAccessor<Map, T> { | ||
/** | ||
* {@link PropertyAccessor} for {@link Map} entries. | ||
* @author Paolo Predonzani - [email protected] | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public class MapEntryAccessor implements PropertyAccessor { | ||
public static final String copyright = | ||
"Copyright (C) 2005-2020 ManyDesigns srl"; | ||
|
||
|
@@ -80,11 +81,11 @@ public Annotation[] getDeclaredAnnotations() { | |
return getAnnotations(); | ||
} | ||
|
||
public String get(Map obj) { | ||
return OgnlUtils.convertValueToString(obj.get(name)); | ||
public String get(Object obj) { | ||
return OgnlUtils.convertValueToString(((Map) obj).get(name)); | ||
} | ||
|
||
public void set(Map obj, Object value) { | ||
obj.put(name, value); | ||
public void set(Object obj, Object value) { | ||
((Map) obj).put(name, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ | |
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public class PropertiesEntryAccessor<T> implements PropertyAccessor<Properties, T> { | ||
public class PropertiesEntryAccessor implements PropertyAccessor { | ||
public static final String copyright = | ||
"Copyright (C) 2005-2020 ManyDesigns srl"; | ||
|
||
|
@@ -78,11 +78,11 @@ public Annotation[] getDeclaredAnnotations() { | |
return getAnnotations(); | ||
} | ||
|
||
public String get(Properties obj) { | ||
return obj.getProperty(name); | ||
public String get(Object obj) { | ||
return ((Properties) obj).getProperty(name); | ||
} | ||
|
||
public void set(Properties obj, Object value) { | ||
obj.setProperty(name, (String)value); | ||
public void set(Object obj, Object value) { | ||
((Properties) obj).setProperty(name, (String)value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,14 @@ | |
|
||
import java.lang.reflect.AnnotatedElement; | ||
|
||
/* | ||
* @author Paolo Predonzani - [email protected] | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public interface PropertyAccessor<OBJ, TYPE> extends AnnotatedElement { | ||
/** | ||
* Describes and allows access to a property of an object. | ||
* @author Paolo Predonzani - [email protected] | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public interface PropertyAccessor extends AnnotatedElement { | ||
public static final String copyright = | ||
"Copyright (C) 2005-2020 ManyDesigns srl"; | ||
|
||
|
@@ -38,15 +39,15 @@ public interface PropertyAccessor<OBJ, TYPE> extends AnnotatedElement { | |
//************************************************************************** | ||
|
||
String getName(); | ||
Class<TYPE> getType(); | ||
Class<?> getType(); | ||
int getModifiers(); | ||
|
||
//************************************************************************** | ||
// Accessors | ||
//************************************************************************** | ||
|
||
Object get(OBJ obj); | ||
void set(OBJ obj, TYPE value); | ||
Object get(Object obj); | ||
void set(Object obj, Object value); | ||
|
||
default boolean isWritable() { | ||
return true; | ||
|
Oops, something went wrong.