Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.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 org.dive4elements.artifactdatabase.state.ArtifactAndFacet; | |
22 import org.dive4elements.artifactdatabase.state.Settings; | |
23 import org.dive4elements.artifacts.Artifact; | |
24 import org.dive4elements.artifacts.ArtifactDatabaseException; | |
25 import org.dive4elements.artifacts.CallContext; | |
26 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
27 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
28 import org.dive4elements.river.artifacts.context.FLYSContext; | |
29 import org.dive4elements.river.collections.FLYSArtifactCollection; | |
30 import org.dive4elements.river.exports.ChartGenerator; | |
31 import org.dive4elements.river.exports.OutGenerator; | |
32 import org.dive4elements.river.exports.OutputHelper; | |
33 | |
34 public class SQOverviewGenerator | |
35 implements OutGenerator | |
36 { | |
37 private static Logger logger = Logger.getLogger(SQOverviewGenerator.class); | |
38 | |
39 public static final String XPATH_CHART_SIZE = | |
40 "/art:action/art:attributes/art:size"; | |
41 | |
42 protected FLYSArtifactCollection collection; | |
43 | |
44 protected Artifact master; | |
45 | |
46 protected Settings settings; | |
47 | |
48 protected Document request; | |
49 | |
50 protected OutputStream out; | |
51 | |
52 protected CallContext context; | |
53 | |
54 protected List<JFreeChart> charts; | |
55 | |
56 /** | |
57 * Produce output. | |
58 * @param artifactAndFacet current facet and artifact. | |
59 * @param attr theme for facet | |
60 */ | |
61 @Override | |
62 public void doOut( | |
63 ArtifactAndFacet artifactAndFacet, | |
64 Document attr, | |
65 boolean visible | |
66 ) { | |
67 logger.debug("doOut()"); | |
68 | |
69 String name = artifactAndFacet.getData(context).toString(); | |
70 if(name != null) { | |
71 logger.debug("name: " + name); | |
72 ChartGenerator g = | |
73 (ChartGenerator)FLYSContext.getOutGenerator( | |
74 context, | |
75 name, | |
76 null); | |
77 | |
78 if (g == null) { | |
79 logger.debug("generator is null."); | |
80 return; | |
81 } | |
82 | |
83 OutputHelper helper = new OutputHelper(master.identifier()); | |
84 Document collectionAttribute = collection.getAttribute(); | |
85 | |
86 try { | |
87 Document cAttr = getAttribute(context, collectionAttribute, name); | |
88 g.init(request, out, context); | |
89 | |
90 helper.doOut(g, name, name, cAttr, context); | |
91 JFreeChart chart = g.generateChart(); | |
92 chart.removeLegend(); | |
93 charts.add(chart); | |
94 } | |
95 catch (IOException e) { | |
96 logger.warn(e); | |
97 } | |
98 catch (ArtifactDatabaseException e) { | |
99 logger.warn(e); | |
100 } | |
101 } | |
102 } | |
103 | |
104 @Override | |
105 public void init(Document request, OutputStream out, CallContext context) { | |
106 this.request = request; | |
107 this.out = out; | |
108 this.context = context; | |
109 charts = new ArrayList<JFreeChart>(); | |
110 } | |
111 | |
112 @Override | |
113 public void setMasterArtifact(Artifact master) { | |
114 this.master = master; | |
115 } | |
116 | |
117 @Override | |
118 public void setCollection(FLYSArtifactCollection collection) { | |
119 this.collection = collection; | |
120 } | |
121 | |
122 @Override | |
123 public void generate() throws IOException { | |
124 logger.debug("SQOverviewGenerator.generate"); | |
125 | |
126 int[] size = getSize(); | |
127 | |
128 if (size == null) { | |
129 size = new int[] {400, 600}; | |
130 } | |
131 BufferedImage result = | |
132 new BufferedImage(size[0], size[1], BufferedImage.TYPE_INT_RGB); | |
133 for (int i = 0; i < charts.size(); i++) { | |
134 logger.debug("index: " + i); | |
135 JFreeChart chart = charts.get(i); | |
136 ChartRenderingInfo info = new ChartRenderingInfo(); | |
137 BufferedImage img = | |
138 chart.createBufferedImage(size[0]/2, size[1]/3, info); | |
139 int horPos = 0; | |
140 int vertPos = 0; | |
141 if (i % 2 == 1) { | |
142 horPos = size[0]/2; | |
143 } | |
144 if (i > 1) { | |
145 vertPos = (size[1] / 3) * (i / 2); | |
146 } | |
147 result.createGraphics().drawImage(img, horPos, vertPos, null); | |
148 } | |
149 ImageIO.write(result, "png", out); | |
150 } | |
151 | |
152 @Override | |
153 public void setSettings(Settings settings) { | |
154 this.settings = settings; | |
155 } | |
156 | |
157 @Override | |
158 public Settings getSettings() { | |
159 return this.settings; | |
160 } | |
161 | |
162 | |
163 /** | |
164 * Returns the "attribute" (part of description document) for a specific | |
165 * output type. | |
166 * | |
167 * @param context The CallContext object. | |
168 * @param attr The xml attribute saved at the collection. | |
169 * @param output The name of the desired output type. | |
170 * | |
171 * @return the attribute for the desired output type. | |
172 */ | |
173 protected Document getAttribute( | |
174 CallContext context, | |
175 Document attr, | |
176 String output) | |
177 throws ArtifactDatabaseException | |
178 { | |
179 logger.debug("find specific XML node for Output: " + output); | |
180 | |
181 Map<String, String> vars = new HashMap<String, String>(); | |
182 vars.put("output", output); | |
183 | |
184 Node out = (Node) XMLUtils.xpath( | |
185 attr, | |
186 "art:attribute/art:outputs/art:output[@name=$output]", | |
187 XPathConstants.NODE, | |
188 ArtifactNamespaceContext.INSTANCE, | |
189 vars); | |
190 | |
191 if (out != null) { | |
192 Document o = XMLUtils.newDocument(); | |
193 o.appendChild(o.importNode(out, true)); | |
194 | |
195 return o; | |
196 } | |
197 | |
198 return null; | |
199 } | |
200 | |
201 | |
202 /** | |
203 * Returns the size of a chart export as array which has been specified by | |
204 * the incoming request document. | |
205 * | |
206 * @return the size of a chart as [width, height] or null if no width or | |
207 * height are given in the request document. | |
208 */ | |
209 protected int[] getSize() { | |
210 int[] size = new int[2]; | |
211 | |
212 Element sizeEl = (Element)XMLUtils.xpath( | |
213 request, | |
214 XPATH_CHART_SIZE, | |
215 XPathConstants.NODE, | |
216 ArtifactNamespaceContext.INSTANCE); | |
217 | |
218 if (sizeEl != null) { | |
219 String uri = ArtifactNamespaceContext.NAMESPACE_URI; | |
220 | |
221 String w = sizeEl.getAttributeNS(uri, "width"); | |
222 String h = sizeEl.getAttributeNS(uri, "height"); | |
223 | |
224 if (w.length() > 0 && h.length() > 0) { | |
225 try { | |
226 size[0] = Integer.parseInt(w); | |
227 size[1] = Integer.parseInt(h); | |
228 } | |
229 catch (NumberFormatException nfe) { | |
230 logger.warn("Wrong values for chart width/height."); | |
231 } | |
232 } | |
233 } | |
234 | |
235 return size[0] > 0 && size[1] > 0 ? size : null; | |
236 } | |
237 } | |
238 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |