Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 5509:627584bc0586
Datacage: Added <dc:filter> element. This allows cleaner way to narrow the datasets.
Example:
<dc:context>
<dc:statement>
SELECT DISTINCT
name AS hws_name,
official AS hws_official,
kind_id AS hws_kind
FROM hws_lines
WHERE river_id = ${river_id}
</dc:statement>
<dc:if test="dc:has-result()">
<lines>
<dc:macro name="hws-lines">
<dc:elements>
<hws factory="hwsfactory" name="{$hws_name}"/>
</dc:elements>
</dc:macro>
<dc:filter expr="$hws_official=1">
<dc:if test="dc:has-result()">
<official>
<dc:filter expr="$hws_kind=1">
<dc:if test="dc:has-result()">
<Durchlass><dc:call-macro name="hws-lines"></Durchlass>
</dc:if>
</dc:filter>
<dc:filter expr="$hws_kind=2">
<dc:if test="dc:has-result()">
<Damm><dc:call-macro name="hws-lines"></Damm>
</dc:if>
</dc:filter>
<dc:filter expr="$hws_kind=3">
<dc:if test="dc:has-result()">
<Graben><dc:call-macro name="hws-lines"></Graben>
</dc:if>
</dc:filter>
</official>
</dc:if>
</dc:filter>
</lines>
</dc:if>
</dc:context>
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 28 Mar 2013 16:51:15 +0100 |
parents | 773899d00234 |
children | 5800a9497b0b |
comparison
equal
deleted
inserted
replaced
5506:57f802db5c5a | 5509:627584bc0586 |
---|---|
184 | 184 |
185 return null; | 185 return null; |
186 } | 186 } |
187 | 187 |
188 /** | 188 /** |
189 * Handle a \<context\> node. | 189 * Handle a dc:context node. |
190 */ | 190 */ |
191 protected void context(Node parent, Element current) | 191 protected void context(Node parent, Element current) |
192 throws SQLException | 192 throws SQLException |
193 { | 193 { |
194 log.debug("dc:context"); | 194 log.debug("dc:context"); |
281 } | 281 } |
282 } | 282 } |
283 return new ResultData(rd.getColumnLabels(), filtered); | 283 return new ResultData(rd.getColumnLabels(), filtered); |
284 } | 284 } |
285 | 285 |
286 protected void filter(Node parent, Element current) | |
287 throws SQLException | |
288 { | |
289 String expr = current.getAttribute("expr"); | |
290 | |
291 if ((expr = expr.trim()).length() == 0) { | |
292 expr = null; | |
293 } | |
294 | |
295 NodeList subs = current.getChildNodes(); | |
296 int S = subs.getLength(); | |
297 if (S == 0) { | |
298 log.debug("dc:filter has no children"); | |
299 return; | |
300 } | |
301 | |
302 ResultData orig = null; | |
303 Pair<Builder.NamedConnection, ResultData> pair = null; | |
304 | |
305 if (expr != null && !connectionsStack.isEmpty()) { | |
306 pair = connectionsStack.peek(); | |
307 orig = pair.getB(); | |
308 pair.setB(createFilteredResultData(orig, expr)); | |
309 } | |
310 | |
311 try { | |
312 for (int i = 0; i < S; ++i) { | |
313 build(parent, subs.item(i)); | |
314 } | |
315 } | |
316 finally { | |
317 if (orig != null) { | |
318 pair.setB(orig); | |
319 } | |
320 } | |
321 } | |
322 | |
286 /** | 323 /** |
287 * Kind of foreach over results of a statement within a context. | 324 * Kind of foreach over results of a statement within a context. |
288 */ | 325 */ |
289 protected void elements(Node parent, Element current) | 326 protected void elements(Node parent, Element current) |
290 throws SQLException | 327 throws SQLException |
311 } | 348 } |
312 | 349 |
313 Pair<Builder.NamedConnection, ResultData> pair = | 350 Pair<Builder.NamedConnection, ResultData> pair = |
314 connectionsStack.peek(); | 351 connectionsStack.peek(); |
315 | 352 |
316 ResultData rd = connectionsStack.peek().getB(); | 353 ResultData rd = pair.getB(); |
317 ResultData orig = rd; | 354 ResultData orig = rd; |
318 | 355 |
319 if (filter != null) { | 356 if (filter != null) { |
320 ResultData rdCopy = createFilteredResultData(rd, filter); | 357 ResultData rdCopy = createFilteredResultData(rd, filter); |
321 pair.setB(rdCopy); | 358 pair.setB(rdCopy); |
322 rd = rdCopy; | 359 rd = rdCopy; |
323 } | 360 } |
324 try { | 361 try { |
325 String [] columns = rd.getColumnLabels(); | 362 String [] columns = rd.getColumnLabels(); |
326 | 363 |
327 //if (log.isDebugEnabled()) { | |
328 // log.debug("pushing vars: " | |
329 // + java.util.Arrays.toString(columns)); | |
330 //} | |
331 | |
332 for (Object [] row: rd.getRows()) { | 364 for (Object [] row: rd.getRows()) { |
333 frames.enter(); | 365 frames.enter(); |
334 try { | 366 try { |
335 frames.put(columns, row); | 367 frames.put(columns, row); |
336 //if (log.isDebugEnabled()) { | |
337 // log.debug("current vars: " + frames.dump()); | |
338 //} | |
339 for (int i = 0; i < S; ++i) { | 368 for (int i = 0; i < S; ++i) { |
340 build(parent, subs.item(i)); | 369 build(parent, subs.item(i)); |
341 } | 370 } |
342 } | 371 } |
343 finally { | 372 finally { |
659 if (current.getNodeType() != Node.ELEMENT_NODE) { | 688 if (current.getNodeType() != Node.ELEMENT_NODE) { |
660 log.warn("need elements here"); | 689 log.warn("need elements here"); |
661 } | 690 } |
662 else { | 691 else { |
663 String localName = current.getLocalName(); | 692 String localName = current.getLocalName(); |
693 Element curr = (Element)curr; | |
664 if ("attribute".equals(localName)) { | 694 if ("attribute".equals(localName)) { |
665 attribute(parent, (Element)current); | 695 attribute(parent, curr); |
666 } | 696 } |
667 else if ("context".equals(localName)) { | 697 else if ("context".equals(localName)) { |
668 context(parent, (Element)current); | 698 context(parent, curr); |
669 } | 699 } |
670 else if ("if".equals(localName)) { | 700 else if ("if".equals(localName)) { |
671 ifClause(parent, (Element)current); | 701 ifClause(parent, curr); |
672 } | 702 } |
673 else if ("choose".equals(localName)) { | 703 else if ("choose".equals(localName)) { |
674 choose(parent, (Element)current); | 704 choose(parent, curr); |
675 } | 705 } |
676 else if ("call-macro".equals(localName)) { | 706 else if ("call-macro".equals(localName)) { |
677 callMacro(parent, (Element)current); | 707 callMacro(parent, curr); |
678 } | 708 } |
679 else if ("macro-body".equals(localName)) { | 709 else if ("macro-body".equals(localName)) { |
680 macroBody(parent, (Element)current); | 710 macroBody(parent, curr); |
681 } | 711 } |
682 else if ("macro".equals(localName)) { | 712 else if ("macro".equals(localName) |
683 // Simply ignore the definition. | 713 || "comment".equals(localName) |
714 || "statement".equals(localName)) { | |
715 // Simply ignore them. | |
684 } | 716 } |
685 else if ("element".equals(localName)) { | 717 else if ("element".equals(localName)) { |
686 element(parent, (Element)current); | 718 element(parent, curr); |
687 } | 719 } |
688 else if ("elements".equals(localName)) { | 720 else if ("elements".equals(localName)) { |
689 elements(parent, (Element)current); | 721 elements(parent, curr); |
722 } | |
723 else if ("filter".equals(localName)) { | |
724 filter(parent, curr); | |
690 } | 725 } |
691 else if ("text".equals(localName)) { | 726 else if ("text".equals(localName)) { |
692 text(parent, (Element)current); | 727 text(parent, curr); |
693 } | 728 } |
694 else if ("variable".equals(localName)) { | 729 else if ("variable".equals(localName)) { |
695 variable((Element)current); | 730 variable(curr); |
696 } | |
697 else if ("comment".equals(localName) | |
698 || "statement".equals(localName)) { | |
699 // ignore comments and statements in output | |
700 } | 731 } |
701 else if ("convert".equals(localName)) { | 732 else if ("convert".equals(localName)) { |
702 convert((Element)current); | 733 convert(curr); |
703 } | 734 } |
704 else { | 735 else { |
705 log.warn("unknown '" + localName + "' -> ignore"); | 736 log.warn("unknown '" + localName + "' -> ignore"); |
706 } | 737 } |
707 } | 738 } |