view artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/App.java @ 9232:5030c46d8cb4

Implemented xpath function that selectes the 'mean year' of a fixation-artifact. Needs to provide the ArtifactDatabase to the builder implementation.
author gernotbelger
date Fri, 06 Jul 2018 13:09:54 +0200
parents dbb26bc81843
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.artifacts.datacage.templating;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.common.utils.Config;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.river.artifacts.datacage.Recommendations;
import org.dive4elements.river.backend.SessionFactoryProvider;
import org.hibernate.Session;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.w3c.dom.Document;

public class App {
    private static Logger log = Logger.getLogger(App.class);

    /**
     * The logging is done via Log4j. To configure the logging
     * a file 'log4j.properties' is search in the configuration directory.
     */
    private static final String LOG4J_PROPERTIES = "log4j.properties";

    public static final String template = System.getProperty("meta.data.template", "meta-data.xml");

    public static final String userId = System.getProperty("user.id");

    public static final String PARAMETERS = System.getProperty("meta.data.parameters", "");

    public static final String OUTPUT = System.getProperty("meta.data.output");

    public static Map<String, Object> getParameters() {
        final HashMap<String, Object> map = new HashMap<>();
        final String[] parts = PARAMETERS.split("\\s*;\\s*");
        for (final String part : parts) {
            final String[] kv = part.split("\\s*:\\s*");
            if (kv.length < 2 || (kv[0] = kv[0].trim()).length() == 0) {
                continue;
            }
            final String[] values = kv[1].split("\\s*,\\s*");
            map.put(kv[0], values.length == 1 ? values[0] : values);
        }
        return map;
    }

    /**
     * Trys to load the Log4j configuration from ${config.dir}/log4j.properties.
     */
    private static final void configureLogging() {
        final File configDir = Config.getConfigDirectory();
        final File propFile = new File(configDir, LOG4J_PROPERTIES);

        if (propFile.isFile() && propFile.canRead()) {
            try {
                PropertyConfigurator.configure(propFile.toURI().toURL());
                SLF4JBridgeHandler.install();
            }
            catch (final MalformedURLException mue) {
                mue.printStackTrace(System.err);
            }
        }
    }

    public static void main(final String[] args) {

        configureLogging();

        final Recommendations rec = Recommendations.createRecommendations(new File(template));

        if (rec == null) {
            System.err.println("No recommendations created");
            return;
        }

        final Document result = XMLUtils.newDocument();

        final Map<String, Object> parameters = getParameters();
        // REMARK/TODO: 'ARTIFACT-OUTS' needs a String-Array, should be handled by parser
        parameters.put("ARTIFACT-OUTS", new String[] { "LONGITUDINAL_SECTION" });
        parameters.put("PARAMETERS", parameters);
        parameters.put("USER-ID", userId);

        final Session session = SessionFactoryProvider.createSessionFactory().openSession();

        try {
            final CallContext context = null;
            rec.recommend(parameters, userId, result, session, context);
        } finally {
            session.close();
        }

        OutputStream out;

        if (OUTPUT == null) {
            out = System.out;
        } else {
            try {
                out = new FileOutputStream(OUTPUT);
            }
            catch (final IOException ioe) {
                log.error(ioe);
                return;
            }
        }

        try {
            out.write('\n');
            XMLUtils.toStream(result, out);
            out.write('\n');
        }
        catch (final IOException e) {
            log.error(e);
        } finally {
            if (OUTPUT != null) {
                try {
                    out.close();
                }
                catch (final IOException ioe) {
                    log.error(ioe);
                }
            }
        }
        System.exit(0);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org