From 59d18679ea529730b5cef03376f98cc993cddf32 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Fri, 6 Dec 2019 12:24:44 +0100 Subject: [PATCH] new admin panel new date creation in dashboard.html --- .../website/Webserver.java | 4 + .../website/datarequests/AdminRequests.java | 52 ++++ .../website/datarequests/DataRequest.java | 27 +- .../website/datarequests/NewDateRequest.java | 76 +++++ .../datarequests/login/CheckLoginState.java | 2 +- .../datarequests/login/LoginRequest.java | 2 +- .../datarequests/login/LoginState.java | 10 +- src/resources/wwwroot/adminpanel.html | 275 ++++++++++++++++++ src/resources/wwwroot/dashboard.html | 45 ++- src/resources/wwwroot/js/adminpanel.js | 34 +++ src/resources/wwwroot/js/dashboard.js | 94 +++++- 11 files changed, 580 insertions(+), 41 deletions(-) create mode 100644 src/java/com/wasteinformationserver/website/datarequests/AdminRequests.java create mode 100644 src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java create mode 100644 src/resources/wwwroot/adminpanel.html create mode 100644 src/resources/wwwroot/js/adminpanel.js diff --git a/src/java/com/wasteinformationserver/website/Webserver.java b/src/java/com/wasteinformationserver/website/Webserver.java index c67c1dd..3006404 100644 --- a/src/java/com/wasteinformationserver/website/Webserver.java +++ b/src/java/com/wasteinformationserver/website/Webserver.java @@ -2,6 +2,8 @@ package com.wasteinformationserver.website; import com.sun.net.httpserver.HttpServer; import com.wasteinformationserver.basicutils.Log; +import com.wasteinformationserver.website.datarequests.AdminRequests; +import com.wasteinformationserver.website.datarequests.NewDateRequest; import com.wasteinformationserver.website.datarequests.login.CheckLoginState; import com.wasteinformationserver.website.datarequests.DataRequest; import com.wasteinformationserver.website.datarequests.login.LoginRequest; @@ -27,6 +29,8 @@ public class Webserver { server.createContext("/senddata/registerpost",new RegisterRequest()); server.createContext("/senddata/checkloginstate",new CheckLoginState()); server.createContext("/senddata/wastedata",new DataRequest()); + server.createContext("/senddata/admindata", new AdminRequests()); + server.createContext("/senddata/newdate", new NewDateRequest()); server.setExecutor(null); // creates a default executor server.start(); diff --git a/src/java/com/wasteinformationserver/website/datarequests/AdminRequests.java b/src/java/com/wasteinformationserver/website/datarequests/AdminRequests.java new file mode 100644 index 0000000..80da6a0 --- /dev/null +++ b/src/java/com/wasteinformationserver/website/datarequests/AdminRequests.java @@ -0,0 +1,52 @@ +package com.wasteinformationserver.website.datarequests; + +import com.wasteinformationserver.basicutils.Log; +import com.wasteinformationserver.db.JDCB; +import com.wasteinformationserver.website.basicrequest.PostRequest; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.HashMap; + +public class AdminRequests extends PostRequest { + @Override + public String request(HashMap params) { + String result = ""; + switch (params.get("action")) { + case "shutdownserver": + System.exit(0); + break; + case "restartserver": + final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; + File currentJar = null; + try { + currentJar = new File(AdminRequests.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + /* is it a jar file? */ + if(!currentJar.getName().endsWith(".jar")) + System.out.println("not jar"); + + /* Build command: java -jar application.jar */ + final ArrayList command = new ArrayList(); + command.add(javaBin); + command.add("-jar"); + command.add(currentJar.getPath()); + + final ProcessBuilder builder = new ProcessBuilder(command); + try { + builder.start(); + } catch (IOException e) { + e.printStackTrace(); + } + System.exit(0); + break; + } + + return result; + } +} diff --git a/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java b/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java index 95babec..d72c020 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java @@ -45,24 +45,24 @@ public class DataRequest extends PostRequest { int status = jdcb.executeUpdate("INSERT INTO `cities`(`userid`, `name`, `wastetype`, `zone`) VALUES ('0','" + params.get("cityname") + "','" + params.get("wastetype") + "','" + params.get("wastezone") + "');"); System.out.println(status); if (status == 1) { - sb.append("\"status\" : \"inserted\"}"); + sb.append("\"status\" : \"inserted\""); } else { sb.append("\"status\" : \"inserterror\""); } } else if (size > 1) { Log.warning("more than one entry in db!!!"); - result = "\"status\" : \"exists\""; + sb.append("\"status\" : \"exists\""); } else { //already exists System.out.println("already exists"); - result = "\"status\" : \"exists\""; + sb.append("\"status\" : \"exists\""); } sb.append(",\"query\":\"ok\""); sb.append("}"); - Log.debug(result); + result = sb.toString(); break; case "getAllCities": StringBuilder builder = new StringBuilder(); @@ -85,17 +85,34 @@ public class DataRequest extends PostRequest { } catch (SQLException e) { e.printStackTrace(); } - builder.append("]}"); + builder.append("]"); + builder.append(",\"query\":\"ok\""); + builder.append("}"); result = builder.toString(); Log.debug(result); break; case "deletecity": //DELETE FROM `cities` WHERE `id`=0 + StringBuilder sbb = new StringBuilder(); // TODO: 06.12.19 better naming and sb for all + Log.debug(params.get("id")); int status= jdcb.executeUpdate("DELETE FROM `cities` WHERE `id`='" + params.get("id")+"'"); Log.debug(status); + sbb.append("{"); + + if (status == 1){ + //success + sbb.append("\"status\" : \"success\""); + }else { + sbb.append("\"status\" : \"error\""); + } + + sbb.append(",\"query\":\"ok\""); + sbb.append("}"); + result = sbb.toString(); + break; } return result; diff --git a/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java b/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java new file mode 100644 index 0000000..d9ece0e --- /dev/null +++ b/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java @@ -0,0 +1,76 @@ +package com.wasteinformationserver.website.datarequests; + +import com.wasteinformationserver.basicutils.Log; +import com.wasteinformationserver.db.JDCB; +import com.wasteinformationserver.website.basicrequest.PostRequest; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; + +public class NewDateRequest extends PostRequest { + @Override + public String request(HashMap params) { + StringBuilder sb = new StringBuilder(); + JDCB jdcb; + try { + jdcb = JDCB.getInstance(); + } catch (IOException e) { + Log.error("no connection to db"); + return "{\"query\" : \"nodbconn\"}"; + } + switch (params.get("action")) { + case "getCitynames": + ResultSet sett = jdcb.executeQuery("select * from cities"); + Log.debug(sett.toString()); + sb.append("{\"data\":["); + try { + String prev = ""; + while (sett.next()) { + if (prev.equals(sett.getString("name"))){ + + }else { + sb.append("{\"cityname\":\"" + sett.getString("name") + "\"}"); + if (!sett.isLast()) { + sb.append(","); + } + } + prev = sett.getString("name"); + } + } catch (SQLException e) { + e.printStackTrace(); + } + sb.append("]"); + sb.append(",\"query\":\"ok\""); + sb.append("}"); + break; + case "getzones": + ResultSet set = jdcb.executeQuery("select * from cities WHERE `name`='"+params.get("cityname")+"' ORDER BY zone ASC"); + Log.debug(set.toString()); + sb.append("{\"data\":["); + try { + int prev = 42; + while (set.next()) { + if (prev == set.getInt("zone")){ + + }else { + sb.append("{\"zone\":\"" + set.getInt("zone") + "\"}"); + if (!set.isLast()) { + sb.append(","); + } + } + prev = set.getInt("zone"); + System.out.println(prev); + } + } catch (SQLException e) { + e.printStackTrace(); + } + sb.append("]"); + sb.append(",\"query\":\"ok\""); + sb.append("}"); + break; + } + return sb.toString(); + } +} diff --git a/src/java/com/wasteinformationserver/website/datarequests/login/CheckLoginState.java b/src/java/com/wasteinformationserver/website/datarequests/login/CheckLoginState.java index 3caeca8..f0ae85d 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/login/CheckLoginState.java +++ b/src/java/com/wasteinformationserver/website/datarequests/login/CheckLoginState.java @@ -11,7 +11,7 @@ public class CheckLoginState extends PostRequest { Log.message("checking login state"); if ((params.get("action")).equals("getloginstate")){ if (LoginState.getObject().isLoggedIn()){ - return "{\"loggedin\":true, \"username\":\""+LoginState.getObject().getUsername()+"\"}"; + return "{\"loggedin\":true, \"username\":\""+LoginState.getObject().getUsername()+"\", \"permission\":\""+LoginState.getObject().getPermission()+"\"}"; }else { return "{\"loggedin\":false}"; } diff --git a/src/java/com/wasteinformationserver/website/datarequests/login/LoginRequest.java b/src/java/com/wasteinformationserver/website/datarequests/login/LoginRequest.java index 5709322..f0a05b3 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/login/LoginRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/login/LoginRequest.java @@ -38,7 +38,7 @@ public class LoginRequest extends PostRequest { if (HttpTools.StringToMD5(password).equals(s.getString("password"))) { Log.debug("login success"); LoginState.getObject().logIn(); - LoginState.getObject().setAccountData(username,"","",""); + LoginState.getObject().setAccountData(username,"","","", s.getInt("permission")); // TODO: 06.12.19 response = "{\"accept\": true}"; } else { Log.debug("wrong password"); diff --git a/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java b/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java index b36a48b..ebeb75a 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java +++ b/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java @@ -13,8 +13,9 @@ public class LoginState { private String firstname; private String lastname; private String email; + private int permission; - boolean loggedin = true; // TODO: 05.12.19 set back!! + boolean loggedin = false; public void logIn(){ loggedin=true; @@ -24,11 +25,12 @@ public class LoginState { loggedin=false; } - public void setAccountData(String username, String firstname, String lastname, String email){ + public void setAccountData(String username, String firstname, String lastname, String email, int permission){ this.username=username; this.firstname=firstname; this.lastname=lastname; this.email=email; + this.permission = permission; } public boolean isLoggedIn(){ @@ -50,4 +52,8 @@ public class LoginState { public String getEmail() { return email; } + + public int getPermission() { + return permission; + } } diff --git a/src/resources/wwwroot/adminpanel.html b/src/resources/wwwroot/adminpanel.html new file mode 100644 index 0000000..18579f8 --- /dev/null +++ b/src/resources/wwwroot/adminpanel.html @@ -0,0 +1,275 @@ + + + + + + WasteInformation Server + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ +
+
+
+
+

Dashboard

+
+
+ +
+
+
+
+ + + +
+
+ +
+
+ +
+
+

150

+ +

Logins

+
+
+ +
+ More info +
+
+ +
+ +
+
+

28.03.2019 18:53:05.300

+ +

Zuletzt eingeloggt

+
+
+ +
+ More info +
+
+ +
+ + +
+ +
+ + +
+
+

+ + Control Panel +

+
+
+
+ + + +
+
+
+ +
+ + + + +
+ + +
+ +
+ +
+
+ +
+ +
+ By Gregor Dutzler & Lukas Heiligenbrunner & Emil Meindl +
+ Version 3.0.0 + Build 2019-8-8 9:30 +
+ + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/resources/wwwroot/dashboard.html b/src/resources/wwwroot/dashboard.html index af3dbf6..4c1fdbb 100644 --- a/src/resources/wwwroot/dashboard.html +++ b/src/resources/wwwroot/dashboard.html @@ -3,7 +3,7 @@ - <title>WasteInformation Server + WasteInformation Server @@ -88,11 +88,11 @@ - @@ -268,28 +268,42 @@

Add Data:

- - - -
- + +
+ +
+ + +
+
@@ -312,7 +326,6 @@ -
diff --git a/src/resources/wwwroot/js/adminpanel.js b/src/resources/wwwroot/js/adminpanel.js new file mode 100644 index 0000000..c8e5cc6 --- /dev/null +++ b/src/resources/wwwroot/js/adminpanel.js @@ -0,0 +1,34 @@ +$(document).ready(function () { + console.log("page loaded"); + $.post('/senddata/checkloginstate', 'action=getloginstate', function (data) { + console.log(data); + if (data.loggedin == true) { + $("#userlabel").html(" " + data.username); + if (data.permission > 0) { + $("#adminpanel").show(); + } + } else { + $("#userlabel").html(" not logged in!!"); + } + }, 'json'); + + + $("#btn-shutdown").click(function (event) { + console.log("shutting down server"); + + $.post('/senddata/admindata', 'action=shutdownserver', function (data) { + console.log(data); + + }, 'json'); + + }); + + $("#btn-restart").click(function (event) { + console.log("restarting server"); + + $.post('/senddata/admindata', 'action=restartserver', function (data) { + console.log(data); + + }, 'json'); + }); +}); \ No newline at end of file diff --git a/src/resources/wwwroot/js/dashboard.js b/src/resources/wwwroot/js/dashboard.js index 47c7a48..ae4a6d5 100644 --- a/src/resources/wwwroot/js/dashboard.js +++ b/src/resources/wwwroot/js/dashboard.js @@ -4,6 +4,9 @@ $(document).ready(function () { console.log(data); if (data.loggedin == true) { $("#userlabel").html(" " + data.username); + if (data.permission > 0) { + $("#adminpanel").show(); + } } else { $("#userlabel").html(" not logged in!!"); } @@ -16,6 +19,8 @@ $(document).ready(function () { console.log(data); if (data.query == "ok") { $('#location-table-data').html(""); + $(".delbtn").off(); + for (var i = 0; i < data.data.length; i++) { $('#location-table-data').append("" + "" + data.data[i].cityname + "" + @@ -23,15 +28,27 @@ $(document).ready(function () { "" + data.data[i].wastetype + "" + "" + "" + "" + ""); - $(".delbtn").click(function (event) { - var id = event.target.getAttribute("dataid"); - console.log("clicked btn data " + id); - $.post('/senddata/wastedata', 'action=deletecity&id=' + id, function (data) { - console.log(data); - reloadtable(); - }); - }); } + + $(".delbtn").click(function (event) { + var id = event.target.getAttribute("dataid"); + console.log("clicked btn data " + id); + $.post('/senddata/wastedata', 'action=deletecity&id=' + id, function (data) { + console.log(data); + Swal.fire({ + type: "success", + title: 'Successfully deleted city!', + html: 'This alert closes automatically.', + timer: 1000, + }).then((result) => { + console.log('Popup closed. ') + + }); + table.destroy(); //todo in reloadtable maybe + reloadtable(); + }); + }); + //todo entweda 1 od 2 // $("#example2").reload(); table = $("#example2").DataTable(); @@ -120,16 +137,61 @@ $(document).ready(function () { }); - $('.btn-addtolist').click(function () { - console.log("added new row to table"); - $('#addtable-body').append("" + - "" + $('#dropdown-wastetype').html() + "" + - "" + $('#input-wastetime').val() + "" + - "" + $('#input-wasteregion').val() + "" + - "" + $('#input-wastezone').val() + "" + - ""); + /* new Date create: */ + $("#dropdown-city").click(function (event) { + event.preventDefault(); + var dropdata = $("#dropdown-city-data"); + dropdata.html(""); + + $.post('/senddata/newdate', 'action=getCitynames', function (data) { + console.log(data); + if (data.query == "ok") { + var prev = ""; + for (var i = 0; i < data.data.length; i++) { + var name = data.data[i].cityname; + dropdata.append("" + name + ""); + } + + $(".dropdown-data-cityname").off(); + $(".dropdown-data-cityname").click(function (evnt) { + evnt.preventDefault(); + console.log($(this).html()); + $("#dropdown-city").html($(this).html()); + }); + } + }); }); + $("#dropdown-zone").click(function (event) { + event.preventDefault(); + var dropdata = $("#dropdown-zone-data"); + dropdata.html(""); + + $.post('/senddata/newdate', 'action=getzones&cityname='+$("#dropdown-city").html(), function (data) { + console.log(data); + if (data.query == "ok") { + var prev = ""; + for (var i = 0; i < data.data.length; i++) { + var zone = data.data[i].zone; + dropdata.append("" + zone + ""); + } + + $(".dropdown-data-zonename").off(); + $(".dropdown-data-zonename").click(function (evnt) { + evnt.preventDefault(); + console.log($(this).html()); + $("#dropdown-zone").html($(this).html()); + }); + } + }); + }); + + $(".dropdown-item-wastetype").click(function (event) { + event.preventDefault(); + $("#dropdown-type-data1").html($(this).html()); + }); + + $('#btn-savelist').click(function () { console.log("saving list"); var wastetypearr = $('.td-dropdown-wastetype');