comparison artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/Builder.java @ 7669:f1abd257f933

Datacage: Added <dc:sort> <dc:sort expr="Expression" type="Type"> ... </dc:sort> sorts the current dataset by Expression. Type is optional (default string).
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 06 Dec 2013 16:34:58 +0100
parents bfec104f20c7
children 36e28ec7ff16
comparison
equal deleted inserted replaced
7668:e0219f4079a8 7669:f1abd257f933
72 protected Document template; 72 protected Document template;
73 73
74 protected Map<String, CompiledStatement> compiledStatements; 74 protected Map<String, CompiledStatement> compiledStatements;
75 75
76 protected Map<String, Element> macros; 76 protected Map<String, Element> macros;
77
78 private static final class KV implements Comparable<KV> {
79
80 private Comparable<Object> key;
81 private Object [] data;
82
83 public KV(Comparable<Object> key, Object [] data) {
84 this.key = key;
85 this.data = data;
86 }
87
88 @Override
89 public int compareTo(KV other) {
90 return key.compareTo(other.key);
91 }
92
93 public Object [] getData() {
94 return data;
95 }
96 }
77 97
78 /** Connection to either of the databases. */ 98 /** Connection to either of the databases. */
79 public static class NamedConnection { 99 public static class NamedConnection {
80 100
81 protected String name; 101 protected String name;
576 } 596 }
577 } 597 }
578 } 598 }
579 finally { 599 finally {
580 pair.setB(orig); 600 pair.setB(orig);
601 }
602 }
603
604
605 protected ResultData createSortedResultData(
606 ResultData orig,
607 String expr,
608 String type
609 ) {
610 XPathExpression x;
611 try {
612 x = getXPathExpression(expr);
613 }
614 catch (XPathExpressionException xee) {
615 log.warn("Invalid sort expression '" + expr + "'.");
616 return orig;
617 }
618
619 QName returnType = typeToQName(type);
620
621 List<Object []> rows = orig.getRows();
622 String [] columns = orig.getColumnLabels();
623
624 List<KV> sorted = new ArrayList<KV>(rows.size());
625
626 for (Object [] row: rows) {
627 frames.enter();
628 try {
629 frames.put(columns, row);
630 Object key = x.evaluate(EVAL_DOCUMENT, returnType);
631
632 if (key instanceof Comparable) {
633 sorted.add(new KV((Comparable<Object>)key, row));
634 }
635 }
636 catch (XPathExpressionException xee) {
637 log.warn("unable to apply expression '" +
638 expr + "' to dataset. " + xee.getMessage(), xee);
639 }
640 finally {
641 frames.leave();
642 }
643 }
644 Collections.sort(sorted);
645 List<Object []> result = new ArrayList<Object []>(sorted.size());
646 for (KV kv: sorted) {
647 result.add(kv.getData());
648 }
649 return new ResultData(columns, result);
650 }
651
652 protected void sort(Node parent, Element current)
653 throws SQLException
654 {
655 log.debug("dc:sort");
656
657 if (connectionsStack.isEmpty()) {
658 log.debug("dc:sort without having results");
659 return;
660 }
661
662 String expr = current.getAttribute("expr").trim();
663 String type = current.getAttribute("type").trim();
664
665 if (expr.isEmpty()) {
666 log.warn("missing 'expr' in dc:sort");
667 return;
668 }
669
670 Pair<Builder.NamedConnection, ResultData> pair =
671 connectionsStack.peek();
672
673 ResultData orig = connectionsStack.peek().getB();
674
675 ResultData sorted = createSortedResultData(orig, expr, type);
676
677 NodeList subs = current.getChildNodes();
678
679 pair.setB(sorted);
680 try {
681 for (int i = 0, S = subs.getLength(); i < S; ++i) {
682 build(parent, subs.item(i));
683 }
684 }
685 finally {
686 if (orig != null) {
687 pair.setB(orig);
688 }
581 } 689 }
582 } 690 }
583 691
584 public Object getGroupKey() { 692 public Object getGroupKey() {
585 return groupExprStack.isEmpty() 693 return groupExprStack.isEmpty()
1143 filter(parent, curr); 1251 filter(parent, curr);
1144 } 1252 }
1145 else if ("group".equals(localName)) { 1253 else if ("group".equals(localName)) {
1146 group(parent, curr); 1254 group(parent, curr);
1147 } 1255 }
1256 else if ("sort".equals(localName)) {
1257 sort(parent, curr);
1258 }
1148 else if ("virtual-column".equals(localName)) { 1259 else if ("virtual-column".equals(localName)) {
1149 virtualColumn(parent, curr); 1260 virtualColumn(parent, curr);
1150 } 1261 }
1151 else if ("text".equals(localName)) { 1262 else if ("text".equals(localName)) {
1152 text(parent, curr); 1263 text(parent, curr);

http://dive4elements.wald.intevation.org