Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java @ 2002:98f3ef8da857
Register under a key on blackboard to meet assumptions by areaartifact.
flys-artifacts/trunk@3444 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Fri, 16 Dec 2011 14:08:22 +0000 |
parents | e71615d95afb |
children | f7e2a84a6d88 |
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(); } /** Get a specific CrossSection from db. */ 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 :