teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.model; felix@1116: felix@1116: import java.util.List; felix@1116: felix@1965: import net.sf.ehcache.Cache; felix@1965: import net.sf.ehcache.Element; felix@1965: teichmann@5831: import org.dive4elements.river.backend.SessionHolder; teichmann@5831: import org.dive4elements.river.model.CrossSection; teichmann@5831: import org.dive4elements.river.model.River; felix@1116: teichmann@5831: import org.dive4elements.river.artifacts.cache.CacheFactory; felix@1965: felix@1116: import org.hibernate.Session; felix@1116: import org.hibernate.Query; felix@1116: felix@1965: felix@1116: /** felix@1116: * Get Cross Sections. felix@1116: */ felix@1116: public class CrossSectionFactory { felix@1116: felix@1965: protected final static String CACHE_NAME = "cross_sections"; felix@1965: felix@1965: // TODO use caching consistently, streamline acces. felix@1116: /** felix@1116: * Get CrossSections for an instantiated River. felix@1116: * felix@1116: * @param river river object. felix@1116: * felix@1116: * @return List of Cross Sections of river. felix@1116: */ felix@1116: public static List getCrossSections(River river) { felix@1116: return getCrossSections(river.getName()); felix@1116: } felix@1116: felix@1116: felix@1116: /** felix@1116: * Get Cross Sections for a river by name. felix@1116: * felix@3269: * @param riverName name of the river of interest. felix@1116: * felix@1116: * @return List of Cross Sections of river. felix@1116: */ felix@1116: public static List getCrossSections(String riverName) { felix@1116: Session session = SessionHolder.HOLDER.get(); felix@1116: Query query = session.createQuery( felix@2142: "from CrossSection where river.name = :rivername"); felix@1116: query.setParameter("rivername", riverName); felix@1116: return query.list(); felix@1116: } felix@1965: felix@1965: felix@2039: felix@2039: /** felix@2039: * Get a specific CrossSection from db. felix@2039: * @param id The dbid of the cross-section to load. felix@2039: */ felix@1965: public static CrossSection getCrossSection(int id) { felix@1965: Cache cache = CacheFactory.getCache(CACHE_NAME); felix@1965: if (cache != null) { felix@1965: Element element = cache.get(Integer.valueOf(id)); felix@1965: if (element != null) { felix@1965: return (CrossSection) element.getValue(); felix@1965: } felix@1965: } felix@1965: felix@1965: CrossSection section = getCrossSectionUncached(id); felix@1965: if (cache != null) { felix@1965: Element element = new Element(Integer.valueOf(id), section); felix@1965: cache.put(element); felix@1965: } felix@1965: felix@1965: return section; felix@1965: } felix@1965: felix@1965: felix@1965: /** Get specific CrossSection from database. */ felix@1965: protected static CrossSection getCrossSectionUncached(int id) { felix@1965: Session session = SessionHolder.HOLDER.get(); felix@1965: Query query = session.createQuery( felix@1965: "from CrossSection where id=:id"); felix@1965: query.setParameter("id", id); felix@1965: List list = query.list(); felix@1965: return list.isEmpty() ? null : list.get(0); felix@1965: } felix@1116: } felix@1116: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :