Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQOverviewGenerator.java @ 3296:45af081061e7
Added overview output for sq relation. Currently the overview generator is
unused and the overview is build by the client.
flys-artifacts/trunk@4979 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Fri, 13 Jul 2012 09:45:11 +0000 |
parents | |
children | 02d5731b43a2 |
comparison
equal
deleted
inserted
replaced
3295:4fc442f1b4f6 | 3296:45af081061e7 |
---|---|
1 package de.intevation.flys.exports.sq; | |
2 | |
3 import java.awt.image.BufferedImage; | |
4 import java.io.IOException; | |
5 import java.io.OutputStream; | |
6 import java.util.ArrayList; | |
7 import java.util.HashMap; | |
8 import java.util.List; | |
9 import java.util.Map; | |
10 | |
11 import javax.imageio.ImageIO; | |
12 import javax.xml.xpath.XPathConstants; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 import org.jfree.chart.ChartRenderingInfo; | |
16 import org.jfree.chart.JFreeChart; | |
17 import org.w3c.dom.Document; | |
18 import org.w3c.dom.Element; | |
19 import org.w3c.dom.Node; | |
20 | |
21 import de.intevation.artifactdatabase.state.ArtifactAndFacet; | |
22 import de.intevation.artifactdatabase.state.Settings; | |
23 import de.intevation.artifacts.Artifact; | |
24 import de.intevation.artifacts.ArtifactDatabaseException; | |
25 import de.intevation.artifacts.CallContext; | |
26 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
27 import de.intevation.artifacts.common.utils.XMLUtils; | |
28 import de.intevation.flys.artifacts.context.FLYSContext; | |
29 import de.intevation.flys.collections.AttributeParser; | |
30 import de.intevation.flys.collections.CollectionAttribute; | |
31 import de.intevation.flys.exports.ChartGenerator; | |
32 import de.intevation.flys.exports.OutGenerator; | |
33 import de.intevation.flys.exports.OutputHelper; | |
34 | |
35 public class SQOverviewGenerator | |
36 implements OutGenerator | |
37 { | |
38 private static Logger logger = Logger.getLogger(SQOverviewGenerator.class); | |
39 | |
40 public static final String XPATH_CHART_SIZE = | |
41 "/art:action/art:attributes/art:size"; | |
42 | |
43 protected Artifact master; | |
44 | |
45 protected Settings settings; | |
46 | |
47 protected Document request; | |
48 | |
49 protected OutputStream out; | |
50 | |
51 protected CallContext context; | |
52 | |
53 protected List<JFreeChart> charts; | |
54 | |
55 /** | |
56 * Produce output. | |
57 * @param artifactAndFacet current facet and artifact. | |
58 * @param attr theme for facet | |
59 */ | |
60 public void doOut( | |
61 ArtifactAndFacet artifactAndFacet, | |
62 Document attr, | |
63 boolean visible | |
64 ) { | |
65 logger.debug("doOut()"); | |
66 | |
67 logger.debug(XMLUtils.toString(attr)); | |
68 String name = artifactAndFacet.getData(context).toString(); | |
69 if(name != null) { | |
70 logger.debug("name: " + name); | |
71 ChartGenerator g = | |
72 (ChartGenerator)FLYSContext.getOutGenerator( | |
73 context, | |
74 name, | |
75 null); | |
76 if (g == null) { | |
77 logger.debug("generator is null."); | |
78 return; | |
79 } | |
80 logger.debug(XMLUtils.toString(attr)); | |
81 OutputHelper helper = new OutputHelper(master.identifier()); | |
82 try { | |
83 AttributeParser parser = new AttributeParser(request); | |
84 CollectionAttribute cAttr = parser.getCollectionAttribute(); | |
85 | |
86 g.init(request, out, context); | |
87 Document chartAttr = getAttribute(context, cAttr, name); | |
88 logger.debug(XMLUtils.toString(chartAttr)); | |
89 helper.doOut(g, name, name, request, context); | |
90 charts.add(g.generateChart()); | |
91 } catch (IOException e) { | |
92 logger.warn(e); | |
93 } catch (ArtifactDatabaseException e) { | |
94 // TODO Auto-generated catch block | |
95 logger.warn(e); | |
96 } | |
97 } | |
98 } | |
99 | |
100 public void init(Document request, OutputStream out, CallContext context) { | |
101 this.request = request; | |
102 this.out = out; | |
103 this.context = context; | |
104 charts = new ArrayList<JFreeChart>(); | |
105 } | |
106 | |
107 public void setMasterArtifact(Artifact master) { | |
108 this.master = master; | |
109 } | |
110 | |
111 public void generate() throws IOException { | |
112 logger.debug("SQOverviewGenerator.generate"); | |
113 | |
114 int[] size = getSize(); | |
115 | |
116 if (size == null) { | |
117 size = new int[] {400, 600}; | |
118 } | |
119 BufferedImage result = | |
120 new BufferedImage(size[0], size[1], BufferedImage.TYPE_INT_RGB); | |
121 for (int i = 0; i < charts.size(); i++) { | |
122 logger.debug("index: " + i); | |
123 JFreeChart chart = charts.get(i); | |
124 ChartRenderingInfo info = new ChartRenderingInfo(); | |
125 BufferedImage img = | |
126 chart.createBufferedImage(size[0]/2, size[1]/3, info); | |
127 int horPos = 0; | |
128 int vertPos = 0; | |
129 if (i % 2 == 1) { | |
130 horPos = size[0]/2; | |
131 } | |
132 if (i > 1) { | |
133 vertPos = (size[1] / 3) * (i / 2); | |
134 } | |
135 result.createGraphics().drawImage(img, horPos, vertPos, null); | |
136 } | |
137 ImageIO.write(result, "png", out); | |
138 } | |
139 | |
140 public void setSettings(Settings settings) { | |
141 this.settings = settings; | |
142 } | |
143 | |
144 public Settings getSettings() { | |
145 return this.settings; | |
146 } | |
147 | |
148 | |
149 /** | |
150 * Returns the "attribute" (part of description document) for a specific | |
151 * output type. | |
152 * | |
153 * @param context The CallContext object. | |
154 * @param cAttr The CollectionAttribute. | |
155 * @param output The name of the desired output type. | |
156 * | |
157 * @return the attribute for the desired output type. | |
158 */ | |
159 protected Document getAttribute( | |
160 CallContext context, | |
161 CollectionAttribute cAttr, | |
162 String output) | |
163 throws ArtifactDatabaseException | |
164 { | |
165 Document attr = cAttr.toXML(); | |
166 | |
167 Map<String, String> vars = new HashMap<String, String>(); | |
168 vars.put("output", output); | |
169 | |
170 Node out = (Node) XMLUtils.xpath( | |
171 attr, | |
172 "art:attribute/art:outputs/art:output[@name=$output]", | |
173 XPathConstants.NODE, | |
174 ArtifactNamespaceContext.INSTANCE, | |
175 vars); | |
176 | |
177 | |
178 if (out != null) { | |
179 Document o = XMLUtils.newDocument(); | |
180 | |
181 o.appendChild(o.importNode(out, true)); | |
182 | |
183 return o; | |
184 } | |
185 | |
186 return null; | |
187 } | |
188 | |
189 | |
190 /** | |
191 * Returns the size of a chart export as array which has been specified by | |
192 * the incoming request document. | |
193 * | |
194 * @return the size of a chart as [width, height] or null if no width or | |
195 * height are given in the request document. | |
196 */ | |
197 protected int[] getSize() { | |
198 int[] size = new int[2]; | |
199 | |
200 Element sizeEl = (Element)XMLUtils.xpath( | |
201 request, | |
202 XPATH_CHART_SIZE, | |
203 XPathConstants.NODE, | |
204 ArtifactNamespaceContext.INSTANCE); | |
205 | |
206 if (sizeEl != null) { | |
207 String uri = ArtifactNamespaceContext.NAMESPACE_URI; | |
208 | |
209 String w = sizeEl.getAttributeNS(uri, "width"); | |
210 String h = sizeEl.getAttributeNS(uri, "height"); | |
211 | |
212 if (w.length() > 0 && h.length() > 0) { | |
213 try { | |
214 size[0] = Integer.parseInt(w); | |
215 size[1] = Integer.parseInt(h); | |
216 } | |
217 catch (NumberFormatException nfe) { | |
218 logger.warn("Wrong values for chart width/height."); | |
219 } | |
220 } | |
221 } | |
222 | |
223 return size[0] > 0 && size[1] > 0 ? size : null; | |
224 } | |
225 } |