Search in sources :

Example 1 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity in project stanbol by apache.

the class EntityhubImpl method importEntity.

/**
     * Imports the Entity
     * @param remoteEntity the Entity to import
     * @return the Entity created and stored within the Entityhub
     * @throws YardException
     */
protected Entity importEntity(Entity remoteEntity) throws YardException {
    if (remoteEntity == null) {
        return null;
    }
    Site site = siteManager.getSite(remoteEntity.getSite());
    if (site == null) {
        log.warn("Unable to import Entity {} because the ReferencedSite {} is currently not active -> return null", remoteEntity.getId(), remoteEntity.getSite());
        return null;
    }
    ValueFactory valueFactory = entityhubYard.getValueFactory();
    //Create the locally managed Entity
    Representation localRep = entityhubYard.create(constructResourceId(DEFAULT_MANAGED_ENTITY_PREFIX));
    Entity localEntity = loadEntity(localRep);
    importEntity(remoteEntity, site, localEntity, valueFactory);
    //Second create and init the Mapping
    Representation entityMappingRepresentation = entityhubYard.create(constructResourceId(DEFAULT_MAPPING_PREFIX));
    Entity entityMappingEntity = loadEntity(entityMappingRepresentation);
    establishMapping(localEntity, remoteEntity, site, entityMappingEntity);
    //Store the entity and the mappedEntity in the entityhubYard
    storeEntity(localEntity);
    storeEntity(entityMappingEntity);
    return localEntity;
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

Example 2 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity in project stanbol by apache.

the class EntityhubImpl method delete.

@Override
public final Entity delete(String id) throws EntityhubException, IllegalArgumentException {
    if (id == null || id.isEmpty()) {
        throw new IllegalArgumentException("The parsed id MUST NOT be NULL nor emtpty!");
    }
    Entity entity = loadEntity(id);
    if (entity != null) {
        log.debug("delete Entity {} as requested by the parsed id {}", entity.getId(), id);
        //we need to remove all mappings for this Entity
        deleteMappingsbyTarget(entity.getId());
        deleteEntity(entity);
    } else {
        log.debug("Unable to delete Entity for id {}, because no Entity for this id is" + "managed by the Entityhub", id);
    }
    return entity;
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity)

Example 3 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity 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 4 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity in project stanbol by apache.

the class SiteManagerImpl method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) {
    log.debug("findEntities for query{}", query);
    //TODO: The QueryResultList expects that the query as executed is added
    //to the response. However when executing queries on multiple site they
    //might support a different set of features and therefore execute
    //different variants. For now I return simple the query as executed by
    //the first Site that contributes results
    FieldQuery processedQuery = null;
    FieldQuery queryWithResults = null;
    Set<Entity> entities = new HashSet<Entity>();
    for (Site site : referencedSites) {
        if (site.supportsSearch()) {
            //do not search on sites that do not support it
            log.debug(" > query site {}", site.getId());
            try {
                QueryResultList<Entity> results = site.findEntities(query);
                if (processedQuery == null) {
                    processedQuery = results.getQuery();
                }
                if (!results.isEmpty() && queryWithResults == null) {
                    processedQuery = results.getQuery();
                }
                for (Entity rep : results) {
                    if (!entities.contains(rep)) {
                        //do not override
                        entities.add(rep);
                    } else {
                        //TODO: find a solution for this problem
                        //      e.g. allow to add the site for entities
                        log.info("Entity {} found on more than one Referenced Site" + " -> Representation of Site {} is ignored", rep.getId(), site.getConfiguration().getName());
                    }
                }
            } catch (SiteException e) {
                log.warn("Unable to access Site " + site.getConfiguration().getName() + " (id = " + site.getId() + ")", e);
            }
        } else {
            log.debug(" > Site {} does not support queries", site.getId());
        }
    }
    return new QueryResultListImpl<Entity>(//use the query with results
    queryWithResults != null ? //use the query with results
    queryWithResults : //if not a processed
    processedQuery != null ? //if not a processed
    processedQuery : //else the parsed one
    query, entities, Entity.class);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 5 with Entity

use of org.apache.stanbol.entityhub.servicesapi.model.Entity in project stanbol by apache.

the class EntityhubImpl method setState.

@Override
public final Entity setState(String id, ManagedEntityState state) throws EntityhubException, IllegalArgumentException {
    if (id == null || id.isEmpty()) {
        throw new IllegalStateException("The parsed id MUST NOT be NULL nor empty!");
    }
    if (state == null) {
        throw new IllegalStateException("The parsed state for the Entity MUST NOT ne NULL");
    }
    Entity entity = loadEntity(id);
    if (entity != null) {
        ManagedEntity managed = new ManagedEntity(entity);
        if (managed.getState() != state) {
            managed.setState(state);
            storeEntity(entity);
        }
    }
    return entity;
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity)

Aggregations

Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)41 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)16 HashSet (java.util.HashSet)12 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)12 MediaType (javax.ws.rs.core.MediaType)11 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)11 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)10 Path (javax.ws.rs.Path)9 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)9 Site (org.apache.stanbol.entityhub.servicesapi.site.Site)9 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)9 GET (javax.ws.rs.GET)7 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)7 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)7 ArrayList (java.util.ArrayList)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 IRI (org.apache.clerezza.commons.rdf.IRI)6 EntityImpl (org.apache.stanbol.entityhub.core.model.EntityImpl)6 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)6 Produces (javax.ws.rs.Produces)5