From 10058b24a7d61a12fed6128a7e96fc70901d1593 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Thu, 23 Jan 2020 18:32:29 +0100 Subject: [PATCH] javadoc improved data encapsulation --- .../basicutils/Info.java | 35 +++++++++++--- .../basicutils/Log.java | 43 ++++++++++++++++- .../wasteinformationserver/db/Database.java | 3 +- .../com/wasteinformationserver/db/JDBC.java | 47 +++++++++++++++---- .../db/MySQLConnector.java | 2 +- .../mqtt/MqttService.java | 4 +- 6 files changed, 113 insertions(+), 21 deletions(-) diff --git a/src/java/com/wasteinformationserver/basicutils/Info.java b/src/java/com/wasteinformationserver/basicutils/Info.java index 87981e5..381648a 100644 --- a/src/java/com/wasteinformationserver/basicutils/Info.java +++ b/src/java/com/wasteinformationserver/basicutils/Info.java @@ -8,37 +8,58 @@ import java.util.Date; import java.util.Properties; public class Info { - private static String version="not init"; - private static String builddate="not init"; - private static String starttime="not init"; + private static String version = "not init"; + private static String builddate = "not init"; + private static String starttime = "not init"; + /** + * get Software Version (defined in gradle build file) + * + * @return Version as string + */ public static String getVersion() { return version; } + /** + * get Software build date + * + * @return Date as string + */ public static String getBuilddate() { return builddate; } + /** + * get Server start time + * + * @return start time + */ public static String getStarttime() { return starttime; } - public static void init(){ + /** + * initialize the version and builddate variables + */ + public static void init() { starttime = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date()); Properties prop = new Properties(); try { URL url = Info.class.getResource("/version.properties"); prop.load(url.openStream()); - version=(String)prop.get("version"); - builddate=(String)prop.get("buildtime"); + version = (String) prop.get("version"); + builddate = (String) prop.get("buildtime"); } catch (IOException e) { e.printStackTrace(); } } - public static void getMemoryUsage(){ + /** + * print memory utilization + */ + public static void getMemoryUsage() { Runtime runtime = Runtime.getRuntime(); NumberFormat format = NumberFormat.getInstance(); diff --git a/src/java/com/wasteinformationserver/basicutils/Log.java b/src/java/com/wasteinformationserver/basicutils/Log.java index 505fed0..0a0c1d1 100644 --- a/src/java/com/wasteinformationserver/basicutils/Log.java +++ b/src/java/com/wasteinformationserver/basicutils/Log.java @@ -28,37 +28,72 @@ public class Log { private static ArrayList colors = new ArrayList(Arrays.asList("", "DEBUG", "MESSAGE", "INFO", "WARNING", "ERROR", "CRITICAL_ERROR")); + /** + * Log critical Error + * + * @param msg message + */ public static void criticalerror(Object msg) { if (Loglevel <= CRITICAL_ERROR) log(msg, CRITICAL_ERROR); } + /** + * Log basic Error + * + * @param msg message + */ public static void error(Object msg) { if (Loglevel <= ERROR) log(msg, ERROR); } + /** + * Log warning + * + * @param msg message + */ public static void warning(Object msg) { if (Loglevel <= WARNING) log(msg, WARNING); } + /** + * Log info + * + * @param msg message + */ public static void info(Object msg) { if (Loglevel <= INFO) log(msg, INFO); } + /** + * Log basic message + * + * @param msg message + */ public static void message(Object msg) { if (Loglevel <= MESSAGE) log(msg, MESSAGE); } + /** + * Log debug Message + * + * @param msg message + */ public static void debug(Object msg) { if (Loglevel <= DEBUG) log(msg, DEBUG); } - + /** + * Log as defined + * + * @param msg message + * @param level Loglevel --> static vals defined + */ public static void log(Object msg, int level) { boolean iswindows = System.getProperty("os.name").contains("Windows"); StringBuilder builder = new StringBuilder(); @@ -116,6 +151,12 @@ public class Log { return date_format.format(resultdate); } + /** + * define Loglevel call on startup or at runtime + * default: 0[DEBUG] --> Max logging + * + * @param level Loglevel --> static vals defined + */ public static void setLevel(int level) { Loglevel = level; } diff --git a/src/java/com/wasteinformationserver/db/Database.java b/src/java/com/wasteinformationserver/db/Database.java index 071db05..94e6766 100755 --- a/src/java/com/wasteinformationserver/db/Database.java +++ b/src/java/com/wasteinformationserver/db/Database.java @@ -9,8 +9,7 @@ import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.Vector; -public abstract class -Database { +abstract class Database { protected String user; protected String password; diff --git a/src/java/com/wasteinformationserver/db/JDBC.java b/src/java/com/wasteinformationserver/db/JDBC.java index 4a5f700..b0b351c 100644 --- a/src/java/com/wasteinformationserver/db/JDBC.java +++ b/src/java/com/wasteinformationserver/db/JDBC.java @@ -6,17 +6,28 @@ import java.sql.ResultSet; import java.sql.SQLException; public class JDBC { - static Connection conn; + private static Connection conn; - static JDBC JDBC; - static boolean loggedin = false; + private static JDBC JDBC; + private static boolean loggedin = false; - static String usernamec; - static String passwordc; - static String dbnamec; - static String ipc; - static int portc; + private static String usernamec; + private static String passwordc; + private static String dbnamec; + private static String ipc; + private static int portc; + /** + * initialize database values + * suggested on startup + * + * @param username db username + * @param password db password + * @param dbname Database name + * @param ip Server ip or hostname + * @param port Server port + * @throws IOException + */ public static void init(String username, String password, String dbname, String ip, int port) throws IOException { usernamec = username; passwordc = password; @@ -28,6 +39,13 @@ public class JDBC { logintodb(username, password, dbname, ip, port); } + /** + * get instance of db object + * logindata has to be set before! + * + * @return JDBC object of this + * @throws IOException + */ public static JDBC getInstance() throws IOException { if (loggedin) { return JDBC; @@ -55,6 +73,12 @@ public class JDBC { } + /** + * execute basic query --> requests only + * + * @param sql query sql statement + * @return ResultSet representating the table + */ public ResultSet executeQuery(String sql) { try { PreparedStatement stmt = conn.prepareStatement(sql); @@ -65,6 +89,13 @@ public class JDBC { return null; } + /** + * update db in some way + * + * @param sql sql insert/update/delete statement + * @return status + * @throws SQLException + */ public int executeUpdate(String sql) throws SQLException { PreparedStatement stmt = conn.prepareStatement(sql); diff --git a/src/java/com/wasteinformationserver/db/MySQLConnector.java b/src/java/com/wasteinformationserver/db/MySQLConnector.java index c4d447b..5f3cabc 100755 --- a/src/java/com/wasteinformationserver/db/MySQLConnector.java +++ b/src/java/com/wasteinformationserver/db/MySQLConnector.java @@ -4,7 +4,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; -public class MySQLConnector extends Database { +class MySQLConnector extends Database { static { try { diff --git a/src/java/com/wasteinformationserver/mqtt/MqttService.java b/src/java/com/wasteinformationserver/mqtt/MqttService.java index 61e78e2..2f37d9f 100644 --- a/src/java/com/wasteinformationserver/mqtt/MqttService.java +++ b/src/java/com/wasteinformationserver/mqtt/MqttService.java @@ -14,7 +14,7 @@ import java.util.Date; public class MqttService { private MqttClient client = null; private String serveruri; - JDBC db; + private JDBC db; public MqttService(String serverurl, String port) { serveruri = "tcp://" + serverurl + ":" + port; @@ -60,7 +60,7 @@ public class MqttService { } else { devicecities.first(); devicecities.previous(); - // TODO: 23.01.20 Test this stuff + // TODO: 23.01.20 Test this stuff while (devicecities.next()) { int cityid = devicecities.getInt("CityID"); checkDatabase(cityid, Integer.parseInt(deviceid));