From e032d47610129907f993a70c7082727393d5f992 Mon Sep 17 00:00:00 2001 From: nmandrescu <17854915+nmandrescu@users.noreply.github.com> Date: Tue, 20 Oct 2020 15:24:56 +0300 Subject: [PATCH 1/4] display reboot alert --- .../toolkit/forms/wicket/page/Header.html | 1 + .../toolkit/forms/wicket/page/Header.java | 73 +++++++++++++++++++ .../forms/wicket/page/Header.properties | 1 + .../forms/wicket/styles/BaseStyles.css | 12 ++- .../persistence/dao/AdminSettings.java | 21 +++++- .../service/AdminSettingsService.java | 1 + .../service/AdminSettingsServiceImpl.java | 32 +++++++- 7 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.properties diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.html b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.html index cd8a4972..2754c11c 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.html +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.html @@ -9,6 +9,7 @@
+
diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.java b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.java index 82824b87..07ed664d 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.java +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.java @@ -11,8 +11,24 @@ *******************************************************************************/ package org.devgateway.toolkit.forms.wicket.page; +import org.apache.commons.lang3.time.DurationFormatUtils; +import org.apache.wicket.Component; +import org.apache.wicket.ajax.AbstractAjaxTimerBehavior; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy; +import org.apache.wicket.markup.html.WebMarkupContainer; +import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.panel.Panel; +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.StringResourceModel; import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.apache.wicket.spring.injection.annot.SpringBean; +import org.devgateway.toolkit.forms.security.SecurityConstants; +import org.devgateway.toolkit.persistence.dao.AdminSettings; +import org.devgateway.toolkit.persistence.service.AdminSettingsService; + +import java.time.Duration; +import java.time.LocalDateTime; /** * @author idobre @@ -22,11 +38,68 @@ public class Header extends Panel { private static final long serialVersionUID = 1L; + private static final Duration ONE_MIN = Duration.ofMinutes(1); + private static final long ALERT_UPDATE_INTERVAL_SECONDS = 15; + + @SpringBean + private AdminSettingsService adminSettingsService; + + private LocalDateTime rebootSince; + public Header(final String markupId) { this(markupId, null); } public Header(final String markupId, final PageParameters parameters) { super(markupId); + + addRebootAlert(); + } + + private void addRebootAlert() { + AdminSettings as = adminSettingsService.get(); + if (as == null) { + add(new WebMarkupContainer("rebootAlert")); + return; + } + rebootSince = as.getRebootAlertSince(); + + IModel rebootDurationModel = new IModel() { + private static final long serialVersionUID = -8601598474017148336L; + + @Override + public String getObject() { + if (rebootSince == null) { + return ""; + } else { + Duration remaining = AdminSettings.REBOOT_ALERT_DURATION + .minus(Duration.between(rebootSince, LocalDateTime.now())) + // round up + .plusSeconds(30); + if (remaining.minus(ONE_MIN).isNegative()) { + remaining = ONE_MIN; + } + return DurationFormatUtils.formatDuration(remaining.toMillis(), "m"); + } + } + }; + + Label rebootAlert = new Label("rebootAlert", new StringResourceModel("rebootAlert", rebootDurationModel)); + rebootAlert.setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true); + rebootAlert.setVisible(as.isRebootServer()); + add(rebootAlert); + MetaDataRoleAuthorizationStrategy.authorize(rebootAlert, Component.RENDER, SecurityConstants.Roles.ROLE_USER); + + add(new AbstractAjaxTimerBehavior(org.apache.wicket.util.time.Duration.seconds(ALERT_UPDATE_INTERVAL_SECONDS)) { + private static final long serialVersionUID = -1168209018766325709L; + + @Override + protected void onTimer(final AjaxRequestTarget target) { + AdminSettings as = adminSettingsService.get(); + rebootSince = as.getRebootAlertSince(); + rebootAlert.setVisible(as.isRebootServer()); + target.add(rebootAlert); + } + }); } } diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.properties b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.properties new file mode 100644 index 00000000..6e9b11c1 --- /dev/null +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.properties @@ -0,0 +1 @@ +rebootAlert=Server will be rebooted within next ${} minute(s). Please save your data and logout. The system will be back online in a few minutes. diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css index 8a2bca13..2b15ac5a 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css @@ -39,7 +39,17 @@ body { * @see: bootstrap::navbar.less */ min-height: 50px; - margin-bottom: 20px; + margin-bottom: 32px; +} + +.rebootAlert { + position: fixed; + top: 50px; + padding: 6px; + text-align: center; + width: 100%; + border-width: 1px; + border-style: solid; } .mainContainer { diff --git a/persistence/src/main/java/org/devgateway/toolkit/persistence/dao/AdminSettings.java b/persistence/src/main/java/org/devgateway/toolkit/persistence/dao/AdminSettings.java index 919bd13b..8caae641 100644 --- a/persistence/src/main/java/org/devgateway/toolkit/persistence/dao/AdminSettings.java +++ b/persistence/src/main/java/org/devgateway/toolkit/persistence/dao/AdminSettings.java @@ -5,7 +5,8 @@ import org.hibernate.envers.Audited; import javax.persistence.Entity; -import java.io.Serializable; +import java.time.Duration; +import java.time.LocalDateTime; /** * @author idobre @@ -15,10 +16,14 @@ @Audited @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class AdminSettings extends AbstractAuditableEntity { - private static final long serialVersionUID = -1051140524022133178L; + + public static final Duration REBOOT_ALERT_DURATION = Duration.ofMinutes(10L); + private Boolean rebootServer = false; + private LocalDateTime rebootAlertSince; + @Override public AbstractAuditableEntity getParent() { return null; @@ -31,4 +36,16 @@ public Boolean getRebootServer() { public void setRebootServer(final Boolean rebootServer) { this.rebootServer = rebootServer; } + + public boolean isRebootServer() { + return Boolean.TRUE.equals(getRebootServer()); + } + + public LocalDateTime getRebootAlertSince() { + return rebootAlertSince; + } + + public void setRebootAlertSince(final LocalDateTime rebootAlertSince) { + this.rebootAlertSince = rebootAlertSince; + } } diff --git a/persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsService.java b/persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsService.java index 021e599f..61e14626 100644 --- a/persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsService.java +++ b/persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsService.java @@ -4,4 +4,5 @@ public interface AdminSettingsService extends BaseJpaService { + AdminSettings get(); } diff --git a/persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsServiceImpl.java b/persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsServiceImpl.java index 20ecf170..a3177998 100644 --- a/persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsServiceImpl.java +++ b/persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsServiceImpl.java @@ -4,10 +4,12 @@ import org.devgateway.toolkit.persistence.repository.AdminSettingsRepository; import org.devgateway.toolkit.persistence.repository.norepository.BaseJpaRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.CacheConfig; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; +import java.util.List; + /** * @author idobre * @since 2019-03-04 @@ -28,4 +30,32 @@ public AdminSettings newInstance() { return new AdminSettings(); } + @Override + @Transactional + public S save(final S entity) { + preProcessRebootAlert(entity); + return repository().save(entity); + } + + @Override + @Transactional + public S saveAndFlush(final S entity) { + preProcessRebootAlert(entity); + return repository().saveAndFlush(entity); + } + + private void preProcessRebootAlert(final S as) { + if (!as.isRebootServer()) { + as.setRebootAlertSince(null); + } else if (as.getRebootAlertSince() == null) { + as.setRebootAlertSince(LocalDateTime.now()); + } + } + + @Override + public AdminSettings get() { + List entries = repository().findAll(); + return entries.isEmpty() ? null : entries.get(0); + } + } From a08e4cee12c50507c96368d85ad58d2f123015a7 Mon Sep 17 00:00:00 2001 From: nmandrescu <17854915+nmandrescu@users.noreply.github.com> Date: Mon, 26 Oct 2020 18:53:57 +0200 Subject: [PATCH 2/4] ensure reboot warning on top of other content as header --- .../org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css | 1 + 1 file changed, 1 insertion(+) diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css index 2b15ac5a..97b2a2be 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css @@ -50,6 +50,7 @@ body { width: 100%; border-width: 1px; border-style: solid; + z-index: 1030; } .mainContainer { From d4371d9ec65ac43598dc1a0719bef0ace07b2503 Mon Sep 17 00:00:00 2001 From: nmandrescu <17854915+nmandrescu@users.noreply.github.com> Date: Mon, 26 Oct 2020 18:54:51 +0200 Subject: [PATCH 3/4] reboot warning visibility check --- .../toolkit/forms/wicket/page/Header.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.java b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.java index 07ed664d..13870ee4 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.java +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.java @@ -62,7 +62,7 @@ private void addRebootAlert() { add(new WebMarkupContainer("rebootAlert")); return; } - rebootSince = as.getRebootAlertSince(); + rebootSince = as.isRebootServer() ? as.getRebootAlertSince() : null; IModel rebootDurationModel = new IModel() { private static final long serialVersionUID = -8601598474017148336L; @@ -84,9 +84,16 @@ public String getObject() { } }; - Label rebootAlert = new Label("rebootAlert", new StringResourceModel("rebootAlert", rebootDurationModel)); + Label rebootAlert = new Label("rebootAlert", new StringResourceModel("rebootAlert", rebootDurationModel)) { + private static final long serialVersionUID = -3562806753180165059L; + + @Override + protected void onConfigure() { + super.onConfigure(); + setVisible(rebootSince != null); + } + }; rebootAlert.setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true); - rebootAlert.setVisible(as.isRebootServer()); add(rebootAlert); MetaDataRoleAuthorizationStrategy.authorize(rebootAlert, Component.RENDER, SecurityConstants.Roles.ROLE_USER); @@ -96,8 +103,7 @@ public String getObject() { @Override protected void onTimer(final AjaxRequestTarget target) { AdminSettings as = adminSettingsService.get(); - rebootSince = as.getRebootAlertSince(); - rebootAlert.setVisible(as.isRebootServer()); + rebootSince = as.isRebootServer() ? as.getRebootAlertSince() : null; target.add(rebootAlert); } }); From 24ab8af19036f8afb347683bd9919979b8895eb9 Mon Sep 17 00:00:00 2001 From: nmandrescu <17854915+nmandrescu@users.noreply.github.com> Date: Mon, 26 Oct 2020 20:33:31 +0200 Subject: [PATCH 4/4] reboot warning z-index lower than header to let its menu over --- .../org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css index 97b2a2be..7fb763a1 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css @@ -50,7 +50,7 @@ body { width: 100%; border-width: 1px; border-style: solid; - z-index: 1030; + z-index: 1029; } .mainContainer {