Skip to content

Commit 0562fc6

Browse files
authored
Merge pull request #329 from devgateway/task/3.x/reboot-alert
task/3.x/reboot alert
2 parents 0a3378c + 83cc4d0 commit 0562fc6

File tree

7 files changed

+144
-5
lines changed

7 files changed

+144
-5
lines changed

forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.html

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<div wicket:id="navbar" role="navigation">
1010
<!-- TOP NAVIGATION CONTAINER -->
1111
</div>
12+
<div wicket:id="rebootAlert" class="rebootAlert alert-danger" />
1213
</wicket:panel>
1314
</body>
1415
</html>

forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/Header.java

+79
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,24 @@
1111
*******************************************************************************/
1212
package org.devgateway.toolkit.forms.wicket.page;
1313

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;
1421
import org.apache.wicket.markup.html.panel.Panel;
22+
import org.apache.wicket.model.IModel;
23+
import org.apache.wicket.model.StringResourceModel;
1524
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;
1632

1733
/**
1834
* @author idobre
@@ -22,11 +38,74 @@
2238
public class Header extends Panel {
2339
private static final long serialVersionUID = 1L;
2440

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+
2549
public Header(final String markupId) {
2650
this(markupId, null);
2751
}
2852

2953
public Header(final String markupId, final PageParameters parameters) {
3054
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+
});
31110
}
32111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

forms/src/main/java/org/devgateway/toolkit/forms/wicket/styles/BaseStyles.css

+12-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ body {
3939
* @see: bootstrap::navbar.less
4040
*/
4141
min-height: 50px;
42-
margin-bottom: 20px;
42+
margin-bottom: 32px;
43+
}
44+
45+
.rebootAlert {
46+
position: fixed;
47+
top: 50px;
48+
padding: 6px;
49+
text-align: center;
50+
width: 100%;
51+
border-width: 1px;
52+
border-style: solid;
53+
z-index: 1029;
4354
}
4455

4556
.navbar-brand {

persistence/src/main/java/org/devgateway/toolkit/persistence/dao/AdminSettings.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import org.hibernate.envers.Audited;
66

77
import javax.persistence.Entity;
8-
import java.io.Serializable;
8+
import java.time.Duration;
9+
import java.time.LocalDateTime;
910

1011
/**
1112
* @author idobre
@@ -15,12 +16,15 @@
1516
@Audited
1617
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
1718
public class AdminSettings extends AbstractAuditableEntity {
18-
1919
private static final long serialVersionUID = -1051140524022133178L;
20+
21+
public static final Duration REBOOT_ALERT_DURATION = Duration.ofMinutes(10L);
22+
2023
private Boolean rebootServer = false;
2124

22-
private Integer autosaveTime;
25+
private LocalDateTime rebootAlertSince;
2326

27+
private Integer autosaveTime;
2428
@Override
2529
public AbstractAuditableEntity getParent() {
2630
return null;
@@ -34,6 +38,18 @@ public void setRebootServer(final Boolean rebootServer) {
3438
this.rebootServer = rebootServer;
3539
}
3640

41+
public boolean isRebootServer() {
42+
return Boolean.TRUE.equals(getRebootServer());
43+
}
44+
45+
public LocalDateTime getRebootAlertSince() {
46+
return rebootAlertSince;
47+
}
48+
49+
public void setRebootAlertSince(final LocalDateTime rebootAlertSince) {
50+
this.rebootAlertSince = rebootAlertSince;
51+
}
52+
3753
public Integer getAutosaveTime() {
3854
return autosaveTime;
3955
}

persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsService.java

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
public interface AdminSettingsService extends BaseJpaService<AdminSettings> {
66

7+
AdminSettings get();
78
}

persistence/src/main/java/org/devgateway/toolkit/persistence/service/AdminSettingsServiceImpl.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import org.devgateway.toolkit.persistence.repository.AdminSettingsRepository;
55
import org.devgateway.toolkit.persistence.repository.norepository.BaseJpaRepository;
66
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.cache.annotation.CacheConfig;
87
import org.springframework.stereotype.Service;
98
import org.springframework.transaction.annotation.Transactional;
109

10+
import java.time.LocalDateTime;
11+
import java.util.List;
12+
1113
/**
1214
* @author idobre
1315
* @since 2019-03-04
@@ -28,4 +30,32 @@ public AdminSettings newInstance() {
2830
return new AdminSettings();
2931
}
3032

33+
@Override
34+
@Transactional
35+
public <S extends AdminSettings> S save(final S entity) {
36+
preProcessRebootAlert(entity);
37+
return repository().save(entity);
38+
}
39+
40+
@Override
41+
@Transactional
42+
public <S extends AdminSettings> S saveAndFlush(final S entity) {
43+
preProcessRebootAlert(entity);
44+
return repository().saveAndFlush(entity);
45+
}
46+
47+
private <S extends AdminSettings> void preProcessRebootAlert(final S as) {
48+
if (!as.isRebootServer()) {
49+
as.setRebootAlertSince(null);
50+
} else if (as.getRebootAlertSince() == null) {
51+
as.setRebootAlertSince(LocalDateTime.now());
52+
}
53+
}
54+
55+
@Override
56+
public AdminSettings get() {
57+
List<AdminSettings> entries = repository().findAll();
58+
return entries.isEmpty() ? null : entries.get(0);
59+
}
60+
3161
}

0 commit comments

Comments
 (0)