Search in sources :

Example 56 with IOMessageImpl

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

the class JacksonMapper method skipChoice.

/**
 * Handles skipping choice groups.
 *
 * @param propertyName the start property name
 * @param obj the object to inspect
 * @param reporter the reporter
 * @return a pair of property name and value to use
 */
private Pair<String, Object> skipChoice(String propertyName, Object obj, IOReporter reporter) {
    if (obj instanceof Group) {
        Group group = (Group) obj;
        // For choices search for the (only!) child and skip the choice.
        if (group.getDefinition() instanceof GroupPropertyDefinition) {
            if (((GroupPropertyDefinition) group.getDefinition()).getConstraint(ChoiceFlag.class).isEnabled()) {
                Iterator<QName> childPropertyNames = group.getPropertyNames().iterator();
                if (!childPropertyNames.hasNext()) {
                    reporter.warn(new IOMessageImpl("Found an empty choice.", null));
                    return null;
                }
                QName childPropertyName = childPropertyNames.next();
                Object[] values = group.getProperty(childPropertyName);
                Object value = values[0];
                if (childPropertyNames.hasNext() || values.length > 1)
                    reporter.warn(new IOMessageImpl("Found a choice with multiple children. Using first.", null));
                // delegate to only value
                return skipChoice(childPropertyName.getLocalPart(), value, reporter);
            }
        }
    }
    return new Pair<>(propertyName, obj);
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) Group(eu.esdihumboldt.hale.common.instance.model.Group) QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ChoiceFlag(eu.esdihumboldt.hale.common.schema.model.constraint.property.ChoiceFlag) Pair(eu.esdihumboldt.util.Pair)

Example 57 with IOMessageImpl

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

the class OrdinatesToPointTranslator method getNewParameters.

/**
 * @see eu.esdihumboldt.hale.io.oml.helper.FunctionTranslator#getNewParameters(java.util.List,
 *      eu.esdihumboldt.hale.common.align.io.impl.internal.CellBean,
 *      eu.esdihumboldt.hale.common.core.io.report.IOReporter,
 *      eu.esdihumboldt.hale.io.oml.internal.model.align.ICell)
 */
@Override
public List<ParameterValueBean> getNewParameters(List<ParameterValueBean> params, CellBean cellBean, IOReporter reporter, ICell cell) {
    reporter.warn(new IOMessageImpl("Behavior of this function has changed.", null));
    List<NamedEntityBean> src = cellBean.getSource();
    for (int i = 0; i < src.size(); i++) {
        NamedEntityBean bean = src.get(i);
        if (i == 0) {
            bean.setName("X");
        }
        if (i == 1) {
            bean.setName("Y");
        }
        if (i == 2) {
            bean.setName("Z");
        }
    }
    return params;
}
Also used : NamedEntityBean(eu.esdihumboldt.hale.common.align.io.impl.internal.NamedEntityBean) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)

Example 58 with IOMessageImpl

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

the class ProjectParser method load.

private void load(URI source, String actionId, String msgCT, String msgIO, @SuppressWarnings("rawtypes") Class clazz) {
    // create IOConfiguration for source data
    IOConfiguration conf = new IOConfiguration();
    // populate IOConfiguration
    // set action ID
    conf.setActionId(actionId);
    // find provider
    File file;
    try {
        file = new File(source);
    } catch (IllegalArgumentException e) {
        file = null;
    }
    @SuppressWarnings("unchecked") IContentType ct = HaleIO.findContentType(clazz, new DefaultInputSupplier(source), (file == null) ? (null) : (file.getAbsolutePath()));
    if (ct == null) {
        report.error(new IOMessageImpl(msgCT, null, -1, -1, source));
        return;
    }
    @SuppressWarnings("unchecked") IOProviderDescriptor srf = HaleIO.findIOProviderFactory(clazz, ct, null);
    if (srf == null) {
        report.error(new IOMessageImpl(msgIO, null, -1, -1, source));
        return;
    }
    conf.setProviderId(srf.getIdentifier());
    // provider configuration
    // source
    conf.getProviderConfiguration().put(AbstractImportProvider.PARAM_SOURCE, Value.of(source.toString()));
    // content type
    conf.getProviderConfiguration().put(AbstractImportProvider.PARAM_CONTENT_TYPE, Value.of(ct.getId()));
    // no dependencies needed
    // add configuration to project
    project.getResources().add(conf);
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IContentType(org.eclipse.core.runtime.content.IContentType) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) File(java.io.File)

