ingo@2063: package de.intevation.flys.artifacts.model; ingo@2063: ingo@2063: import net.sf.ehcache.Cache; ingo@2063: import net.sf.ehcache.Element; ingo@2063: ingo@2063: import org.apache.log4j.Logger; ingo@2063: ingo@2063: import de.intevation.flys.model.Annotation; ingo@2063: import de.intevation.flys.model.Position; ingo@2063: ingo@2063: import de.intevation.flys.artifacts.cache.CacheFactory; ingo@2063: import de.intevation.flys.artifacts.model.AnnotationsFactory; ingo@2063: ingo@2063: ingo@2063: public class LocationProvider { ingo@2063: ingo@2063: public static final String CACHE_KEY = "location-provider"; ingo@2063: ingo@2063: ingo@2063: private static final Logger logger = ingo@2063: Logger.getLogger(LocationProvider.class); ingo@2063: ingo@2063: ingo@2063: private LocationProvider() { ingo@2063: } ingo@2063: ingo@2063: ingo@2063: public static String getLocation(String river, double km) { ingo@2063: return getLocation(getLocationHash(river, km), river, km); ingo@2063: } ingo@2063: ingo@2063: ingo@2063: public static String getLocation(String hash, String river, double km) { ingo@2063: logger.debug("Fetch location for '" + river + "' at '" + km + "'"); ingo@2063: ingo@2063: Cache cache = CacheFactory.getCache(CACHE_KEY); ingo@2063: ingo@2063: if (cache != null) { ingo@2063: return getCachedLocation(cache, hash, river, km); ingo@2063: } ingo@2063: else { ingo@2063: logger.info("No Cache for Locations configured."); ingo@2063: return getUncachedLocation(river, km); ingo@2063: } ingo@2063: } ingo@2063: ingo@2063: ingo@2063: protected static String getCachedLocation( ingo@2063: Cache cache, ingo@2063: String hash, ingo@2063: String river, ingo@2063: double km ingo@2063: ) { ingo@2063: logger.debug("Fetch location from cache."); ingo@2063: ingo@2063: Element element = cache.get(hash); ingo@2063: ingo@2063: if (element == null) { ingo@2063: logger.debug("Element is not in cache yet."); ingo@2063: ingo@2063: String location = getUncachedLocation(river, km); ingo@2063: element = new Element(hash, location); ingo@2063: cache.put(element); ingo@2063: } ingo@2063: ingo@2063: return (String) element.getValue(); ingo@2063: } ingo@2063: ingo@2063: ingo@2063: protected static String getUncachedLocation(String river, double km) { ingo@2063: logger.debug("Fetch location from backend."); ingo@2063: ingo@2063: Annotation annotation = AnnotationsFactory.getAnnotation(river, km); ingo@2063: ingo@2063: if (annotation != null) { ingo@2063: logger.debug("Found an annotation."); ingo@2063: Position pos = annotation.getPosition(); ingo@2063: return pos.getValue(); ingo@2063: } ingo@2063: ingo@2063: return ""; ingo@2063: } ingo@2063: ingo@2063: ingo@2063: protected static String getLocationHash(String river, double km) { ingo@2063: return river + "#" + km; ingo@2063: } ingo@2063: } ingo@2063: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :