Search in sources :

Example 1 with GtfsReaderContext

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() + "\"");
}
Also used : Serializable(java.io.Serializable) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Converter(org.apache.commons.beanutils.Converter) GtfsReaderContext(org.onebusaway.gtfs.serialization.GtfsReaderContext) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 2 with GtfsReaderContext

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);
    }
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) Stop(org.onebusaway.gtfs.model.Stop) GtfsReaderContext(org.onebusaway.gtfs.serialization.GtfsReaderContext) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 3 with GtfsReaderContext

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);
}
Also used : NoDefaultAgencyIdException(org.onebusaway.gtfs.serialization.NoDefaultAgencyIdException) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) GtfsReaderContext(org.onebusaway.gtfs.serialization.GtfsReaderContext)

Aggregations

GtfsReaderContext (org.onebusaway.gtfs.serialization.GtfsReaderContext)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 Serializable (java.io.Serializable)1 Converter (org.apache.commons.beanutils.Converter)1 IdentityBean (org.onebusaway.gtfs.model.IdentityBean)1 Stop (org.onebusaway.gtfs.model.Stop)1 StopTime (org.onebusaway.gtfs.model.StopTime)1 Trip (org.onebusaway.gtfs.model.Trip)1 NoDefaultAgencyIdException (org.onebusaway.gtfs.serialization.NoDefaultAgencyIdException)1