Search in sources :

Example 1 with EntityTransformStrategy

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);
                }
            }
        }
    }
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) EntityTransformStrategy(org.onebusaway.gtfs_transformer.services.EntityTransformStrategy) ArrayList(java.util.ArrayList) IdKeyMatch(org.onebusaway.gtfs_transformer.collections.IdKeyMatch)

Example 2 with EntityTransformStrategy

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);
    }
}
Also used : EntityTransformStrategy(org.onebusaway.gtfs_transformer.services.EntityTransformStrategy) GtfsEntityTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsEntityTransformStrategy) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) TransformSpecificationException(org.onebusaway.gtfs_transformer.TransformSpecificationException) Pair(org.onebusaway.collections.tuple.Pair)

Example 3 with EntityTransformStrategy

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());
}
Also used : EntityTransformStrategy(org.onebusaway.gtfs_transformer.services.EntityTransformStrategy) MatchAndTransform(org.onebusaway.gtfs_transformer.factory.EntitiesTransformStrategy.MatchAndTransform) GtfsTransformStrategy(org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy) EntityMatch(org.onebusaway.gtfs_transformer.match.EntityMatch) Route(org.onebusaway.gtfs.model.Route) Test(org.junit.Test)

Aggregations

EntityTransformStrategy (org.onebusaway.gtfs_transformer.services.EntityTransformStrategy)3 ArrayList (java.util.ArrayList)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1 Pair (org.onebusaway.collections.tuple.Pair)1 Route (org.onebusaway.gtfs.model.Route)1 TransformSpecificationException (org.onebusaway.gtfs_transformer.TransformSpecificationException)1 IdKeyMatch (org.onebusaway.gtfs_transformer.collections.IdKeyMatch)1 MatchAndTransform (org.onebusaway.gtfs_transformer.factory.EntitiesTransformStrategy.MatchAndTransform)1 EntityMatch (org.onebusaway.gtfs_transformer.match.EntityMatch)1 TypedEntityMatch (org.onebusaway.gtfs_transformer.match.TypedEntityMatch)1 GtfsEntityTransformStrategy (org.onebusaway.gtfs_transformer.services.GtfsEntityTransformStrategy)1 GtfsTransformStrategy (org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy)1