Search in sources :

Example 1 with IOMessageImpl

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

the class ProjectParser method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin(Messages.ProjectParser_0, ProgressIndicator.UNKNOWN);
    try {
        File file;
        try {
            file = new File(getSource().getLocation());
        } catch (IllegalArgumentException e) {
            file = null;
        }
        String basePath = (file == null) ? (new File(".").getAbsolutePath()) : (FilenameUtils.getFullPath(file.getAbsolutePath()));
        // Unmarshal the project file
        JAXBContext jc;
        JAXBElement<HaleProject> root;
        try {
            jc = JAXBContext.newInstance(PROJECT_CONTEXT, ObjectFactory.class.getClassLoader());
            Unmarshaller u = jc.createUnmarshaller();
            u.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
            root = u.unmarshal(new StreamSource(getSource().getInput()), HaleProject.class);
        } catch (JAXBException e) {
            reporter.error(new IOMessageImpl("Unmarshalling the HaleProject from the given resource failed: {0}", e, -1, -1, getSource().getLocation()));
            reporter.setSuccess(false);
            return reporter;
        }
        project = new Project();
        projectFiles = new HashMap<String, ProjectFile>();
        report = reporter;
        HaleProject haleProject = root.getValue();
        // populate project and project files
        loadProject(haleProject);
        loadSchemas(haleProject, basePath);
        loadAlignment(haleProject, basePath);
        loadStyle(haleProject, basePath);
        loadInstances(haleProject, basePath);
        loadTasks(haleProject, basePath);
        loadConfig(haleProject);
        report = null;
        reporter.setSuccess(true);
        return reporter;
    } finally {
        progress.end();
    }
}
Also used : HaleProject(eu.esdihumboldt.hale.io.project.jaxb.generated.HaleProject) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(javax.xml.bind.JAXBException) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) JAXBContext(javax.xml.bind.JAXBContext) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) HaleProject(eu.esdihumboldt.hale.io.project.jaxb.generated.HaleProject) Unmarshaller(javax.xml.bind.Unmarshaller) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) File(java.io.File)

Example 2 with IOMessageImpl

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

the class SchematronInstanceValidator method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    URI schematronLoc = getSchematronLocation();
    if (schematronLoc == null) {
        throw new IOProviderConfigurationException("Providing a schematron file is required");
    }
    progress.begin("Performing Schematron validation", ProgressIndicator.UNKNOWN);
    final InputStream sourceInput = this.getSource().getInput();
    if (sourceInput == null) {
        throw new RuntimeException("No input for Schematron validator");
    }
    final Source xmlSource = new StreamSource(sourceInput);
    final DefaultInputSupplier schematronInputSupplier = new DefaultInputSupplier(schematronLoc);
    final InputStream schematronInput = schematronInputSupplier.getInput();
    if (schematronInput == null) {
        throw new RuntimeException("No rules input for Schematron validator");
    }
    final Source schematronSource = new StreamSource(schematronInput);
    try {
        final SchematronValidator validator = new SchematronValidator(schematronSource);
        final Result result = validator.validate(xmlSource, /* svrlReport */
        true);
        final StringWriter reportWriter = new StringWriter();
        SchematronUtils.convertValidatorResult(result, reportWriter);
        reporter.setSuccess(!validator.ruleViolationsDetected());
        if (validator.ruleViolationsDetected()) {
            SchematronReportParser parser = new SchematronReportParser(reportWriter.toString());
            parser.reportFailedAssertions(reporter);
        }
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Error running schematron validation", e));
        reporter.setSuccess(false);
    } finally {
        schematronInput.close();
        progress.end();
    }
    return reporter;
}
Also used : SchematronReportParser(eu.esdihumboldt.hale.io.schematron.util.SchematronReportParser) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) URI(java.net.URI) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) Result(javax.xml.transform.Result) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) StringWriter(java.io.StringWriter) SchematronValidator(org.opengis.cite.validation.SchematronValidator)

Example 3 with IOMessageImpl

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

the class ProjectValidator method execute.

