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