Search in sources :

Example 1 with IdentityBean

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

the class AbstractIdentifiableSingleEntityMergeStrategy method getFuzzyDuplicate.

@SuppressWarnings("unchecked")
@Override
protected IdentityBean<?> getFuzzyDuplicate(GtfsMergeContext context, IdentityBean<?> entity) {
    GtfsMutableRelationalDao targetDao = context.getTarget();
    Collection<T> targets = (Collection<T>) targetDao.getAllEntitiesForType(_entityType);
    if (targets.isEmpty()) {
        return null;
    }
    Max<T> best = new Max<T>();
    for (T target : targets) {
        /**
         * If we just added the target entity as part of the current feed, do not
         * attempt a fuzzy match against it.
         */
        String targetRawId = getRawId(target.getId());
        if (context.isEntityJustAddedWithRawId(targetRawId)) {
            continue;
        }
        double score = _duplicateScoringStrategy.score(context, (T) entity, target);
        best.add(score, target);
    }
    if (best.getMaxValue() < _minElementsDuplicateScoreForAutoDetect) {
        return null;
    }
    return (IdentityBean<?>) best.getMaxElement();
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) Max(org.onebusaway.collections.Max) Collection(java.util.Collection) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 2 with IdentityBean

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

the class DeferredValueConverter method convertValue.

/**
 * Converts the specified value as appropriate such that the resulting value
 * can be assigned to the specified property of the specified bean.
 */
public Object convertValue(BeanWrapper targetBean, String targetPropertyName, Object value) {
    if (value == null) {
        return null;
    }
    Class<?> expectedValueType = targetBean.getPropertyType(targetPropertyName);
    Class<?> actualValueType = value.getClass();
    /**
     * When we introspect the "id" property of an IdentityBean instance, the
     * return type is always Serializable, when the actual type is String or
     * AgencyAndId. This causes trouble with the "isAssignableFrom" check below,
     * so we do a first check here.
     */
    Object parentObject = targetBean.getWrappedInstance(Object.class);
    if (parentObject instanceof IdentityBean && targetPropertyName.equals("id")) {
        Class<?> idType = getIdentityBeanIdType(parentObject);
        if (idType == AgencyAndId.class && actualValueType == String.class) {
            return _support.resolveAgencyAndId(targetBean, targetPropertyName, (String) value);
        }
    }
    if (expectedValueType.isAssignableFrom(actualValueType)) {
        return value;
    }
    if (isPrimitiveAssignable(expectedValueType, actualValueType)) {
        return value;
    }
    if (actualValueType == String.class) {
        String stringValue = (String) value;
        if (AgencyAndId.class.isAssignableFrom(expectedValueType)) {
            return _support.resolveAgencyAndId(targetBean, targetPropertyName, stringValue);
        }
        if (IdentityBean.class.isAssignableFrom(expectedValueType)) {
            Serializable id = stringValue;
            if (getIdType(expectedValueType) == AgencyAndId.class) {
                GtfsReaderContext context = _support.getReader().getGtfsReaderContext();
                String agencyId = context.getAgencyForEntity(expectedValueType, stringValue);
                id = new AgencyAndId(agencyId, stringValue);
            }
            Object entity = _dao.getEntityForId(expectedValueType, id);
            if (entity == null) {
                throw new IllegalStateException("entity not found: type=" + expectedValueType.getName() + " id=" + id);
            }
            return entity;
        }
        Class<?> parentEntityType = parentObject.getClass();
        Converter converter = _support.resolveConverter(parentEntityType, targetPropertyName, expectedValueType);
        if (converter != null) {
            return converter.convert(expectedValueType, value);
        }
    }
    throw new IllegalStateException("no conversion possible from type \"" + actualValueType.getName() + "\" to type \"" + expectedValueType.getName() + "\"");
}
Also used : Serializable(java.io.Serializable) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Converter(org.apache.commons.beanutils.Converter) GtfsReaderContext(org.onebusaway.gtfs.serialization.GtfsReaderContext) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 3 with IdentityBean

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

the class RetainEntitiesTransformStrategy method run.

