Search in sources :

Example 6 with FieldMapper

use of org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper in project stanbol by apache.

the class EntityhubImpl method importEntity.

/**
 * Imports a {@link Entity} from a {@link Site}. This Method imports
 * the {@link Representation} by applying all configured mappings. It also
 * sets the {@link ManagedEntityState} to the configured default value by the
 * referenced site of the imported entity or the default for the Entityhub
 * if the site does not define this configuration.<p>
 * @param remoteEntity The entity to import
 * @param site the referenced site of the entity to import
 * @param localEntity the target entity for the import
 * @param valueFactory the valusFactory used to create instance while importing
 */
private void importEntity(Entity remoteEntity, Site site, Entity localEntity, ValueFactory valueFactory) {
    SiteConfiguration siteConfig = site.getConfiguration();
    ManagedEntityState state;
    state = siteConfig.getDefaultManagedEntityState();
    if (state == null) {
        state = config.getDefaultManagedEntityState();
    }
    // this wrapper allows to use an API to write metadata
    ManagedEntity managedEntity = ManagedEntity.init(localEntity, state);
    FieldMapper siteMapper = site.getFieldMapper();
    FieldMapper mapper = this.fieldMapper.clone();
    for (FieldMapping siteMapping : siteMapper.getMappings()) {
        mapper.addMapping(siteMapping);
    }
    // TODO: As soon as MappingActivities are implemented we need to add such
    // information to the EntityMapping instance!
    mapper.applyMappings(remoteEntity.getRepresentation(), localEntity.getRepresentation(), valueFactory);
    // set general metadata
    managedEntity.setCreated(new Date());
    // set the metadata required by the referenced site
    managedEntity.addAttributionLink(site.getConfiguration().getAttributionUrl());
    managedEntity.addAttributionText(site.getConfiguration().getAttribution(), null);
    // TODO: maybe replace with the URL of the site
    managedEntity.addContributorName(site.getConfiguration().getName());
}
Also used : FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) SiteConfiguration(org.apache.stanbol.entityhub.servicesapi.site.SiteConfiguration) ManagedEntityState(org.apache.stanbol.entityhub.servicesapi.model.ManagedEntityState) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper) Date(java.util.Date)

Example 7 with FieldMapper

use of org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper in project stanbol by apache.

the class ReferencedSiteComponent method activate.

