Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
1491:2a00f4849738 | 3814:8083f6384023 |
---|---|
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 : |