66package com .gooddata .executeafm .resultspec ;
77
88import com .fasterxml .jackson .annotation .JsonCreator ;
9+ import com .fasterxml .jackson .annotation .JsonInclude ;
910import com .fasterxml .jackson .annotation .JsonProperty ;
1011import com .fasterxml .jackson .annotation .JsonTypeInfo ;
1112import com .fasterxml .jackson .annotation .JsonTypeName ;
1718
1819/**
1920 * Define sort by specific attribute
21+ *
22+ * <p>With "aggregation" active you can sort all elements of attribute
23+ * by "aggregation fn" applied to all valid values belonging to each
24+ * element. This is extremely useful when sorting stacked
25+ * visualizations like stack bar/area charts. Currently supported is
26+ * only "sum", see {@link AttributeSortAggregation}</p>
27+ *
28+ * <p>Simple example (dimension = Year, measureGroup; 2 metrics; sort
29+ * on Year with aggregation="sum", descending):</p>
30+ * <pre>
31+ * Year 2006 2007
32+ * Names M1 M2 M1 M2
33+ * Values 1 2 3 4
34+ * </pre>
35+ *
36+ * <p>We take all values belonging to each attribute element of chosen attribute
37+ * and apply selected function (sum) on them. Notice that we are summarising
38+ * values from different metrics:</p>
39+ * <pre>
40+ * 2006 (1 + 2 = 3)
41+ * 2007 (3 + 4 = 7)
42+ * </pre>
43+ *
44+ * <p>After that we shuffle year attribute elements related to results from "sum"
45+ * function:</p>
46+ * <pre>
47+ * Year 2007 2006
48+ * Names M1 M2 M1 M2
49+ * Values 3 4 1 2
50+ * </pre>
2051 */
2152@ JsonTypeInfo (include = As .WRAPPER_OBJECT , use = Id .NAME )
2253@ JsonTypeName ("attributeSortItem" )
54+ @ JsonInclude (JsonInclude .Include .NON_NULL )
2355public class AttributeSortItem implements SortItem {
2456
2557 private final String direction ;
26-
2758 private final String attributeIdentifier ;
59+ private final String aggregation ;
2860
2961 @ JsonCreator
3062 public AttributeSortItem (@ JsonProperty ("direction" ) final String direction ,
31- @ JsonProperty ("attributeIdentifier" ) final String attributeIdentifier ) {
63+ @ JsonProperty ("attributeIdentifier" ) final String attributeIdentifier ,
64+ @ JsonProperty ("aggregation" ) final String aggregation ) {
3265 this .attributeIdentifier = attributeIdentifier ;
3366 this .direction = direction ;
67+ this .aggregation = aggregation ;
68+ }
69+
70+ public AttributeSortItem (final String direction ,
71+ final String attributeIdentifier ) {
72+ this (direction , attributeIdentifier , null );
3473 }
3574
3675 public AttributeSortItem (final Direction direction , final String attributeIdentifier ) {
37- this (notNull (direction , "direction" ).toString (), attributeIdentifier );
76+ this (notNull (direction , "direction" ).toString (), attributeIdentifier , null );
77+ }
78+
79+ public AttributeSortItem (final Direction direction , final String attributeIdentifier , final AttributeSortAggregation aggregation ) {
80+ this (notNull (direction , "direction" ).toString (), attributeIdentifier , notNull (aggregation , "aggregation" ).toString ());
3881 }
3982
4083 public String getDirection () {
@@ -45,6 +88,10 @@ public String getAttributeIdentifier() {
4588 return attributeIdentifier ;
4689 }
4790
91+ public String getAggregation () {
92+ return aggregation ;
93+ }
94+
4895 @ Override
4996 public String toString () {
5097 return GoodDataToStringBuilder .defaultToString (this );
0 commit comments