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 de.agilecoders.wicket.core.markup.html.bootstrap.image.Icon;
22  import de.agilecoders.wicket.extensions.markup.html.bootstrap.icon.FontAwesome5IconType;
23  import java.time.Duration;
24  import java.time.temporal.ChronoUnit;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  import org.apache.syncope.client.console.BookmarkablePageLinkBuilder;
29  import org.apache.syncope.client.console.SyncopeConsoleSession;
30  import org.apache.syncope.client.console.pages.UserRequests;
31  import org.apache.syncope.client.console.rest.UserRequestRestClient;
32  import org.apache.syncope.client.console.wicket.ajax.IndicatorAjaxTimerBehavior;
33  import org.apache.syncope.client.ui.commons.annotations.ExtWidget;
34  import org.apache.syncope.common.lib.to.UserRequestForm;
35  import org.apache.syncope.common.lib.types.FlowableEntitlement;
36  import org.apache.wicket.PageReference;
37  import org.apache.wicket.ajax.AjaxRequestTarget;
38  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
39  import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
40  import org.apache.wicket.markup.html.WebPage;
41  import org.apache.wicket.markup.html.link.AbstractLink;
42  import org.apache.wicket.markup.html.link.BookmarkablePageLink;
43  import org.apache.wicket.model.IModel;
44  import org.apache.wicket.model.util.ListModel;
45  import org.apache.wicket.spring.injection.annot.SpringBean;
46  
47  @ExtWidget(priority = 10)
48  public class UserRequestFormsWidget extends ExtAlertWidget<UserRequestForm> {
49  
50      private static final long serialVersionUID = 7667120094526529934L;
51  
52      @SpringBean
53      protected UserRequestRestClient userRequestRestClient;
54  
55      protected final List<UserRequestForm> lastForms = new ArrayList<>();
56  
57      public UserRequestFormsWidget(final String id, final PageReference pageRef) {
58          super(id, pageRef);
59          setOutputMarkupId(true);
60  
61          latestAlertsList.add(new IndicatorAjaxTimerBehavior(Duration.of(30, ChronoUnit.SECONDS)) {
62  
63              private static final long serialVersionUID = 7298597675929755960L;
64  
65              @Override
66              protected void onTimer(final AjaxRequestTarget target) {
67                  if (!latestAlerts.getObject().equals(lastForms)) {
68                      refreshLatestAlerts(target);
69                  }
70              }
71          });
72      }
73  
74      public final void refreshLatestAlerts(final AjaxRequestTarget target) {
75          latestAlerts.getObject().clear();
76          latestAlerts.getObject().addAll(lastForms);
77  
78          int latestAlertSize = getLatestAlertsSize();
79          linkAlertsNumber.setDefaultModelObject(latestAlertSize);
80          target.add(linkAlertsNumber);
81  
82          headerAlertsNumber.setDefaultModelObject(latestAlertSize);
83          target.add(headerAlertsNumber);
84  
85          target.add(latestAlertsList);
86  
87          lastForms.clear();
88          lastForms.addAll(latestAlerts.getObject());
89      }
90  
91      @Override
92      protected int getLatestAlertsSize() {
93          return SyncopeConsoleSession.get().owns(FlowableEntitlement.USER_REQUEST_FORM_LIST)
94                  ? userRequestRestClient.countForms(null)
95                  : 0;
96      }
97  
98      @Override
99      protected IModel<List<UserRequestForm>> getLatestAlerts() {
100         return new ListModel<>() {
101 
102             private static final long serialVersionUID = -2583290457773357445L;
103 
104             @Override
105             public List<UserRequestForm> getObject() {
106                 List<UserRequestForm> updatedForms;
107                 if (SyncopeConsoleSession.get().owns(FlowableEntitlement.USER_REQUEST_FORM_LIST)) {
108                     updatedForms = userRequestRestClient.listForms(
109                             null, 1, MAX_SIZE, new SortParam<>("createTime", true));
110                 } else {
111                     updatedForms = Collections.emptyList();
112                 }
113 
114                 return updatedForms;
115             }
116         };
117     }
118 
119     @Override
120     protected AbstractLink getEventsLink(final String linkid) {
121         BookmarkablePageLink<UserRequests> userRequests = BookmarkablePageLinkBuilder.build(linkid, UserRequests.class);
122         MetaDataRoleAuthorizationStrategy.authorize(
123                 userRequests, WebPage.ENABLE, FlowableEntitlement.USER_REQUEST_FORM_LIST);
124         return userRequests;
125     }
126 
127     @Override
128     protected Icon getIcon(final String iconid) {
129         return new Icon(iconid, FontAwesome5IconType.handshake_r);
130     }
131 }