Search in sources :

Example 11 with IOReporter

use of eu.esdihumboldt.hale.common.core.io.report.IOReporter in project hale by halestudio.

the class ActionProjectFile method executeProvider.

private <P extends IOProvider> void executeProvider(P provider, IOAdvisor<P> advisor) throws Exception {
    IOReporter reporter = provider.createReporter();
    ATransaction trans = log.begin(reporter.getTaskName());
    try {
        // use advisor to configure provider
        advisor.prepareProvider(provider);
        advisor.updateConfiguration(provider);
        // execute
        IOReport report = provider.execute(new LogProgressIndicator());
        // handle results
        if (report.isSuccess()) {
            advisor.handleResults(provider);
        } else {
            // TODO propagate report errors somehow?
            throw new IOException("Project file action was not successful");
        }
    } finally {
        trans.end();
    }
}
Also used : IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) ATransaction(de.fhg.igd.slf4jplus.ATransaction) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) IOException(java.io.IOException) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)

Example 12 with IOReporter

use of eu.esdihumboldt.hale.common.core.io.report.IOReporter in project hale by halestudio.

the class JaxbAlignmentIO method printCell.

/**
 * Print a cell to an output stream (intended for tests/debugging).
 *
 * @param cell the cell to print
 * @param out the output stream
 * @throws Exception if an error occurs trying to print the cell
 */
public static void printCell(MutableCell cell, OutputStream out) throws Exception {
    DefaultAlignment alignment = new DefaultAlignment();
    alignment.addCell(cell);
    IOReporter reporter = new DefaultIOReporter(new Locatable() {

        @Override
        public URI getLocation() {
            return null;
        }
    }, "Print cell", null, false);
    PathUpdate pathUpdate = new PathUpdate(null, null);
    AlignmentType at = convert(alignment, reporter, pathUpdate);
    CellType ct = (CellType) at.getCellOrModifier().get(0);
    JAXBContext jc = JAXBContext.newInstance(ALIGNMENT_CONTEXT, ObjectFactory.class.getClassLoader());
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    ObjectFactory of = new ObjectFactory();
    try {
        m.marshal(of.createCell(ct), out);
    } finally {
        out.flush();
        out.close();
    }
}
Also used : AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) DefaultIOReporter(eu.esdihumboldt.hale.common.core.io.report.impl.DefaultIOReporter) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) Marshaller(javax.xml.bind.Marshaller) DefaultIOReporter(eu.esdihumboldt.hale.common.core.io.report.impl.DefaultIOReporter) ObjectFactory(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ObjectFactory) CellType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) JAXBContext(javax.xml.bind.JAXBContext) URI(java.net.URI) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 13 with IOReporter

use of eu.esdihumboldt.hale.common.core.io.report.IOReporter in project hale by halestudio.

the class JaxbToAlignment method createUnmigratedCell.

