Search in sources :

Example 1 with EntityImpl

use of org.apache.stanbol.entityhub.core.model.EntityImpl in project stanbol by apache.

the class EntityhubImpl method loadEntity.

/**
     * Loads the Entity based on the parsed representation. The parsed
     * {@link Representation} can be both the data and the metadata. In case the
     * parsed representation are metadat the id of the returned Entity will be
     * not the same as the id of the parsed {@link Representation}.
     * @param rep the representation or metadata of an entity
     * @return the created Entity including both data and metadata or 
     * <code>null</code> if the parsed Representation does not represent a 
     * Representation managed by the Entityhub (this may be the case if an other
     * thread has deleted that Entity in the meantime)
     * @throws YardException On any error with the parsed Yard.
     */
private Entity loadEntity(Representation rep) throws YardException {
    if (rep != null) {
        Representation data;
        Representation metadata = null;
        String entityId = ModelUtils.getAboutRepresentation(rep);
        if (entityId != null) {
            data = entityhubYard.getRepresentation(entityId);
            metadata = rep;
        } else {
            data = rep;
            //needed for logs
            entityId = rep.getId();
        }
        if (data != null) {
            metadata = lookupMetadata(rep.getId(), true);
            return new EntityImpl(config.getID(), data, metadata);
        } else {
            log.warn("Unable find representation for Entity {} (metadata: {}", entityId, metadata);
            return null;
        }
    } else {
        return null;
    }
}
Also used : EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 2 with EntityImpl

use of org.apache.stanbol.entityhub.core.model.EntityImpl in project stanbol by apache.

the class EntityhubImpl method store.

@Override
public final Entity store(Representation representation) throws EntityhubException, IllegalArgumentException {
    if (representation == null) {
        throw new IllegalArgumentException("The parsed Representation MUST NOT be NULL!");
    }
    //parse only the id of the representation, because we need the current
    //stored version of the entity!
    Entity entity = loadEntity(representation.getId());
    //now we need to check if the parsed representation is the data or the
    //metadata of the Entity
    ManagedEntity updated;
    if (entity == null || representation.getId().equals(entity.getRepresentation().getId())) {
        //update the data or create a new entity
        updated = ManagedEntity.init(new EntityImpl(config.getID(), representation, entity != null ? entity.getMetadata() : null), config.getDefaultManagedEntityState());
        if (entity == null) {
            //add creation date
            updated.setCreated(new Date());
        }
    } else {
        //update the metadata
        entity = new EntityImpl(config.getID(), entity.getRepresentation(), representation);
        //we need to validate the metadata
        updated = ManagedEntity.init(entity, config.getDefaultManagedEntityState());
    }
    storeEntity(updated.getWrappedEntity());
    return updated.getWrappedEntity();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) Date(java.util.Date)

Example 3 with EntityImpl

use of org.apache.stanbol.entityhub.core.model.EntityImpl in project stanbol by apache.

the class YardSite method getEntity.

@Override
public Entity getEntity(String id) throws ManagedSiteException {
    Representation rep;
    try {
        rep = getYard().getRepresentation(id);
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
    if (rep != null) {
        Entity entity = new EntityImpl(config.getId(), rep, null);
        SiteUtils.initEntityMetadata(entity, siteMetadata, null);
        return entity;
    } else {
        return null;
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 4 with EntityImpl

use of org.apache.stanbol.entityhub.core.model.EntityImpl in project stanbol by apache.

the class MockEntityhub method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws EntityhubException {
    log.info("Performing Query: {}", query);
    QueryResultList<Representation> results = yard.findRepresentation(query);
    log.info("  ... {} results", results.size());
    Collection<Entity> entities = new ArrayList<Entity>(results.size());
    for (Representation r : results) {
        log.info("    > {}", r.getId());
        entities.add(new EntityImpl("dbpedia", r, null));
    }
    return new QueryResultListImpl<Entity>(results.getQuery(), entities, Entity.class);
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ArrayList(java.util.ArrayList) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 5 with EntityImpl

use of org.apache.stanbol.entityhub.core.model.EntityImpl in project stanbol by apache.

the class ReferencedSiteImpl method getEntity.

@Override
public Entity getEntity(String id) throws SiteException {
    Representation rep = null;
    Boolean cachedVersion = Boolean.FALSE;
    long start = System.currentTimeMillis();
    if (cache != null) {
        try {
            rep = cache.getRepresentation(id);
            if (rep == null) {
                if (siteConfiguration.getCacheStrategy() == CacheStrategy.all) {
                    // do no remote lookups on CacheStrategy.all!!
                    return null;
                }
            } else {
                cachedVersion = Boolean.TRUE;
            }
        } catch (YardException e) {
            if (dereferencer == null) {
                throw new SiteException(String.format("Unable to get Represetnation %s form Cache %s", id, siteConfiguration.getCacheId()), e);
            } else {
                log.warn(String.format("Unable to get Represetnation %s form Cache %s. Will dereference from remote site %s", id, siteConfiguration.getCacheId(), siteConfiguration.getAccessUri()), e);
            }
        }
    }
    if (rep == null && dereferencer != null) {
        try {
            rep = dereferencer.dereference(id);
        } catch (IOException e) {
            throw new SiteException(String.format("Unable to load Representation for entity %s form remote site %s with dereferencer %s", id, siteConfiguration.getAccessUri(), siteConfiguration.getEntityDereferencerType()), e);
        }
        // representation loaded from remote site and cache is available
        if (rep != null && cache != null) {
            // -> cache the representation
            try {
                start = System.currentTimeMillis();
                // return the the cached version
                rep = cache.store(rep);
                cachedVersion = Boolean.TRUE;
                log.debug("  - cached Representation {} in {} ms", id, (System.currentTimeMillis() - start));
            } catch (YardException e) {
                log.warn(String.format("Unable to cache Represetnation %s in Cache %s! Representation not cached!", id, siteConfiguration.getCacheId()), e);
            }
        }
    }
    if (rep != null) {
        Entity entity = new EntityImpl(getId(), rep, null);
        initEntityMetadata(entity, siteMetadata, singletonMap(RdfResourceEnum.isChached.getUri(), (Object) cachedVersion));
        return entity;
    } else {
        return null;
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Aggregations

EntityImpl (org.apache.stanbol.entityhub.core.model.EntityImpl)7 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)6 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)4 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)2 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)2 Date (java.util.Date)1