@SuppressWarnings("unchecked")
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    if (_retentionMatchesByType.isEmpty())
        return;
    EntityRetentionGraph graph = new EntityRetentionGraph(dao);
    graph.setRetainBlocks(_retainBlocks);
    for (Map.Entry<Class<?>, List<EntityRetention>> entry : _retentionMatchesByType.entrySet()) {
        Class<?> entityType = entry.getKey();
        List<EntityRetention> retentions = entry.getValue();
        if (IdKey.class.isAssignableFrom(entityType)) {
            for (EntityRetention retention : retentions) {
                TypedEntityMatch typedMatch = retention.getMatch();
                IdKeyMatch match = (IdKeyMatch) typedMatch.getPropertyMatches();
                graph.retain(match.getKey(), retention.isRetainUp());
            }
        } else {
            Collection<Object> entities = new ArrayList<Object>(dao.getAllEntitiesForType(entityType));
            for (Object object : entities) {
                for (EntityRetention retention : retentions) {
                    EntityMatch match = retention.getMatch();
                    if (match.isApplicableToObject(object))
                        graph.retain(object, retention.isRetainUp());
                }
            }
        }
    }
    for (Class<?> entityClass : GtfsEntitySchemaFactory.getEntityClasses()) {
        List<Object> objectsToRemove = new ArrayList<Object>();
        for (Object entity : dao.getAllEntitiesForType(entityClass)) {
            if (!graph.isRetained(entity))
                objectsToRemove.add(entity);
        }
        for (Object toRemove : objectsToRemove) dao.removeEntity((IdentityBean<Serializable>) toRemove);
    }
}
Also used : TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) ArrayList(java.util.ArrayList) EntityMatch(org.onebusaway.gtfs_transformer.match.EntityMatch) TypedEntityMatch(org.onebusaway.gtfs_transformer.match.TypedEntityMatch) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) IdKeyMatch(org.onebusaway.gtfs_transformer.collections.IdKeyMatch) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 4 with IdentityBean

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

the class DeferredValueMatcher method matches.

public boolean matches(Class<?> parentEntityType, String propertyName, Object value) {
    if (value == null) {
        return _value == null;
    } else if (_value == null) {
        return false;
    }
    if (_resolvedValueSet) {
        return value.equals(_resolvedValue);
    }
    Class<?> expectedValueType = value.getClass();
    Class<?> actualValueType = _value.getClass();
    if (expectedValueType.isAssignableFrom(actualValueType)) {
        if (isRegexObj(_value)) {
            return regexMatch((String) value, ((String) _value));
        }
        return value.equals(_value);
    }
    if (actualValueType == String.class) {
        String actualValue = (String) _value;
        if (expectedValueType == AgencyAndId.class) {
            AgencyAndId expectedId = (AgencyAndId) value;
            if (isRegexObj(_value)) {
                return regexMatch(expectedId.getId(), actualValue);
            }
            return expectedId.getId().equals(actualValue);
        } else if (IdentityBean.class.isAssignableFrom(expectedValueType)) {
            IdentityBean<?> bean = (IdentityBean<?>) value;
            Object expectedId = bean.getId();
            if (expectedId == null) {
                return false;
            }
            if (expectedId instanceof AgencyAndId) {
                AgencyAndId expectedFullId = (AgencyAndId) expectedId;
                return expectedFullId.getId().equals(actualValue);
            } else if (expectedId instanceof String) {
                return expectedId.equals(actualValue);
            }
        } else {
            Converter converter = _support.resolveConverter(parentEntityType, propertyName, expectedValueType);
            if (converter != null) {
                _resolvedValue = converter.convert(expectedValueType, _value);
                _resolvedValueSet = true;
                return value.equals(_resolvedValue);
            } else {
                throw new IllegalStateException("no type conversion from type String to type \"" + expectedValueType.getName() + "\" for value comparison");
            }
        }
    }
    throw new IllegalStateException("no type conversion from type \"" + actualValueType.getName() + "\" to type \"" + expectedValueType.getName() + "\" for value comparison");
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Converter(org.apache.commons.beanutils.Converter) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 5 with IdentityBean

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

the class EntityFieldMappingImpl method translateFromObjectToCSV.

@SuppressWarnings("unchecked")
public void translateFromObjectToCSV(CsvEntityContext context, BeanWrapper object, Map<String, Object> csvValues) {
    IdentityBean<AgencyAndId> entity = (IdentityBean<AgencyAndId>) object.getPropertyValue(_objFieldName);
    if (isOptional() && entity == null)
        return;
    AgencyAndId id = entity.getId();
    csvValues.put(_csvFieldName, id.getId());
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Aggregations

IdentityBean (org.onebusaway.gtfs.model.IdentityBean)6 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)4 Converter (org.apache.commons.beanutils.Converter)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Max (org.onebusaway.collections.Max)1 GtfsReaderContext (org.onebusaway.gtfs.serialization.GtfsReaderContext)1 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)1 IdKeyMatch (org.onebusaway.gtfs_transformer.collections.IdKeyMatch)1 EntityMatch (org.onebusaway.gtfs_transformer.match.EntityMatch)1 TypedEntityMatch (org.onebusaway.gtfs_transformer.match.TypedEntityMatch)1