Search in sources :

Example 1 with Pair

use of org.onebusaway.collections.tuple.Pair in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method getEntityPropertiesAndStringReplacementsFromJsonObject.

@SuppressWarnings("unchecked")
private Map<String, Pair<String>> getEntityPropertiesAndStringReplacementsFromJsonObject(Class<?> entityType, JSONObject obj) throws JSONException {
    Map<String, Pair<String>> map = new HashMap<String, Pair<String>>();
    for (Iterator<String> it = obj.keys(); it.hasNext(); ) {
        String property = it.next();
        JSONObject pairs = obj.getJSONObject(property);
        String from = (String) pairs.keys().next();
        String to = pairs.getString(from);
        Pair<String> pair = Tuples.pair(from, to);
        map.put(property, pair);
    }
    return map;
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Pair(org.onebusaway.collections.tuple.Pair)

Example 2 with Pair

use of org.onebusaway.collections.tuple.Pair in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method handleUpdateOperation.

private void handleUpdateOperation(String line, JSONObject json) throws JSONException, TransformSpecificationException {
    EntitiesTransformStrategy strategy = getStrategy(EntitiesTransformStrategy.class);
    TypedEntityMatch match = getMatch(line, json);
    if (json.has("factory")) {
        String factoryType = json.getString("factory");
        try {
            Class<?> clazz = Class.forName(factoryType);
            Object factoryObj = clazz.newInstance();
            if (!(factoryObj instanceof EntityTransformStrategy)) {
                throw new TransformSpecificationException("factory object is not an instance of EntityTransformStrategy: " + clazz.getName(), line);
            }
            strategy.addModification(match, (EntityTransformStrategy) factoryObj);
        } catch (Throwable ex) {
            throw new TransformSpecificationException("error creating factory ModificationStrategy instance", ex, line);
        }
        return;
    }
    if (json.has(ARG_UPDATE)) {
        JSONObject update = json.getJSONObject(ARG_UPDATE);
        EntityTransformStrategy mod = getUpdateEntityTransformStrategy(line, match, update);
        strategy.addModification(match, mod);
    }
    if (json.has("strings")) {
        JSONObject strings = json.getJSONObject("strings");
        Map<String, Pair<String>> replacements = getEntityPropertiesAndStringReplacementsFromJsonObject(match.getType(), strings);
        StringModificationStrategy mod = new StringModificationStrategy(replacements);
        strategy.addModification(match, mod);
    }
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) EntityTransformStrategy(org.onebusaway.gtfs_transformer.services.EntityTransformStrategy) GtfsEntityTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsEntityTransformStrategy) JSONObject(org.json.JSONObject) StringModificationStrategy(org.onebusaway.gtfs_transformer.impl.StringModificationStrategy) JSONObject(org.json.JSONObject) TransformSpecificationException(org.onebusaway.gtfs_transformer.TransformSpecificationException) Pair(org.onebusaway.collections.tuple.Pair)

Example 3 with Pair

use of org.onebusaway.collections.tuple.Pair in project onebusaway-gtfs-modules by OneBusAway.

the class StringModificationStrategy method run.

public void run(TransformContext context, GtfsMutableRelationalDao dao, Object entity) {
    BeanWrapper wrapper = BeanWrapperFactory.wrap(entity);
    for (Map.Entry<String, Pair<String>> entry : _propertyUpdates.entrySet()) {
        String property = entry.getKey();
        Pair<String> value = entry.getValue();
        Object propertyValue = wrapper.getPropertyValue(property);
        if (propertyValue != null) {
            String propertyStringValue = propertyValue.toString();
            propertyStringValue = propertyStringValue.replaceAll(value.getFirst(), value.getSecond());
            wrapper.setPropertyValue(property, propertyStringValue);
        }
    }
}
Also used : BeanWrapper(org.onebusaway.csv_entities.schema.BeanWrapper) Map(java.util.Map) Pair(org.onebusaway.collections.tuple.Pair)

Aggregations

Pair (org.onebusaway.collections.tuple.Pair)3 JSONObject (org.json.JSONObject)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BeanWrapper (org.onebusaway.csv_entities.schema.BeanWrapper)1 TransformSpecificationException (org.onebusaway.gtfs_transformer.TransformSpecificationException)1 StringModificationStrategy (org.onebusaway.gtfs_transformer.impl.StringModificationStrategy)1 TypedEntityMatch (org.onebusaway.gtfs_transformer.match.TypedEntityMatch)1 EntityTransformStrategy (org.onebusaway.gtfs_transformer.services.EntityTransformStrategy)1 GtfsEntityTransformStrategy (org.onebusaway.gtfs_transformer.services.GtfsEntityTransformStrategy)1