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