|
11 | 11 | *******************************************************************************/
|
12 | 12 | package org.devgateway.toolkit.forms.wicket.page;
|
13 | 13 |
|
| 14 | +import org.apache.commons.lang3.time.DurationFormatUtils; |
| 15 | +import org.apache.wicket.Component; |
| 16 | +import org.apache.wicket.ajax.AbstractAjaxTimerBehavior; |
| 17 | +import org.apache.wicket.ajax.AjaxRequestTarget; |
| 18 | +import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy; |
| 19 | +import org.apache.wicket.markup.html.WebMarkupContainer; |
| 20 | +import org.apache.wicket.markup.html.basic.Label; |
14 | 21 | import org.apache.wicket.markup.html.panel.Panel;
|
| 22 | +import org.apache.wicket.model.IModel; |
| 23 | +import org.apache.wicket.model.StringResourceModel; |
15 | 24 | import org.apache.wicket.request.mapper.parameter.PageParameters;
|
| 25 | +import org.apache.wicket.spring.injection.annot.SpringBean; |
| 26 | +import org.devgateway.toolkit.forms.security.SecurityConstants; |
| 27 | +import org.devgateway.toolkit.persistence.dao.AdminSettings; |
| 28 | +import org.devgateway.toolkit.persistence.service.AdminSettingsService; |
| 29 | + |
| 30 | +import java.time.Duration; |
| 31 | +import java.time.LocalDateTime; |
16 | 32 |
|
17 | 33 | /**
|
18 | 34 | * @author idobre
|
|
22 | 38 | public class Header extends Panel {
|
23 | 39 | private static final long serialVersionUID = 1L;
|
24 | 40 |
|
| 41 | + private static final Duration ONE_MIN = Duration.ofMinutes(1); |
| 42 | + private static final long ALERT_UPDATE_INTERVAL_SECONDS = 15; |
| 43 | + |
| 44 | + @SpringBean |
| 45 | + private AdminSettingsService adminSettingsService; |
| 46 | + |
| 47 | + private LocalDateTime rebootSince; |
| 48 | + |
25 | 49 | public Header(final String markupId) {
|
26 | 50 | this(markupId, null);
|
27 | 51 | }
|
28 | 52 |
|
29 | 53 | public Header(final String markupId, final PageParameters parameters) {
|
30 | 54 | super(markupId);
|
| 55 | + |
| 56 | + addRebootAlert(); |
| 57 | + } |
| 58 | + |
| 59 | + private void addRebootAlert() { |
| 60 | + AdminSettings as = adminSettingsService.get(); |
| 61 | + if (as == null) { |
| 62 | + add(new WebMarkupContainer("rebootAlert")); |
| 63 | + return; |
| 64 | + } |
| 65 | + rebootSince = as.isRebootServer() ? as.getRebootAlertSince() : null; |
| 66 | + |
| 67 | + IModel<String> rebootDurationModel = new IModel<String>() { |
| 68 | + private static final long serialVersionUID = -8601598474017148336L; |
| 69 | + |
| 70 | + @Override |
| 71 | + public String getObject() { |
| 72 | + if (rebootSince == null) { |
| 73 | + return ""; |
| 74 | + } else { |
| 75 | + Duration remaining = AdminSettings.REBOOT_ALERT_DURATION |
| 76 | + .minus(Duration.between(rebootSince, LocalDateTime.now())) |
| 77 | + // round up |
| 78 | + .plusSeconds(30); |
| 79 | + if (remaining.minus(ONE_MIN).isNegative()) { |
| 80 | + remaining = ONE_MIN; |
| 81 | + } |
| 82 | + return DurationFormatUtils.formatDuration(remaining.toMillis(), "m"); |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + Label rebootAlert = new Label("rebootAlert", new StringResourceModel("rebootAlert", rebootDurationModel)) { |
| 88 | + private static final long serialVersionUID = -3562806753180165059L; |
| 89 | + |
| 90 | + @Override |
| 91 | + protected void onConfigure() { |
| 92 | + super.onConfigure(); |
| 93 | + setVisible(rebootSince != null); |
| 94 | + } |
| 95 | + }; |
| 96 | + rebootAlert.setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true); |
| 97 | + add(rebootAlert); |
| 98 | + MetaDataRoleAuthorizationStrategy.authorize(rebootAlert, Component.RENDER, SecurityConstants.Roles.ROLE_USER); |
| 99 | + |
| 100 | + add(new AbstractAjaxTimerBehavior(org.apache.wicket.util.time.Duration.seconds(ALERT_UPDATE_INTERVAL_SECONDS)) { |
| 101 | + private static final long serialVersionUID = -1168209018766325709L; |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void onTimer(final AjaxRequestTarget target) { |
| 105 | + AdminSettings as = adminSettingsService.get(); |
| 106 | + rebootSince = as.isRebootServer() ? as.getRebootAlertSince() : null; |
| 107 | + target.add(rebootAlert); |
| 108 | + } |
| 109 | + }); |
31 | 110 | }
|
32 | 111 | }
|
0 commit comments