# HG changeset patch # User Felix Wolfsteller # Date 1323445942 0 # Node ID e71615d95afb55258018eacfd492e0b9306cedb3 # Parent 85e442933e6dbd0b8e6d0b255bb57a379733f48f Added method to CrossSectionFactory to get single CrossSection, employ cache. flys-artifacts/trunk@3376 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 85e442933e6d -r e71615d95afb flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Fri Dec 09 13:58:29 2011 +0000 +++ b/flys-artifacts/ChangeLog Fri Dec 09 15:52:22 2011 +0000 @@ -1,3 +1,9 @@ +2011-12-09 Felix Wolfsteller + + * src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java: + (getCrossSection, getCrossSectionUncached): New, access specific + CrossSection, employ caching. + 2011-12-09 Felix Wolfsteller * src/main/java/de/intevation/flys/artifacts/CrossSectionArtifact.java: diff -r 85e442933e6d -r e71615d95afb flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java Fri Dec 09 13:58:29 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java Fri Dec 09 15:52:22 2011 +0000 @@ -2,18 +2,27 @@ 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. * @@ -40,5 +49,36 @@ 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 list = query.list(); + return list.isEmpty() ? null : list.get(0); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :