Enumeration Mapping

An Enum Mapping is used to comfortable store enumeration values into a database. An Enumeration (short Enum) is an element which can only have a fixed amount of possible values.

To use the Enum Mappings in intelliBO you should provide the following elements:

1) The actual enumeration class

In order to be compatible with Java 1.4 Signsoft intelliBO provides its own enum base class AbstractEnum in package com.signsoft.ibo.mapping.impl . Your enumeration class should be inherited from this class.

Please note, that it is recommended to associate the factory described below with the enumeration. The factory is registered with the static method com.signsoft.ibo.core.services.datastore.AbstractEnum.setFactory which is called with the shared factory instance.

The following shows a possible enumeration implementation:

import com.signsoft.ibo.mapping.impl;

public class Color extends AbstractEnum
{
  public final static Color RED = new Color  (1, "red");
  public final static Color WHITE = new Color (2, "white");
  public final static Color BLUE = new Color (3, "blue");
  public final static Color VIOLET = new Color (4, "violet");

  private Color(int code, String text)
  {
    super (code, text);
  }

  static {
    setFactory(Color.class, ColorFactory.getInstance());
  }
}
2) An enumeration factory

This factory manages enumeration instances and make sure, that only a limited amount of instances is created. The factory class should be inherited from the base class EnumFactory in package com.signsoft.ibo.mapping .

The following shows a possible enumeration factory implementation:

com.signsoft.ibo.mapping.EnumFactory;

public class ColorFactory extends EnumFactory
{
  private ColorFactory() {}

  private static ColorFactory shared = null;

  public static ColorFactory getInstance ()
  {
    if (shared == null)
    {
      shared = new ColorFactory();

      shared.register (Color.RED);
      shared.register (Color.WHITE);
      shared.register (Color.BLUE);
      shared.register (Color.VIOLET);
    }
    return shared;
  }

  public Color getObject(int id, Color def)
  {
    return (Color)getObjectFor(id, def);
  }

  public Color getObject(int id)
  {
    return (Color)getObjectFor(id, Color.WHITE);
  }

  public Color getDefaultObject()
  {
    return Color.WHITE;
  }

  protected Identifiable internalGetDefaultObject()
  {
    return getDefaultObject();
  }
}
The enumeration can be used in modelling your business objects, e.g. like the following:
public class Person
{
  private int id;
  private Color favoriteColor;

  public int getId ()
  {
    return id;
  }
  public Color getFavoriteColor ()
  {
    return favoriteColor;
  }
  public void setId (int value)
  {
    id = value;
  }
  public void setFavoriteColor (Color value)
  {
    favoriteColor = value;
  }
}
The mapping of enumerations in Signsoft intelliBO is related to that of a direct field mapping. The mapping name for using an enumeration mapping is " enum ".

Please note: There is no support for exchange operations (see section "Exchange Operations") for enumeration fields. You can specify the enumeration factory in the enumfactory-name vendor extension instead. You may also specify the normal other elements, like field name, SQL type and so on. The following shows a possible mapping of the above implemented "Color" enumeration:

<field name="favoriteColor" persistence-modifier="persistent">
  <extension key="jdbc" vendor-name="ssibo">
    <extension key="mapping" value="enum" vendor-name="ssibo">
      <extension key="field-name" value="FFAVCOLOR" vendor-name="ssibo"/>
      <extension key="sql-type" value="INTEGER" vendor-name="ssibo"/>
      <extension key="enumfactory-name" value="com.signsoft.test.enums.ColorFactory" vendor-name="ssibo"/>
    </extension>
  </extension>
</field>
In your application, you may use an enumeration field like any other direct mapping, e.g. like the following:
   Person p = new Person();
      p.setFavoriteColor (Color.RED);

      pm.makePersistent(p);

Enumerations in JDOQLQueries

You may also use Enumeration values in JDOQL queries (queries are described in chapter "Queries"). Here you have the following possibilities in specifying enumeration values:

1. use the mapped field name and supply the enum value as parameter, like:

PersistenceManager pm;
Query q = pm.newQuery (Person.class, "favoriteColor == c");
q.declareParameters ("Color c");
Object o = q.execute (Color.RED);
2. use the mapped field name and the numerical value as explicit code, as in:
PersistenceManager pm;
Query q = pm.newQuery (Person.class, "FFAVCOLOR == 3");
Object o = q.execute ();
3. use the enumeration explicitly specified in Java syntax:
PersistenceManager pm;
Query q = pm.newQuery (Person.class, "favoriteColor == com.signsoft.ibo.sample.Color.BLUE");
Object o = q.execute ();
4. use the numerical key of the enumeration as a literal value, with a cast expression to specify the enumeration class to use. In order to be able to use this syntax, the factory associated to the enumeration class has to be registered (see above). The following shows an example:
PersistenceManager pm;
Query q = pm.newQuery (Person.class, "favoriteColor == (com.signsoft.ibo.sample.Color) 1");
Object o = q.execute ();
5. use the mapped field name and the numerical value as a parameter, as in:
PersistenceManager pm;
Query q = pm.newQuery (Person.class, "FFAVCOLOR == c");
q.declareParameters ("int c");
Object o = q.execute (Color.BLUE.getId());