Search in sources :

Example 1 with InstanceProcessingExtension

use of eu.esdihumboldt.hale.common.instance.processing.InstanceProcessingExtension in project hale by halestudio.

the class HaleServiceFactory method create.

/**
 * @see AbstractServiceFactory#create(Class, IServiceLocator,
 *      IServiceLocator)
 */
@Override
public Object create(@SuppressWarnings("rawtypes") Class serviceInterface, IServiceLocator parentLocator, IServiceLocator locator) {
    if (AlignmentService.class.equals(serviceInterface)) {
        return new AlignmentServiceUndoSupport(new AlignmentServiceImpl(locator.getService(ProjectService.class)));
    }
    if (CompatibilityService.class.equals(serviceInterface)) {
        return new CompatibilityServiceImpl();
    }
    if (EntityDefinitionService.class.equals(serviceInterface)) {
        return new EntityDefinitionServiceUndoSupport(new EntityDefinitionServiceImpl(locator.getService(AlignmentService.class), locator.getService(ProjectService.class)));
    }
    if (EntityResolver.class.equals(serviceInterface)) {
        return new UserFallbackEntityResolver();
    }
    if (FunctionService.class.equals(serviceInterface)) {
        return new HaleFunctionService(locator.getService(AlignmentService.class));
    }
    if (GeometrySchemaService.class.equals(serviceInterface)) {
        return new ProjectGeometrySchemaService(locator.getService(ProjectService.class));
    }
    if (GroovyService.class.equals(serviceInterface)) {
        return new PreferencesGroovyService(locator.getService(ProjectService.class), locator.getService(AlignmentService.class));
    }
    if (InstanceSampleService.class.equals(serviceInterface)) {
        return new InstanceSampleServiceImpl();
    }
    if (InstanceService.class.equals(serviceInterface)) {
        final InstanceService is = OrientInstanceService.getInstance(locator.getService(SchemaService.class), locator.getService(ProjectService.class), locator.getService(AlignmentService.class), locator.getService(GroovyService.class));
        // Add a listener to close all InstanceProcessors when source data
        // is cleared
        // XXX There may a better place to add this listener
        is.addListener(new InstanceServiceAdapter() {

            @Override
            public void datasetChanged(DataSet type) {
                if (type != DataSet.SOURCE) {
                    return;
                }
                InstanceCollection instances = is.getInstances(type);
                if (instances.isEmpty()) {
                    // data was cleared, close instance
                    // processors
                    final InstanceProcessingExtension ext = new InstanceProcessingExtension(HaleUI.getServiceProvider());
                    for (InstanceProcessor processor : ext.getInstanceProcessors()) {
                        try {
                            processor.close();
                        } catch (IOException e) {
                        // Ignore
                        }
                    }
                }
            }
        });
        return is;
    }
    if (InstanceValidationService.class.equals(serviceInterface))
        return new InstanceValidationServiceImpl(locator.getService(InstanceService.class), locator.getService(ReportService.class));
    if (InstanceViewService.class.equals(serviceInterface)) {
        return new InstanceViewServiceImpl(locator.getService(ProjectService.class));
    }
    if (OccurringValuesService.class.equals(serviceInterface)) {
        return new OccurringValuesServiceImpl(locator.getService(InstanceService.class), locator.getService(ProjectService.class));
    }
    if (PopulationService.class.equals(serviceInterface)) {
        return new PopulationServiceImpl(locator.getService(InstanceService.class));
    }
    if (ProjectService.class.equals(serviceInterface)) {
        return new ProjectServiceImpl();
    }
    if (ProjectInfoService.class.equals(serviceInterface)) {
        return locator.getService(ProjectService.class);
    }
    if (RecentProjectsService.class.equals(serviceInterface)) {
        return new RecentProjectsServiceImpl();
    }
    if (RecentResources.class.equals(serviceInterface)) {
        return new RecentResourcesService(locator.getService(ProjectService.class));
    }
    if (ReportService.class.equals(serviceInterface)) {
        return new ReportServiceImpl();
    }
    if (SchemaService.class.equals(serviceInterface)) {
        return new SchemaServiceImpl(locator.getService(ProjectService.class));
    }
    if (TaskService.class.equals(serviceInterface)) {
        return new TaskServiceImpl();
    }
    if (TransformationFunctionService.class.equals(serviceInterface)) {
        return new HaleTransformationFunctionService(locator.getService(AlignmentService.class));
    }
    if (TransformationSchemas.class.equals(serviceInterface)) {
        return locator.getService(SchemaService.class);
    }
    return null;
}
Also used : UserFallbackEntityResolver(eu.esdihumboldt.hale.ui.service.align.resolver.UserFallbackEntityResolver) DataSet(eu.esdihumboldt.hale.common.instance.model.DataSet) TaskServiceImpl(eu.esdihumboldt.hale.ui.service.tasks.internal.TaskServiceImpl) CompatibilityServiceImpl(eu.esdihumboldt.hale.ui.compatibility.extension.impl.CompatibilityServiceImpl) EntityDefinitionServiceUndoSupport(eu.esdihumboldt.hale.ui.service.entity.internal.EntityDefinitionServiceUndoSupport) AlignmentServiceUndoSupport(eu.esdihumboldt.hale.ui.service.align.internal.AlignmentServiceUndoSupport) PreferencesGroovyService(eu.esdihumboldt.hale.ui.service.groovy.internal.PreferencesGroovyService) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) ProjectServiceImpl(eu.esdihumboldt.hale.ui.service.project.internal.ProjectServiceImpl) InstanceViewServiceImpl(eu.esdihumboldt.hale.ui.service.instance.sample.internal.InstanceViewServiceImpl) PopulationServiceImpl(eu.esdihumboldt.hale.ui.service.population.internal.PopulationServiceImpl) AlignmentService(eu.esdihumboldt.hale.ui.service.align.AlignmentService) InstanceSampleServiceImpl(eu.esdihumboldt.hale.ui.service.instance.sample.internal.InstanceSampleServiceImpl) SchemaServiceImpl(eu.esdihumboldt.hale.ui.service.schema.internal.SchemaServiceImpl) InstanceProcessor(eu.esdihumboldt.hale.common.instance.processing.InstanceProcessor) RecentResourcesService(eu.esdihumboldt.hale.ui.service.project.internal.resources.RecentResourcesService) InstanceServiceAdapter(eu.esdihumboldt.hale.ui.service.instance.InstanceServiceAdapter) PreferencesGroovyService(eu.esdihumboldt.hale.ui.service.groovy.internal.PreferencesGroovyService) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) InstanceValidationServiceImpl(eu.esdihumboldt.hale.ui.service.instance.validation.internal.InstanceValidationServiceImpl) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) IOException(java.io.IOException) ReportServiceImpl(eu.esdihumboldt.hale.ui.service.report.internal.ReportServiceImpl) RecentProjectsServiceImpl(eu.esdihumboldt.hale.ui.service.project.internal.RecentProjectsServiceImpl) EntityDefinitionServiceImpl(eu.esdihumboldt.hale.ui.service.entity.internal.EntityDefinitionServiceImpl) InstanceProcessingExtension(eu.esdihumboldt.hale.common.instance.processing.InstanceProcessingExtension) AlignmentServiceImpl(eu.esdihumboldt.hale.ui.service.align.internal.AlignmentServiceImpl) SchemaService(eu.esdihumboldt.hale.ui.service.schema.SchemaService) ProjectGeometrySchemaService(eu.esdihumboldt.hale.ui.service.geometry.ProjectGeometrySchemaService) GeometrySchemaService(eu.esdihumboldt.hale.ui.geometry.service.GeometrySchemaService) OrientInstanceService(eu.esdihumboldt.hale.ui.service.instance.internal.orient.OrientInstanceService) InstanceService(eu.esdihumboldt.hale.ui.service.instance.InstanceService) OccurringValuesServiceImpl(eu.esdihumboldt.hale.ui.service.values.internal.OccurringValuesServiceImpl) ProjectGeometrySchemaService(eu.esdihumboldt.hale.ui.service.geometry.ProjectGeometrySchemaService)

