001: package org.osbl.client;
002:
003: import org.conform.*;
004: import org.conform.wings.editor.StringEditor;
005: import org.osbl.persistence.model.Concrete;
006: import org.osbl.client.wings.form.initializer.*;
007:
008: import java.lang.reflect.Method;
009: import java.util.*;
010:
011: public class ConventionModifier implements Modifier {
012: public static final ValidFromInitializer VALID_FROM_INITIALIZER = new ValidFromInitializer();
013: public static final ValidUntilInitializer VALID_UNTIL_INITIALIZER = new ValidUntilInitializer();
014: public static final CreatedInitializer CREATED_INITIALIZER = new CreatedInitializer();
015: public static final CreatedByInitializer CREATED_BY_INITIALIZER = new CreatedByInitializer();
016:
017: public void modify(final org.conform.BeanMeta beanMeta)
018: throws ModifierException {
019: if (Concrete.class.isAssignableFrom(beanMeta.getType())) {
020: try {
021: Method getter = beanMeta.getType().getMethod(
022: "getGeneral");
023: System.out
024: .println("getter.getGenericReturnType().getClass() = "
025: + getter.getGenericReturnType()
026: .getClass());
027: System.out
028: .println("getter.getReturnType().getClass() = "
029: + getter.getReturnType().getClass());
030: System.out.println("getter.getGenericReturnType() = "
031: + Arrays.asList(getter.getTypeParameters()));
032: } catch (NoSuchMethodException e) {
033: e.printStackTrace();
034: }
035: }
036:
037: boolean fromProperty = false, untilProperty = false;
038: PropertyMeta[] properties = beanMeta.getProperties();
039: for (int i = 0; i < properties.length; i++) {
040: PropertyMeta propertyMeta = properties[i];
041:
042: if ("key".equals(propertyMeta.getName())) {
043: //propertyMeta.setWritable(false);
044: //propertyMeta.setInitializer(VALID_FROM_INITIALIZER);
045: } else if ("validFrom".equals(propertyMeta.getName())) {
046: propertyMeta.setInitializer(VALID_FROM_INITIALIZER);
047: fromProperty = true;
048: } else if ("validUntil".equals(propertyMeta.getName())) {
049: propertyMeta.setInitializer(VALID_UNTIL_INITIALIZER);
050: untilProperty = true;
051: } else if ("created".equals(propertyMeta.getName())) {
052: propertyMeta.setInitializer(CREATED_INITIALIZER);
053: propertyMeta.setWritable(false);
054: } else if ("createdBy".equals(propertyMeta.getName())) {
055: propertyMeta.setInitializer(CREATED_BY_INITIALIZER);
056: propertyMeta.setWritable(false);
057: } else if ("description".equals(propertyMeta.getName())) {
058: propertyMeta.setAttribute(StringEditor.ROWS, 3);
059: } else if ("general".equals(propertyMeta.getName())) {
060: propertyMeta.setApplicable(false);
061: } else if (propertyMeta.getType().isEnum()) {
062: EnumDomainProvider enumDomainProvider = new EnumDomainProvider(
063: propertyMeta.getType());
064: propertyMeta.setFormat(enumDomainProvider);
065: propertyMeta.setDomainProvider(enumDomainProvider);
066: } else if (propertyMeta.getAttribute("hibernate.user.type") != null) {
067: Class type = (Class) propertyMeta
068: .getAttribute("hibernate.user.type");
069: System.out.println("hibernate.user.type = "
070: + type.getName());
071: try {
072: Class.forName(type.getName() + "Enum");
073: NoEnumDomainProvider noEnumDomainProvider = new NoEnumDomainProvider(
074: type);
075: propertyMeta.setFormat(noEnumDomainProvider);
076: propertyMeta
077: .setDomainProvider(noEnumDomainProvider);
078: } catch (ClassNotFoundException e) {
079: // ignore
080: }
081: }
082:
083: if (fromProperty && untilProperty) {
084: beanMeta.addValidator(new BeanValidator() {
085: public Object validate(Object value)
086: throws ValidationException {
087: if (value == null)
088: return value;
089:
090: try {
091: Method fromGetter = beanMeta.getType()
092: .getMethod("getValidFrom");
093: Method untilGetter = beanMeta.getType()
094: .getMethod("getValidUntil");
095: java.util.Date from = (java.util.Date) fromGetter
096: .invoke(value);
097: java.util.Date until = (java.util.Date) untilGetter
098: .invoke(value);
099:
100: if (from != null && until != null
101: && until.before(from))
102: throw new ValidationException(
103: new ValidationException.Message(
104: "validation.dateRange"));
105: } catch (ValidationException e) {
106: throw e;
107: } catch (Exception e) {
108: throw new RuntimeException(e);
109: }
110:
111: return value;
112: }
113:
114: public String[] getAffectedPropertyNames() {
115: return new String[] { "validFrom", "validUntil" };
116: }
117: });
118: }
119:
120: if (Number.class.isAssignableFrom(propertyMeta.getType()))
121: propertyMeta.setAttribute(
122: PropertyMeta.ATTRIBUTE_ALIGNMENT, "right");
123:
124: if (propertyMeta.isWritable()) {
125: if (List.class.equals(propertyMeta.getType()))
126: propertyMeta
127: .setInitializer(new NewInstanceInitializer(
128: ArrayList.class));
129: else if (SortedSet.class.equals(propertyMeta.getType()))
130: propertyMeta
131: .setInitializer(new NewInstanceInitializer(
132: TreeSet.class));
133: else if (Set.class.equals(propertyMeta.getType()))
134: propertyMeta
135: .setInitializer(new NewInstanceInitializer(
136: HashSet.class));
137: else if (Map.class.equals(propertyMeta.getType()))
138: propertyMeta
139: .setInitializer(new NewInstanceInitializer(
140: HashMap.class));
141: }
142: }
143: }
144:
145: protected String getWidth(PropertyMeta propertyMeta) {
146: if ("descriptions".equals(propertyMeta.getName()))
147: return "*";
148: else if ("key".equals(propertyMeta.getName()))
149: return "160";
150: else
151: return "10";//super.getWidth(propertyMeta);
152: }
153: }
|