Search in sources :

Example 1 with FieldMapping

use of org.onebusaway.csv_entities.schema.FieldMapping in project onebusaway-gtfs-modules by OneBusAway.

the class ServiceDateFieldMappingFactoryTest method test.

@Test
public void test() {
    ServiceDateFieldMappingFactory factory = new ServiceDateFieldMappingFactory();
    DefaultEntitySchemaFactory schemaFactory = new DefaultEntitySchemaFactory();
    String propName = "date";
    FieldMapping mapping = factory.createFieldMapping(schemaFactory, Dummy.class, propName, propName, ServiceDate.class, true);
    CsvEntityContext context = new CsvEntityContextImpl();
    Map<String, Object> csvValues = new HashMap<String, Object>();
    csvValues.put(propName, "20100212");
    Dummy obj = new Dummy();
    BeanWrapper wrapped = BeanWrapperFactory.wrap(obj);
    mapping.translateFromCSVToObject(context, csvValues, wrapped);
    assertEquals(new ServiceDate(2010, 2, 12), obj.getDate());
    csvValues.clear();
    mapping.translateFromObjectToCSV(context, wrapped, csvValues);
    assertEquals("20100212", csvValues.get(propName));
}
Also used : CsvEntityContextImpl(org.onebusaway.csv_entities.CsvEntityContextImpl) BeanWrapper(org.onebusaway.csv_entities.schema.BeanWrapper) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) HashMap(java.util.HashMap) FieldMapping(org.onebusaway.csv_entities.schema.FieldMapping) CsvEntityContext(org.onebusaway.csv_entities.CsvEntityContext) DefaultEntitySchemaFactory(org.onebusaway.csv_entities.schema.DefaultEntitySchemaFactory) Test(org.junit.Test)

Example 2 with FieldMapping

use of org.onebusaway.csv_entities.schema.FieldMapping in project onebusaway-gtfs-modules by OneBusAway.

the class StopTimeFieldMappingFactoryTest method test.

@Test
public void test() {
    StopTimeFieldMappingFactory factory = new StopTimeFieldMappingFactory();
    DefaultEntitySchemaFactory schemaFactory = new DefaultEntitySchemaFactory();
    String propName = "time";
    FieldMapping mapping = factory.createFieldMapping(schemaFactory, Dummy.class, propName, propName, Integer.class, true);
    CsvEntityContext context = new CsvEntityContextImpl();
    Map<String, Object> csvValues = new HashMap<String, Object>();
    csvValues.put(propName, "1234:23:32");
    Dummy obj = new Dummy();
    BeanWrapper wrapped = BeanWrapperFactory.wrap(obj);
    mapping.translateFromCSVToObject(context, csvValues, wrapped);
    assertEquals(new Integer(1234 * 60 * 60 + 23 * 60 + 32), obj.getTime());
    csvValues.clear();
    mapping.translateFromObjectToCSV(context, wrapped, csvValues);
    assertEquals("1234:23:32", csvValues.get(propName));
}
Also used : CsvEntityContextImpl(org.onebusaway.csv_entities.CsvEntityContextImpl) BeanWrapper(org.onebusaway.csv_entities.schema.BeanWrapper) HashMap(java.util.HashMap) FieldMapping(org.onebusaway.csv_entities.schema.FieldMapping) CsvEntityContext(org.onebusaway.csv_entities.CsvEntityContext) DefaultEntitySchemaFactory(org.onebusaway.csv_entities.schema.DefaultEntitySchemaFactory) StopTimeFieldMappingFactory.getSecondsAsString(org.onebusaway.gtfs.serialization.mappings.StopTimeFieldMappingFactory.getSecondsAsString) Test(org.junit.Test)

Example 3 with FieldMapping

use of org.onebusaway.csv_entities.schema.FieldMapping in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method setObjectPropertiesFromJsonUsingCsvFields.

private void setObjectPropertiesFromJsonUsingCsvFields(Object object, JSONObject json, String line) throws JSONException, TransformSpecificationMissingArgumentException {
    EntitySchemaFactory entitySchemaFactory = _transformer.getReader().getEntitySchemaFactory();
    EntitySchema schema = entitySchemaFactory.getSchema(object.getClass());
    BeanWrapper wrapped = BeanWrapperFactory.wrap(object);
    Map<String, Object> values = new HashMap<String, Object>();
    for (Iterator<?> it = json.keys(); it.hasNext(); ) {
        String key = (String) it.next();
        Object v = json.get(key);
        if (v instanceof JSONArray) {
            JSONArray array = (JSONArray) v;
            List<Object> asList = new ArrayList<Object>();
            for (int i = 0; i < array.length(); ++i) {
                asList.add(array.get(i));
            }
            v = asList;
        }
        values.put(key, v);
    }
    CsvEntityContext context = new CsvEntityContextImpl();
    for (FieldMapping mapping : schema.getFields()) {
        try {
            mapping.translateFromCSVToObject(context, values, wrapped);
        } catch (MissingRequiredFieldException ex) {
            String verboseMessage = "line=" + line + ", context=" + context + ", json=" + json + ", object=" + object;
            _log.error("missing required field; details:" + verboseMessage);
            throw new TransformSpecificationMissingArgumentException(verboseMessage, ex.getFieldName());
        }
    }
}
Also used : MissingRequiredFieldException(org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException) HashMap(java.util.HashMap) FieldMapping(org.onebusaway.csv_entities.schema.FieldMapping) SingleFieldMapping(org.onebusaway.csv_entities.schema.SingleFieldMapping) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) CsvEntityContextImpl(org.onebusaway.csv_entities.CsvEntityContextImpl) BeanWrapper(org.onebusaway.csv_entities.schema.BeanWrapper) EntitySchemaFactory(org.onebusaway.csv_entities.schema.EntitySchemaFactory) CsvEntityContext(org.onebusaway.csv_entities.CsvEntityContext) JSONObject(org.json.JSONObject) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException) EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema)

Example 4 with FieldMapping

use of org.onebusaway.csv_entities.schema.FieldMapping in project onebusaway-gtfs-modules by OneBusAway.

the class EntitySchemaCache method addEntitySchema.

public void addEntitySchema(EntitySchema schema) {
    _entitySchemasByEntityType.put(schema.getEntityClass(), schema);
    if (schema.getFilename() != null) {
        _entitySchemasByFileName.put(schema.getFilename(), schema);
    }
    for (FieldMapping mapping : schema.getFields()) {
        if (mapping instanceof SingleFieldMapping) {
            SingleFieldMapping single = (SingleFieldMapping) mapping;
            putMappingForEntityTypeAndName(_mappingsByTypeAndCsvFieldName, schema.getEntityClass(), single.getCsvFieldName(), single);
            putMappingForEntityTypeAndName(_mappingsByTypeAndObjectFieldName, schema.getEntityClass(), single.getObjFieldName(), single);
        }
    }
}
Also used : FieldMapping(org.onebusaway.csv_entities.schema.FieldMapping) SingleFieldMapping(org.onebusaway.csv_entities.schema.SingleFieldMapping) SingleFieldMapping(org.onebusaway.csv_entities.schema.SingleFieldMapping)

Example 5 with FieldMapping

use of org.onebusaway.csv_entities.schema.FieldMapping in project onebusaway-gtfs-modules by OneBusAway.

the class EntitySchemaCacheTest method test.

@Test
public void test() {
    EntitySchemaCache cache = new EntitySchemaCache();
    EntitySchema schema = new EntitySchema(Route.class, "routes.txt", true);
    FieldMapping fieldMapping = new DefaultFieldMapping(Route.class, "route_short_name", "shortName", String.class, false);
    schema.addField(fieldMapping);
    cache.addEntitySchema(schema);
    assertSame(cache.getSchemaForFileName("routes.txt"), schema);
    assertSame(cache.getSchemaForEntityType(Route.class), schema);
    assertSame(cache.getFieldMappingForCsvFieldName(Route.class, "route_short_name"), fieldMapping);
    assertSame(cache.getFieldMappingForObjectFieldName(Route.class, "shortName"), fieldMapping);
}
Also used : DefaultFieldMapping(org.onebusaway.csv_entities.schema.DefaultFieldMapping) FieldMapping(org.onebusaway.csv_entities.schema.FieldMapping) EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema) DefaultFieldMapping(org.onebusaway.csv_entities.schema.DefaultFieldMapping) Route(org.onebusaway.gtfs.model.Route) Test(org.junit.Test)

Aggregations

FieldMapping (org.onebusaway.csv_entities.schema.FieldMapping)5 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 CsvEntityContext (org.onebusaway.csv_entities.CsvEntityContext)3 CsvEntityContextImpl (org.onebusaway.csv_entities.CsvEntityContextImpl)3 BeanWrapper (org.onebusaway.csv_entities.schema.BeanWrapper)3 DefaultEntitySchemaFactory (org.onebusaway.csv_entities.schema.DefaultEntitySchemaFactory)2 EntitySchema (org.onebusaway.csv_entities.schema.EntitySchema)2 SingleFieldMapping (org.onebusaway.csv_entities.schema.SingleFieldMapping)2 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 MissingRequiredFieldException (org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException)1 DefaultFieldMapping (org.onebusaway.csv_entities.schema.DefaultFieldMapping)1 EntitySchemaFactory (org.onebusaway.csv_entities.schema.EntitySchemaFactory)1 Route (org.onebusaway.gtfs.model.Route)1 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)1 StopTimeFieldMappingFactory.getSecondsAsString (org.onebusaway.gtfs.serialization.mappings.StopTimeFieldMappingFactory.getSecondsAsString)1 TransformSpecificationMissingArgumentException (org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException)1