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;
}
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;
}
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();
}
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);
}
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;
}
Aggregations