From afcad4b31f0f84c3791822a33d364d58068b6d4e Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Thu, 21 Jan 2016 10:32:01 +0100 Subject: [PATCH] Alerts are now dismissible. [issue 3] Former-commit-id: a14d76e8da8aac6139dd1e130a9d0be63205a7ea --- src/se/hal/HalServer.java | 5 +- src/se/hal/intf/HalHttpPage.java | 2 +- src/se/hal/page/HalAlertManager.java | 51 ++++++++++++++++--- .../{alerts.tmpl => main_alerts.tmpl} | 26 ++++++++-- web-resource/{index.tmpl => main_index.tmpl} | 0 5 files changed, 69 insertions(+), 15 deletions(-) rename web-resource/{alerts.tmpl => main_alerts.tmpl} (66%) rename web-resource/{index.tmpl => main_index.tmpl} (100%) diff --git a/src/se/hal/HalServer.java b/src/se/hal/HalServer.java index 92307989..da3637b2 100755 --- a/src/se/hal/HalServer.java +++ b/src/se/hal/HalServer.java @@ -73,8 +73,8 @@ public class HalServer { // init http server - HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.ERROR, "error test")); - HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.INFO, "info test")); + HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.ERROR, "one view alert", AlertTTL.ONE_VIEW)); + HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.INFO, "dissmised allert", AlertTTL.DISMISSED)); HalHttpPage.getRootNav().addSubNav(new HalNavigation("sensors", "Sensors")); HalHttpPage.getRootNav().addSubNav(new HalNavigation("events", "Events")); @@ -88,6 +88,7 @@ public class HalServer { HttpServer http = new HttpServer(HalContext.getIntegerProperty("http_port")); http.setDefaultPage(new HttpFilePage(FileUtil.find("web-resource/"))); http.setPage("/", pages[0]); + http.setPage(HalAlertManager.getInstance().getUrl(), HalAlertManager.getInstance()); for(HalHttpPage page : pages){ http.setPage(page.getId(), page); } diff --git a/src/se/hal/intf/HalHttpPage.java b/src/se/hal/intf/HalHttpPage.java index 621539eb..51be4a75 100755 --- a/src/se/hal/intf/HalHttpPage.java +++ b/src/se/hal/intf/HalHttpPage.java @@ -19,7 +19,7 @@ import java.util.Map; * Created by Ziver on 2015-12-10. */ public abstract class HalHttpPage implements HttpPage{ - private static final String TEMPLATE = "web-resource/index.tmpl"; + private static final String TEMPLATE = "web-resource/main_index.tmpl"; private static HalNavigation rootNav = new HalNavigation(); private static HalNavigation userNav = new HalNavigation(); diff --git a/src/se/hal/page/HalAlertManager.java b/src/se/hal/page/HalAlertManager.java index a2938e0f..1e58cfbb 100755 --- a/src/se/hal/page/HalAlertManager.java +++ b/src/se/hal/page/HalAlertManager.java @@ -8,10 +8,7 @@ import zutil.net.http.HttpPrintStream; import zutil.parser.Templator; import java.io.IOException; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.Map; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -20,7 +17,7 @@ import java.util.logging.Logger; */ public class HalAlertManager implements HttpPage { private static final Logger logger = LogUtil.getLogger(); - private static final String TMPL_PATH = "web-resource/alerts.tmpl"; + private static final String TMPL_PATH = "web-resource/main_alerts.tmpl"; private static final String PAGE_NAME = "alert"; private static HalAlertManager instance; @@ -30,12 +27,19 @@ public class HalAlertManager implements HttpPage { SUCCESS, INFO } + public enum AlertTTL{ + ONE_VIEW, + DISMISSED + } private List alerts = new LinkedList<>(); private HalAlertManager(){} + public String getUrl(){ + return "/"+PAGE_NAME; + } public void addAlert(HalAlert alert){ alerts.add(alert); @@ -43,8 +47,21 @@ public class HalAlertManager implements HttpPage { public Templator generateAlerts(){ try { + // clone alert list and update ttl of alerts + List alertsClone = new ArrayList<>(alerts.size()); + for(Iterator it = alerts.iterator(); it.hasNext(); ){ + HalAlert alert = it.next(); + alertsClone.add(alert); + alert.ttl--; + if(alert.ttl <= 0) { // if alert is to old, remove it + logger.fine("Alert dismissed with end of life, alert id: "+ alert.id); + it.remove(); + } + } + Templator tmpl = new Templator(FileUtil.find(TMPL_PATH)); - tmpl.set("alerts", alerts); + tmpl.set("url", getUrl()); + tmpl.set("alerts", alertsClone); return tmpl; }catch (IOException e){ logger.log(Level.SEVERE, null, e); @@ -59,6 +76,21 @@ public class HalAlertManager implements HttpPage { Map cookie, Map request) throws IOException { + if (request.containsKey("action")){ + if (request.get("action").equals("dismiss")){ + // parse alert id + int id = Integer.parseInt(request.get("id")); + // Find alert + for(Iterator it = alerts.iterator(); it.hasNext(); ){ + HalAlert alert = it.next(); + if(alert.getId() == id) { + logger.fine("User dismissed alert id: "+ id); + it.remove(); + break; + } + } + } + } } @@ -77,11 +109,16 @@ public class HalAlertManager implements HttpPage { private int id; private AlertLevel level; private String msg; + protected int ttl; - public HalAlert(AlertLevel level, String msg) { + public HalAlert(AlertLevel level, String msg, AlertTTL ttl) { this.id = nextId++; this.level = level; this.msg = msg; + switch (ttl){ + case ONE_VIEW: this.ttl = 1; break; + case DISMISSED: this.ttl = Integer.MAX_VALUE; break; + } } diff --git a/web-resource/alerts.tmpl b/web-resource/main_alerts.tmpl similarity index 66% rename from web-resource/alerts.tmpl rename to web-resource/main_alerts.tmpl index de11c0b5..043da6ac 100755 --- a/web-resource/alerts.tmpl +++ b/web-resource/main_alerts.tmpl @@ -1,30 +1,46 @@ {{#alerts}} {{#.isError()}} {{/.isError()}} {{#.isWarning()}} {{/.isWarning()}} {{#.isSuccess()}} {{/.isSuccess()}} {{#.isInfo()}} {{/.isInfo()}} -{{/alerts}} \ No newline at end of file +{{/alerts}} + + \ No newline at end of file diff --git a/web-resource/index.tmpl b/web-resource/main_index.tmpl similarity index 100% rename from web-resource/index.tmpl rename to web-resource/main_index.tmpl