Example 2 with InstanceProcessingExtension

use of eu.esdihumboldt.hale.common.instance.processing.InstanceProcessingExtension in project hale by halestudio.

the class StoreInstancesJob method run.

/**
 * @see Job#run(IProgressMonitor)
 */
@Override
public IStatus run(IProgressMonitor monitor) {
    boolean exactProgress = instances.hasSize();
    monitor.beginTask("Store instances in database", (exactProgress) ? (instances.size()) : (IProgressMonitor.UNKNOWN));
    AtomicInteger count = new AtomicInteger(0);
    TObjectIntHashMap<QName> typeCount = new TObjectIntHashMap<>();
    if (report != null) {
        // set the correct start time
        report.setStartTime(new Date());
    }
    // get database connection
    DatabaseReference<ODatabaseDocumentTx> ref = database.openWrite();
    ODatabaseDocumentTx db = ref.getDatabase();
    ATransaction trans = log.begin("Store instances in database");
    try {
        // use intent
        db.declareIntent(new OIntentMassiveInsert());
        // Find all the InstanceProcessors to feed them the stored Instances
        final List<InstanceProcessor> processors;
        if (doProcessing) {
            final InstanceProcessingExtension ext = new InstanceProcessingExtension(serviceProvider);
            processors = ext.getInstanceProcessors();
        } else {
            processors = Collections.emptyList();
        }
        BrowseOrientInstanceCollection browser = new BrowseOrientInstanceCollection(database, null, DataSet.SOURCE);
        final InstanceIndexService indexService;
        if (doProcessing) {
            indexService = serviceProvider.getService(InstanceIndexService.class);
        } else {
            indexService = null;
        }
        // TODO decouple next() and save()?
        SimpleLogContext.withLog(report, () -> {
            if (report != null && instances instanceof LogAware) {
                ((LogAware) instances).setLog(report);
            }
            ResourceIterator<Instance> it = instances.iterator();
            int size = instances.size();
            try {
                while (it.hasNext() && !monitor.isCanceled()) {
                    // last count update
                    long lastUpdate = 0;
                    if (report != null && instances instanceof LogAware) {
                        ((LogAware) instances).setLog(report);
                    }
                    Instance instance = it.next();
                    // further processing before storing
                    processInstance(instance);
                    // get/create OInstance
                    OInstance conv = ((instance instanceof OInstance) ? ((OInstance) instance) : (new OInstance(instance)));
                    conv.setInserted(true);
                    // update the instance to store, e.g. generating
                    // metadata
                    updateInstance(conv);
                    ODatabaseRecordThreadLocal.INSTANCE.set(db);
                    // configure the document
                    ODocument doc = conv.configureDocument(db);
                    // and save it
                    doc.save();
                    // Create an InstanceReference for the saved instance
                    // and
                    // feed it to all known InstanceProcessors. The
                    // decoration
                    // with ResolvableInstanceReference allows the
                    // InstanceProcessors to resolve the instances if
                    // required.
                    OrientInstanceReference oRef = new OrientInstanceReference(doc.getIdentity(), conv.getDataSet(), conv.getDefinition());
                    IdentifiableInstanceReference idRef = new IdentifiableInstanceReference(oRef, doc.getIdentity());
                    ResolvableInstanceReference resolvableRef = new ResolvableInstanceReference(idRef, browser);
                    processors.forEach(p -> p.process(instance, resolvableRef));
                    if (indexService != null) {
                        indexService.add(instance, resolvableRef);
                    }
                    count.incrementAndGet();
                    TypeDefinition type = instance.getDefinition();
                    if (type != null) {
                        typeCount.adjustOrPutValue(type.getName(), 1, 1);
                    }
                    if (exactProgress) {
                        monitor.worked(1);
                    }
                    long now = System.currentTimeMillis();
                    if (now - lastUpdate > 100) {
                        // only update every 100
                        // milliseconds
                        monitor.subTask(MessageFormat.format("{0}{1} instances processed", String.valueOf(count.get()), size != InstanceCollection.UNKNOWN_SIZE ? "/" + String.valueOf(size) : ""));
                        lastUpdate = now;
                    }
                }
            } finally {
                it.close();
                if (report != null && instances instanceof LogAware) {
                    ((LogAware) instances).setLog(null);
                }
            }
        });
        db.declareIntent(null);
    } catch (RuntimeException e) {
        if (report != null) {
            reportTypeCount(report, typeCount);
            report.error(new MessageImpl("Error storing instances in database", e));
            report.setSuccess(false);
            reportHandler.publishReport(report);
        }
        throw e;
    } finally {
        ref.dispose();
        trans.end();
        /*
			 * Reset instances to prevent memory leak. It seems Eclipse
			 * internally holds a reference to the job (in JobInfo and/or
			 * ProgressMonitorFocusJobDialog) and this results in the instance
			 * collection not being garbage collected. This is especially bad,
			 * if an in-memory instance collection is used, e.g. a
			 * DefaultInstanceCollection that is used when loading a Shapefile.
			 */
        instances = null;
    }
    try {
        onComplete();
    } catch (RuntimeException e) {
        String message = "Error while post processing stored instances";
        if (report != null) {
            report.error(new MessageImpl(message, e));
        } else {
            log.error(message, e);
        }
    }
    String message = MessageFormat.format("Stored {0} instances in the database.", count);
    if (monitor.isCanceled()) {
        String warn = "Loading instances was canceled, incomplete data set in the database.";
        if (report != null) {
            report.warn(new MessageImpl(warn, null));
        } else {
            log.warn(warn);
        }
    }
    if (report != null) {
        reportTypeCount(report, typeCount);
        report.setSuccess(true);
        report.setSummary(message);
        reportHandler.publishReport(report);
    } else {
        log.info(message);
    }
    monitor.done();
    return new Status((monitor.isCanceled()) ? (IStatus.CANCEL) : (IStatus.OK), "eu.esdihumboldt.hale.common.instance.orient", message);
}
Also used : MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) OInstance(eu.esdihumboldt.hale.common.instance.orient.OInstance) IdentifiableInstanceReference(eu.esdihumboldt.hale.common.instance.model.IdentifiableInstanceReference) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OInstance(eu.esdihumboldt.hale.common.instance.orient.OInstance) OIntentMassiveInsert(com.orientechnologies.orient.core.intent.OIntentMassiveInsert) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) InstanceProcessor(eu.esdihumboldt.hale.common.instance.processing.InstanceProcessor) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) LogAware(eu.esdihumboldt.hale.common.core.report.LogAware) QName(javax.xml.namespace.QName) Date(java.util.Date) InstanceProcessingExtension(eu.esdihumboldt.hale.common.instance.processing.InstanceProcessingExtension) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ATransaction(de.fhg.igd.slf4jplus.ATransaction) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) MessageImpl(eu.esdihumboldt.hale.common.core.report.impl.MessageImpl)

