View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.widgets;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Objects;
27  import java.util.Set;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.syncope.client.console.chartjs.Bar;
30  import org.apache.syncope.client.console.chartjs.BarDataSet;
31  import org.apache.syncope.client.console.chartjs.ChartJSPanel;
32  import org.apache.wicket.model.Model;
33  
34  public class AnyByRealmWidget extends BaseWidget {
35  
36      private static final long serialVersionUID = -816175678514035085L;
37  
38      private static final int MAX_REALMS = 9;
39  
40      private Map<String, Integer> usersByRealm;
41  
42      private Map<String, Integer> groupsByRealm;
43  
44      private String anyType1;
45  
46      private Map<String, Integer> any1ByRealm;
47  
48      private String anyType2;
49  
50      private Map<String, Integer> any2ByRealm;
51  
52      private final ChartJSPanel chart;
53  
54      public AnyByRealmWidget(
55              final String id,
56              final Map<String, Integer> usersByRealm,
57              final Map<String, Integer> groupsByRealm,
58              final String anyType1,
59              final Map<String, Integer> any1ByRealm,
60              final String anyType2,
61              final Map<String, Integer> any2ByRealm) {
62  
63          super(id);
64          this.usersByRealm = usersByRealm;
65          this.groupsByRealm = groupsByRealm;
66          this.anyType1 = anyType1;
67          this.any1ByRealm = any1ByRealm;
68          this.anyType2 = anyType2;
69          this.any2ByRealm = any2ByRealm;
70          setOutputMarkupId(true);
71  
72          chart = new ChartJSPanel(
73                  "chart",
74                  Model.of(build(usersByRealm, groupsByRealm, anyType1, any1ByRealm, anyType2, any2ByRealm)));
75          add(chart);
76      }
77  
78      private Bar build(
79              final Map<String, Integer> usersByRealm,
80              final Map<String, Integer> groupsByRealm,
81              final String anyType1,
82              final Map<String, Integer> any1ByRealm,
83              final String anyType2,
84              final Map<String, Integer> any2ByRealm) {
85  
86          List<Integer> userValues = new ArrayList<>();
87          List<Integer> groupValues = new ArrayList<>();
88          List<Integer> any1Values = new ArrayList<>();
89          List<Integer> any2Values = new ArrayList<>();
90  
91          Set<String> realmSet = new HashSet<>();
92          realmSet.addAll(usersByRealm.keySet());
93          realmSet.addAll(groupsByRealm.keySet());
94          if (any1ByRealm != null) {
95              realmSet.addAll(any1ByRealm.keySet());
96          }
97          if (any2ByRealm != null) {
98              realmSet.addAll(any2ByRealm.keySet());
99          }
100         List<String> realms = new ArrayList<>(realmSet);
101         Collections.sort(realms);
102 
103         Bar bar = new Bar();
104         bar.getOptions().setBarShowStroke(true);
105         bar.getOptions().setBarStrokeWidth(2);
106         bar.getOptions().setBarValueSpacing(5);
107         bar.getOptions().setBarDatasetSpacing(1);
108         bar.getOptions().setResponsive(true);
109         bar.getOptions().setMaintainAspectRatio(true);
110         bar.getOptions().setMultiTooltipTemplate("<%= datasetLabel %> - <%= value %>");
111 
112         for (int i = 0; i < realms.size() && i < MAX_REALMS; i++) {
113             bar.getData().getLabels().add(
114                     StringUtils.prependIfMissing(StringUtils.substringAfterLast(realms.get(i), "/"), "/"));
115 
116             userValues.add(usersByRealm.get(realms.get(i)));
117             groupValues.add(groupsByRealm.get(realms.get(i)));
118             if (any1ByRealm != null) {
119                 any1Values.add(any1ByRealm.get(realms.get(i)));
120             }
121             if (any2ByRealm != null) {
122                 any2Values.add(any2ByRealm.get(realms.get(i)));
123             }
124         }
125 
126         BarDataSet userDataSet = new BarDataSet(userValues);
127         userDataSet.setBackgroundColor("orange");
128         userDataSet.setLabel(getString("users"));
129         bar.getData().getDatasets().add(userDataSet);
130         BarDataSet groupDataSet = new BarDataSet(groupValues);
131         groupDataSet.setBackgroundColor("red");
132         groupDataSet.setLabel(getString("groups"));
133         bar.getData().getDatasets().add(groupDataSet);
134         if (anyType1 != null) {
135             BarDataSet any1DataSet = new BarDataSet(any1Values);
136             any1DataSet.setBackgroundColor("green");
137             any1DataSet.setLabel(anyType1);
138             bar.getData().getDatasets().add(any1DataSet);
139         }
140         if (anyType2 != null) {
141             BarDataSet any2DataSet = new BarDataSet(any2Values);
142             any2DataSet.setBackgroundColor("aqua");
143             any2DataSet.setLabel(anyType2);
144             bar.getData().getDatasets().add(any2DataSet);
145         }
146 
147         return bar;
148     }
149 
150     public boolean refresh(
151             final Map<String, Integer> usersByRealm,
152             final Map<String, Integer> groupsByRealm,
153             final String anyType1,
154             final Map<String, Integer> any1ByRealm,
155             final String anyType2,
156             final Map<String, Integer> any2ByRealm) {
157 
158         if (!this.usersByRealm.equals(usersByRealm)
159                 || !this.groupsByRealm.equals(groupsByRealm)
160                 || (!(this.anyType1 == null && anyType1 == null) && !Objects.equals(this.anyType1, anyType1))
161                 || !this.any1ByRealm.equals(any1ByRealm)
162                 || (!(this.anyType2 == null && anyType2 == null) && !Objects.equals(this.anyType2, anyType2))
163                 || !this.any2ByRealm.equals(any2ByRealm)) {
164 
165             this.usersByRealm = usersByRealm;
166             this.groupsByRealm = groupsByRealm;
167             this.anyType1 = anyType1;
168             this.any1ByRealm = any1ByRealm;
169             this.anyType2 = anyType2;
170             this.any2ByRealm = any2ByRealm;
171 
172             chart.setDefaultModelObject(
173                     build(usersByRealm, groupsByRealm, anyType1, any1ByRealm, anyType2, any2ByRealm));
174 
175             return true;
176         }
177         return false;
178     }
179 }