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.Map;
22  import org.apache.commons.lang3.BooleanUtils;
23  import org.apache.commons.lang3.tuple.Pair;
24  import org.apache.syncope.client.console.BookmarkablePageLinkBuilder;
25  import org.apache.syncope.client.console.chartjs.ChartJSPanel;
26  import org.apache.syncope.client.console.chartjs.Doughnut;
27  import org.apache.syncope.client.console.chartjs.DoughnutAndPieChartData;
28  import org.apache.syncope.client.console.pages.Notifications;
29  import org.apache.syncope.client.console.pages.Policies;
30  import org.apache.syncope.client.console.pages.Security;
31  import org.apache.syncope.client.console.pages.Types;
32  import org.apache.syncope.common.lib.info.NumbersInfo;
33  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
34  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
35  import org.apache.wicket.markup.html.WebMarkupContainer;
36  import org.apache.wicket.markup.html.WebPage;
37  import org.apache.wicket.markup.html.link.BookmarkablePageLink;
38  import org.apache.wicket.model.Model;
39  
40  public class CompletenessWidget extends BaseWidget {
41  
42      private static final long serialVersionUID = 7667120094526529934L;
43  
44      private Map<String, Boolean> confCompleteness;
45  
46      private final ChartJSPanel chart;
47  
48      private final WebMarkupContainer actions;
49  
50      private final BookmarkablePageLink<Policies> policies;
51  
52      private final BookmarkablePageLink<Notifications> notifications;
53  
54      private final BookmarkablePageLink<Types> types;
55  
56      private final BookmarkablePageLink<Security> securityquestions;
57  
58      private final BookmarkablePageLink<Security> roles;
59  
60      public CompletenessWidget(final String id, final Map<String, Boolean> confCompleteness) {
61          super(id);
62          this.confCompleteness = confCompleteness;
63          setOutputMarkupId(true);
64  
65          Pair<Doughnut, Integer> built = build(confCompleteness);
66  
67          chart = new ChartJSPanel("chart", Model.of(built.getLeft()));
68          add(chart);
69  
70          actions = new WebMarkupContainer("actions");
71          actions.setOutputMarkupPlaceholderTag(true);
72          actions.setVisible(built.getRight() > 0);
73  
74          add(actions);
75  
76          policies = BookmarkablePageLinkBuilder.build("policies", Policies.class);
77          policies.setOutputMarkupPlaceholderTag(true);
78          MetaDataRoleAuthorizationStrategy.authorize(policies, WebPage.ENABLE, IdRepoEntitlement.POLICY_LIST);
79          actions.add(policies);
80          policies.setVisible(
81                  !confCompleteness.get(NumbersInfo.ConfItem.ACCOUNT_POLICY.name())
82                  || !confCompleteness.get(NumbersInfo.ConfItem.PASSWORD_POLICY.name()));
83  
84          notifications = BookmarkablePageLinkBuilder.build("notifications", Notifications.class);
85          notifications.setOutputMarkupPlaceholderTag(true);
86          MetaDataRoleAuthorizationStrategy.authorize(
87                  notifications, WebPage.ENABLE, IdRepoEntitlement.NOTIFICATION_LIST);
88          actions.add(notifications);
89          notifications.setVisible(!confCompleteness.get(NumbersInfo.ConfItem.NOTIFICATION.name()));
90  
91          types = BookmarkablePageLinkBuilder.build("types", Types.class);
92          types.setOutputMarkupPlaceholderTag(true);
93          MetaDataRoleAuthorizationStrategy.authorize(types, WebPage.ENABLE, IdRepoEntitlement.ANYTYPECLASS_LIST);
94          actions.add(types);
95          types.setVisible(
96                  !confCompleteness.get(NumbersInfo.ConfItem.VIR_SCHEMA.name())
97                  || !confCompleteness.get(NumbersInfo.ConfItem.ANY_TYPE.name()));
98  
99          securityquestions = BookmarkablePageLinkBuilder.build("securityquestions", Security.class);
100         securityquestions.setOutputMarkupPlaceholderTag(true);
101         actions.add(securityquestions);
102         securityquestions.setVisible(!confCompleteness.get(NumbersInfo.ConfItem.SECURITY_QUESTION.name()));
103 
104         roles = BookmarkablePageLinkBuilder.build("roles", Security.class);
105         roles.setOutputMarkupPlaceholderTag(true);
106         MetaDataRoleAuthorizationStrategy.authorize(roles, WebPage.ENABLE, IdRepoEntitlement.ROLE_LIST);
107         actions.add(roles);
108         roles.setVisible(!confCompleteness.get(NumbersInfo.ConfItem.ROLE.name()));
109     }
110 
111     private Pair<Doughnut, Integer> build(final Map<String, Boolean> confCompleteness) {
112         Doughnut doughnut = new Doughnut();
113         doughnut.getOptions().setResponsive(true);
114         doughnut.getOptions().setMaintainAspectRatio(true);
115         doughnut.getOptions().setTooltipTemplate("<%= label %>");
116 
117         int done = 0;
118         int todo = 0;
119         for (Map.Entry<String, Boolean> entry : confCompleteness.entrySet()) {
120             if (BooleanUtils.isTrue(entry.getValue())) {
121                 done += NumbersInfo.ConfItem.getScore(entry.getKey());
122             } else {
123                 todo++;
124             }
125         }
126 
127         DoughnutAndPieChartData data = new DoughnutAndPieChartData();
128         doughnut.setData(data);
129 
130         DoughnutAndPieChartData.DataSet dataset = new DoughnutAndPieChartData.DataSet();
131         data.getDatasets().add(dataset);
132 
133         dataset.getData().add(done);
134         dataset.getData().add(100 - done);
135 
136         dataset.getBackgroundColor().add("green");
137         dataset.getBackgroundColor().add("red");
138 
139         data.getLabels().add(getString("done"));
140         data.getLabels().add(getString("todo") + ": " + todo);
141 
142         return Pair.of(doughnut, todo);
143     }
144 
145     public boolean refresh(final Map<String, Boolean> confCompleteness) {
146         if (!this.confCompleteness.equals(confCompleteness)) {
147             this.confCompleteness = confCompleteness;
148 
149             Pair<Doughnut, Integer> built = build(confCompleteness);
150 
151             chart.setDefaultModelObject(built.getLeft());
152 
153             actions.setVisible(built.getRight() > 0);
154 
155             policies.setVisible(
156                     !confCompleteness.get(NumbersInfo.ConfItem.ACCOUNT_POLICY.name())
157                     || !confCompleteness.get(NumbersInfo.ConfItem.PASSWORD_POLICY.name()));
158 
159             notifications.setVisible(!confCompleteness.get(NumbersInfo.ConfItem.NOTIFICATION.name()));
160 
161             types.setVisible(
162                     !confCompleteness.get(NumbersInfo.ConfItem.VIR_SCHEMA.name())
163                     || !confCompleteness.get(NumbersInfo.ConfItem.ANY_TYPE.name()));
164 
165             securityquestions.setVisible(!confCompleteness.get(NumbersInfo.ConfItem.SECURITY_QUESTION.name()));
166 
167             roles.setVisible(!confCompleteness.get(NumbersInfo.ConfItem.ROLE.name()));
168 
169             return true;
170         }
171         return false;
172     }
173 }