Fix productSupplier table name case sensitivity and add run configurations
This commit is contained in:
@@ -6,6 +6,7 @@ import org.example.petshopdesktop.DTOs.ProductDTO;
|
|||||||
import org.example.petshopdesktop.DTOs.ProductSupplierDTO;
|
import org.example.petshopdesktop.DTOs.ProductSupplierDTO;
|
||||||
import org.example.petshopdesktop.models.Product;
|
import org.example.petshopdesktop.models.Product;
|
||||||
import org.example.petshopdesktop.models.ProductSupplier;
|
import org.example.petshopdesktop.models.ProductSupplier;
|
||||||
|
import org.example.petshopdesktop.util.ActivityLogger;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
||||||
@@ -23,7 +24,7 @@ public class ProductSupplierDB {
|
|||||||
//Execute Query
|
//Execute Query
|
||||||
Statement stmt = conn.createStatement();
|
Statement stmt = conn.createStatement();
|
||||||
String sql = "SELECT ps.supId, ps.prodId, s.supCompany, p.prodName, ps.cost " +
|
String sql = "SELECT ps.supId, ps.prodId, s.supCompany, p.prodName, ps.cost " +
|
||||||
"FROM productsupplier ps " +
|
"FROM productSupplier ps " +
|
||||||
"LEFT JOIN product p " +
|
"LEFT JOIN product p " +
|
||||||
"ON p.prodId = ps.prodId " +
|
"ON p.prodId = ps.prodId " +
|
||||||
"LEFT JOIN supplier s " +
|
"LEFT JOIN supplier s " +
|
||||||
@@ -62,7 +63,7 @@ public class ProductSupplierDB {
|
|||||||
String sql =
|
String sql =
|
||||||
"SELECT ps.supId, ps.prodId, s.supCompany, p.prodName, ps.cost " +
|
"SELECT ps.supId, ps.prodId, s.supCompany, p.prodName, ps.cost " +
|
||||||
"FROM product p " +
|
"FROM product p " +
|
||||||
"LEFT JOIN productsupplier ps " +
|
"LEFT JOIN productSupplier ps " +
|
||||||
"ON p.prodId = ps.prodId " +
|
"ON p.prodId = ps.prodId " +
|
||||||
"LEFT JOIN supplier s " +
|
"LEFT JOIN supplier s " +
|
||||||
"ON s.supId = ps.supId " +
|
"ON s.supId = ps.supId " +
|
||||||
@@ -108,7 +109,7 @@ public class ProductSupplierDB {
|
|||||||
int numRows = 0;
|
int numRows = 0;
|
||||||
|
|
||||||
Connection conn = ConnectionDB.getConnection();
|
Connection conn = ConnectionDB.getConnection();
|
||||||
String sql = "INSERT INTO productsupplier (prodId, supId, cost) " +
|
String sql = "INSERT INTO productSupplier (prodId, supId, cost) " +
|
||||||
"VALUES (?, ?, ?)";
|
"VALUES (?, ?, ?)";
|
||||||
|
|
||||||
//These are the values from productSupplier to put into query above
|
//These are the values from productSupplier to put into query above
|
||||||
@@ -121,6 +122,13 @@ public class ProductSupplierDB {
|
|||||||
numRows = stmt.executeUpdate();
|
numRows = stmt.executeUpdate();
|
||||||
conn.close();
|
conn.close();
|
||||||
|
|
||||||
|
// Log the operation
|
||||||
|
if (numRows > 0) {
|
||||||
|
ActivityLogger.getInstance().logInsert("productSupplier",
|
||||||
|
String.format("ProdID:%d-SupID:%d", productSupplier.getProdId(), productSupplier.getSupId()),
|
||||||
|
String.format("Product-Supplier relation added for Product ID %d, Supplier ID %d", productSupplier.getProdId(), productSupplier.getSupId()));
|
||||||
|
}
|
||||||
|
|
||||||
return numRows;
|
return numRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +149,7 @@ public class ProductSupplierDB {
|
|||||||
|
|
||||||
try{
|
try{
|
||||||
//Delete old data first
|
//Delete old data first
|
||||||
String sql = "DELETE FROM productsupplier WHERE supId = ? AND prodId = ?";
|
String sql = "DELETE FROM productSupplier WHERE supId = ? AND prodId = ?";
|
||||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||||
stmt.setInt(1, oldSupId);
|
stmt.setInt(1, oldSupId);
|
||||||
stmt.setInt(2, oldProdId);
|
stmt.setInt(2, oldProdId);
|
||||||
@@ -149,7 +157,7 @@ public class ProductSupplierDB {
|
|||||||
|
|
||||||
//Then change the data by inserting a new relation with given keys (only if delete worked)
|
//Then change the data by inserting a new relation with given keys (only if delete worked)
|
||||||
if(numRows > 0){
|
if(numRows > 0){
|
||||||
sql = "INSERT INTO productsupplier (prodId, supId, cost) " +
|
sql = "INSERT INTO productSupplier (prodId, supId, cost) " +
|
||||||
"VALUES (?, ?, ?)";
|
"VALUES (?, ?, ?)";
|
||||||
stmt = conn.prepareStatement(sql);
|
stmt = conn.prepareStatement(sql);
|
||||||
stmt.setInt(1, productSupplier.getProdId());
|
stmt.setInt(1, productSupplier.getProdId());
|
||||||
@@ -159,6 +167,13 @@ public class ProductSupplierDB {
|
|||||||
}
|
}
|
||||||
//Commit changes if both delete and insert worked
|
//Commit changes if both delete and insert worked
|
||||||
conn.commit();
|
conn.commit();
|
||||||
|
|
||||||
|
// Log the operation
|
||||||
|
if (numRows > 0) {
|
||||||
|
ActivityLogger.getInstance().logUpdate("productSupplier",
|
||||||
|
String.format("ProdID:%d-SupID:%d", productSupplier.getProdId(), productSupplier.getSupId()),
|
||||||
|
String.format("Product-Supplier relation updated from ProdID:%d-SupID:%d to ProdID:%d-SupID:%d", oldProdId, oldSupId, productSupplier.getProdId(), productSupplier.getSupId()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(SQLException e){
|
catch(SQLException e){
|
||||||
//Rollback CRUD failed
|
//Rollback CRUD failed
|
||||||
@@ -184,7 +199,7 @@ public class ProductSupplierDB {
|
|||||||
int numRows = 0;
|
int numRows = 0;
|
||||||
Connection conn = ConnectionDB.getConnection();
|
Connection conn = ConnectionDB.getConnection();
|
||||||
|
|
||||||
String sql = "DELETE FROM productsupplier WHERE supId = ? AND prodId = ?";
|
String sql = "DELETE FROM productSupplier WHERE supId = ? AND prodId = ?";
|
||||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||||
stmt.setInt(1, supId);
|
stmt.setInt(1, supId);
|
||||||
stmt.setInt(2, prodId);
|
stmt.setInt(2, prodId);
|
||||||
@@ -192,6 +207,13 @@ public class ProductSupplierDB {
|
|||||||
numRows = stmt.executeUpdate();
|
numRows = stmt.executeUpdate();
|
||||||
conn.close();
|
conn.close();
|
||||||
|
|
||||||
|
// Log the operation
|
||||||
|
if (numRows > 0) {
|
||||||
|
ActivityLogger.getInstance().logDelete("productSupplier",
|
||||||
|
String.format("ProdID:%d-SupID:%d", prodId, supId),
|
||||||
|
String.format("Product-Supplier relation deleted for Product ID %d, Supplier ID %d", prodId, supId));
|
||||||
|
}
|
||||||
|
|
||||||
return numRows;
|
return numRows;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user