comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java @ 2063:97a25b54eea3

Part 1 of #125: added a description for a location to the WaterlevelExport. flys-artifacts/trunk@3557 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 02 Jan 2012 08:50:50 +0000
parents
children 0c7847b8e85e
comparison
equal deleted inserted replaced
2062:ed6c6d437875 2063:97a25b54eea3
1 package de.intevation.flys.artifacts.model;
2
3 import net.sf.ehcache.Cache;
4 import net.sf.ehcache.Element;
5
6 import org.apache.log4j.Logger;
7
8 import de.intevation.flys.model.Annotation;
9 import de.intevation.flys.model.Position;
10
11 import de.intevation.flys.artifacts.cache.CacheFactory;
12 import de.intevation.flys.artifacts.model.AnnotationsFactory;
13
14
15 public class LocationProvider {
16
17 public static final String CACHE_KEY = "location-provider";
18
19
20 private static final Logger logger =
21 Logger.getLogger(LocationProvider.class);
22
23
24 private LocationProvider() {
25 }
26
27
28 public static String getLocation(String river, double km) {
29 return getLocation(getLocationHash(river, km), river, km);
30 }
31
32
33 public static String getLocation(String hash, String river, double km) {
34 logger.debug("Fetch location for '" + river + "' at '" + km + "'");
35
36 Cache cache = CacheFactory.getCache(CACHE_KEY);
37
38 if (cache != null) {
39 return getCachedLocation(cache, hash, river, km);
40 }
41 else {
42 logger.info("No Cache for Locations configured.");
43 return getUncachedLocation(river, km);
44 }
45 }
46
47
48 protected static String getCachedLocation(
49 Cache cache,
50 String hash,
51 String river,
52 double km
53 ) {
54 logger.debug("Fetch location from cache.");
55
56 Element element = cache.get(hash);
57
58 if (element == null) {
59 logger.debug("Element is not in cache yet.");
60
61 String location = getUncachedLocation(river, km);
62 element = new Element(hash, location);
63 cache.put(element);
64 }
65
66 return (String) element.getValue();
67 }
68
69
70 protected static String getUncachedLocation(String river, double km) {
71 logger.debug("Fetch location from backend.");
72
73 Annotation annotation = AnnotationsFactory.getAnnotation(river, km);
74
75 if (annotation != null) {
76 logger.debug("Found an annotation.");
77 Position pos = annotation.getPosition();
78 return pos.getValue();
79 }
80
81 return "";
82 }
83
84
85 protected static String getLocationHash(String river, double km) {
86 return river + "#" + km;
87 }
88 }
89 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org