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.text.NumberFormat;
22  import java.util.List;
23  import org.apache.syncope.client.console.SyncopeConsoleSession;
24  import org.apache.syncope.client.console.SyncopeWebApplication;
25  import org.apache.syncope.client.console.pages.BasePage;
26  import org.apache.syncope.client.console.pages.Realms;
27  import org.apache.syncope.client.console.pages.Security;
28  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
29  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
30  import org.apache.wicket.ajax.AjaxEventBehavior;
31  import org.apache.wicket.ajax.AjaxRequestTarget;
32  import org.apache.wicket.behavior.AttributeAppender;
33  import org.apache.wicket.markup.html.WebMarkupContainer;
34  import org.apache.wicket.markup.html.basic.Label;
35  import org.apache.wicket.request.mapper.parameter.PageParameters;
36  import org.apache.wicket.spring.injection.annot.SpringBean;
37  
38  public class NumberWidget extends BaseWidget {
39  
40      private static final long serialVersionUID = -816175678514035085L;
41  
42      @SpringBean
43      protected AnyTypeRestClient anyTypeRestClient;
44  
45      protected Number number;
46  
47      protected final Label numberLabel;
48  
49      public NumberWidget(final String id, final String bg, final Number number, final String label, final String icon) {
50          super(id);
51          this.number = number;
52          setOutputMarkupId(true);
53  
54          WebMarkupContainer box = new WebMarkupContainer("card");
55          box.add(new AttributeAppender("class", ' ' + bg));
56  
57          @SuppressWarnings("unchecked")
58          Class<? extends BasePage> realmsPage =
59                  (Class<? extends BasePage>) SyncopeWebApplication.get().getPageClass("realms");
60          if (realmsPage == null) {
61              realmsPage = Realms.class;
62          }
63  
64          boolean isAuthorized = true;
65          PageParameters pageParameters = new PageParameters();
66          Class<? extends BasePage> responsePage;
67          List<String> anyTypes = anyTypeRestClient.list();
68          switch (id) {
69              case "totalUsers":
70                  pageParameters.add(Realms.SELECTED_INDEX, 1);
71                  responsePage = realmsPage;
72                  isAuthorized = SyncopeConsoleSession.get().owns(IdRepoEntitlement.USER_SEARCH);
73                  break;
74  
75              case "totalGroups":
76                  pageParameters.add(Realms.SELECTED_INDEX, 2);
77                  responsePage = realmsPage;
78                  isAuthorized = SyncopeConsoleSession.get().owns(IdRepoEntitlement.GROUP_SEARCH);
79                  break;
80  
81              case "totalAny1OrRoles":
82                  if (icon.equals("ion ion-gear-a")) {
83                      Integer selectedIndex = null;
84                      for (int i = 0; i < anyTypes.size() && selectedIndex == null; i++) {
85                          if (anyTypes.get(i).equals(label)) {
86                              selectedIndex = i + 1;
87                              pageParameters.add(Realms.SELECTED_INDEX, selectedIndex);
88                          }
89                      }
90                      responsePage = realmsPage;
91                      isAuthorized = SyncopeConsoleSession.get().owns(label + "_SEARCH");
92                  } else {
93                      responsePage = Security.class;
94                      isAuthorized = SyncopeConsoleSession.get().owns(IdRepoEntitlement.ROLE_LIST);
95                  }
96                  break;
97  
98              case "totalAny2OrResources":
99                  Integer selectedIndex = null;
100                 for (int i = 0; i < anyTypes.size() && selectedIndex == null; i++) {
101                     if (anyTypes.get(i).equals(label)) {
102                         selectedIndex = i + 1;
103                         pageParameters.add(Realms.SELECTED_INDEX, selectedIndex);
104                     }
105                 }
106                 responsePage = realmsPage;
107                 isAuthorized = SyncopeConsoleSession.get().owns(label + "_SEARCH");
108                 break;
109 
110             default:
111                 pageParameters.add(Realms.SELECTED_INDEX, 0);
112                 responsePage = realmsPage;
113         }
114 
115         AjaxEventBehavior clickToRealms = new AjaxEventBehavior("mousedown") {
116 
117             private static final long serialVersionUID = -7133385027739964990L;
118 
119             @Override
120             protected void onEvent(final AjaxRequestTarget target) {
121                 setResponsePage(responsePage, pageParameters);
122             }
123         };
124         if (isAuthorized) {
125             box.add(clickToRealms);
126         }
127 
128         add(box);
129 
130         numberLabel = new Label(
131                 "number",
132                 NumberFormat.getInstance(SyncopeConsoleSession.get().getLocale()).format(number));
133         numberLabel.setOutputMarkupId(true);
134         box.add(numberLabel);
135         box.add(new Label("label", label));
136 
137         Label iconLabel = new Label("icon");
138         iconLabel.add(new AttributeAppender("class", icon));
139         box.add(iconLabel);
140     }
141 
142     public boolean refresh(final Number number) {
143         if (this.number != number) {
144             this.number = number;
145             numberLabel.setDefaultModelObject(
146                     NumberFormat.getInstance(SyncopeConsoleSession.get().getLocale()).format(number));
147             return true;
148         }
149         return false;
150     }
151 }