Skip to content

Commit

Permalink
add javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
NightDev701 committed Sep 30, 2023
1 parent 033e9a3 commit 7ee10b2
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 32 deletions.
11 changes: 5 additions & 6 deletions src/main/java/pl/nightdev701/OpenAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,29 @@

import java.util.UUID;

/* main api class */
public class OpenAPI {

/* encrypt and decrypt strings */
/** encrypt and decrypt strings */
public static CryptoManager getCryptoManager(String value) {
return new CryptoManager(value);
}

/* manage database connections and command easy */
/** manage database connections and command easy */
public static DatabaseConnector getDatabaseManager(DatabaseFormular formular, DatabaseType type) {
return new DatabaseConnector(formular, type);
}

/* request http and handle it */
/** request http and handle it */
public static HttpRequestHandler getRequestHandler(String url) {
return new HttpRequestHandler(url);
}

/* key to save values */
/** key to save values */
public static BaseKey getValueKey(String value){
return ValueKey.getKey(value);
}

/* key to save uuid */
/** key to save uuid */
public static BaseKey getValueKey(UUID value){
return UniqueValueKey.getKey(value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pl/nightdev701/base/BaseKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

/* base for keys */
/** base for keys */
public interface BaseKey {

Object baseValue();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/pl/nightdev701/crypto/CryptoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public CryptoManager(String encryptionKey) {
this.encryptionKey = encryptionKey;
}

/* encrypt data */
/** encrypt data */
public String encrypt(String plainText) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(this.encryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Expand All @@ -32,7 +32,7 @@ public String encrypt(String plainText) throws Exception {
return Base64.getEncoder().encodeToString(encryptedBytes);
}

/* decrypt data */
/** decrypt data */
public String decrypt(String encryptedText) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(this.encryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Expand Down
51 changes: 34 additions & 17 deletions src/main/java/pl/nightdev701/database/DatabaseConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

import pl.nightdev701.database.formular.DatabaseFormular;
import pl.nightdev701.database.type.DatabaseType;
import pl.nightdev701.logger.AbstractLogger;
import pl.nightdev701.logger.standard.DefaultLogger;

import java.sql.*;
import java.util.logging.Level;

public class DatabaseConnector {

Expand All @@ -23,6 +26,7 @@ public class DatabaseConnector {
private final String password;
private final String database;
private final DatabaseType databaseType;
private final AbstractLogger logger;
private Connection connection;

public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType) {
Expand All @@ -32,16 +36,34 @@ public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType) {
this.database = formular.database();
this.databaseType = databaseType;

this.logger = new DefaultLogger();

if (databaseType == DatabaseType.MYSQL) {
setPort(3306);
} else if (databaseType == DatabaseType.POSTGRESQL) {
setPort(5432);
}
}

/* connect to database */
public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, AbstractLogger logger) {
this.ip = formular.ip();
this.user = formular.user();
this.password = formular.password();
this.database = formular.database();
this.databaseType = databaseType;

this.logger = logger;

if (databaseType == DatabaseType.MYSQL) {
setPort(3306);
} else if (databaseType == DatabaseType.POSTGRESQL) {
setPort(5432);
}
}

/** connect to database */
public void connect() throws SQLException {
System.out.println("Connect to database, with the type \"" + databaseType.name() + "\"!");
logger.log(Level.INFO,"Connect to database, with the type \"" + databaseType.name() + "\"!");
if (databaseType == DatabaseType.MYSQL) {
connection = DriverManager.getConnection("jdbc:mysql://" +
ip + ":" + port + "/" + database + "?autoReconnect=true" +
Expand All @@ -53,50 +75,45 @@ public void connect() throws SQLException {
"?reWriteBatchedInserts=true" +
"&charSet=utf-8", user, password);
}
System.out.println("Connected to database!");
logger.log(Level.INFO, "Connected to database!");
}

/* close connection */
/** close connection */
public void close() throws SQLException {
if (isConnected()) {
connection.close();
System.out.println("Connection closed");
logger.log(Level.INFO,"Connection closed");
}
}

/* read value from database */
/** read value from database */
public Object getDatabaseStatement(String command, String data) throws SQLException {
PreparedStatement statement = connection.prepareStatement(command);
ResultSet result = statement.executeQuery();
result.next();
return result.getObject(data);
}

/* execute action to database */
/** execute action to database */
public void executeDatabaseStatement(String command) throws SQLException {
PreparedStatement statement = connection.prepareStatement(command);
statement.executeUpdate();
}

/* check if connected */
public boolean isConnected() {
/** check if connected */
public boolean isConnected() throws SQLException {
if (connection == null) {
return false;
}
try {
return !connection.isClosed();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return !connection.isClosed();
}

/* get port */
/** get port */
public int getPort() {
return port;
}

/* set port */
/** set port */
public void setPort(int port) {
this.port = port;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/pl/nightdev701/http/HttpRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public HttpRequestHandler(String url) {
this.url = url;
}

/* fire request */
/** fire request */
public void request() {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
Expand All @@ -31,7 +31,7 @@ public void request() {
}
}

/* read body */
/** read body */
public String getBody() {
return body;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/pl/nightdev701/key/UniqueValueKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@

import java.util.UUID;

/** uuid key */
public class UniqueValueKey<T extends UUID> implements BaseKey {

T key;

/* create key */
/** create key */
private UniqueValueKey(T key) {
this.key = key;
}

/* read value */
/** read value */
public static UniqueValueKey<UUID> getKey(UUID key) {
return new UniqueValueKey<>(key);
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/pl/nightdev701/key/ValueKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import pl.nightdev701.base.BaseKey;

/** string key */
public class ValueKey<T extends String> implements BaseKey {

T key;
Expand All @@ -20,12 +21,18 @@ private ValueKey(T key) {
this.key = key;
}

/* create key */
/**
* create key
* @return key value
* */
public static ValueKey<String> getKey(Object key) {
return new ValueKey<>(key.toString());
}

/* read value */
/**
* read value
* @return key value
* */
@Override
public Object baseValue() {
return key;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/pl/nightdev701/logger/AbstractLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pl.nightdev701.logger;

/*
Lukas - 15:00
30.09.2023
https://github.com/NightDev701
© SunLightScorpion 2020 - 2023
*/

import java.util.logging.Level;

public abstract class AbstractLogger {

public abstract void log(Level level, String msg);

}

26 changes: 26 additions & 0 deletions src/main/java/pl/nightdev701/logger/standard/DefaultLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pl.nightdev701.logger.standard;

/*
Lukas - 15:02
30.09.2023
https://github.com/NightDev701
© SunLightScorpion 2020 - 2023
*/

import pl.nightdev701.logger.AbstractLogger;

import java.util.logging.Level;
import java.util.logging.Logger;

public class DefaultLogger extends AbstractLogger {

@Override
public void log(Level level, String msg) {
Logger logger = Logger.getLogger("openapi");
logger.log(level, msg);
}

}

0 comments on commit 7ee10b2

Please sign in to comment.