use of org.onebusaway.csv_entities.schema.BeanWrapper in project onebusaway-gtfs-modules by OneBusAway.
the class DeferredValueSupportTest method testResolveAgencyAndId_ExistingAgencyId.
@Test
public void testResolveAgencyAndId_ExistingAgencyId() {
Stop stop = new Stop();
stop.setId(new AgencyAndId("a1", "2"));
BeanWrapper bean = BeanWrapperFactory.wrap(stop);
AgencyAndId id = _support.resolveAgencyAndId(bean, "id", "1");
assertEquals(new AgencyAndId("a1", "1"), id);
}
use of org.onebusaway.csv_entities.schema.BeanWrapper 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));
}
use of org.onebusaway.csv_entities.schema.BeanWrapper 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));
}
use of org.onebusaway.csv_entities.schema.BeanWrapper 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.BeanWrapper in project onebusaway-gtfs-modules by OneBusAway.
the class SimpleModificationStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao, Object entity) {
BeanWrapper wrapper = BeanWrapperFactory.wrap(entity);
for (Map.Entry<String, ValueSetter> entry : _propertyUpdates.entrySet()) {
String propertyName = entry.getKey();
ValueSetter setter = entry.getValue();
setter.setValue(wrapper, propertyName);
}
}
Aggregations