comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java @ 922:95356252c309

Added the option to call the distance info service with a filter to fetch locations or distances only. flys-artifacts/trunk@2272 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 01 Jul 2011 08:33:52 +0000
parents eab5e5089d77
children a63d79107289
comparison
equal deleted inserted replaced
921:610d0e0f4f85 922:95356252c309
35 * 35 *
36 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 36 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
37 */ 37 */
38 public class DistanceInfoService extends DefaultService { 38 public class DistanceInfoService extends DefaultService {
39 39
40 private static enum DistanceFilter {
41 NONE, LOCATIONS, DISTANCES
42 }
43
40 /** The logger used in this service.*/ 44 /** The logger used in this service.*/
41 private static Logger logger = Logger.getLogger(DistanceInfoService.class); 45 private static Logger logger = Logger.getLogger(DistanceInfoService.class);
42 46
43 public static final String CACHE_NAME = "service-distanceinfo"; 47 public static final String CACHE_NAME = "service-distanceinfo";
44 48
45 public static final String RIVER_XPATH = "/art:river/text()"; 49 public static final String RIVER_XPATH = "/art:river/text()";
50
51 public static final String FILTER_XPATH = "/art:river/art:filter/text()";
46 52
47 53
48 /** 54 /**
49 * The default constructor. 55 * The default constructor.
50 */ 56 */
60 logger.debug("DistanceInfoService.process"); 66 logger.debug("DistanceInfoService.process");
61 67
62 String river = XMLUtils.xpathString( 68 String river = XMLUtils.xpathString(
63 data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE); 69 data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE);
64 70
71 String filter = XMLUtils.xpathString(
72 data, FILTER_XPATH, ArtifactNamespaceContext.INSTANCE);
73
65 if (river == null || (river = river.trim()).length() == 0) { 74 if (river == null || (river = river.trim()).length() == 0) {
66 logger.warn("No river specified. Cannot return distance info!"); 75 logger.warn("No river specified. Cannot return distance info!");
67 return XMLUtils.newDocument(); 76 return XMLUtils.newDocument();
68 } 77 }
69 78
71 80
72 Cache cache = CacheFactory.getCache(CACHE_NAME); 81 Cache cache = CacheFactory.getCache(CACHE_NAME);
73 82
74 if (cache == null) { 83 if (cache == null) {
75 logger.debug("no cache configured for distance info"); 84 logger.debug("no cache configured for distance info");
76 return getUncached(river); 85 return getUncached(river, filter);
77 } 86 }
78 87
79 net.sf.ehcache.Element element = cache.get(river); 88
89 String key = getCacheKey(river, filter);
90
91 net.sf.ehcache.Element element = cache.get(key);
80 92
81 if (element != null) { 93 if (element != null) {
82 logger.debug("distance info found in cache"); 94 logger.debug("distance info found in cache");
83 return (Document)element.getValue(); 95 return (Document)element.getValue();
84 } 96 }
85 97
86 Document result = getUncached(river); 98 Document result = getUncached(river, filter);
87 99
88 element = new net.sf.ehcache.Element(river, result); 100 element = new net.sf.ehcache.Element(key, result);
89 101
90 logger.debug("store distance info found into cache"); 102 logger.debug("store distance info found into cache");
91 103
92 cache.put(element); 104 cache.put(element);
93 105
94 return result; 106 return result;
95 } 107 }
96 108
97 protected Document getUncached(String river) { 109
110 protected String getCacheKey(String river, String filtertype) {
111 return filtertype != null && filtertype.length() > 0
112 ? river + "_" + filtertype
113 : river;
114 }
115
116
117 protected Document getUncached(String river, String filtertype) {
98 118
99 Document result = XMLUtils.newDocument(); 119 Document result = XMLUtils.newDocument();
100 120
101 Session session = SessionHolder.acquire(); 121 Session session = SessionHolder.acquire();
102 122
104 Iterator<Annotation> iter = 124 Iterator<Annotation> iter =
105 AnnotationsFactory.getAnnotationsIterator(river); 125 AnnotationsFactory.getAnnotationsIterator(river);
106 126
107 Element all = result.createElement("distances"); 127 Element all = result.createElement("distances");
108 128
129 DistanceFilter filter = getDistanceFilter(filtertype);
130
109 while (iter.hasNext()) { 131 while (iter.hasNext()) {
110 Annotation a = iter.next(); 132 Annotation a = iter.next();
111 Element distance = buildDistanceNode(result, a); 133 Element distance = buildDistanceNode(result, a, filter);
112 all.appendChild(distance); 134
135 if (distance != null) {
136 all.appendChild(distance);
137 }
113 } 138 }
114 139
115 result.appendChild(all); 140 result.appendChild(all);
116 } 141 }
117 finally { 142 finally {
118 session.close(); 143 session.close();
119 SessionHolder.release(); 144 SessionHolder.release();
120 } 145 }
121 146
122 return result; 147 return result;
148 }
149
150
151 protected static DistanceFilter getDistanceFilter(String type) {
152 if (type.equals("locations")) {
153 logger.debug("Found 'location' filter.");
154 return DistanceFilter.LOCATIONS;
155 }
156 else if (type.equals("distances")) {
157 logger.debug("Found 'distances' filter.");
158 return DistanceFilter.DISTANCES;
159 }
160
161 logger.debug("Do not use any filter at all.");
162
163 return DistanceFilter.NONE;
123 } 164 }
124 165
125 166
126 /** 167 /**
127 * This method build an Element for a distance info. 168 * This method build an Element for a distance info.
130 * @param anno The Annotation that provides information about the distance. 171 * @param anno The Annotation that provides information about the distance.
131 * 172 *
132 * @return an Element that contains information about a distance. 173 * @return an Element that contains information about a distance.
133 */ 174 */
134 protected static Element buildDistanceNode( 175 protected static Element buildDistanceNode(
135 Document document, 176 Document document,
136 Annotation anno 177 Annotation anno,
178 DistanceFilter filter
137 ) { 179 ) {
138 Position pos = anno.getPosition(); 180 Position pos = anno.getPosition();
139 Range range = anno.getRange(); 181 Range range = anno.getRange();
140 Attribute attr = anno.getAttribute(); 182 Attribute attr = anno.getAttribute();
141 Edge edge = anno.getEdge(); 183 Edge edge = anno.getEdge();
142 BigDecimal a = range.getA(); 184 BigDecimal a = range.getA();
143 BigDecimal b = range.getB(); 185 BigDecimal b = range.getB();
186
187 if (b == null && filter == DistanceFilter.DISTANCES) {
188 return null;
189 }
190
191 if (b != null && filter == DistanceFilter.LOCATIONS) {
192 return null;
193 }
144 194
145 Element distance = document.createElement("distance"); 195 Element distance = document.createElement("distance");
146 196
147 distance.setAttribute("description", pos.getValue()); 197 distance.setAttribute("description", pos.getValue());
148 198

http://dive4elements.wald.intevation.org