use of org.onebusaway.gtfs_transformer.services.EntityTransformStrategy in project onebusaway-gtfs-modules by OneBusAway.
the class EntitiesTransformStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
for (MatchAndTransform modification : _modifications) {
TypedEntityMatch match = modification.getMatch();
Class<?> entityType = match.getType();
EntityTransformStrategy transform = modification.getTransform();
if (IdKey.class.isAssignableFrom(entityType)) {
IdKeyMatch keyMatch = (IdKeyMatch) match.getPropertyMatches();
transform.run(context, dao, keyMatch.getKey());
} else {
Collection<Object> entities = new ArrayList<Object>(dao.getAllEntitiesForType(entityType));
for (Object object : entities) {
if (match.isApplicableToObject(object)) {
transform.run(context, dao, object);
}
}
}
}
}
use of org.onebusaway.gtfs_transformer.services.EntityTransformStrategy 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.gtfs_transformer.services.EntityTransformStrategy in project onebusaway-gtfs-modules by OneBusAway.
the class TransformFactoryTest method test.
@Test
public void test() throws IOException, TransformSpecificationException {
_factory.addModificationsFromString("{'op':'remove', 'match':{'class':'Route', 'shortName':'10'}}");
GtfsTransformStrategy transform = _transformer.getLastTransform();
assertEquals(EntitiesTransformStrategy.class, transform.getClass());
EntitiesTransformStrategy strategy = (EntitiesTransformStrategy) transform;
List<MatchAndTransform> transforms = strategy.getModifications();
assertEquals(1, transforms.size());
MatchAndTransform pair = transforms.get(0);
EntityMatch match = pair.getMatch();
Route route = new Route();
assertFalse(match.isApplicableToObject(route));
route.setShortName("10");
assertTrue(match.isApplicableToObject(route));
EntityTransformStrategy entityTransform = pair.getTransform();
assertEquals(RemoveEntityUpdateStrategy.class, entityTransform.getClass());
}
Aggregations