Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java @ 3814:8083f6384023
merged flys-artifacts/pre2.6-2012-01-04
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:56 +0200 |
parents | 97a25b54eea3 |
children | 0c7847b8e85e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java Fri Sep 28 12:14:56 2012 +0200 @@ -0,0 +1,89 @@ +package de.intevation.flys.artifacts.model; + +import net.sf.ehcache.Cache; +import net.sf.ehcache.Element; + +import org.apache.log4j.Logger; + +import de.intevation.flys.model.Annotation; +import de.intevation.flys.model.Position; + +import de.intevation.flys.artifacts.cache.CacheFactory; +import de.intevation.flys.artifacts.model.AnnotationsFactory; + + +public class LocationProvider { + + public static final String CACHE_KEY = "location-provider"; + + + private static final Logger logger = + Logger.getLogger(LocationProvider.class); + + + private LocationProvider() { + } + + + public static String getLocation(String river, double km) { + return getLocation(getLocationHash(river, km), river, km); + } + + + public static String getLocation(String hash, String river, double km) { + logger.debug("Fetch location for '" + river + "' at '" + km + "'"); + + Cache cache = CacheFactory.getCache(CACHE_KEY); + + if (cache != null) { + return getCachedLocation(cache, hash, river, km); + } + else { + logger.info("No Cache for Locations configured."); + return getUncachedLocation(river, km); + } + } + + + protected static String getCachedLocation( + Cache cache, + String hash, + String river, + double km + ) { + logger.debug("Fetch location from cache."); + + Element element = cache.get(hash); + + if (element == null) { + logger.debug("Element is not in cache yet."); + + String location = getUncachedLocation(river, km); + element = new Element(hash, location); + cache.put(element); + } + + return (String) element.getValue(); + } + + + protected static String getUncachedLocation(String river, double km) { + logger.debug("Fetch location from backend."); + + Annotation annotation = AnnotationsFactory.getAnnotation(river, km); + + if (annotation != null) { + logger.debug("Found an annotation."); + Position pos = annotation.getPosition(); + return pos.getValue(); + } + + return ""; + } + + + protected static String getLocationHash(String river, double km) { + return river + "#" + km; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :