use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory 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.ValueFactory in project stanbol by apache.
the class YardSite method store.
/**
* Stores the parsed representations to the Yard and also applies the
* configured {@link #getFieldMapper() FieldMappings}.
* @param The representations to store
*/
@Override
public void store(final Iterable<Representation> representations) throws ManagedSiteException {
try {
Yard yard = getYard();
final ValueFactory vf = yard.getValueFactory();
yard.store(new Iterable<Representation>() {
@Override
public Iterator<Representation> iterator() {
return new Iterator<Representation>() {
Iterator<Representation> it = representations.iterator();
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public Representation next() {
Representation next = it.next();
fieldMapper.applyMappings(next, next, vf);
return next;
}
@Override
public void remove() {
it.remove();
}
};
}
});
} catch (YardException e) {
throw new ManagedSiteException(e.getMessage(), e);
}
}
use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory in project stanbol by apache.
the class CacheImpl method applyCacheMappings.
/**
* Applies the mappings defined by the {@link #baseMapper} and the {@link #additionalMapper}
* to the parsed Representation.
*
* @param yard The yard (local reference to avoid syncronization)
* @param representation The representation to map
* @return the mapped representation
*/
private Representation applyCacheMappings(Yard yard, Representation representation) {
long start = System.currentTimeMillis();
Representation mapped = null;
ValueFactory valueFactory = getValueFactory();
if (baseMapper != null) {
mapped = yard.getValueFactory().createRepresentation(representation.getId());
baseMapper.applyMappings(representation, mapped, valueFactory);
}
if (additionalMapper != null) {
if (mapped == null) {
mapped = yard.getValueFactory().createRepresentation(representation.getId());
}
additionalMapper.applyMappings(representation, mapped, valueFactory);
}
log.info(" -- applied mappings in " + (System.currentTimeMillis() - start) + "ms");
return mapped != null ? mapped : representation;
}
use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory in project stanbol by apache.
the class RepresentationTest method testURIToReferenceConversion.
/**
* Tests if {@link Reference} instances are correctly generated for {@link URI}. This test also depends on
* the correct implementation of the {@link Reference#equals(Object)} method
*
* @throws URISyntaxException
*/
@Test
public void testURIToReferenceConversion() throws URISyntaxException {
String field = "urn:the.field:used.for.this.Test";
URI uri = new URI("http://www.test.org/uriTest");
ValueFactory vf = getValueFactory();
Representation rep = createRepresentation(null);
// test conversion
rep.add(field, uri);
Iterator<Reference> refs = rep.getReferences(field);
assertTrue(refs.hasNext());
assertEquals(refs.next().getReference(), uri.toString());
assertFalse(refs.hasNext());
// test multiple adds do not generate duplicate References
rep.add(field, uri);
assertTrue(asCollection(rep.get(field)).size() == 1);
// test adding a equivalent reference
rep.add(field, vf.createReference(uri.toString()));
assertTrue(asCollection(rep.get(field)).size() == 1);
// test removing
rep.remove(field, uri);
assertFalse(rep.get(field).hasNext());
}
use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory in project stanbol by apache.
the class RepresentationTest method testFieldRemoval.
@Test
public void testFieldRemoval() throws URISyntaxException {
String field = "urn:the.field:used.for.this.Test";
ValueFactory vf = getValueFactory();
Representation rep = createRepresentation(null);
// Test removal for References
String strRef = "urn:testValue";
rep.addReference(field, strRef);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.removeReference(field, strRef);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
Reference ref = vf.createReference("urn:testValue2");
rep.add(field, ref);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.remove(field, ref);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
// test removal for texts (with and without language)
String strText = "test text";
String strTextLang = "en";
rep.addNaturalText(field, strText, strTextLang);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.removeNaturalText(field, strText, strTextLang);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
String strTextNoLang = "test text without lang";
rep.addNaturalText(field, strTextNoLang);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.removeNaturalText(field, strTextNoLang);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
// there is also the possibility to explicitly parse null as language
// could internally case differences however externally this is the same
rep.addNaturalText(field, strTextNoLang, (String) null);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.removeNaturalText(field, strTextNoLang, (String) null);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
Text text = vf.createText("Das ist ein Text zum testen des Text Objektes", "de");
rep.add(field, text);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.remove(field, text);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
// Test a dataTypes values
Integer intValue = 42;
rep.add(field, intValue);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.remove(field, intValue);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
// Some Values are converted by the add(String field,Object value) Method
// to other data types. This MUST also be assured for removal
// NOTE: testing the conversions is done in other test methods!
URI testURI = new URI("http://www.test.org/test");
rep.add(field, testURI);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.remove(field, testURI);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
}
Aggregations