changeset 1965:e71615d95afb

Added method to CrossSectionFactory to get single CrossSection, employ cache. flys-artifacts/trunk@3376 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 09 Dec 2011 15:52:22 +0000
parents 85e442933e6d
children f572536af56c
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java
diffstat 2 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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	<felix.wolfsteller@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java:
+	  (getCrossSection, getCrossSectionUncached): New, access specific
+	  CrossSection, employ caching.
+
 2011-12-09	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/CrossSectionArtifact.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<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