Search in sources :

Example 1 with EntitySchema

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

the class DeferredValueSupportTest method testResolveConverter_FieldMappingConverter.

@Test
public void testResolveConverter_FieldMappingConverter() {
    EntitySchema schema = new EntitySchema(Object.class, "object.txt", false);
    FieldMappingAndConverter field = mock(FieldMappingAndConverter.class);
    when(field.getCsvFieldName()).thenReturn("xyz");
    schema.addField(field);
    _schemaCache.addEntitySchema(schema);
    Converter converter = _support.resolveConverter(Object.class, "xyz", String.class);
    assertSame(field, converter);
}
Also used : Converter(org.apache.commons.beanutils.Converter) EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema) Test(org.junit.Test)

Example 2 with EntitySchema

use of org.onebusaway.csv_entities.schema.EntitySchema 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 3 with EntitySchema

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

the class TransformFactory method getEntityClassFromJsonSpec.

private Class<?> getEntityClassFromJsonSpec(String line, JSONObject objectSpec) throws JSONException, TransformSpecificationException, TransformSpecificationMissingArgumentException {
    if (objectSpec.has(ARG_FILE)) {
        String fileName = objectSpec.getString(ARG_FILE);
        EntitySchema schema = _schemaCache.getSchemaForFileName(fileName);
        if (schema == null) {
            throw new TransformSpecificationException("unknown file type: " + fileName, line);
        }
        return schema.getEntityClass();
    } else if (objectSpec.has(ARG_CLASS)) {
        return getEntityTypeForName(line, objectSpec.getString(ARG_CLASS));
    }
    return null;
}
Also used : EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema) TransformSpecificationException(org.onebusaway.gtfs_transformer.TransformSpecificationException)

Example 4 with EntitySchema

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

the class EntitySchemaCache method addEntitySchemasFromGtfsReader.

public void addEntitySchemasFromGtfsReader(GtfsReader reader) {
    EntitySchemaFactory factory = reader.getEntitySchemaFactory();
    for (Class<?> entityType : reader.getEntityClasses()) {
        EntitySchema schema = factory.getSchema(entityType);
        addEntitySchema(schema);
    }
}
Also used : EntitySchemaFactory(org.onebusaway.csv_entities.schema.EntitySchemaFactory) EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema)

Example 5 with EntitySchema

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

the class PropertyMethodResolverImplTest method testUseCsvFieldMappings.

@Test
public void testUseCsvFieldMappings() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    DefaultEntitySchemaFactory factory = GtfsEntitySchemaFactory.createEntitySchemaFactory();
    EntitySchema entitySchema = factory.getSchema(Route.class);
    _schemaCache.addEntitySchema(entitySchema);
    PropertyMethod method = _resolver.getPropertyMethod(Route.class, "route_id");
    Route route = new Route();
    AgencyAndId id = new AgencyAndId("1", "10");
    route.setId(id);
    assertSame(id, method.invoke(route));
}
Also used : PropertyMethod(org.onebusaway.collections.beans.PropertyMethod) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) DefaultEntitySchemaFactory(org.onebusaway.csv_entities.schema.DefaultEntitySchemaFactory) EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema) Route(org.onebusaway.gtfs.model.Route) Test(org.junit.Test)

Aggregations

EntitySchema (org.onebusaway.csv_entities.schema.EntitySchema)6 Test (org.junit.Test)3 EntitySchemaFactory (org.onebusaway.csv_entities.schema.EntitySchemaFactory)2 FieldMapping (org.onebusaway.csv_entities.schema.FieldMapping)2 Route (org.onebusaway.gtfs.model.Route)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Converter (org.apache.commons.beanutils.Converter)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 PropertyMethod (org.onebusaway.collections.beans.PropertyMethod)1 CsvEntityContext (org.onebusaway.csv_entities.CsvEntityContext)1 CsvEntityContextImpl (org.onebusaway.csv_entities.CsvEntityContextImpl)1 MissingRequiredFieldException (org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException)1 BeanWrapper (org.onebusaway.csv_entities.schema.BeanWrapper)1 DefaultEntitySchemaFactory (org.onebusaway.csv_entities.schema.DefaultEntitySchemaFactory)1 DefaultFieldMapping (org.onebusaway.csv_entities.schema.DefaultFieldMapping)1 SingleFieldMapping (org.onebusaway.csv_entities.schema.SingleFieldMapping)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 TransformSpecificationException (org.onebusaway.gtfs_transformer.TransformSpecificationException)1