Browse Source

Minor 1.7.0

- Hopefully fix the db timeout issue
- Upgrade from stoneage jconnector to current one (facepalm)
- Add pseudo_id for ADD_CAFE tickets
tags/v1.7.0_ASIATICO^2
Epileptic 11 months ago
parent
commit
5bbfee6823
6 changed files with 97 additions and 48 deletions
  1. BIN
      WebContent/WEB-INF/lib/mariadb-java-client-2.6.0.jar
  2. +8
    -3
      src/app/pott/kaffeepott/barista/Constants.java
  3. +1
    -1
      src/app/pott/kaffeepott/barista/core/BaristaMain.java
  4. +48
    -33
      src/app/pott/kaffeepott/barista/db/DatabaseAdapter.java
  5. +8
    -11
      src/app/pott/kaffeepott/barista/requesthandler/vanilla/BaristaVanillaHandler.java
  6. +32
    -0
      src/app/pott/kaffeepott/barista/requesthandler/vanilla/IDTK.java

BIN
WebContent/WEB-INF/lib/mariadb-java-client-2.6.0.jar View File


+ 8
- 3
src/app/pott/kaffeepott/barista/Constants.java View File

@@ -3,7 +3,7 @@ package app.pott.kaffeepott.barista;
public final class Constants {

public static final class Version {
public static final String VERSION_MMP = "1.6.0";
public static final String VERSION_MMP = "1.7.0";
public static final boolean IS_BETA = VERSION_MMP.toLowerCase().contains("beta");
public static final String VERSION_STATE = "ASIATICO";
public static final String VERSION_STRING = "v" + VERSION_MMP + "_" + VERSION_STATE;
@@ -139,6 +139,7 @@ public final class Constants {
public static final String CHANGE_KEY = "change";

public static final String HASH_KEY = "hash";
public static final String PSEUDO_ID_KEY = "pseudo_id";
}

public static final class Queries {
@@ -152,13 +153,13 @@ public final class Constants {
public static final String QUERY_RATING = "SELECT AVG(Rating) as Rating FROM CafeRating WHERE CafeID = ?";

// openticket
public static final String QUERY_ADD_CAFE = "INSERT INTO Ticket (UserID, NewCafeName, NewLocationLat, NewLocationLon, Mode, CompanionType) VALUES (?, ?, ?, ?, 'add', ?)";
public static final String QUERY_ADD_CAFE = "INSERT INTO Ticket (UserID, AffectedCafeId, NewCafeName, NewLocationLat, NewLocationLon, Mode, CompanionType) VALUES (?, ?, ?, ?, ?, 'add', ?)";
public static final String QUERY_REMOVE_CAFE = "INSERT INTO Ticket (UserID, AffectedCafeID, Mode, CompanionType) VALUES (?, ?, 'delete', ?)";
public static final String QUERY_CHANGE_CAFE_NAME = "INSERT INTO Ticket (UserID, AffectedCafeID, NewCafeName, Mode, CompanionType) VALUES (?, ?, ?, 'modify', ?)";
public static final String QUERY_CHANGE_CAFE_LOCATION = "INSERT INTO Ticket (UserID, AffectedCafeID, NewLocationLat, NewLocationLon, Mode, CompanionType) VALUES (?, ?, ?, ?, 'modify', ?)";
public static final String QUERY_CHANGE_PRODUCT_PRICE = "INSERT INTO Ticket (UserID, AffectedCafeID, AffectedProductName, AffectedProductSize, NewProductPrice, Mode, CompanionType) VALUES (?, ?, ?, ?, ?, 'modify', ?)";
public static final String QUERY_DELETE_PRODUCT = "INSERT INTO Ticket (UserID, AffectedCafeID, AffectedProductName, AffectedProductSize, Mode, CompanionType) VALUES (?, ?, ?, ?, 'delete', ?)";
public static final String QUERY_ADD_PRODUCT = "INSERT INTO Ticket (UserID, AffectedCafeID, AffectedProductName, AffectedProductSize, Mode, CompanionType) VALUES (?, ?, ?, ?, 'add', ?)";
public static final String QUERY_ADD_PRODUCT = "INSERT INTO Ticket (UserID, AffectedCafeID, AffectedProductName, AffectedProductSize, NewProductPrice, Mode, CompanionType) VALUES (?, ?, ?, ?, ?, 'add', ?)";

public static final String QUERY_ADD_BADGE = "INSERT INTO Ticket (UserID, AffectedCafeID, NewBadgeType, Mode, CompanionType) VALUES (?, ?, ?, 'add', ?)";
public static final String QUERY_REMOVE_BADGE = "INSERT INTO Ticket (UserID, AffectedCafeID, NewBadgeType, Mode, CompanionType) VALUES (?, ?, ?, 'delete', ?)";
@@ -183,6 +184,10 @@ public final class Constants {
// productschanged
public static final String QUERY_PRODUCT_TYPES_HASHDATA_START = "SELECT Name, Name_";
public static final String QUERY_PRODUCT_TYPES_HASHDATA_END = " FROM ProductType ORDER BY Name ASC";

// getrandomvalidlong
public static final String QUERY_COUNT_CAFE_ID = "SELECT COUNT(*) FROM Cafe WHERE ID = ?";
public static final String QUERY_COUNT_TICKET_CAFE_ID = "SELECT COUNT(*) FROM Ticket WHERE AffectedCafeID = ?";
}

public static final class Exceptions {


+ 1
- 1
src/app/pott/kaffeepott/barista/core/BaristaMain.java View File

@@ -27,7 +27,7 @@ import de.epileptic.eplib.log.Log;
@WebServlet("/api")
/**
* @author epileptic
* @version 1.6.0
* @version 1.7.0
*/
public class BaristaMain extends HttpServlet {
private static final long serialVersionUID = 1L;


+ 48
- 33
src/app/pott/kaffeepott/barista/db/DatabaseAdapter.java View File

@@ -1,55 +1,60 @@
package app.pott.kaffeepott.barista.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import org.mariadb.jdbc.MariaDbDataSource;

public class DatabaseAdapter {

private Connection connection;
MariaDbDataSource dataSource;
// private Connection connection;

private HashMap<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();
// private HashMap<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();

public DatabaseAdapter(String ip, String port, String dbName, String user, String password) throws SQLException {
statements = new HashMap<String, PreparedStatement>();
connection = DriverManager.getConnection("jdbc:mariadb://" + ip + ":" + port + "/" + dbName, user, password);
DriverManager.getConnection(
"jdbc:mariadb://" + ip + ":" + port + "/" + dbName + "?user=" + user + "&password=" + password);
}

public String testQuery() {
try {
Statement st = connection.createStatement();
ResultSet result = st.executeQuery("SELECT * FROM debug");
StringBuilder sb = new StringBuilder();
// statements = new HashMap<String, PreparedStatement>();

while (result.next())
sb.append(result.getString(2) + "\n");
dataSource = new MariaDbDataSource();
dataSource
.setUrl("jdbc:mariadb://" + ip + ":" + port + "/" + dbName + "?user=" + user + "&password=" + password);
dataSource.setPassword(password);

return sb.toString();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
// connection = DriverManager.getConnection("" + ip + ":" + port + "/" + dbName, user, password);
// DriverManager.getConnection(
// "jdbc:mariadb://" + ip + ":" + port + "/" + dbName + "?user=" + user + "&password=" + password);
}

public ResultSet query(String query) throws SQLException {
Statement st = connection.createStatement();
ResultSet result = st.executeQuery(query);
return result;
}
// public String testQuery() {
// try {
// Statement st = connection.createStatement();
// ResultSet result = st.executeQuery("SELECT * FROM debug");
// StringBuilder sb = new StringBuilder();
//
// while (result.next())
// sb.append(result.getString(2) + "\n");
//
// return sb.toString();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// return null;
// }

// public ResultSet query(String query) throws SQLException {
// Statement st = connection.createStatement();
// ResultSet result = st.executeQuery(query);
// return result;
// }

public ResultSet query(String query, Object... values) throws SQLException {
// Check for statement
if (!statements.containsKey(query))
statements.put(query, connection.prepareStatement(query));
// if (!statements.containsKey(query))
// statements.put(query, dataSource.getConnection().prepareStatement(query));

// Get statement
PreparedStatement statement = statements.get(query);
PreparedStatement statement = dataSource.getConnection().prepareStatement(query);
for (int i = 0; i < values.length; i++)
statement.setObject(i + 1, values[i]);
statement.execute();
@@ -58,7 +63,9 @@ public class DatabaseAdapter {

public long queryGetId(String query, Object... values) throws SQLException {
// Get statement
PreparedStatement statement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
// PreparedStatement statement = dataSource.getConnection().prepareStatement(query,
// Statement.RETURN_GENERATED_KEYS);
PreparedStatement statement = dataSource.getConnection().prepareStatement(query);
for (int i = 0; i < values.length; i++)
statement.setObject(i + 1, values[i]);
statement.executeUpdate();
@@ -69,4 +76,12 @@ public class DatabaseAdapter {

return result.getLong(1);
}

// public boolean isValid() {
// try {
// return connection.isValid(5);
// } catch (SQLException e) {
// return false;
// }
// }
}

+ 8
- 11
src/app/pott/kaffeepott/barista/requesthandler/vanilla/BaristaVanillaHandler.java View File

@@ -221,10 +221,11 @@ public class BaristaVanillaHandler implements BaristaRequestHandler {
switch (type) {
case ADD_CAFE:
OpenTicketTK.checkParameterCount(select, change, 0, 3);
db.query(Queries.QUERY_ADD_CAFE, userId, change.getString(JsonKey.NAME_KEY),
long l = IDTK.getRandomValidLong(db);
db.query(Queries.QUERY_ADD_CAFE, userId, l, change.getString(JsonKey.NAME_KEY),
change.getDouble(JsonKey.LONGITUDE_KEY), change.getDouble(JsonKey.LATITUDE_KEY),
type.toString());
break;
return "{\"ok\":true, \"" + JsonKey.PSEUDO_ID_KEY + "\":" + l + "}";

case REMOVE_CAFE:
OpenTicketTK.checkParameterCount(select, change, 1, 0);
@@ -299,25 +300,21 @@ public class BaristaVanillaHandler implements BaristaRequestHandler {
case CHANGE_PRODUCT_PRICE:
OpenTicketTK.checkParameterCount(select, change, 4, 1);
db.query(Queries.QUERY_CHANGE_PRODUCT_PRICE, userId, select.getLong(JsonKey.CAFE_ID_KEY),
LocaleTK.getProductType(db, select.getString(JsonKey.LOCALE_KEY),
select.getString(JsonKey.NAME_KEY)),
select.getString(JsonKey.SIZE_KEY), change.getDouble(JsonKey.PRICE_KEY), type.toString());
select.getString(JsonKey.NAME_KEY), select.getString(JsonKey.SIZE_KEY),
change.getDouble(JsonKey.PRICE_KEY), type.toString());
break;

case DELETE_PRODUCT:
OpenTicketTK.checkParameterCount(select, change, 4, 1);
db.query(Queries.QUERY_DELETE_PRODUCT, userId, select.getLong(JsonKey.CAFE_ID_KEY),
LocaleTK.getProductType(db, select.getString(JsonKey.LOCALE_KEY),
select.getString(JsonKey.NAME_KEY)),
select.getString(JsonKey.SIZE_KEY), type.toString());
select.getString(JsonKey.NAME_KEY), select.getString(JsonKey.SIZE_KEY), type.toString());
break;

case ADD_PRODUCT:
OpenTicketTK.checkParameterCount(select, change, 4, 1);
db.query(Queries.QUERY_ADD_PRODUCT, userId, select.getLong(JsonKey.CAFE_ID_KEY),
LocaleTK.getProductType(db, select.getString(JsonKey.LOCALE_KEY),
select.getString(JsonKey.NAME_KEY)),
select.getString(JsonKey.SIZE_KEY), type.toString());
select.getString(JsonKey.NAME_KEY), select.getString(JsonKey.SIZE_KEY),
change.getDouble(JsonKey.PRICE_KEY), type.toString());
break;

default:


+ 32
- 0
src/app/pott/kaffeepott/barista/requesthandler/vanilla/IDTK.java View File

@@ -0,0 +1,32 @@
package app.pott.kaffeepott.barista.requesthandler.vanilla;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;

import app.pott.kaffeepott.barista.Constants.VVanilla.Queries;
import app.pott.kaffeepott.barista.db.DatabaseAdapter;

public class IDTK {
private IDTK() {
}

public static long getRandomValidLong(DatabaseAdapter db) throws SQLException {
long random;
do {
Random rand = new Random(System.nanoTime());
random = rand.nextLong();
} while (!isValid(db, random));
return random;
}

private static boolean isValid(DatabaseAdapter db, long l) throws SQLException {
ResultSet resultCafe = db.query(Queries.QUERY_COUNT_CAFE_ID, l);
resultCafe.next();
ResultSet resultTicket = db.query(Queries.QUERY_COUNT_CAFE_ID, l);
resultTicket.next();
return resultCafe.getInt(1) == 0 && resultTicket.getInt(1) == 0;
}
}

Loading…
Cancel
Save