Skip to content

Extender Guide

Bryan Hunt edited this page Apr 5, 2014 · 6 revisions

This guide will cover the various ways MongoEMF can be extended.

This guide has been updated for version 0.8.0

Type Converters

The API to IValueConverter is:

public interface IValueConverter
{
  /**
   * Convert a value from MongoDB to a value used by EMF of the specified type.
   * 
   * An example might be converting a long read from MongoDB into an EDataType of
   * java.util.Calendar.
   * 
   * @param eDataType the EMF type that the value needs to be converted to
   * @param databaseValue the value read from MongoDB
   * @return the value that will be set in the EMF object being built.
   */
  Object convertMongoDBValueToEMFValue(EDataType eDataType, Object databaseValue);

  /**
   * Convert a value from EMF of the specified type to a value stored in MongoDB.
   * 
   * An example might be converting an EDataType of java.util.Calendar to a long
   * that is stored in MongoDB
   * 
   * @param eDataType the EMF type that the value needs to be converted from
   * @param emfValue the value from the EMF object
   * @return the value that will be stored in MongoDB
   */
  Object convertEMFValueToMongoDBValue(EDataType eDataType, Object emfValue);

  /**
   * Determines whether or not this converter can convert a value of a specific type.
   * 
   * @param eDataType the type of the value that needs to be converted
   * @return true if this converter can handle values of the specified type; false otherwise
   */
  boolean isConverterForType(EDataType eDataType);
}

The corresponding API for IConverterService is:

public interface IConverterService
{
  /**
   * Adds the converter and makes it available for consideration when serializing and de-serializing an object.
   * Converters are considered in the order in which they are added with the last one added being first.  The
   * default converter is added by the constructor and will therefore be considered last.  The first converter
   * where isConverterForType() returns true is the one used to convert the value.
   * 
   * @param converter the converter to add
   */
  public void addConverter(IValueConverter converter)
  { ... }

  /**
   * Locates an appropriate converter for a given EDataType
   * 
   * @param eDataType the data type needing conversion
   * @return the converter for the specified data type
   */
  IValueConverter getConverter(EDataType eDataType);

  /**
   * Removes the converter and the converter will no longer be considered during serialization and de-serialization of an object.
   * 
   * @param converter the converter to remove
   */
  public void removeConverter(IValueConverter converter)
  { ... }
}

Here is the implementation of the default converter as an example:

public class DefaultConverter implements IValueConverter
{
  @Override
  public Object convertMongoDBValueToEMFValue(EDataType eDataType, Object databaseValue)
  {
    return EcoreUtil.createFromString(eDataType, (String) databaseValue);
  }

  @Override
  public Object convertEMFValueToMongoDBValue(EDataType eDataType, Object emfValue)
  {
    return EcoreUtil.convertToString(eDataType, emfValue);
  }

  @Override
  public boolean isConverterForType(EDataType eDataType)
  {
    return true;
  }
}
Clone this wiki locally