private static UnmigratedCell createUnmigratedCell(CellType cell, LoadAlignmentContext context, IOReporter reporter, EntityResolver resolver, ServiceProvider serviceProvider) {
    // The sourceCell represents the cell as it was imported from the
    // XML alignment. The conversion to the resolved cell must be performed
    // later by migrating the UnmigratedCell returned from this function.
    final DefaultCell sourceCell = new DefaultCell();
    sourceCell.setTransformationIdentifier(cell.getRelation());
    final FunctionDefinition<?> cellFunction = FunctionUtil.getFunction(sourceCell.getTransformationIdentifier(), serviceProvider);
    final CellMigrator migrator;
    if (cellFunction != null) {
        migrator = cellFunction.getCustomMigrator().orElse(new DefaultCellMigrator());
    } else {
        migrator = new DefaultCellMigrator();
    }
    Map<EntityDefinition, EntityDefinition> mappings = new HashMap<>();
    try {
        // The returned Entity pair consists of
        // (1st) a dummy entity representing the entity read from JAXB
        // (2nd) the resolved entity
        ListMultimap<String, Pair<Entity, Entity>> convertedSourceEntities = convertEntities(cell.getSource(), context.getSourceTypes(), SchemaSpaceID.SOURCE, resolver);
        if (convertedSourceEntities == null) {
            sourceCell.setSource(null);
        } else {
            sourceCell.setSource(Multimaps.transformValues(convertedSourceEntities, pair -> pair.getFirst()));
            for (Pair<Entity, Entity> pair : convertedSourceEntities.values()) {
                mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
            }
        }
        ListMultimap<String, Pair<Entity, Entity>> convertedTargetEntities = convertEntities(cell.getTarget(), context.getTargetTypes(), SchemaSpaceID.TARGET, resolver);
        if (convertedTargetEntities == null) {
            sourceCell.setTarget(null);
        } else {
            sourceCell.setTarget(Multimaps.transformValues(convertedTargetEntities, pair -> pair.getFirst()));
            for (Pair<Entity, Entity> pair : convertedTargetEntities.values()) {
                mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
            }
        }
        if (sourceCell.getTarget() == null || sourceCell.getTarget().isEmpty()) {
            // target is mandatory for cells!
            throw new IllegalStateException("Cannot create cell without target");
        }
    } catch (Exception e) {
        if (reporter != null) {
            reporter.error(new IOMessageImpl("Could not create cell", e));
        }
        return null;
    }
    if (!cell.getAbstractParameter().isEmpty()) {
        ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
        for (JAXBElement<? extends AbstractParameterType> param : cell.getAbstractParameter()) {
            AbstractParameterType apt = param.getValue();
            if (apt instanceof ParameterType) {
                // treat string parameters or null parameters
                ParameterType pt = (ParameterType) apt;
                ParameterValue pv = new ParameterValue(pt.getType(), Value.of(pt.getValue()));
                parameters.put(pt.getName(), pv);
            } else if (apt instanceof ComplexParameterType) {
                // complex parameters
                ComplexParameterType cpt = (ComplexParameterType) apt;
                parameters.put(cpt.getName(), new ParameterValue(new ElementValue(cpt.getAny(), context)));
            } else
                throw new IllegalStateException("Illegal parameter type");
        }
        sourceCell.setTransformationParameters(parameters);
    }
    // annotations & documentation
    for (Object element : cell.getDocumentationOrAnnotation()) {
        if (element instanceof AnnotationType) {
            // add annotation to the cell
            AnnotationType annot = (AnnotationType) element;
            // but first load it from the DOM
            AnnotationDescriptor<?> desc = AnnotationExtension.getInstance().get(annot.getType());
            if (desc != null) {
                try {
                    Object value = desc.fromDOM(annot.getAny(), null);
                    sourceCell.addAnnotation(annot.getType(), value);
                } catch (Exception e) {
                    if (reporter != null) {
                        reporter.error(new IOMessageImpl("Error loading cell annotation", e));
                    } else
                        throw new IllegalStateException("Error loading cell annotation", e);
                }
            } else
                reporter.error(new IOMessageImpl("Cell annotation of type {0} unknown, cannot load the annotation object", null, -1, -1, annot.getType()));
        } else if (element instanceof DocumentationType) {
            // add documentation to the cell
            DocumentationType doc = (DocumentationType) element;
            sourceCell.getDocumentation().put(doc.getType(), doc.getValue());
        }
    }
    sourceCell.setId(cell.getId());
    // a default value is assured for priority
    String priorityStr = cell.getPriority().value();
    Priority priority = Priority.fromValue(priorityStr);
    if (priority != null) {
        sourceCell.setPriority(priority);
    } else {
        // used.
        throw new IllegalArgumentException();
    }
    return new UnmigratedCell(sourceCell, migrator, mappings);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) CellMigrator(eu.esdihumboldt.hale.common.align.migrate.CellMigrator) Collections2(com.google.common.collect.Collections2) ModifierType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ModifierType) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) ElementValue(eu.esdihumboldt.hale.common.core.io.impl.ElementValue) HaleIO(eu.esdihumboldt.hale.common.core.io.HaleIO) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Map(java.util.Map) ParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ParameterType) Pair(eu.esdihumboldt.util.Pair) AnnotationDescriptor(eu.esdihumboldt.hale.common.align.model.AnnotationDescriptor) URI(java.net.URI) AnnotationExtension(eu.esdihumboldt.hale.common.align.extension.annotation.AnnotationExtension) Value(eu.esdihumboldt.hale.common.core.io.Value) Function(com.google.common.base.Function) Collection(java.util.Collection) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) JAXBException(javax.xml.bind.JAXBException) LoadAlignmentContext(eu.esdihumboldt.hale.common.align.io.LoadAlignmentContext) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) SchemaSpaceID(eu.esdihumboldt.hale.common.schema.SchemaSpaceID) CustomFunctionType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CustomFunctionType) DummyEntityResolver(eu.esdihumboldt.hale.common.align.io.impl.dummy.DummyEntityResolver) List(java.util.List) Base(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType.Base) ComplexParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ComplexParameterType) TypeIndex(eu.esdihumboldt.hale.common.schema.model.TypeIndex) CellType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType) Priority(eu.esdihumboldt.hale.common.align.model.Priority) StreamSource(javax.xml.transform.stream.StreamSource) AnnotationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AnnotationType) HashMap(java.util.HashMap) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) ArrayList(java.util.ArrayList) Multimaps(com.google.common.collect.Multimaps) JaxbAlignmentIO(eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentIO) EntityResolver(eu.esdihumboldt.hale.common.align.io.EntityResolver) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) DisableFor(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ModifierType.DisableFor) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) CustomPropertyFunction(eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction) DocumentationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.DocumentationType) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) DefaultEntityResolver(eu.esdihumboldt.hale.common.align.io.impl.DefaultEntityResolver) Entity(eu.esdihumboldt.hale.common.align.model.Entity) FunctionUtil(eu.esdihumboldt.hale.common.align.extension.function.FunctionUtil) AbstractParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AbstractParameterType) TransformationMode(eu.esdihumboldt.hale.common.align.model.TransformationMode) FunctionDefinition(eu.esdihumboldt.hale.common.align.extension.function.FunctionDefinition) Element(org.w3c.dom.Element) NamedEntityType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.NamedEntityType) AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) InputStream(java.io.InputStream) Entity(eu.esdihumboldt.hale.common.align.model.Entity) HashMap(java.util.HashMap) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) CellMigrator(eu.esdihumboldt.hale.common.align.migrate.CellMigrator) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) AnnotationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AnnotationType) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell) AbstractParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AbstractParameterType) Pair(eu.esdihumboldt.util.Pair) ParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ParameterType) ComplexParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ComplexParameterType) AbstractParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AbstractParameterType) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Priority(eu.esdihumboldt.hale.common.align.model.Priority) ElementValue(eu.esdihumboldt.hale.common.core.io.impl.ElementValue) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) ComplexParameterType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ComplexParameterType) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) DocumentationType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.DocumentationType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)

