view artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/PorosityFactory.java @ 8202:e4606eae8ea5

sed src/**/*.java 's/logger/log/g'
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 05 Sep 2014 12:58:17 +0200
parents d9d2bd78eff6
children 0a5239a1e46e
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.model.minfo;

import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.type.StandardBasicTypes;

import org.dive4elements.river.artifacts.cache.CacheFactory;
import org.dive4elements.river.artifacts.model.StaticPorosityCacheKey;
import org.dive4elements.river.backend.SessionHolder;


public class PorosityFactory
{
    /** Private log to use here. */
    private static Logger log = Logger.getLogger(PorosityFactory.class);

    public static final String SQL_SELECT =
        "SELECT pv.station AS station, pv.porosity AS porosity " +
        "   FROM porosity p" +
        "       JOIN porosity_values pv on pv.porosity_id = p.id" +
        "   WHERE p.id = :porosity_id" +
        "   ORDER BY station";

    private PorosityFactory() {
    }


    /**
     * Get WKms for given column and wst_id, caring about the cache.
     */
    public static Porosity getPorosity(int porosity_id) {
        log.debug("PorosityFactory.getPorosity");
        Cache cache = CacheFactory.getCache(StaticPorosityCacheKey.CACHE_NAME);

        StaticPorosityCacheKey cacheKey;

        if (cache != null) {
            cacheKey = new StaticPorosityCacheKey(porosity_id);
            Element element = cache.get(cacheKey);
            if (element != null) {
                log.debug("Got static porosity values from cache");
                return (Porosity)element.getValue();
            }
        }
        else {
            cacheKey = null;
        }

        Porosity values = getPorosityUncached(porosity_id);

        if (values != null && cacheKey != null) {
            log.debug("Store static porosity values in cache.");
            Element element = new Element(cacheKey, values);
            cache.put(element);
        }
        return values;
    }

    private static Porosity getPorosityUncached(int porosity_id) {
        if (log.isDebugEnabled()) {
            log.debug("PorosityFactory.getPorosityUncached");
        }

        Session session = SessionHolder.HOLDER.get();
        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT)
                .addScalar("station", StandardBasicTypes.DOUBLE)
                .addScalar("porosity", StandardBasicTypes.DOUBLE);
        sqlQuery.setInteger("porosity_id", porosity_id);
        List<Object []> results = sqlQuery.list();

        Porosity porosities = new Porosity();
        for (Object [] row: results) {
            log.debug("got station: " + (Double)row[0]);
            porosities.add(
                (Double) row[0],
                (Double) row[1]);
        }
        return porosities;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org