From abb052d8f3572d1f3c1310d5e80862d4370b58e2 Mon Sep 17 00:00:00 2001 From: vovasty Date: Sat, 21 Feb 2009 15:45:41 +0300 Subject: [PATCH] common-components simple selection model --- aramzamzam-commons/commons-components/pom.xml | 19 +++++ .../components/services/ComponentsModule.java | 27 +++++++ .../services/InjectSelectionModelWorker.java | 92 ++++++++++++++++++++++ .../utils/select/GenericSelectionModel.java | 57 ++++++++++++++ .../utils/select/GenericValueEncoder.java | 52 ++++++++++++ .../utils/select/InjectSelectionModel.java | 33 ++++++++ aramzamzam-commons/pom.xml | 3 +- 7 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 aramzamzam-commons/commons-components/pom.xml create mode 100644 aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/services/ComponentsModule.java create mode 100644 aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/services/InjectSelectionModelWorker.java create mode 100644 aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/GenericSelectionModel.java create mode 100644 aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/GenericValueEncoder.java create mode 100644 aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/InjectSelectionModel.java diff --git a/aramzamzam-commons/commons-components/pom.xml b/aramzamzam-commons/commons-components/pom.xml new file mode 100644 index 0000000..6bee4ce --- /dev/null +++ b/aramzamzam-commons/commons-components/pom.xml @@ -0,0 +1,19 @@ + + + + aramzamzam-commons + net.aramzamzam.commons + 1.0-SNAPSHOT + + 4.0.0 + commons-components + commons-components + http://maven.apache.org + + + org.apache.tapestry + tapestry-core + ${tapestry-release-version} + + + \ No newline at end of file diff --git a/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/services/ComponentsModule.java b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/services/ComponentsModule.java new file mode 100644 index 0000000..a3737e5 --- /dev/null +++ b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/services/ComponentsModule.java @@ -0,0 +1,27 @@ +package net.aramzamzam.commons.components.services; + +import org.apache.tapestry5.ioc.ObjectLocator; +import org.apache.tapestry5.ioc.OrderedConfiguration; +import org.apache.tapestry5.ioc.services.PropertyAccess; +import org.apache.tapestry5.services.ComponentClassTransformWorker; + +public class ComponentsModule { + /** + * Adds the CommitAfterPreparation logic, to modify processing of the + * {@link org.apache.tapestry5.hibernate.annotations.CommitAfter} + * annotation according to Studynet application needs. + */ + public static void contributeComponentClassTransformWorker( + OrderedConfiguration configuration, + PropertyAccess propertyAccess, + ObjectLocator locator) { + /** + * Adds a number of standard component class transform workers: + * + */ + + configuration.add("InjectSelectionModel", new InjectSelectionModelWorker(propertyAccess), "after:Inject*"); + } +} diff --git a/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/services/InjectSelectionModelWorker.java b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/services/InjectSelectionModelWorker.java new file mode 100644 index 0000000..2c7c420 --- /dev/null +++ b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/services/InjectSelectionModelWorker.java @@ -0,0 +1,92 @@ +package net.aramzamzam.commons.components.services; +import java.lang.reflect.Modifier; + +import org.apache.tapestry5.ioc.internal.util.InternalUtils; +import org.apache.tapestry5.ioc.services.PropertyAccess; +import org.apache.tapestry5.ioc.util.BodyBuilder; +import org.apache.tapestry5.model.MutableComponentModel; +import org.apache.tapestry5.services.ClassTransformation; +import org.apache.tapestry5.services.ComponentClassTransformWorker; +import org.apache.tapestry5.services.TransformMethodSignature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import net.aramzamzam.commons.components.utils.select.GenericSelectionModel; +import net.aramzamzam.commons.components.utils.select.GenericValueEncoder; +import net.aramzamzam.commons.components.utils.select.InjectSelectionModel; + +public class InjectSelectionModelWorker implements ComponentClassTransformWorker { + + final private Logger _logger = LoggerFactory.getLogger(InjectSelectionModelWorker.class); + final private PropertyAccess _access; + + public InjectSelectionModelWorker(PropertyAccess propertyAccess) { + _access = propertyAccess; + } + + public void transform(ClassTransformation transformation, MutableComponentModel componentModel) { + + for (String fieldName : transformation.findFieldsWithAnnotation(InjectSelectionModel.class)) { + InjectSelectionModel annotation = transformation.getFieldAnnotation(fieldName, InjectSelectionModel.class); + + if (_logger.isDebugEnabled()){ + _logger.debug("Creating selection model getter method for the field " + fieldName); + } + String accessActualName = transformation.addField(Modifier.PRIVATE + , "org.apache.tapestry5.ioc.services.PropertyAccess", "_access"); + transformation.injectField(accessActualName, _access); + + addGetSelectionModelMethod(transformation, fieldName, annotation.labelField() + , accessActualName); + + if (_logger.isDebugEnabled()){ + _logger.debug("Creating value encoder getter method for the field " + fieldName); + } + + addGetValueEncoderMethod(transformation, fieldName, annotation.idField(), accessActualName); + + } + + } + + private void addGetSelectionModelMethod(ClassTransformation transformation, String fieldName, String labelField, String accessName) { + + String methodName = "get" + InternalUtils.capitalize(InternalUtils + .stripMemberName(fieldName)) + "SelectionModel"; + + String modelQualifiedName = (GenericSelectionModel.class).getName(); + TransformMethodSignature sig = + new TransformMethodSignature(Modifier.PUBLIC, modelQualifiedName + , methodName, null, null); + + BodyBuilder builder = new BodyBuilder(); + builder.begin(); + builder.addln("return new " + modelQualifiedName + "(" + fieldName + + ", \"" + labelField +"\", " + accessName + ");"); + builder.end(); + + transformation.addMethod(sig, builder.toString()); + + } + + private void addGetValueEncoderMethod(ClassTransformation transformation, String fieldName, String idField, String accessName) { + + String methodName = "get" + InternalUtils.capitalize(InternalUtils + .stripMemberName(fieldName)) + "ValueEncoder"; + + String encoderQualifiedName = (GenericValueEncoder.class).getName(); + TransformMethodSignature sig = + new TransformMethodSignature(Modifier.PUBLIC, encoderQualifiedName + , methodName, null, null); + + BodyBuilder builder = new BodyBuilder(); + builder.begin(); + String line = "return new " + encoderQualifiedName + "(" + fieldName + ",\"" + idField +"\", " + accessName + ");"; + builder.addln(line); + builder.end(); + + transformation.addMethod(sig, builder.toString()); + + } + +} \ No newline at end of file diff --git a/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/GenericSelectionModel.java b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/GenericSelectionModel.java new file mode 100644 index 0000000..01ccbae --- /dev/null +++ b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/GenericSelectionModel.java @@ -0,0 +1,57 @@ +package net.aramzamzam.commons.components.utils.select; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tapestry5.OptionGroupModel; +import org.apache.tapestry5.OptionModel; +import org.apache.tapestry5.internal.OptionModelImpl; +import org.apache.tapestry5.ioc.services.PropertyAccess; +import org.apache.tapestry5.ioc.services.PropertyAdapter; +import org.apache.tapestry5.util.AbstractSelectModel; +/** + * http://wiki.apache.org/tapestry/Tapestry5AnotherSelectWithObjects + * @author solomenchuk + * + * @param + */ +public class GenericSelectionModel extends AbstractSelectModel { + + private PropertyAdapter labelFieldAdapter = null; + + private List list; + + public GenericSelectionModel(List list, String labelField, PropertyAccess access) { + if(list == null) list = new ArrayList(); + if(labelField != null && !labelField.equalsIgnoreCase("null")){ + if(list.size() > 0){ + this.labelFieldAdapter = access.getAdapter(list.get(0).getClass()).getPropertyAdapter(labelField); + } + } + this.list = list; + } + + public List getOptionGroups() { + return null; + } + + public List getOptions() { + List optionModelList = new ArrayList(); + for (T obj : list) { + if (labelFieldAdapter == null) { + optionModelList.add(new OptionModelImpl(nvl(obj) + "", obj)); + } else { + optionModelList.add(new OptionModelImpl(nvl(labelFieldAdapter.get(obj)), obj)); + } + } + return optionModelList; + } + + private String nvl(Object o) { + if (o == null) + return ""; + else + return o.toString(); + } + +} \ No newline at end of file diff --git a/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/GenericValueEncoder.java b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/GenericValueEncoder.java new file mode 100644 index 0000000..0bcf4a3 --- /dev/null +++ b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/GenericValueEncoder.java @@ -0,0 +1,52 @@ +package net.aramzamzam.commons.components.utils.select; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tapestry5.ValueEncoder; +import org.apache.tapestry5.ioc.services.PropertyAccess; +import org.apache.tapestry5.ioc.services.PropertyAdapter; + +public class GenericValueEncoder implements ValueEncoder { + + private PropertyAdapter idFieldAdapter = null; + private List list; + + public GenericValueEncoder(List list, String idField, PropertyAccess access) { + if(list == null) list = new ArrayList(); + if (idField != null && !idField.equalsIgnoreCase("null")){ + if(list.size() > 0){ + this.idFieldAdapter = access.getAdapter(list.get(0).getClass()).getPropertyAdapter(idField); + } + } + this.list = list; + } + + public String toClient(T obj) { + if (idFieldAdapter == null) { + return nvl(obj); + } else { + return nvl(idFieldAdapter.get(obj)); + } + } + + public T toValue(String string) { + if (idFieldAdapter == null) { + for (T obj : list) { + if (nvl(obj).equals(string)) return obj; + } + } else { + for (T obj : list) { + if (nvl(idFieldAdapter.get(obj)).equals(string)) return obj; + } + } + return null; + } + + private String nvl(Object o) { + if (o == null) + return ""; + else + return o.toString(); + } +} \ No newline at end of file diff --git a/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/InjectSelectionModel.java b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/InjectSelectionModel.java new file mode 100644 index 0000000..96745a6 --- /dev/null +++ b/aramzamzam-commons/commons-components/src/main/java/net/aramzamzam/commons/components/utils/select/InjectSelectionModel.java @@ -0,0 +1,33 @@ +package net.aramzamzam.commons.components.utils.select; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * В шаблоне используйте Select компонент с параметрами: getSelectionModel() and getValueEncoder(). + * например: + * java + * @InjectSelectionModel(labelField = "name", idField = "code") + * private List _list = new ArrayList(); + * private Bank _item = null + * + * tml + *