I'd like to add an "enum" basetype and "enum" convertor to Woody. Those would work with types implementing Joshua Bloch's "typesafe enum" pattern [1].

The reason for this is as follows: when representing business entities with Java objects, I always try to assign a significant type to fields that can assume a value taken from a list, e.g.

package com.example;

public class Person {
  private Sex sex;
  ...
}

public class Sex {
  public static final Sex MALE = new Sex("M");
  public static final Sex FEMALE = new Sex("F");
  private String code;

  private Sex(String code) { this.code = code; }
  public String toString() {
    return code; // Will probably have some i18n support here
  }
}

...

person.setSex(Sex.FEMALE);

...

Currently, in Woody we cannot directly bind form fields to objects of type "Sex" and it would be cumbersome to add a datatype and convertor for every possible enum (there could be many in even a small application) in cocoon.xconf. So we have to resort to manually converting to and from enums and strings (or integers).

I am thinking about adding something that might allow to specify:

<wd:field id="sex">
  <wd:datatype base="enum">
    <wd:convertor type="enum">
      <wd:enum>com.example.Sex</wd:enum>
    </wd:convertor>
  </wd:datatype>
</wd:field>

Maybe we could also generate a selection list from the enum type.

One possible problem could be that the enum type must implement an interface providing methods for converting to/from strings or integers. This could be impossible or problematic when dealing with existing classes. However, I haven't really thought about the implementation, yet.

WDYT?

Ugo

[1]: http://developer.java.sun.com/developer/Books/shiftintojava/page1.html#replaceenums

--
Ugo Cei - Consorzio di Bioingegneria e Informatica Medica
P.le Volontari del Sangue, 2 - 27100 Pavia - Italy
Phone: +39.0382.525100 - E-mail: [EMAIL PROTECTED]



Reply via email to