view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java @ 2792:fe987587ebc9

Merged revisions 4539-4540,4543,4545-4546 via svnmerge from file:///home/clients/bsh/bsh-generischer-viewer/Material/SVN/flys-artifacts/trunk ........ r4539 | teichmann | 2012-05-27 20:02:13 +0200 (So, 27 Mai 2012) | 1 line FixA: Added forgotten csv/report facets/generators to conf. ........ r4540 | teichmann | 2012-05-27 20:11:31 +0200 (So, 27 Mai 2012) | 1 line FixA: Fixed class cast bug in report facet. ........ r4543 | teichmann | 2012-05-28 20:35:01 +0200 (Mo, 28 Mai 2012) | 1 line FixA: Added facet to return delta w/t as CSV ........ r4545 | teichmann | 2012-05-28 22:59:27 +0200 (Mo, 28 Mai 2012) | 1 line FixA: Made Delta W/t calculation finally work ........ r4546 | teichmann | 2012-05-28 23:34:24 +0200 (Mo, 28 Mai 2012) | 1 line FixA: corrected fitting (Q->W instead W->Q). ........ flys-artifacts/tags/2.7@4547 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 29 May 2012 04:58:29 +0000
parents c9815016a3bb
children b2ea89a665bc
line wrap: on
line source
package de.intevation.flys.artifacts.model;

import java.util.List;

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

import de.intevation.flys.backend.SessionHolder;
import de.intevation.flys.model.CrossSection;
import de.intevation.flys.model.River;

import de.intevation.flys.artifacts.cache.CacheFactory;

import org.hibernate.Session;
import org.hibernate.Query;


/**
 * Get Cross Sections.
 */
public class CrossSectionFactory {

    protected final static String CACHE_NAME = "cross_sections";

    // TODO use caching consistently, streamline acces.
    /**
     * Get CrossSections for an instantiated River.
     *
     * @param river river object.
     *
     * @return List of Cross Sections of river.
     */
    public static List<CrossSection> getCrossSections(River river) {
        return getCrossSections(river.getName());
    }


    /**
     * Get Cross Sections for a river by name.
     *
     * @param river name of the river of interest.
     *
     * @return List of Cross Sections of river.
     */
    public static List<CrossSection> getCrossSections(String riverName) {
        Session session = SessionHolder.HOLDER.get();
        Query query = session.createQuery(
            "from CrossSection where river.name = :rivername");
        query.setParameter("rivername", riverName);
        return query.list();
    }


    /**
     * True if the given section is the "newest" for that river.
     * @param section Given section
     * @return true if the section has the most advanced end of its validity interval
     *         or the most advanced start of its validity interval.
     */
    public static boolean isNewest(CrossSection section) {
        Session session = SessionHolder.HOLDER.get();
        Query query = session.createQuery(
            "from CrossSection where river.id = :riverid "
            + " order by timeInterval.stopTime desc, timeInterval.startTime desc");
        query.setParameter("riverid", section.getRiver().getId());

        List result = query.list();

        if (result == null || result.isEmpty()) {
            return true;
        }
        else {
            CrossSection cs = (CrossSection) result.get(0);
            return section.getId().equals(cs.getId());
        }
    }


    /**
     *  Get a specific CrossSection from db.
     *  @param id The dbid of the cross-section to load.
     */
    public static CrossSection getCrossSection(int id) {
        Cache cache = CacheFactory.getCache(CACHE_NAME);
        if (cache != null) {
            Element element = cache.get(Integer.valueOf(id));
            if (element != null) {
                return (CrossSection) element.getValue();
            }
        }

        CrossSection section = getCrossSectionUncached(id);
        if (cache != null) {
            Element element = new Element(Integer.valueOf(id), section);
            cache.put(element);
        }

        return section;
    }


    /** Get specific CrossSection from database. */
    protected static CrossSection getCrossSectionUncached(int id) {
        Session session = SessionHolder.HOLDER.get();
        Query query = session.createQuery(
                "from CrossSection where id=:id");
        query.setParameter("id", id);
        List<CrossSection> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org