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;
}
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);
}
}
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);
}
}
}
Aggregations