view flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java @ 1245:0414181b0859

Fixed table river_axes_km. flys-backend/trunk@2723 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Schilberg <bjoern@intevation.de>
date Wed, 14 Sep 2011 06:57:28 +0000
parents 774dff3ee546
children 52d712cd2384
line wrap: on
line source
package de.intevation.flys.backend;

import de.intevation.artifacts.common.utils.Config;

import java.util.Properties;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;

import de.intevation.flys.model.Annotation;
import de.intevation.flys.model.AnnotationType;
import de.intevation.flys.model.Attribute;
import de.intevation.flys.model.Building;
import de.intevation.flys.model.CrossSection;
import de.intevation.flys.model.CrossSectionLine;
import de.intevation.flys.model.CrossSectionPoint;
import de.intevation.flys.model.CrossSectionTrack;
import de.intevation.flys.model.DGM;
import de.intevation.flys.model.DischargeTable;
import de.intevation.flys.model.DischargeTableValue;
import de.intevation.flys.model.Edge;
import de.intevation.flys.model.Fixpoint;
import de.intevation.flys.model.Floodplain;
import de.intevation.flys.model.Gauge;
import de.intevation.flys.model.HYK;
import de.intevation.flys.model.HYKEntry;
import de.intevation.flys.model.HYKFormation;
import de.intevation.flys.model.HYKFlowZoneType;
import de.intevation.flys.model.HYKFlowZone;
import de.intevation.flys.model.Line;
import de.intevation.flys.model.MainValueType;
import de.intevation.flys.model.NamedMainValue;
import de.intevation.flys.model.MainValue;
import de.intevation.flys.model.Position;
import de.intevation.flys.model.Range;
import de.intevation.flys.model.River;
import de.intevation.flys.model.RiverAxis;
import de.intevation.flys.model.TimeInterval;
import de.intevation.flys.model.WstColumn;
import de.intevation.flys.model.WstColumnQRange;
import de.intevation.flys.model.WstColumnValue;
import de.intevation.flys.model.Wst;
import de.intevation.flys.model.WstQRange;

import org.apache.log4j.Logger;

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

    public static final String XPATH_USER =
        "/artifact-database/backend-database/user/text()";

    public static final String XPATH_PASSWORD =
        "/artifact-database/backend-database/password/text()";

    public static final String XPATH_DIALECT =
        "/artifact-database/backend-database/dialect/text()";

    public static final String XPATH_DRIVER =
        "/artifact-database/backend-database/driver/text()";

    public static final String XPATH_URL =
        "/artifact-database/backend-database/url/text()";

    public static final String DEFAULT_USER =
        System.getProperty("flys.backend.user", "flys");

    public static final String DEFAULT_PASSWORD =
        System.getProperty("flys.backend.password", "flys");

    public static final String DEFAULT_DIALECT =
        System.getProperty(
            "flys.backend.dialect",
            "org.hibernate.dialect.PostgreSQLDialect");

    public static final String DEFAULT_DRIVER =
        System.getProperty(
            "flys.backend.driver",
            "org.postgresql.Driver");

    public static final String DEFAULT_URL =
        System.getProperty(
            "flys.backend.url",
            "jdbc:postgresql://localhost:5432/flys");

    private static SessionFactory sessionFactory;

    private SessionFactoryProvider() {
    }

    public static synchronized SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            String user =
                Config.getStringXPath(XPATH_USER, DEFAULT_USER);
            String password =
                Config.getStringXPath(XPATH_PASSWORD, DEFAULT_PASSWORD);
            String dialect =
                Config.getStringXPath(XPATH_DIALECT, DEFAULT_DIALECT);
            String driver =
                Config.getStringXPath(XPATH_DRIVER, DEFAULT_DRIVER);
            String url =
                Config.getStringXPath(XPATH_URL, DEFAULT_URL);

            sessionFactory = createSessionFactory(
                user, password, dialect, driver, url);
        }
        return sessionFactory;
    }

    public static SessionFactory createSessionFactory() {
        return createSessionFactory(
            DEFAULT_USER,
            DEFAULT_PASSWORD,
            DEFAULT_DIALECT,
            DEFAULT_DRIVER,
            DEFAULT_URL);
    }

    public static SessionFactory createSessionFactory(
        String user,
        String password,
        String dialect,
        String driver,
        String url
    ) {
        Configuration cfg = createConfiguration(
            user, password, dialect, driver, url);

        return cfg.buildSessionFactory();
    }

    public static Configuration createConfiguration() {
        return createConfiguration(
            DEFAULT_USER,
            DEFAULT_PASSWORD,
            DEFAULT_DIALECT,
            DEFAULT_DRIVER,
            DEFAULT_URL);
    }

    public static Configuration createConfiguration(
        String user,
        String password,
        String dialect,
        String driver,
        String url
    ) {
        Configuration cfg = new Configuration();

        // TODO: Use package reflection here.
        cfg.addAnnotatedClass(Annotation.class);
        cfg.addAnnotatedClass(AnnotationType.class);
        cfg.addAnnotatedClass(Attribute.class);
        cfg.addAnnotatedClass(Building.class);
        cfg.addAnnotatedClass(CrossSection.class);
        cfg.addAnnotatedClass(CrossSectionLine.class);
        cfg.addAnnotatedClass(CrossSectionPoint.class);
        cfg.addAnnotatedClass(CrossSectionTrack.class);
        cfg.addAnnotatedClass(DGM.class);
        cfg.addAnnotatedClass(DischargeTable.class);
        cfg.addAnnotatedClass(DischargeTableValue.class);
        cfg.addAnnotatedClass(Edge.class);
        cfg.addAnnotatedClass(Fixpoint.class);
        cfg.addAnnotatedClass(Floodplain.class);
        cfg.addAnnotatedClass(Gauge.class);
        cfg.addAnnotatedClass(HYK.class);
        cfg.addAnnotatedClass(HYKEntry.class);
        cfg.addAnnotatedClass(HYKFormation.class);
        cfg.addAnnotatedClass(HYKFlowZoneType.class);
        cfg.addAnnotatedClass(HYKFlowZone.class);
        cfg.addAnnotatedClass(Line.class);
        cfg.addAnnotatedClass(MainValueType.class);
        cfg.addAnnotatedClass(NamedMainValue.class);
        cfg.addAnnotatedClass(MainValue.class);
        cfg.addAnnotatedClass(Position.class);
        cfg.addAnnotatedClass(Range.class);
        cfg.addAnnotatedClass(River.class);
        cfg.addAnnotatedClass(RiverAxis.class);
        cfg.addAnnotatedClass(TimeInterval.class);
        cfg.addAnnotatedClass(WstColumn.class);
        cfg.addAnnotatedClass(WstColumnQRange.class);
        cfg.addAnnotatedClass(WstColumnValue.class);
        cfg.addAnnotatedClass(Wst.class);
        cfg.addAnnotatedClass(WstQRange.class);

        if (log.isDebugEnabled()) {
            log.debug("user: "    + user);
            log.debug("dialect: " + dialect);
            log.debug("driver: "  + driver);
            log.debug("url: "     + url);
        }

        Properties props = new Properties();

        // We rely on our own connection pool
        props.setProperty(
            "hibernate.connection.provider_class",
            "org.hibernate.connection.DBCPConnectionProvider");

        props.setProperty(Environment.DIALECT, dialect);
        props.setProperty(Environment.USER,    user);
        props.setProperty(Environment.PASS,    password);
        props.setProperty(Environment.DRIVER,  driver);
        props.setProperty(Environment.URL,     url);

        cfg.mergeProperties(props);

        return cfg;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org