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 java.io.Serializable;
23  import java.util.List;
24  import org.apache.wicket.markup.ComponentTag;
25  import org.apache.wicket.markup.html.WebMarkupContainer;
26  import org.apache.wicket.markup.html.basic.Label;
27  import org.apache.wicket.markup.html.link.AbstractLink;
28  import org.apache.wicket.markup.html.panel.Panel;
29  import org.apache.wicket.model.IModel;
30  import org.apache.wicket.model.LoadableDetachableModel;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  public abstract class AlertWidget<T extends Serializable> extends Panel {
35  
36      private static final long serialVersionUID = 7667120094526529934L;
37  
38      protected static final Logger LOG = LoggerFactory.getLogger(AlertWidget.class);
39  
40      protected static final int MAX_SIZE = 5;
41  
42      protected final Label linkAlertsNumber;
43  
44      protected final Label headerAlertsNumber;
45  
46      protected final WebMarkupContainer latestAlertsList;
47  
48      protected IModel<List<T>> latestAlerts;
49  
50      public AlertWidget(final String id) {
51          super(id);
52          this.latestAlerts = getLatestAlerts();
53  
54          setOutputMarkupId(true);
55  
56          final LoadableDetachableModel<Integer> size = new LoadableDetachableModel<>() {
57  
58              private static final long serialVersionUID = 7474274077691068779L;
59  
60              @Override
61              protected Integer load() {
62                  return getLatestAlertsSize();
63              }
64          };
65  
66          add(getIcon("icon"));
67  
68          linkAlertsNumber = new Label("alerts", size) {
69  
70              private static final long serialVersionUID = 4755868673082976208L;
71  
72              @Override
73              protected void onComponentTag(final ComponentTag tag) {
74                  super.onComponentTag(tag);
75  
76                  boolean warning = false;
77                  try {
78                      warning = Integer.parseInt(getDefaultModelObject().toString()) > 0;
79                  } catch (Exception e) {
80                      LOG.error("Invalid value found: {}", getDefaultModelObject(), e);
81                  }
82  
83                  if (warning) {
84                      tag.put("class", "badge badge-warning navbar-badge");
85                  } else {
86                      tag.put("class", "badge badge-success navbar-badge");
87                  }
88              }
89          };
90          add(linkAlertsNumber.setOutputMarkupId(true));
91  
92          headerAlertsNumber = new Label("number", size);
93          headerAlertsNumber.setOutputMarkupId(true);
94          add(headerAlertsNumber);
95  
96          add(getEventsLink("alertsLink"));
97  
98          latestAlertsList = new WebMarkupContainer("latestAlertsList");
99          latestAlertsList.setOutputMarkupId(true);
100         add(latestAlertsList);
101     }
102 
103     protected int getLatestAlertsSize() {
104         return latestAlerts.getObject().size();
105     }
106 
107     protected abstract IModel<List<T>> getLatestAlerts();
108 
109     protected abstract AbstractLink getEventsLink(String linkid);
110 
111     protected abstract Icon getIcon(String iconid);
112 
113     public static class AlertLink<T> extends Panel {
114 
115         private static final long serialVersionUID = -6011939604125512766L;
116 
117         public AlertLink(final String id, final T alert) {
118             super(id);
119             add(new Label("alert", alert.toString()));
120         }
121     }
122 }