@SuppressWarnings("unchecked")
@Activate
protected void activate(final ComponentContext ctx) throws ConfigurationException, YardException, InvalidSyntaxException {
    log.debug("in {} activate with properties {}", ReferencedSiteImpl.class.getSimpleName(), ctx.getProperties());
    if (ctx == null || ctx.getProperties() == null) {
        throw new IllegalStateException("No Component Context and/or Dictionary properties object parsed to the acticate methode");
    }
    this.cc = ctx;
    this.bc = ctx.getBundleContext();
    // create the SiteConfiguration based on the parsed properties
    // NOTE that the constructor also validation of the parsed configuration
    siteConfiguration = new ReferencedSiteConfigurationImpl(ctx.getProperties());
    if (PROHIBITED_SITE_IDS.contains(siteConfiguration.getId().toLowerCase())) {
        throw new ConfigurationException(SiteConfiguration.ID, String.format("The ID '%s' of this Referenced Site is one of the following " + "prohibited IDs: {} (case insensitive)", siteConfiguration.getId(), PROHIBITED_SITE_IDS));
    }
    log.info(" > initialise Referenced Site {}", siteConfiguration.getName());
    // if the accessUri is the same as the queryUri and both the
    // dereferencer and the entitySearcher uses the same component, than we
    // need only one component for both dependencies.
    this.dereferencerEqualsEntitySearcherComponent = // (1) accessURI == queryURI
    siteConfiguration.getAccessUri() != null && siteConfiguration.getAccessUri().equals(siteConfiguration.getQueryUri()) && // (2) entity dereferencer == entity searcher
    siteConfiguration.getEntityDereferencerType() != null && siteConfiguration.getEntityDereferencerType().equals(siteConfiguration.getEntitySearcherType());
    // init the fieldMapper based on the configuration
    FieldMapper fieldMappings = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
    if (siteConfiguration.getFieldMappings() != null) {
        log.debug(" > Initialise configured field mappings");
        for (String configuredMapping : siteConfiguration.getFieldMappings()) {
            FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping, nsPrefixService);
            if (mapping != null) {
                log.debug("   - add FieldMapping {}", mapping);
                fieldMappings.addMapping(mapping);
            }
        }
    }
    // now init the referenced Services
    initDereferencerAndEntitySearcher();
    // Reference to the cache!
    if (siteConfiguration.getCacheId() != null) {
        String cacheFilter = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, Cache.class.getName(), Cache.CACHE_YARD, siteConfiguration.getCacheId());
        cacheTracker = new ServiceTracker(ctx.getBundleContext(), ctx.getBundleContext().createFilter(cacheFilter), new ServiceTrackerCustomizer() {

            @Override
            public void removedService(ServiceReference reference, Object service) {
                if (service.equals(cache)) {
                    cache = (Cache) cacheTracker.getService();
                    updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
                }
                bc.ungetService(reference);
            }

            @Override
            public void modifiedService(ServiceReference reference, Object service) {
                // the service.ranking might have changed ... so check if the
                // top ranked Cache is a different one
                Cache newCache = (Cache) cacheTracker.getService();
                if (newCache == null || !newCache.equals(cache)) {
                    // set the new cahce
                    cache = newCache;
                    // and update the service registration
                    updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
                }
            }

            @Override
            public Object addingService(ServiceReference reference) {
                Object service = bc.getService(reference);
                if (service != null) {
                    if (// the first added Service or
                    cacheTracker.getServiceReference() == null || // the new service as higher ranking as the current
                    (reference.compareTo(cacheTracker.getServiceReference()) > 0)) {
                        cache = (Cache) service;
                        updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
                    }
                // else the new service has lower ranking as the currently use one
                }
                // else service == null -> ignore
                return service;
            }
        });
        cacheTracker.open();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ServiceReference(org.osgi.framework.ServiceReference) ReferencedSiteConfigurationImpl(org.apache.stanbol.entityhub.core.site.ReferencedSiteConfigurationImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper) Cache(org.apache.stanbol.entityhub.servicesapi.yard.Cache) Activate(org.apache.felix.scr.annotations.Activate)

Example 8 with FieldMapper

use of org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper in project stanbol by apache.

the class CacheImpl method setAdditionalMappings.

/**
 * Internally used in the initialisation to be able to parse the Yard instance
 *
 * @param yard the yard used to set the configured additional mappings
 * @param fieldMapper the configuration
 * @throws YardException on any error while accessing the yard
 */
protected void setAdditionalMappings(Yard yard, FieldMapper fieldMapper) throws YardException {
    FieldMapper old = this.additionalMapper;
    this.additionalMapper = fieldMapper;
    try {
        CacheUtils.storeAdditionalMappingsConfiguration(yard, additionalMapper);
    } catch (YardException e) {
        this.additionalMapper = old;
        throw e;
    }
}
Also used : YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper)

Aggregations

FieldMapper (org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper)8 FieldMapping (org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping)3 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)3 ArrayList (java.util.ArrayList)2 DefaultFieldMapperImpl (org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl)2 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)2 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 Activate (org.apache.felix.scr.annotations.Activate)1 IndexSchema (org.apache.solr.schema.IndexSchema)1 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)1 DereferenceException (org.apache.stanbol.enhancer.engines.dereference.DereferenceException)1 ReferencedSiteConfigurationImpl (org.apache.stanbol.entityhub.core.site.ReferencedSiteConfigurationImpl)1 CorpusCreationInfo (org.apache.stanbol.entityhub.indexing.destination.solryard.fst.CorpusCreationInfo)1 CorpusCreationTask (org.apache.stanbol.entityhub.indexing.destination.solryard.fst.CorpusCreationTask)1 FstConfig (org.apache.stanbol.entityhub.indexing.destination.solryard.fst.FstConfig)1