Example 59 with IOMessageImpl

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

the class ShapeInstanceReader method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    // $NON-NLS-1$
    progress.begin(Messages.getString("ShapeSchemaProvider.1"), ProgressIndicator.UNKNOWN);
    // DataStore store = new ShapefileDataStoreFactory().createDataStore(location.toURL());
    // DataStore store = FileDataStoreFinder.getDataStore(getSource().getLocation().toURL());
    ShapefileDataStore store = new ShapefileDataStore(getSource().getLocation().toURL());
    store.setCharset(getCharset());
    progress.setCurrentTask("Extracting shape instances");
    String typename = getParameter(PARAM_TYPENAME).as(String.class);
    TypeDefinition defaultType = null;
    if (typename != null && !typename.isEmpty()) {
        try {
            defaultType = getSourceSchema().getType(QName.valueOf(typename));
        } catch (Exception e) {
        // ignore
        }
    }
    if (defaultType == null) {
        // check if typename was supplied w/o namespace
        try {
            defaultType = getSourceSchema().getType(new QName(ShapefileConstants.SHAPEFILE_NS, typename));
        } catch (Exception e) {
        // ignore
        // TODO report?
        }
    }
    if (defaultType == null) {
        reporter.info(new IOMessageImpl("No type name supplied as parameter, trying to auto-detect the schema type.", null));
        TypeDefinition dataType = ShapeSchemaReader.readShapeType(getSource());
        if (dataType == null) {
            throw new IOException("Could not read shapefile structure information");
        }
        String preferredName = null;
        Name name = store.getNames().iterator().next();
        if (name != null) {
            preferredName = name.getLocalPart();
        }
        Pair<TypeDefinition, Integer> tp = getMostCompatibleShapeType(getSourceSchema(), dataType, preferredName);
        if (tp == null) {
            throw new IOProviderConfigurationException("No schema type specified and auto-detection failed");
        }
        defaultType = tp.getFirst();
        reporter.info(new IOMessageImpl(MessageFormat.format("Auto-deteted {0} as schema type, with a {1}% compatibility rating.", defaultType.getName(), tp.getSecond()), null));
    }
    Map<TypeDefinition, InstanceCollection> collections = new HashMap<>();
    // create a collection for each type
    for (Name name : store.getNames()) {
        SimpleFeatureSource features = store.getFeatureSource(name);
        TypeDefinition type = defaultType;
        if (type == null) {
            QName typeName = new QName(ShapefileConstants.SHAPEFILE_NS, name.getLocalPart());
            type = getSourceSchema().getType(typeName);
        }
        collections.put(type, new ShapesInstanceCollection(features, type, getCrsProvider(), name.getLocalPart()));
    }
    instances = new PerTypeInstanceCollection(collections);
    reporter.setSuccess(true);
    return reporter;
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOException(java.io.IOException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Name(org.opengis.feature.type.Name) QName(javax.xml.namespace.QName) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection)

Example 60 with IOMessageImpl

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

the class AbstractWFSWriter method execute.

