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);
}
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());
}
}
}
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;
}
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);
}
}
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));
}
Aggregations