Search in sources :

Example 1 with PropertyTranslation

use of org.onebusaway.gtfs.model.translation.PropertyTranslation in project onebusaway-gtfs-modules by OneBusAway.

the class TranslationServiceDataFactoryImpl method getTranslationServiceData.

@Override
public TranslationServiceData getTranslationServiceData() {
    if (_dao.getAllFeedInfos().isEmpty()) {
        _log.warn("No feed_info present, there will be no translations available.");
        return null;
    }
    TranslationServiceData data = new TranslationServiceData();
    FeedInfo feedInfo = _dao.getAllFeedInfos().iterator().next();
    if (feedInfo.getDefaultLang() != null) {
        data.setFeedLanguage(feedInfo.getDefaultLang());
    } else {
        data.setFeedLanguage(feedInfo.getLang());
    }
    for (Translation translation : _dao.getAllTranslations()) {
        Class<?> type = getEntityTypeForTableName(translation.getTableName());
        if (type == null) {
            _log.error("No entity type for table_name {}, skipping.", translation.getTableName());
            continue;
        }
        String propertyName = getPropertyNameByClassAndCsvName(type, translation.getFieldName());
        if (propertyName == null) {
            _log.error("No property for field_name {}, skipping.", translation.getFieldName());
            continue;
        }
        PropertyTranslation propertyTranslation = new PropertyTranslation(propertyName, translation);
        data.putTranslation(type, translation.getLanguage(), propertyTranslation);
    }
    return data;
}
Also used : PropertyTranslation(org.onebusaway.gtfs.model.translation.PropertyTranslation) Translation(org.onebusaway.gtfs.model.Translation) TranslationServiceData(org.onebusaway.gtfs.model.translation.TranslationServiceData) PropertyTranslation(org.onebusaway.gtfs.model.translation.PropertyTranslation) FeedInfo(org.onebusaway.gtfs.model.FeedInfo)

Example 2 with PropertyTranslation

use of org.onebusaway.gtfs.model.translation.PropertyTranslation in project onebusaway-gtfs-modules by OneBusAway.

the class TranslationServiceImpl method getTranslatedEntity.

@Override
public <T> T getTranslatedEntity(String language, Class<T> type, T instance) {
    // initialize the translation map, or if there aren't any translations for this entity type.
    if (_data == null || language.equals(_data.getFeedLanguage())) {
        return instance;
    }
    List<PropertyTranslation> translationsForClass = _data.getTranslationsByTypeAndLanguage(type, language);
    if (translationsForClass == null || translationsForClass.isEmpty()) {
        return instance;
    }
    // Get cloned entity via typical OBA model constructor
    T translatedInstance;
    try {
        translatedInstance = type.getConstructor(type).newInstance(instance);
    } catch (Exception ex) {
        _log.error("Unable to process instance with entity type={} due to: {}", type.getName(), ex.getMessage());
        return instance;
    }
    // Wrap instance, and set translated properties if applicable.
    BeanWrapper wrapper = BeanWrapperFactory.wrap(translatedInstance);
    for (PropertyTranslation translation : translationsForClass) {
        String propertyName = translation.getPropertyName();
        String translationStr = null;
        if (objectIdMatches(translatedInstance, translation.getEntityId(), translation.getEntitySubId())) {
            translationStr = translation.getTranslation();
        } else if (translation.getPropertyValue() != null && translation.getPropertyValue().equals(wrapper.getPropertyValue(propertyName))) {
            translationStr = translation.getTranslation();
        }
        if (translationStr != null) {
            wrapper.setPropertyValue(propertyName, translationStr);
        }
    }
    return wrapper.getWrappedInstance(type);
}
Also used : BeanWrapper(org.onebusaway.csv_entities.schema.BeanWrapper) PropertyTranslation(org.onebusaway.gtfs.model.translation.PropertyTranslation)

Aggregations

PropertyTranslation (org.onebusaway.gtfs.model.translation.PropertyTranslation)2 BeanWrapper (org.onebusaway.csv_entities.schema.BeanWrapper)1 FeedInfo (org.onebusaway.gtfs.model.FeedInfo)1 Translation (org.onebusaway.gtfs.model.Translation)1 TranslationServiceData (org.onebusaway.gtfs.model.translation.TranslationServiceData)1