@Override
public IOReport execute(ProgressIndicator progress) throws IOProviderConfigurationException, IOException {
    progress.begin("WFS Transaction", ProgressIndicator.UNKNOWN);
    // configure internal provider
    internalProvider.setDocumentWrapper(createTransaction());
    final PipedInputStream pIn = new PipedInputStream();
    PipedOutputStream pOut = new PipedOutputStream(pIn);
    currentExecuteStream = pOut;
    Future<Response> futureResponse = null;
    IOReporter reporter = createReporter();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        // read the stream (in another thread)
        futureResponse = executor.submit(new Callable<Response>() {

            @Override
            public Response call() throws Exception {
                Proxy proxy = ProxyUtil.findProxy(targetWfs.getLocation());
                Request request = Request.Post(targetWfs.getLocation()).bodyStream(pIn, ContentType.APPLICATION_XML);
                Executor executor = FluentProxyUtil.setProxy(request, proxy);
                // authentication
                String user = getParameter(PARAM_USER).as(String.class);
                String password = getParameter(PARAM_PASSWORD).as(String.class);
                if (user != null) {
                    // target host
                    int port = targetWfs.getLocation().getPort();
                    String hostName = targetWfs.getLocation().getHost();
                    String scheme = targetWfs.getLocation().getScheme();
                    HttpHost host = new HttpHost(hostName, port, scheme);
                    // add credentials
                    Credentials cred = ClientProxyUtil.createCredentials(user, password);
                    executor.auth(new AuthScope(host), cred);
                    executor.authPreemptive(host);
                }
                try {
                    return executor.execute(request);
                } finally {
                    pIn.close();
                }
            }
        });
        // write the stream
        SubtaskProgressIndicator subprogress = new SubtaskProgressIndicator(progress);
        reporter = (IOReporter) super.execute(subprogress);
    } finally {
        executor.shutdown();
    }
    try {
        Response response = futureResponse.get();
        HttpResponse res = response.returnResponse();
        int statusCode = res.getStatusLine().getStatusCode();
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        if (statusCode >= 200 && statusCode < 300) {
            // success
            reporter.setSuccess(reporter.isSuccess());
            // construct summary from response
            try {
                Document responseDoc = parseResponse(res.getEntity());
                // totalInserted
                String inserted = xpath.compile("//TransactionSummary/totalInserted").evaluate(responseDoc);
                // XXX totalUpdated
                // XXX totalReplaced
                // XXX totalDeleted
                reporter.setSummary("Inserted " + inserted + " features.");
            } catch (XPathExpressionException e) {
                log.error("Error in XPath used to evaluate service response");
            } catch (ParserConfigurationException | SAXException e) {
                reporter.error(new IOMessageImpl(MessageFormat.format("Server returned status code {0}, but could not parse server response", statusCode), e));
                reporter.setSuccess(false);
            }
        } else {
            // failure
            reporter.error(new IOMessageImpl("Server reported failure with code " + res.getStatusLine().getStatusCode() + ": " + res.getStatusLine().getReasonPhrase(), null));
            reporter.setSuccess(false);
            try {
                Document responseDoc = parseResponse(res.getEntity());
                String errorText = xpath.compile("//ExceptionText/text()").evaluate(responseDoc);
                reporter.setSummary("Request failed: " + errorText);
            } catch (XPathExpressionException e) {
                log.error("Error in XPath used to evaluate service response");
            } catch (ParserConfigurationException | SAXException e) {
                reporter.error(new IOMessageImpl("Could not parse server response", e));
                reporter.setSuccess(false);
            }
        }
    } catch (ExecutionException | InterruptedException e) {
        reporter.error(new IOMessageImpl("Failed to execute WFS-T request", e));
        reporter.setSuccess(false);
    }
    progress.end();
    return reporter;
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PipedOutputStream(java.io.PipedOutputStream) Document(org.w3c.dom.Document) Callable(java.util.concurrent.Callable) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) Proxy(java.net.Proxy) Executor(org.apache.http.client.fluent.Executor) HttpHost(org.apache.http.HttpHost) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ExecutionException(java.util.concurrent.ExecutionException) XPath(javax.xml.xpath.XPath) Request(org.apache.http.client.fluent.Request) HttpResponse(org.apache.http.HttpResponse) SubtaskProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.SubtaskProgressIndicator) PipedInputStream(java.io.PipedInputStream) HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) ExecutorService(java.util.concurrent.ExecutorService) AuthScope(org.apache.http.auth.AuthScope) UserPasswordCredentials(eu.esdihumboldt.hale.common.core.io.config.UserPasswordCredentials) Credentials(org.apache.http.auth.Credentials)

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