/**
 * @see eu.esdihumboldt.hale.common.core.io.impl.AbstractIOProvider#execute(eu.esdihumboldt.hale.common.core.io.ProgressIndicator,
 *      eu.esdihumboldt.hale.common.core.io.report.IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    ValidatorConfigurationService service = getServiceProvider().getService(ValidatorConfigurationService.class);
    if (service == null) {
        reporter.setSuccess(false);
        throw new RuntimeException("Unable to find validator configurations");
    }
    Collection<IOProviderDescriptor> validators = new ArrayList<>();
    validators.addAll(HaleIO.getProviderFactories(ConfigurableInstanceValidator.class));
    List<ValidatorConfiguration> configurations = service.getConfigurations();
    progress.begin("Performing project validation", configurations.size());
    reporter.setSuccess(true);
    SubtaskProgressIndicator subProgress = new SubtaskProgressIndicator(progress);
    int i = 0;
    for (ValidatorConfiguration configuration : configurations) {
        for (IOProviderDescriptor validatorFactory : HaleIO.filterFactoriesByConfigurationType(validators, configuration.getContentType())) {
            try {
                // Assert that the validator can validate the exported
                // content type, skip otherwise
                boolean compatible = validatorFactory.getSupportedTypes().stream().anyMatch(type -> getContentType().isKindOf(type));
                if (!compatible) {
                    reporter.info(new IOMessageImpl(MessageFormat.format("Validator \"{0}\" skipped: cannot validate exported content type \"{1}\"", validatorFactory.getIdentifier(), getContentType().getId()), null));
                    continue;
                }
                ConfigurableInstanceValidator validator = (ConfigurableInstanceValidator) validatorFactory.createExtensionObject();
                subProgress.begin(MessageFormat.format("Executing project validator ({0}/{1})", ++i, configurations.size()), ProgressIndicator.UNKNOWN);
                validator.setSchemas(getSchemas());
                validator.setSource(getSource());
                validator.setContentType(getContentType());
                validator.setServiceProvider(getServiceProvider());
                validator.configure(configuration);
                validator.validate();
                IOReport result = validator.execute(null);
                if (result != null) {
                    reporter.importMessages(result);
                    if (!result.isSuccess()) {
                        reporter.setSuccess(false);
                    }
                }
            } catch (Exception e) {
                reporter.error(new IOMessageImpl("Error running project validator", e));
                reporter.setSuccess(false);
            }
            subProgress.end();
            progress.advance(1);
        }
    }
    progress.end();
    return reporter;
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) ArrayList(java.util.ArrayList) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) SubtaskProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.SubtaskProgressIndicator) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) ValidatorConfigurationService(eu.esdihumboldt.hale.io.validation.service.ValidatorConfigurationService)

Example 4 with IOMessageImpl

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

the class PartitioningWFSWriter method execute.

@Override
protected IOReport execute(final ProgressIndicator progress, final IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Upload to WFS-T", IProgressMonitor.UNKNOWN);
    try {
        progress.setCurrentTask("Partitioning data");
        // create the partitioner
        InstanceCollectionPartitioner partitioner = StreamGmlWriter.getPartitioner(this, reporter);
        // partition the graph
        int threshold = getParameter(PARAM_INSTANCES_THRESHOLD).as(Integer.class, DEFAULT_INSTANCES_THRESHOLD);
        try (ResourceIterator<InstanceCollection> parts = partitioner.partition(getInstances(), threshold, reporter)) {
            if (partitioner.requiresImmediateConsumption()) {
                // handle all parts right here, one after another
                int partCount = 0;
                boolean failed = false;
                if (parts.hasNext()) {
                    while (parts.hasNext() && !progress.isCanceled()) {
                        partCount++;
                        SubtaskProgressIndicator partitionProgress = new SubtaskProgressIndicator(progress);
                        partitionProgress.begin("Assembling part " + partCount, ProgressIndicator.UNKNOWN);
                        InstanceCollection part = parts.next();
                        partitionProgress.end();
                        progress.setCurrentTask("Upload part " + partCount + ((part.hasSize()) ? (" (" + part.size() + " instances)") : ("")));
                        IOReport report = uploadInstances(part, reporter, new SubtaskProgressIndicator(progress));
                        if (!report.isSuccess()) {
                            failed = true;
                            reporter.error("Upload of part {0} - {1}", partCount, report.getSummary());
                        } else {
                            reporter.info("Upload of part {0} - {1}", partCount, report.getSummary());
                        }
                    }
                    reporter.setSuccess(!failed && reporter.getErrors().isEmpty());
                    if (!reporter.isSuccess()) {
                        reporter.setSummary("Errors during upload to WFS-T, please see the report.");
                    } else {
                        reporter.setSummary("Successfully uploaded data via WFS-T");
                    }
                } else {
                    reporter.setSuccess(false);
                    reporter.setSummary("Partitioning yielded no instances to upload");
                }
            } else {
                // can start requests with separate thread (potentially
                // threads, but tests with WFSes show that this usually is
                // too much to handle for the service)
                int partCount = 0;
                final AtomicBoolean failed = new AtomicBoolean();
                if (parts.hasNext()) {
                    ExecutorService requestThread = Executors.newSingleThreadExecutor();
                    while (parts.hasNext() && !progress.isCanceled()) {
                        partCount++;
                        SubtaskProgressIndicator partitionProgress = new SubtaskProgressIndicator(// only used for first
                        progress);
                        // partitioning
                        if (partCount == 1)
                            partitionProgress.begin("Assembling part " + partCount, ProgressIndicator.UNKNOWN);
                        // not
                        final InstanceCollection part = parts.next();
                        // safe
                        if (partCount == 1)
                            partitionProgress.end();
                        progress.setCurrentTask("Upload part " + partCount + ((part.hasSize()) ? (" (" + part.size() + " instances)") : ("")));
                        final int currentPart = partCount;
                        requestThread.submit(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    IOReport report = uploadInstances(part, reporter, new SubtaskProgressIndicator(progress));
                                    if (!report.isSuccess()) {
                                        failed.set(true);
                                        reporter.error(new IOMessageImpl("Upload of part " + currentPart + " - " + report.getSummary(), null));
                                    } else {
                                        reporter.info(new IOMessageImpl("Upload of part " + currentPart + " - " + report.getSummary(), null));
                                    }
                                } catch (Exception e) {
                                    failed.set(true);
                                    reporter.error(new IOMessageImpl("Upload of part " + currentPart + " failed", e));
                                }
                            }
                        });
                    }
                    // wait for requests completion
                    requestThread.shutdown();
                    if (!requestThread.awaitTermination(24, TimeUnit.HOURS)) {
                        reporter.error(new IOMessageImpl("Timeout reached waiting for completion of WFS requests", null));
                    }
                    reporter.setSuccess(!failed.get() && reporter.getErrors().isEmpty());
                    if (!reporter.isSuccess()) {
                        reporter.setSummary("Errors during upload to WFS-T, please see the report.");
                    } else {
                        reporter.setSummary("Successfully uploaded data via WFS-T");
                    }
                } else {
                    reporter.setSuccess(false);
                    reporter.setSummary("Partitioning yielded no instances to upload");
                }
            }
        }
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Error during attempt to upload to WFS-T", e));
        reporter.setSuccess(false);
    } finally {
        progress.end();
    }
    return reporter;
}
Also used : InstanceCollectionPartitioner(eu.esdihumboldt.hale.common.instance.tools.InstanceCollectionPartitioner) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) SubtaskProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.SubtaskProgressIndicator) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService)

Example 5 with IOMessageImpl

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

the class XLSInstanceReader method createInstanceCollection.

/**
 * create instances, see
 * {@link CSVInstanceReader#execute(ProgressIndicator, IOReporter)}
 *
 * @param row the current row
 * @param reporter the reporter of the writer
 * @param solveNestedProperties true, if schema should not be flat <b>(not
 *            implemented yet)</b>
 */
