All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onebusaway.gtfs.csv.schema.FlattenFieldMapping Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
/**
 * 
 */
package org.onebusaway.gtfs.csv.schema;

import org.onebusaway.gtfs.csv.CsvEntityContext;

import java.util.Collection;
import java.util.Map;

class FlattenFieldMapping extends AbstractFieldMapping {

  private Class _objFieldType;

  private EntitySchema _schema;

  public FlattenFieldMapping(String csvFieldName, String objFieldName,
      Class objFieldType, boolean required, EntitySchema schema) {
    super(csvFieldName, objFieldName, required);
    _objFieldType = objFieldType;
    _schema = schema;
  }

  public void getCSVFieldNames(Collection names) {
    for (FieldMapping mapping : _schema.getFields())
      mapping.getCSVFieldNames(names);
  }

  public void translateFromCSVToObject(CsvEntityContext context,
      Map csvValues, BeanWrapper object) {

    Object id = getInstance(_objFieldType);
    BeanWrapper wrapper = BeanWrapperFactory.wrap(id);
    for (FieldMapping mapping : _schema.getFields())
      mapping.translateFromCSVToObject(context, csvValues, wrapper);
    object.setPropertyValue(_objFieldName, id);
  }

  public void translateFromObjectToCSV(CsvEntityContext context,
      BeanWrapper object, Map csvValues) {
    Object id = object.getPropertyValue(_objFieldName);
    BeanWrapper wrapper = BeanWrapperFactory.wrap(id);
    for (FieldMapping mapping : _schema.getFields())
      mapping.translateFromObjectToCSV(context, wrapper, csvValues);
  }

  private Object getInstance(Class type) {
    try {
      return type.newInstance();
    } catch (Exception ex) {
      throw new IllegalStateException("error instantiating embedded id class "
          + type, ex);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy