comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java @ 430:7ab81ff32111 2.3

merged flys-artifacts/2.3
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:10 +0200
parents 4aa078e28cfd
children 02c0cce0e469
comparison
equal deleted inserted replaced
290:a6f56ed9238b 430:7ab81ff32111
1 package de.intevation.flys.artifacts.services;
2
3 import java.math.BigDecimal;
4 import java.util.List;
5
6 import org.apache.log4j.Logger;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10
11 import de.intevation.artifacts.CallMeta;
12
13 import de.intevation.artifacts.common.ArtifactNamespaceContext;
14 import de.intevation.artifacts.common.utils.XMLUtils;
15 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
16
17 import de.intevation.artifactdatabase.DefaultService;
18
19 import de.intevation.flys.backend.SessionHolder;
20 import de.intevation.flys.model.Annotation;
21 import de.intevation.flys.model.Attribute;
22 import de.intevation.flys.model.Position;
23 import de.intevation.flys.model.Range;
24
25 import de.intevation.flys.artifacts.model.AnnotationsFactory;
26
27 import org.hibernate.Session;
28
29 /**
30 * This service provides information about distances of a specified river.
31 *
32 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
33 */
34 public class DistanceInfoService extends DefaultService {
35
36 /** The logger used in this service.*/
37 private static Logger logger = Logger.getLogger(DistanceInfoService.class);
38
39
40 /**
41 * The default constructor.
42 */
43 public DistanceInfoService() {
44 }
45
46
47 public Document process(
48 Document data,
49 Object globalContext,
50 CallMeta callMeta)
51 {
52 logger.debug("DistanceInfoService.process");
53
54 Document result = XMLUtils.newDocument();
55
56 String river = XMLUtils.xpathString(
57 data, "/art:river/text()", ArtifactNamespaceContext.INSTANCE);
58
59 if (river == null || river.trim().length() == 0) {
60 logger.warn("No river specified. Cannot return distance info!");
61 return result;
62 }
63
64 logger.debug("Search distances for river: " + river);
65
66 ElementCreator ec = new ElementCreator(
67 result,
68 ArtifactNamespaceContext.NAMESPACE_URI,
69 ArtifactNamespaceContext.NAMESPACE_PREFIX);
70
71 Session session = SessionHolder.acquire();
72 try {
73 List<Annotation> annotations = AnnotationsFactory.getAnnotations(river);
74
75 if (annotations == null || annotations.size() == 0) {
76 logger.warn("No information found for the specified river!");
77 return result;
78 }
79
80 Element all = ec.create("distances");
81
82 for (Annotation a: annotations) {
83 Element distance = buildDistanceNode(ec, a);
84
85 if (distance != null) {
86 all.appendChild(distance);
87 }
88 }
89
90 result.appendChild(all);
91 }
92 finally {
93 session.close();
94 SessionHolder.release();
95 }
96
97 return result;
98 }
99
100
101 /**
102 * This method build an Element for a distance info.
103 *
104 * @param ec The ElementCreator.
105 * @param anno The Annotation that provides information about the distance.
106 *
107 * @return an Element that contains information about a distance.
108 */
109 protected Element buildDistanceNode(ElementCreator ec, Annotation anno) {
110 Position pos = anno.getPosition();
111 Range range = anno.getRange();
112 Attribute attr = anno.getAttribute();
113
114 BigDecimal a = range.getA();
115 BigDecimal b = range.getB();
116
117 Element distance = ec.create("distance");
118 ec.addAttr(distance, "description", pos.getValue(), true);
119 ec.addAttr(distance, "from", a != null ? a.toString() : "", true);
120 ec.addAttr(distance, "to", b != null ? b.toString() : "", true);
121 ec.addAttr(distance, "riverside", attr.getValue(), true);
122
123 return distance;
124 }
125 }
126 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org