comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/LocationProvider.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/LocationProvider.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.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 org.dive4elements.river.model.Annotation;
9 import org.dive4elements.river.model.FastAnnotations;
10
11 import org.dive4elements.river.artifacts.cache.CacheFactory;
12
13
14 /** Make FastAnnotations (db unbound) available. */
15 public class LocationProvider {
16
17 private static final Logger log =
18 Logger.getLogger(LocationProvider.class);
19
20
21 public static final String CACHE_KEY = "location-provider";
22
23 public static final String PREFIX = "lp-";
24
25
26 private LocationProvider() {
27 }
28
29 public static String getLocation(String river, double km) {
30
31 FastAnnotations fas = getAnnotations(river, km);
32
33 FastAnnotations.Annotation an = fas.findByKm(km);
34
35 return an != null ? an.getPosition() : null;
36 }
37
38 public static FastAnnotations getAnnotations(String river) {
39 return getAnnotations(river, Double.MAX_VALUE);
40 }
41
42 protected static FastAnnotations getAnnotations(String river, double km) {
43 // TODO issue880: Make annotations available _per type_
44
45 Cache cache = CacheFactory.getCache(CACHE_KEY);
46
47 if (cache == null) {
48 return uncachedAnnotations(river, km);
49 }
50
51 String key = PREFIX + river;
52
53 Element element = cache.get(key);
54
55 if (element != null) {
56 return (FastAnnotations)element.getValue();
57 }
58
59 FastAnnotations fas = uncachedAnnotations(river, Double.MAX_VALUE);
60
61 cache.put(new Element(key, fas));
62
63 return fas;
64 }
65
66 protected static FastAnnotations uncachedAnnotations(
67 String river,
68 double km
69 ) {
70 if (km != Double.MAX_VALUE) {
71 // XXX Fake it by using a standard Annotation.
72
73 Annotation annotation =
74 AnnotationsFactory.getAnnotation(river, km);
75
76 if (annotation != null) {
77 FastAnnotations.Annotation fa =
78 new FastAnnotations.Annotation(
79 km, Double.NaN,
80 annotation.getPosition().getValue(), null, null,
81 Double.NaN, Double.NaN);
82 return new FastAnnotations(
83 new FastAnnotations.Annotation [] { fa });
84 }
85
86 return new FastAnnotations(new FastAnnotations.Annotation[0]);
87 }
88
89 long startTime = System.currentTimeMillis();
90
91 FastAnnotations fas = new FastAnnotations(river);
92
93 long stopTime = System.currentTimeMillis();
94
95 if (log.isDebugEnabled()) {
96 log.debug("Loading locations took " +
97 (stopTime-startTime)/1000f + " secs.");
98 }
99
100 return fas;
101 }
102 }
103 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org