Example 14 with IOReporter

use of eu.esdihumboldt.hale.common.core.io.report.IOReporter in project hale by halestudio.

the class InstanceExportWizard method performValidation.

/**
 * Run the configured validators on the exported instance. May be overriden
 * to customize validation process.
 *
 * @return true if all validations were successful
 */
protected boolean performValidation() {
    boolean success = true;
    for (InstanceValidator validator : validators) {
        // set schemas
        List<? extends Locatable> schemas = getProvider().getValidationSchemas();
        validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
        // set service provider
        validator.setServiceProvider(HaleUI.getServiceProvider());
        ExportTarget<?> exportTarget = getSelectTargetPage().getExportTarget();
        if (exportTarget instanceof FileTarget) {
            LocatableOutputSupplier<? extends OutputStream> target = getProvider().getTarget();
            List<String> fileNames = new ArrayList<>();
            if (target instanceof MultiLocationOutputSupplier) {
                for (URI location : ((MultiLocationOutputSupplier) target).getLocations()) {
                    if (!"file".equals(location.getScheme())) {
                        continue;
                    }
                    File targetFile = new File(location);
                    fileNames.add(targetFile.getAbsolutePath());
                }
            } else {
                fileNames.add(((FileTarget<?>) exportTarget).getTargetFileName());
            }
            for (String fileName : fileNames) {
                LocatableInputSupplier<? extends InputStream> source = new FileIOSupplier(new File(fileName));
                validator.setSource(source);
                validator.setContentType(getContentType());
                IOReporter defReport = validator.createReporter();
                // validate and execute provider
                try {
                    // validate configuration
                    validator.validate();
                    IOReport report = execute(validator, defReport);
                    if (report != null) {
                        // add report to report server
                        ReportService repService = PlatformUI.getWorkbench().getService(ReportService.class);
                        repService.addReport(report);
                        if (report.isSuccess()) {
                            log.info(report.getSummary());
                        } else {
                            log.error(report.getSummary());
                            success = false;
                        }
                    }
                } catch (IOProviderConfigurationException e) {
                    log.error(MessageFormat.format("The validator '{0}' could not be executed", validator.getClass().getCanonicalName()), e);
                    success = false;
                }
            }
        } else {
            log.error("No input can be provided for validation (no file target)");
            success = false;
        }
    }
    if (success) {
        log.userInfo("All validations completed successfully.");
    } else {
        log.userError("There were validation failures. Please check the report for more details.");
    }
    return success;
}
Also used : InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) ArrayList(java.util.ArrayList) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) URI(java.net.URI) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) FileTarget(eu.esdihumboldt.hale.ui.io.target.FileTarget) ReportService(eu.esdihumboldt.hale.ui.service.report.ReportService) MultiLocationOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.MultiLocationOutputSupplier) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 15 with IOReporter

