From cddec2cb9120e9acc5d1824df78af8bb204388fe Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sun, 19 Jun 2016 17:13:55 +0200 Subject: [PATCH] Using proper Navigation object, one step closer to implementing Page plugins --- resource/web/main_index.tmpl | 24 ++-- src/se/hal/HalServer.java | 13 +- src/se/hal/intf/HalHttpPage.java | 38 +++--- src/se/hal/page/EventConfigHttpPage.java | 4 +- src/se/hal/page/EventOverviewHttpPage.java | 4 +- src/se/hal/page/HalNavigation.java | 138 -------------------- src/se/hal/page/PCHeatMapHttpPage.java | 4 +- src/se/hal/page/PCOverviewHttpPage.java | 4 +- src/se/hal/page/SensorConfigHttpPage.java | 4 +- src/se/hal/page/SensorOverviewHttpPage.java | 4 +- src/se/hal/page/UserConfigHttpPage.java | 4 +- 11 files changed, 55 insertions(+), 186 deletions(-) delete mode 100755 src/se/hal/page/HalNavigation.java diff --git a/resource/web/main_index.tmpl b/resource/web/main_index.tmpl index 28f78d72..af35b089 100755 --- a/resource/web/main_index.tmpl +++ b/resource/web/main_index.tmpl @@ -41,17 +41,20 @@
  • - {{#userNav}}
  • {{.getName()}}
  • + {{#userNav}} +
  • + {{.getName()}} +
  • {{/userNav}} @@ -79,9 +85,11 @@
    diff --git a/src/se/hal/HalServer.java b/src/se/hal/HalServer.java index b16fe5c6..8d330087 100755 --- a/src/se/hal/HalServer.java +++ b/src/se/hal/HalServer.java @@ -15,6 +15,7 @@ import zutil.io.file.FileUtil; import zutil.log.LogUtil; import zutil.net.http.HttpServer; import zutil.net.http.page.HttpFilePage; +import zutil.net.http.page.HttpRedirectPage; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -53,9 +54,10 @@ public class HalServer { // init daemons daemons = new HalDaemon[]{ new SensorDataAggregatorDaemon(), + new SensorDataCleanupDaemon(), + new PCDataSynchronizationDaemon(), new PCDataSynchronizationClient(), - new SensorDataCleanupDaemon() }; // We set only one thread for easier troubleshooting ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); @@ -65,13 +67,14 @@ public class HalServer { // init http server - HalHttpPage.getRootNav().addSubNav(new HalNavigation("sensors", "Sensors")); - HalHttpPage.getRootNav().addSubNav(new HalNavigation("events", "Events")); + HalHttpPage.getRootNav().createSubNav("Sensors"); + HalHttpPage.getRootNav().createSubNav("Events").setWeight(100); pages = new HalHttpPage[]{ new SensorOverviewHttpPage(), + new SensorConfigHttpPage(), + new PCOverviewHttpPage(), new PCHeatMapHttpPage(), - new SensorConfigHttpPage(), new EventOverviewHttpPage(), new EventConfigHttpPage(), @@ -79,7 +82,7 @@ public class HalServer { }; HttpServer http = new HttpServer(HalContext.getIntegerProperty("http_port")); http.setDefaultPage(new HttpFilePage(FileUtil.find("resource/web/"))); - http.setPage("/", pages[0]); + http.setPage("/", new HttpRedirectPage("/"+pages[0].getId())); 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 370fc333..ee0af4ba 100755 --- a/src/se/hal/intf/HalHttpPage.java +++ b/src/se/hal/intf/HalHttpPage.java @@ -2,7 +2,6 @@ package se.hal.intf; import se.hal.HalContext; import se.hal.page.HalAlertManager; -import se.hal.page.HalNavigation; import se.hal.struct.User; import zutil.db.DBConnection; import zutil.io.file.FileUtil; @@ -12,8 +11,10 @@ import zutil.net.http.HttpPrintStream; import zutil.parser.DataNode; import zutil.parser.Templator; import zutil.parser.json.JSONWriter; +import zutil.ui.Navigation; import java.io.IOException; +import java.util.List; import java.util.Map; /** @@ -21,35 +22,28 @@ import java.util.Map; */ public abstract class HalHttpPage implements HttpPage{ private static final String TEMPLATE = "resource/web/main_index.tmpl"; - private static HalNavigation rootNav = new HalNavigation(); - private static HalNavigation userNav = new HalNavigation(); + private static Navigation rootNav = Navigation.createRootNav(); + private static Navigation userNav = Navigation.createRootNav(); - private HalNavigation nav; + private String pageId; - - public HalHttpPage(String name, String id){ - this.nav = new HalNavigation(id, name); + public HalHttpPage(String id){ + this.pageId = id; } - public String getName(){ - return nav.getName(); - } public String getId(){ - return nav.getId(); - } - public HalNavigation getNav(){ - return nav; + return pageId; } @Override - public void respond(HttpPrintStream out, HttpHeader client_info, + public void respond(HttpPrintStream out, HttpHeader header, Map session, Map cookie, Map request) throws IOException { try { if(this instanceof HalJsonPage && - (("application/json").equals(client_info.getHeader("ContentType")) || + (("application/json").equals(header.getHeader("ContentType")) || request.containsKey("json"))){ out.setHeader("Content-Type", "application/json"); JSONWriter writer = new JSONWriter(out); @@ -61,9 +55,11 @@ public abstract class HalHttpPage implements HttpPage{ Templator tmpl = new Templator(FileUtil.find(TEMPLATE)); tmpl.set("user", User.getLocalUser(db)); - tmpl.set("nav", nav.getNavBreadcrumb().get(1)); - tmpl.set("rootNav", rootNav); - tmpl.set("userNav", userNav); + List breadcrumb = Navigation.getBreadcrumb(Navigation.getPagedNavigation(header)); + if(!breadcrumb.isEmpty()) + tmpl.set("subNav", breadcrumb.get(1).createPagedNavInstance(header).getSubNavs()); + tmpl.set("rootNav", rootNav.createPagedNavInstance(header).getSubNavs()); + tmpl.set("userNav", userNav.createPagedNavInstance(header).getSubNavs()); tmpl.set("alerts", HalAlertManager.getInstance().generateAlerts()); tmpl.set("content", httpRespond(session, cookie, request)); out.print(tmpl.compile()); @@ -74,10 +70,10 @@ public abstract class HalHttpPage implements HttpPage{ } - public static HalNavigation getRootNav(){ + public static Navigation getRootNav(){ return rootNav; } - public static HalNavigation getUserNav(){ + public static Navigation getUserNav(){ return userNav; } diff --git a/src/se/hal/page/EventConfigHttpPage.java b/src/se/hal/page/EventConfigHttpPage.java index b1ee18fe..1d9210b2 100755 --- a/src/se/hal/page/EventConfigHttpPage.java +++ b/src/se/hal/page/EventConfigHttpPage.java @@ -24,8 +24,8 @@ public class EventConfigHttpPage extends HalHttpPage { public EventConfigHttpPage() { - super("Configuration", "event_config"); - super.getRootNav().getSubNav("events").addSubNav(super.getNav()); + super("event_config"); + super.getRootNav().createSubNav("Events").createSubNav(this.getId(), "Configuration").setWeight(100); eventConfigurations = new EventDataParams[ ControllerManager.getInstance().getAvailableEvents().size()]; diff --git a/src/se/hal/page/EventOverviewHttpPage.java b/src/se/hal/page/EventOverviewHttpPage.java index 46eace8d..8002f9cc 100755 --- a/src/se/hal/page/EventOverviewHttpPage.java +++ b/src/se/hal/page/EventOverviewHttpPage.java @@ -23,8 +23,8 @@ public class EventOverviewHttpPage extends HalHttpPage { public EventOverviewHttpPage(){ - super("Overview", "event_overview"); - super.getRootNav().getSubNav("events").addSubNav(super.getNav()); + super("event_overview"); + super.getRootNav().createSubNav("Events").createSubNav(this.getId(), "Overview"); } @Override diff --git a/src/se/hal/page/HalNavigation.java b/src/se/hal/page/HalNavigation.java deleted file mode 100755 index 4a34b4fe..00000000 --- a/src/se/hal/page/HalNavigation.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (c) 2015 Ziver - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package se.hal.page; - -import se.hal.intf.HalHttpPage; - -import java.util.*; - -/** - * Created by Ziver on 2015-04-02. - */ -public class HalNavigation implements Iterable{ - private static HashMap navMap = new HashMap<>(); - - private final String id; - private String name; - private HalNavigation parentNav; - private ArrayList subNav; - private HalHttpPage resource; - - - /** - * Create a root navigation object - */ - public HalNavigation() { - this(null, null); - } - /** - * Create a sub navigation object with no resource - */ - public HalNavigation(String id, String name) { - this.id = id; - this.name = name; - this.subNav = new ArrayList<>(); - navMap.put(id, this); - } - /** - * Create a sub navigation object - */ -/* public HalNavigation(HalHttpPage page) { - this.id = page.getId(); - this.name = page.getName(); - this.subNav = new ArrayList<>(); - this.resource = page; - navMap.put(id, this); - } -*/ - - @Override - public Iterator iterator() { - return subNav.iterator(); - } - public List getSubNavs() { - return subNav; - } - public HalNavigation getSubNav(String id) { - for(HalNavigation nav : subNav) { - if(nav.equals(id)) - return nav; - } - return null; - } - - public HalNavigation addSubNav(HalNavigation nav) { - nav.setParentNav(this); - subNav.add(nav); - return nav; - } - - - private void setParentNav(HalNavigation nav){ - this.parentNav = nav; - } - - @Override - public boolean equals(Object o){ - if(o instanceof String) - return this.id.equals(o); - return this == o || - (o != null && this.id.equals(((HalNavigation)o).id)); - } - - public List getNavBreadcrumb() { - LinkedList list = new LinkedList(); - - HalNavigation current = this; - while(current != null && id != null){ - list.addFirst(current); - current = current.parentNav; - } - - return list; - } - - - public String getURL(){ - return "/" + this.id; - } - - - public String getId(){ - return id; - } - public String getName(){ - return name; - } - public void setName(String name){ - this.name = name; - } - public HalNavigation getParent(){ - return parentNav; - } - - - public static HalNavigation getNav(String id){ - return navMap.get(id); - } -} diff --git a/src/se/hal/page/PCHeatMapHttpPage.java b/src/se/hal/page/PCHeatMapHttpPage.java index 0076e699..9b93f1bc 100755 --- a/src/se/hal/page/PCHeatMapHttpPage.java +++ b/src/se/hal/page/PCHeatMapHttpPage.java @@ -11,8 +11,8 @@ public class PCHeatMapHttpPage extends HalHttpPage { public PCHeatMapHttpPage() { - super("Heatmap", "pc_heatmap"); - super.getRootNav().getSubNav("sensors").addSubNav(super.getNav()); + super("pc_heatmap"); + super.getRootNav().createSubNav("Sensors").createSubNav(this.getId(), "Heatmap").setWeight(60); } @Override diff --git a/src/se/hal/page/PCOverviewHttpPage.java b/src/se/hal/page/PCOverviewHttpPage.java index fdbc5b1f..89696512 100755 --- a/src/se/hal/page/PCOverviewHttpPage.java +++ b/src/se/hal/page/PCOverviewHttpPage.java @@ -23,8 +23,8 @@ public class PCOverviewHttpPage extends HalHttpPage implements HalHttpPage.HalJs private static final String TEMPLATE = "resource/web/pc_overview.tmpl"; public PCOverviewHttpPage() { - super("Power;Challenge", "pc_overview"); - super.getRootNav().getSubNav("sensors").addSubNav(super.getNav()); + super("pc_overview"); + super.getRootNav().createSubNav("Sensors").createSubNav(this.getId(), "Power;Challenge").setWeight(50); } @Override diff --git a/src/se/hal/page/SensorConfigHttpPage.java b/src/se/hal/page/SensorConfigHttpPage.java index 8aadbb4f..91feadc8 100755 --- a/src/se/hal/page/SensorConfigHttpPage.java +++ b/src/se/hal/page/SensorConfigHttpPage.java @@ -24,8 +24,8 @@ public class SensorConfigHttpPage extends HalHttpPage { public SensorConfigHttpPage() { - super("Configuration", "sensor_config"); - super.getRootNav().getSubNav("sensors").addSubNav(super.getNav()); + super("sensor_config"); + super.getRootNav().createSubNav("Sensors").createSubNav(this.getId(), "Configuration").setWeight(100); sensorConfigurations = new SensorDataParams[ ControllerManager.getInstance().getAvailableSensors().size()]; diff --git a/src/se/hal/page/SensorOverviewHttpPage.java b/src/se/hal/page/SensorOverviewHttpPage.java index 69944bb7..c3e86cf1 100755 --- a/src/se/hal/page/SensorOverviewHttpPage.java +++ b/src/se/hal/page/SensorOverviewHttpPage.java @@ -23,8 +23,8 @@ public class SensorOverviewHttpPage extends HalHttpPage { public SensorOverviewHttpPage(){ - super("Overview", "sensor_overview"); - super.getRootNav().getSubNav("sensors").addSubNav(super.getNav()); + super("sensor_overview"); + super.getRootNav().createSubNav("Sensors").createSubNav(this.getId(), "Overview"); } @Override diff --git a/src/se/hal/page/UserConfigHttpPage.java b/src/se/hal/page/UserConfigHttpPage.java index d10a624a..a69216ee 100755 --- a/src/se/hal/page/UserConfigHttpPage.java +++ b/src/se/hal/page/UserConfigHttpPage.java @@ -14,8 +14,8 @@ public class UserConfigHttpPage extends HalHttpPage { public UserConfigHttpPage() { - super("Profile", "user_profile"); - super.getUserNav().addSubNav(super.getNav()); + super("user_profile"); + super.getUserNav().createSubNav(this.getId(), "Profile"); } @Override