Aggregations

InstanceProcessingExtension (eu.esdihumboldt.hale.common.instance.processing.InstanceProcessingExtension)2 InstanceProcessor (eu.esdihumboldt.hale.common.instance.processing.InstanceProcessor)2 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OIntentMassiveInsert (com.orientechnologies.orient.core.intent.OIntentMassiveInsert)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 ATransaction (de.fhg.igd.slf4jplus.ATransaction)1 LogAware (eu.esdihumboldt.hale.common.core.report.LogAware)1 MessageImpl (eu.esdihumboldt.hale.common.core.report.impl.MessageImpl)1 InstanceIndexService (eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)1 DataSet (eu.esdihumboldt.hale.common.instance.model.DataSet)1 IdentifiableInstanceReference (eu.esdihumboldt.hale.common.instance.model.IdentifiableInstanceReference)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)1 ResolvableInstanceReference (eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference)1 OInstance (eu.esdihumboldt.hale.common.instance.orient.OInstance)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 CompatibilityServiceImpl (eu.esdihumboldt.hale.ui.compatibility.extension.impl.CompatibilityServiceImpl)1 GeometrySchemaService (eu.esdihumboldt.hale.ui.geometry.service.GeometrySchemaService)1 AlignmentService (eu.esdihumboldt.hale.ui.service.align.AlignmentService)1