use of eu.esdihumboldt.hale.common.core.io.report.IOReporter in project hale by halestudio.

the class ProjectResourcesUtil method executeProvider.

/**
 * Execute the given I/O provider with the given I/O advisor.
 *
 * @param provider the I/O provider
 * @param advisor the I/O advisor
 * @param publishReport if the report should be published
 * @param cacheCallback call back that is notified on cache changes for the
 *            I/O provider, may be <code>null</code>
 * @return the future yielding the report on success
 */
public static ListenableFuture<IOReport> executeProvider(final IOProvider provider, @SuppressWarnings("rawtypes") final IOAdvisor advisor, final boolean publishReport, final CacheCallback cacheCallback) {
    final SettableFuture<IOReport> result = SettableFuture.create();
    IRunnableWithProgress op = new IRunnableWithProgress() {

        @SuppressWarnings("unchecked")
        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            if (cacheCallback != null && provider instanceof CachingImportProvider) {
                // enable cache generation
                ((CachingImportProvider) provider).setProvideCache();
            }
            IOReporter reporter = provider.createReporter();
            ATransaction trans = log.begin(reporter.getTaskName());
            try {
                // use advisor to configure provider
                advisor.prepareProvider(provider);
                advisor.updateConfiguration(provider);
                // execute
                IOReport report = provider.execute(new ProgressMonitorIndicator(monitor));
                if (publishReport) {
                    // publish report
                    ReportService rs = PlatformUI.getWorkbench().getService(ReportService.class);
                    rs.addReport(report);
                }
                // handle cache update
                if (cacheCallback != null && provider instanceof CachingImportProvider) {
                    CachingImportProvider cip = (CachingImportProvider) provider;
                    if (cip.isCacheUpdate()) {
                        Value cache = cip.getCache();
                        cacheCallback.update(cache);
                    }
                }
                // handle results
                if (report.isSuccess()) {
                    advisor.handleResults(provider);
                }
                result.set(report);
            } catch (Exception e) {
                log.error("Error executing an I/O provider.", e);
                result.setException(e);
            } finally {
                trans.end();
            }
        }
    };
    try {
        ThreadProgressMonitor.runWithProgressDialog(op, provider.isCancelable());
    } catch (Exception e) {
        log.error("Error executing an I/O provider.", e);
        result.setException(e);
    }
    return result;
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) ReportService(eu.esdihumboldt.hale.ui.service.report.ReportService) ProgressMonitorIndicator(eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) ATransaction(de.fhg.igd.slf4jplus.ATransaction) Value(eu.esdihumboldt.hale.common.core.io.Value) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Aggregations

IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)15 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)7 DefaultIOReporter (eu.esdihumboldt.hale.common.core.io.report.impl.DefaultIOReporter)6 ATransaction (de.fhg.igd.slf4jplus.ATransaction)5 URI (java.net.URI)5 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)4 Locatable (eu.esdihumboldt.hale.common.core.io.supplier.Locatable)4 FeatureTypeMapping (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.TypeMappingsPropertyType.FeatureTypeMapping)4 Namespace (eu.esdihumboldt.hale.io.geoserver.Namespace)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)3 Value (eu.esdihumboldt.hale.common.core.io.Value)3 ReportService (eu.esdihumboldt.hale.ui.service.report.ReportService)3 AlignmentType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType)2 CellType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 ProgressMonitorIndicator (eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2