use of org.onebusaway.gtfs.serialization.GtfsReaderContext in project onebusaway-gtfs-modules by OneBusAway.
the class DeferredValueConverter method convertValue.
/**
* Converts the specified value as appropriate such that the resulting value
* can be assigned to the specified property of the specified bean.
*/
public Object convertValue(BeanWrapper targetBean, String targetPropertyName, Object value) {
if (value == null) {
return null;
}
Class<?> expectedValueType = targetBean.getPropertyType(targetPropertyName);
Class<?> actualValueType = value.getClass();
/**
* When we introspect the "id" property of an IdentityBean instance, the
* return type is always Serializable, when the actual type is String or
* AgencyAndId. This causes trouble with the "isAssignableFrom" check below,
* so we do a first check here.
*/
Object parentObject = targetBean.getWrappedInstance(Object.class);
if (parentObject instanceof IdentityBean && targetPropertyName.equals("id")) {
Class<?> idType = getIdentityBeanIdType(parentObject);
if (idType == AgencyAndId.class && actualValueType == String.class) {
return _support.resolveAgencyAndId(targetBean, targetPropertyName, (String) value);
}
}
if (expectedValueType.isAssignableFrom(actualValueType)) {
return value;
}
if (isPrimitiveAssignable(expectedValueType, actualValueType)) {
return value;
}
if (actualValueType == String.class) {
String stringValue = (String) value;
if (AgencyAndId.class.isAssignableFrom(expectedValueType)) {
return _support.resolveAgencyAndId(targetBean, targetPropertyName, stringValue);
}
if (IdentityBean.class.isAssignableFrom(expectedValueType)) {
Serializable id = stringValue;
if (getIdType(expectedValueType) == AgencyAndId.class) {
GtfsReaderContext context = _support.getReader().getGtfsReaderContext();
String agencyId = context.getAgencyForEntity(expectedValueType, stringValue);
id = new AgencyAndId(agencyId, stringValue);
}
Object entity = _dao.getEntityForId(expectedValueType, id);
if (entity == null) {
throw new IllegalStateException("entity not found: type=" + expectedValueType.getName() + " id=" + id);
}
return entity;
}
Class<?> parentEntityType = parentObject.getClass();
Converter converter = _support.resolveConverter(parentEntityType, targetPropertyName, expectedValueType);
if (converter != null) {
return converter.convert(expectedValueType, value);
}
}
throw new IllegalStateException("no conversion possible from type \"" + actualValueType.getName() + "\" to type \"" + expectedValueType.getName() + "\"");
}
use of org.onebusaway.gtfs.serialization.GtfsReaderContext in project onebusaway-gtfs-modules by OneBusAway.
the class StopTimesFactoryStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
GtfsReaderContext gtfsReaderContext = context.getReader().getGtfsReaderContext();
Trip trip = getTrip(gtfsReaderContext, dao);
List<Stop> stops = getStops(gtfsReaderContext, dao);
int[] times = getTimesForStops(stops);
for (int i = 0; i < stops.size(); ++i) {
StopTime stopTime = new StopTime();
stopTime.setStop(stops.get(i));
stopTime.setStopSequence(i);
stopTime.setArrivalTime(times[i]);
stopTime.setDepartureTime(times[i]);
stopTime.setTrip(trip);
dao.saveEntity(stopTime);
}
}
use of org.onebusaway.gtfs.serialization.GtfsReaderContext in project onebusaway-gtfs-modules by OneBusAway.
the class DeferredValueSupport method resolveAgencyAndId.
/**
* Returns a {@link AgencyAndId} with the specified new id value and the
* appropriate agency id prefix. By default, we use the GTFS reader's default
* agency id. However, if the specified bean+property has an existing
* {@link AgencyAndId} value, we use the agency-id specified there.
*/
public AgencyAndId resolveAgencyAndId(BeanWrapper bean, String propertyName, String newId) {
GtfsReaderContext context = _reader.getGtfsReaderContext();
String agencyId = null;
try {
agencyId = context.getDefaultAgencyId();
} catch (NoDefaultAgencyIdException ndaie) {
agencyId = null;
}
AgencyAndId existingId = (AgencyAndId) bean.getPropertyValue(propertyName);
if (existingId != null) {
agencyId = existingId.getAgencyId();
}
return new AgencyAndId(agencyId, newId);
}
Aggregations