@SuppressWarnings("javadoc")
private void createInstanceCollection(List<String> row, IOReporter reporter) {
    MutableInstance instance = new DefaultInstance(type, null);
    // int propertyIndex = 0;
    for (int index = 0; index < propAr.length; index++) {
        String part = null;
        if (index < row.size())
            part = row.get(index);
        if (part != null) {
            PropertyDefinition property = propAr[index];
            if (part.isEmpty()) {
                // FIXME make this configurable
                part = null;
            }
            Object value = part;
            if (value != null) {
                Binding binding = property.getPropertyType().getConstraint(Binding.class);
                try {
                    if (!binding.getBinding().equals(String.class)) {
                        ConversionService conversionService = HalePlatform.getService(ConversionService.class);
                        if (conversionService.canConvert(String.class, binding.getBinding())) {
                            value = conversionService.convert(part, binding.getBinding());
                        } else {
                            throw new IllegalStateException("Conversion not possible!");
                        }
                    }
                } catch (Exception e) {
                    reporter.error(new IOMessageImpl("Cannot convert property value to {0}", e, line, -1, binding.getBinding().getSimpleName()));
                }
                instance.addProperty(property.getName(), value);
            }
        // propertyIndex++;
        }
    }
    instances.add(instance);
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) ConversionService(org.springframework.core.convert.ConversionService) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Aggregations

IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)85 IOException (java.io.IOException)43 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)33 QName (javax.xml.namespace.QName)20 URI (java.net.URI)15 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)14 InputStream (java.io.InputStream)13 File (java.io.File)12 HashMap (java.util.HashMap)11 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)9 FileOutputStream (java.io.FileOutputStream)9 ArrayList (java.util.ArrayList)9 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)8 OutputStream (java.io.OutputStream)8 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)7 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)7 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)6 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)5 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)4 Value (eu.esdihumboldt.hale.common.core.io.Value)4