Skip to content

Commit

Permalink
#666 ObjectField helper and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiostalla committed Oct 13, 2023
1 parent eeaf6ae commit d928e23
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2020 ManyDesigns srl. All rights reserved.
* Copyright (C) 2005-2023 ManyDesigns srl. All rights reserved.
* http://www.manydesigns.com/
*
* This is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -40,7 +40,7 @@
*/
public final class ElementsProperties {
public static final String copyright =
"Copyright (C) 2005-2020 ManyDesigns srl";
"Copyright (C) 2005-2023 ManyDesigns srl";

//**************************************************************************
// Default and custom properties location
Expand Down Expand Up @@ -84,30 +84,33 @@ public final class ElementsProperties {

static {
configuration = new CompositeConfiguration();
addConfiguration(CUSTOM_PROPERTIES_RESOURCE);
addConfiguration(PROPERTIES_RESOURCE);
addConfiguration(CUSTOM_PROPERTIES_RESOURCE, false);
addConfiguration(PROPERTIES_RESOURCE, true);
}

public static void addConfiguration(String resource) {
addConfiguration(resource, true);
}

public static void addConfiguration(String resource, boolean warnOnFail) {
try {
Configurations configurations = new Configurations();
BuilderParameters parameters =
new Parameters().properties().setListDelimiterHandler(new DefaultListDelimiterHandler(',')).setFileName(resource);
configuration.addConfiguration(configurations.propertiesBuilder(resource).configure(parameters).getConfiguration());
} catch (Exception e) {
logger.warn(String.format(
"Error loading properties from: %s", resource), e);
if (warnOnFail) {
logger.warn(String.format("Error loading properties from: %s", resource), e);
} else if (logger.isDebugEnabled()) {
logger.debug(String.format("Error loading properties from: %s", resource), e);
}
}
}

public static Configuration getConfiguration() {
return configuration;
}

//**************************************************************************
// Dummy constructor
//**************************************************************************

private ElementsProperties() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@
public class ObjectField extends AbstractField<Object> {

protected final ClassAccessor classAccessor;
protected final Form form;
protected final String prefix;
protected Object value;
private Form form;

public ObjectField(@NotNull PropertyAccessor accessor, @NotNull Mode mode, @Nullable String prefix) {
super(accessor, mode, prefix);
this.prefix = prefix;
classAccessor = ClassAccessorFactories.get(accessor.getType());
form = new FormBuilder(classAccessor)
.configPrefix(StringUtils.defaultString(prefix) + accessor.getName() + ".")
.build();
}

protected Form ensureForm() {
if (form == null) {
form = new FormBuilder(classAccessor)
.configPrefix(StringUtils.defaultString(prefix) + accessor.getName() + ".")
.build();
}
return form;
}

public ObjectField(@NotNull PropertyAccessor accessor, @NotNull Mode mode) {
Expand All @@ -39,22 +47,22 @@ public boolean validate() {
errors.add(getText("elements.error.field.required"));
return false;
}
return form.validate();
return ensureForm().validate();
}

@Override
public boolean isValid() {
return super.isValid() && form.isValid();
return super.isValid() && ensureForm().isValid();
}

@Override
public void writeToObject(Object obj) {
form.writeToObject(accessor.get(obj));
ensureForm().writeToObject(accessor.get(obj));
}

@Override
public void valueToXhtml(XhtmlBuffer xb) {
form.toXhtml(xb);
ensureForm().toXhtml(xb);
}

@Override
Expand All @@ -75,7 +83,7 @@ public Object getValue() {
@Override
public void setValue(Object value) {
this.value = value;
form.readFromObject(value);
ensureForm().readFromObject(value);
}

@Override
Expand All @@ -84,7 +92,7 @@ public void readFromRequest(HttpServletRequest req) {
if (mode.isView(insertable, updatable)) {
return;
}
form.readFromRequest(req);
ensureForm().readFromRequest(req);
setValueFromForm();
}

Expand Down Expand Up @@ -112,7 +120,7 @@ public void readFrom(KeyValueAccessor keyValueAccessor) {
if (value == null) {
setValue(null);
} else {
form.readFrom(keyValueAccessor.inner(value));
ensureForm().readFrom(keyValueAccessor.inner(value));
setValueFromForm();
}
}
Expand All @@ -122,7 +130,7 @@ protected void setValueFromForm() {
if (object == null) {
throw new IllegalStateException("Could not create an instance using " + classAccessor);
}
form.writeToObject(object);
ensureForm().writeToObject(object);
this.value = object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public class BooleanFieldHelper implements FieldHelper {
public static final String copyright =
"Copyright (C) 2005-2020 ManyDesigns srl";

public Field tryToInstantiateField(ClassAccessor classAccessor,
public BooleanField tryToInstantiateField(ClassAccessor classAccessor,
PropertyAccessor propertyAccessor,
Mode mode,
String prefix) {
Field result;
Class type = propertyAccessor.getType();
BooleanField result;
Class<?> type = propertyAccessor.getType();
if (type == Boolean.class || type == Boolean.TYPE) {
result = new BooleanField(propertyAccessor, mode, prefix);
} else {
Expand All @@ -56,7 +56,7 @@ public SearchField tryToInstantiateSearchField(ClassAccessor classAccessor,
PropertyAccessor propertyAccessor,
String prefix) {
SearchField result;
Class type = propertyAccessor.getType();
Class<?> type = propertyAccessor.getType();
if (type == Boolean.class || type == Boolean.TYPE) {
result = new BooleanSearchField(propertyAccessor, prefix);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface FieldHelper {
public static final String copyright =
"Copyright (C) 2005-2020 ManyDesigns srl";

Field tryToInstantiateField(ClassAccessor classAccessor,
Field<?> tryToInstantiateField(ClassAccessor classAccessor,
PropertyAccessor propertyAccessor,
Mode mode,
String prefix);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2005-2020 ManyDesigns srl. All rights reserved.
* http://www.manydesigns.com/
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package com.manydesigns.elements.fields.helpers;

import com.manydesigns.elements.Mode;
import com.manydesigns.elements.fields.BooleanField;
import com.manydesigns.elements.fields.ObjectField;
import com.manydesigns.elements.fields.search.BooleanSearchField;
import com.manydesigns.elements.fields.search.SearchField;
import com.manydesigns.elements.reflection.ClassAccessor;
import com.manydesigns.elements.reflection.PropertyAccessor;

/*
* @author Paolo Predonzani - paolo.predonzani@manydesigns.com
* @author Angelo Lupo - angelo.lupo@manydesigns.com
* @author Giampiero Granatella - giampiero.granatella@manydesigns.com
* @author Alessio Stalla - alessio.stalla@manydesigns.com
*/
public class ObjectFieldHelper implements FieldHelper {
public static final String copyright =
"Copyright (C) 2005-2023 ManyDesigns srl";

public ObjectField tryToInstantiateField(ClassAccessor classAccessor,
PropertyAccessor propertyAccessor,
Mode mode,
String prefix) {
return new ObjectField(propertyAccessor, mode, prefix);
}

public SearchField tryToInstantiateSearchField(ClassAccessor classAccessor,
PropertyAccessor propertyAccessor,
String prefix) {
return null; // TODO
}
}
3 changes: 2 additions & 1 deletion elements/src/main/resources/elements.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ fields.list = com.manydesigns.elements.fields.helpers.SelectFieldHelper, \
com.manydesigns.elements.fields.helpers.DatabaseBlobFieldHelper, \
com.manydesigns.elements.fields.helpers.FileBlobFieldHelper, \
com.manydesigns.elements.fields.helpers.EncryptedFieldHelper, \
com.manydesigns.elements.fields.helpers.TextFieldHelper
com.manydesigns.elements.fields.helpers.TextFieldHelper, \
com.manydesigns.elements.fields.helpers.ObjectFieldHelper

annotations.manager = com.manydesigns.elements.annotations.AnnotationsManager
annotations.implementation.list = \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public void setAPrivateInt(int aPrivateInt) {
@FileBlob
public String aBlob;

public AnnotatedBean1 anObject;

public AllDefaultFieldsBean aSelfReference;

// none of the following fields should be detected due to their modifiers

static int aStaticInt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ public void setUp() throws Exception {

public void testRegistry() {
FieldsManager fieldsManager = FieldsManager.getManager();
assertEquals(15, fieldsManager.getHelperList().size());
assertEquals(16, fieldsManager.getHelperList().size());

Form form = formBuilder1.build();

assertEquals(1, form.size());

FieldSet fieldSet = form.get(0);
assertEquals(16, fieldSet.size());
assertEquals(18, fieldSet.size());

Field field = (Field) fieldSet.get(0);
assertEquals("A private int", field.getLabel());
Expand Down Expand Up @@ -145,6 +145,16 @@ public void testRegistry() {
assertEquals("a blob", field.getLabel());
assertFalse(field.isRequired());
assertEquals(FileBlobField.class, field.getClass());

field = (Field) fieldSet.get(16);
assertEquals("an object", field.getLabel());
assertFalse(field.isRequired());
assertEquals(ObjectField.class, field.getClass());

field = (Field) fieldSet.get(17);
assertEquals("a self reference", field.getLabel());
assertFalse(field.isRequired());
assertEquals(ObjectField.class, field.getClass());
}

public void testConfigFields() throws NoSuchFieldException {
Expand Down

0 comments on commit d928e23

Please sign in to comment.