view artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensityFactory.java @ 7198:f707ee04ac80

issue1435: Prepare SedimentDensityFactory to delive data by sedimentdesity.id and .year.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 30 Sep 2013 09:08:50 +0200
parents 1750b4448c0c
children 8079e3ba31c9
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.backend.SessionHolder;


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

    private static final String DENSITY_CACHE_NAME = "sedimentdensity";

    /** Query to get sediment density values and kms. */
    private static final String SQL_SELECT_DENSITY =
        "SELECT sdv.station AS km, " +
        "       sdv.density AS density," +
        "       sdv.year AS year " +
        "   FROM sediment_density sd" +
        "       JOIN rivers r ON sd.river_id = r.id " +
        "       JOIN sediment_density_values sdv ON sd.id = sdv.sediment_density_id" +
        "   WHERE r.name = :name";

    /** Query to get sediment density values and kms by id and year. */
    private static final String SQL_SELECT_DENSITY_BY_ID_YEAR =
        "SELECT sdv.station AS km, " +
        "       sdv.density AS density," +
        "       sdv.year AS year " +
        "   FROM sediment_density sd" +
        "       JOIN sediment_density_values sdv ON sd.id = sdv.sediment_density_id" +
        "   WHERE sdv.year = :year AND sd.id = :id";

    private SedimentDensityFactory() {}

    public static SedimentDensity getSedimentDensity(
        String river,
        double startKm,
        double endKm,
        int year
    ) {
        log.debug("getSedimentDensity");
        Cache cache = CacheFactory.getCache(DENSITY_CACHE_NAME);

        if (cache == null) {
            log.debug("Cache not configured.");
            return getSedimentDensityUncached(river, startKm, endKm, year);
        }

        String key = river + startKm + endKm;
        Element element = cache.get(key);
        if (element != null) {
            log.debug("SedimentDensity found in cache!");
            return (SedimentDensity)element.getValue();
        }
        SedimentDensity value =
            getSedimentDensityUncached(river, startKm, endKm, year);

        if (value != null && key != null) {
            log.debug("Store sediment density values in cache.");
            element = new Element(key, value);
            cache.put(element);
        }
        return value;
    }

    private static SedimentDensity getSedimentDensityUncached(
        String river,
        double startKm,
        double endKm,
        int year
    ) {
        log.debug("getSedimentDensityUncached");
        Session session = SessionHolder.HOLDER.get();
        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_DENSITY)
            .addScalar("km", StandardBasicTypes.DOUBLE)
            .addScalar("density", StandardBasicTypes.DOUBLE)
            .addScalar("year", StandardBasicTypes.INTEGER);
        sqlQuery.setString("name", river);
        List<Object[]> results = sqlQuery.list();
        SedimentDensity density = new SedimentDensity();
        for (Object[] row : results) {
            if (row[0] != null && row[1] != null && row[2] != null) {
                density.addDensity((Double)row[0], (Double)row[1], (Integer)row[2]);
            }
        }

        density.cleanUp();
        return density;
    }

    public static SedimentDensity getSedimentDensityUncached(
        int id,
        int year
    ) {
        log.debug("getSedimentDensityUncached id/year");
        Session session = SessionHolder.HOLDER.get();
        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_DENSITY)
            .addScalar("km", StandardBasicTypes.DOUBLE)
            .addScalar("density", StandardBasicTypes.DOUBLE)
            .addScalar("year", StandardBasicTypes.INTEGER);
        sqlQuery.setInteger("id", id);
        sqlQuery.setInteger("year", year);
        List<Object[]> results = sqlQuery.list();
        SedimentDensity density = new SedimentDensity();
        for (Object[] row : results) {
            if (row[0] != null && row[1] != null && row[2] != null) {
                density.addDensity((Double)row[0], (Double)row[1], (Integer)row[2]);
            }
        }

        density.cleanUp();
        return density;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org