annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java @ 925:0cb1a70b8b92

Added the math needed to calculate "W-Differenzen" in "Laengsschnitten" flys-artifacts/trunk@2277 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 03 Jul 2011 15:33:33 +0000
parents 24b53f9aa9dc
children db8d93cb65fd
rev   line source
372
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.artifacts.services;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import java.io.InputStream;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import java.io.IOException;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import java.sql.Connection;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.sql.SQLException;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import org.w3c.dom.Document;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import org.apache.log4j.Logger;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12
415
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
13 import java.util.Map;
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
14 import java.util.HashMap;
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
15
372
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import de.intevation.artifacts.CallMeta;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import de.intevation.artifacts.ServiceFactory;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 import de.intevation.artifactdatabase.DefaultService;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 import de.intevation.artifacts.common.utils.XMLUtils;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
421
24b53f9aa9dc MetaDataService: Make incoming XML symmetric to DistanceInfoService.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 415
diff changeset
23 import de.intevation.artifacts.common.ArtifactNamespaceContext;
24b53f9aa9dc MetaDataService: Make incoming XML symmetric to DistanceInfoService.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 415
diff changeset
24
372
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 import de.intevation.flys.artifacts.services.meta.Builder;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 import de.intevation.flys.backend.SessionHolder;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 import org.hibernate.Session;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 import org.hibernate.jdbc.Work;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 public class MetaDataService
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 extends DefaultService
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 private static Logger log = Logger.getLogger(MetaDataService.class);
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37
421
24b53f9aa9dc MetaDataService: Make incoming XML symmetric to DistanceInfoService.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 415
diff changeset
38 public static final String XPATH_RIVER = "/art:river/text()";
415
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
39
372
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 public static final String META_DATA_TEMPLATE = "/metadata/template.xml";
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 protected Builder builder;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 public MetaDataService() {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46
415
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
47 protected static Map<String, Object> extractParameters(Document data) {
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
48 HashMap<String, Object> parameters = new HashMap<String, Object>();
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
49
421
24b53f9aa9dc MetaDataService: Make incoming XML symmetric to DistanceInfoService.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 415
diff changeset
50 String river = XMLUtils.xpathString(
24b53f9aa9dc MetaDataService: Make incoming XML symmetric to DistanceInfoService.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 415
diff changeset
51 data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE);
415
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
52
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
53 if (river == null || (river = river.trim()).length() == 0) {
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
54 river = "%"; // matches all rivers
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
55 }
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
56
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
57 parameters.put("river", river);
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
58
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
59 return parameters;
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
60 }
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
61
372
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 @Override
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 public Document process(
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 Document data,
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 Object globalContext,
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 CallMeta callMeta
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 ) {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 log.debug("MetaDataService.process");
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 final Document result = XMLUtils.newDocument();
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 if (builder == null) {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 log.error("MetaDataService is not setup properly.");
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 return result;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76
415
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
77 final Map<String, Object> parameters = extractParameters(data);
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
78
372
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 Session session = SessionHolder.acquire();
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 try {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 session.doWork(new Work() {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 @Override
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 public void execute(Connection connection)
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 throws SQLException
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 log.debug("MetaDataService.execute");
415
7c018f466d6d Datacage: Added river parameter to meta data services.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
87 builder.build(connection, result, parameters);
372
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 });
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 finally {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 session.close();
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 SessionHolder.release();
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 return result;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 @Override
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 public void setup(ServiceFactory factory, Object globalContext) {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 log.debug("MetaDataService.setup");
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 InputStream in = getClass().getResourceAsStream(META_DATA_TEMPLATE);
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 if (in == null) {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 log.error("cannot get template resource");
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 return;
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 try {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 Document template = XMLUtils.parseDocument(in);
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 if (template == null) {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 log.error("cannot parse meta data template");
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 else {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 builder = new Builder(template);
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 finally {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 try {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 in.close();
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 catch (IOException ioe) {
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 log.error(ioe);
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 }
fc3cf0ef777e Added meta data service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org