diff --git a/src/main/java/fr/unistra/sil/erp/back/Config.java b/src/main/java/fr/unistra/sil/erp/back/Config.java index dd7c0537e53030b395dbeb0d048a3ac6c3cfcb5a..ed8cc4d04ba091dec77b3f0b818d535de39bef61 100644 --- a/src/main/java/fr/unistra/sil/erp/back/Config.java +++ b/src/main/java/fr/unistra/sil/erp/back/Config.java @@ -10,18 +10,37 @@ package fr.unistra.sil.erp.back; */ public class Config { + /** + * API version. + */ public static final String API_VERSION = "v1"; + /** + * Prefix for API calls. + */ public static final String URL_PREFIX = "/api/" + API_VERSION; + /** + * API Mapping for retrieving all items. + */ public static final String MAPPING_RETRIEVEALL = URL_PREFIX + "/retrieveAll"; + + /** + * API Mapping for retrieving all categories. + */ public static final String MAPPING_GETCATEGORIES = URL_PREFIX + "/retrieveCategories"; + /** + * API Mapping for submitting transactions. + */ public static final String MAPPING_SUBTRANSAC = URL_PREFIX + "/submitTransaction"; + /** + * API Mapping for retrieving stocks. + */ public static final String MAPPING_GETSTOCKS = URL_PREFIX + "/retrieveStocks"; } diff --git a/src/main/java/fr/unistra/sil/erp/back/DatabaseSystem.java b/src/main/java/fr/unistra/sil/erp/back/DatabaseSystem.java index 321f1193dba8b92d75b88e48189081063483f573..e32e6a8aa5a5410eea224ef2db3cc13d14f34904 100644 --- a/src/main/java/fr/unistra/sil/erp/back/DatabaseSystem.java +++ b/src/main/java/fr/unistra/sil/erp/back/DatabaseSystem.java @@ -10,7 +10,7 @@ import fr.unistra.sil.erp.back.db.DatabaseSQLiteImpl; /** * Manages the database implementation to use. - * @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr> + * @author BEAUVAIS ANTOINE */ public class DatabaseSystem { @@ -33,7 +33,7 @@ public class DatabaseSystem { DatabaseSystem.instance = new DatabaseSQLiteImpl(); if(DatabaseSystem.instance == null) - throw new DatabaseConnectionException(); + throw new DatabaseConnectionException("Failed to create DB."); return DatabaseSystem.instance; } diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/IRetrieveStocks.java b/src/main/java/fr/unistra/sil/erp/back/controller/IRetrieveStocks.java index 883ba1d0be2eff2a4bba39ca484c6248f8332f02..dd9d14b1b09752984e1d01df9134c87ed0180c89 100644 --- a/src/main/java/fr/unistra/sil/erp/back/controller/IRetrieveStocks.java +++ b/src/main/java/fr/unistra/sil/erp/back/controller/IRetrieveStocks.java @@ -14,6 +14,13 @@ import org.springframework.http.ResponseEntity; */ public interface IRetrieveStocks { + /** + * Returns the list of stocks to the user. + * @param request the HTTP Servlet request, provided by Spring. + * @param response the HTTP Servlet response, provided by Spring. + * @return the response to the user. + * @throws Exception if the query fails. + */ public ResponseEntity<Object> retrieveStocks(HttpServletRequest request, HttpServletResponse response) throws Exception; diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiBadRequestException.java b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiBadRequestException.java index 7443d03e099094f3e357ca526c2e9d7d4ed61af0..07cf910a9152abcd24a404c6573eda8a140aef97 100644 --- a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiBadRequestException.java +++ b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiBadRequestException.java @@ -5,11 +5,26 @@ package fr.unistra.sil.erp.back.controller.api; /** - * HTTP error 400 Bad Request for the API. + * HTTP error 400 Bad Request JSON content. + * + * If the user sends invalid or incomplete data, the query will typically + * fail and this Exception will be thrown. + * + * This Exception allows the application to tell the user what went + * wrong with his query. + * + * Bad Request errors should only be sent if the error is caused by the user's + * input. If the error happens because of a server problem, + * the ApiServerErrorException should be used instead. + * * @author BEAUVAIS ANTOINE */ public class ApiBadRequestException extends Exception { + /** + * Class constructor. + * @param errMsg the error message. + */ public ApiBadRequestException(String errMsg) { super(errMsg); diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiErrorHandler.java b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiErrorHandler.java index 2e85fb611529485eb509262a33e6e22b77bee8cf..e3114b405da4c041301edc87e19bb9a5ab73b958 100644 --- a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiErrorHandler.java +++ b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiErrorHandler.java @@ -14,11 +14,22 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; /** * Handles API exceptions. + * + * When an Exception is thrown while processing a request, the behavior + * to adopt for each Exception is defined here. + * * @author BEAUVAIS ANTOINE */ @RestControllerAdvice public class ApiErrorHandler { + /** + * Handler for HTTP 500 Internal Server Error. + * @param ex the thrown Exception. + * @param request the HTTP Servlet request, provided by Spring. + * @param response the HTTP Servlet response, provided by Spring. + * @return the response to the client. + */ @ExceptionHandler(ApiServerErrorException.class) public ResponseEntity<Object> handleServerError(Exception ex, HttpServletRequest request, HttpServletResponse response) @@ -27,6 +38,13 @@ public class ApiErrorHandler { HttpStatus.INTERNAL_SERVER_ERROR); } + /** + * Handler for HTTP 400 Bad Request. + * @param ex the thrown Exception. + * @param request the HTTP Servlet request. + * @param response the HTTP Servlet response. + * @return the response to the client. + */ @ExceptionHandler(ApiBadRequestException.class) public ResponseEntity<Object> handleBadRequest(Exception ex, HttpServletRequest request, HttpServletResponse response) @@ -35,4 +53,19 @@ public class ApiErrorHandler { HttpStatus.BAD_REQUEST); } + /** + * Handler for HTTP 404 Not Found. + * @param ex the thrown Exception. + * @param request the HTTP Servlet request. + * @param response the HTTP Servlet response. + * @return the response to the client. + */ + @ExceptionHandler(ApiResourceNotFoundException.class) + public ResponseEntity<Object> handleResourceNotFound(Exception ex, + HttpServletRequest request, HttpServletResponse response) + { + return new ResponseEntity<>(new ErrorMessage(ex.getMessage()), + HttpStatus.NOT_FOUND); + } + } diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiResourceNotFoundException.java b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiResourceNotFoundException.java new file mode 100644 index 0000000000000000000000000000000000000000..0676ceff96fb6638645f806e561af0875cb133a7 --- /dev/null +++ b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiResourceNotFoundException.java @@ -0,0 +1,28 @@ +/* + * CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B + * https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html + */ +package fr.unistra.sil.erp.back.controller.api; + +/** + * API Exception for HTTP 404 Not Found. + * + * If a resource requested by the user does not exist, this exception + * is thrown and handled through ApiErrorHandler.class. + * + * @see ApiErrorHandler + * + * @author BEAUVAIS ANTOINE + */ +public class ApiResourceNotFoundException extends Exception { + + /** + * Class constructor. + * @param errMsg the error message. + */ + public ApiResourceNotFoundException(String errMsg) + { + super(errMsg); + } + +} diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiRetrieveCategoriesController.java b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiRetrieveCategoriesController.java index 977b42ac2a0a98e445380991927cef9d2d34fda0..fa4a767df3d1d302acd0bec3952e3620f000e2b8 100644 --- a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiRetrieveCategoriesController.java +++ b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiRetrieveCategoriesController.java @@ -5,7 +5,6 @@ package fr.unistra.sil.erp.back.controller.api; import static fr.unistra.sil.erp.back.Config.MAPPING_GETCATEGORIES; -import static fr.unistra.sil.erp.back.Config.MAPPING_GETSTOCKS; import fr.unistra.sil.erp.back.DatabaseSystem; import fr.unistra.sil.erp.back.controller.IRetrieveCategoriesController; import fr.unistra.sil.erp.back.model.Category; diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiRetrieveInfoController.java b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiRetrieveInfoController.java index 0b9e426bd58c3bd455aa145c897d04478633b97e..ba8c6afd0d04eab84780a06057e61b2683d81868 100644 --- a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiRetrieveInfoController.java +++ b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiRetrieveInfoController.java @@ -13,7 +13,6 @@ import fr.unistra.sil.erp.back.db.DatabaseInterface; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiServerErrorException.java b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiServerErrorException.java index 7cfe6a04e2f1049bfd5c91c077e507196f95a6ba..2ba9a230f69c3e8d5d601609804648c56dbe5afa 100644 --- a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiServerErrorException.java +++ b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiServerErrorException.java @@ -5,11 +5,21 @@ package fr.unistra.sil.erp.back.controller.api; /** - * Returns HTTP 500 error page. - * @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr> + * Returns HTTP 500 JSON error. + * + * This exception allows the application to handle internal + * server errors by replying proper JSON content to the caller. + * + * The message will be given to the user. + * + * @author BEAUVAIS ANTOINE */ public class ApiServerErrorException extends Exception { + /** + * Class constructor. + * @param errMsg the error message to display to the end-user. + */ public ApiServerErrorException(String errMsg) { super(errMsg); diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiSubmitTransactionController.java b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiSubmitTransactionController.java index 37189f0370ae9e38725a92824cdfaffd27dcf65b..b39c3031d09cf32350521eb95e9bf2188969e7a1 100644 --- a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiSubmitTransactionController.java +++ b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiSubmitTransactionController.java @@ -1,13 +1,19 @@ /* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + * CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B + * https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html */ package fr.unistra.sil.erp.back.controller.api; import com.google.gson.Gson; import com.google.gson.JsonParseException; import static fr.unistra.sil.erp.back.Config.MAPPING_SUBTRANSAC; +import fr.unistra.sil.erp.back.DatabaseSystem; import fr.unistra.sil.erp.back.controller.ISubmitTransactionController; +import fr.unistra.sil.erp.back.db.DatabaseConnectionException; +import fr.unistra.sil.erp.back.db.DatabaseInterface; +import fr.unistra.sil.erp.back.db.DatabaseResourceNotFoundException; +import fr.unistra.sil.erp.back.db.DatabaseUpdateException; +import fr.unistra.sil.erp.back.model.Stock; import fr.unistra.sil.erp.back.model.Transaction; import java.io.IOException; import java.util.logging.Level; @@ -29,10 +35,21 @@ import org.springframework.web.bind.annotation.RestController; public class ApiSubmitTransactionController implements ISubmitTransactionController { + /** + * Handler for transaction submissions. + * @param request the HTTP Servlet Request provided by Spring. + * @param response the HTTP Servlet Response provided by Spring. + * @return the response for the user. + * @throws ApiBadRequestException if the query failed. + * @throws ApiServerErrorException Server error. + * @throws ApiResourceNotFoundException Stock not found. + */ @RequestMapping(value=MAPPING_SUBTRANSAC, method = RequestMethod.POST) @Override public ResponseEntity<Object> submitTransaction(HttpServletRequest request, - HttpServletResponse response) throws ApiBadRequestException + HttpServletResponse response) + throws ApiBadRequestException, ApiServerErrorException, + ApiResourceNotFoundException { Gson gson = new Gson(); String body; @@ -63,9 +80,34 @@ public class ApiSubmitTransactionController if(!t.checkIfValid()) throw new ApiBadRequestException("Invalid JSON schema."); - System.out.println("Transaction : " + t.getItem() + - t.getType() + " " + t.getAmount()); - + DatabaseInterface db; + Stock s; + try { + db = DatabaseSystem.getInstance(); + s = db.getStockForItem(t.getItem()); + if(s == null) + throw new ApiServerErrorException("Database failure."); + + int newQuantity = s.getQuantity() + t.getQuantity(); + if(newQuantity < 0) + newQuantity = 0; + + db.updateStock(s.getId(), newQuantity); + } catch (DatabaseConnectionException ex) { + Logger.getLogger(ApiSubmitTransactionController.class.getName()) + .log(Level.SEVERE, ex.getMessage(), ex); + throw new ApiServerErrorException("Database failure."); + } catch (DatabaseResourceNotFoundException ex) { + //Logger.getLogger(ApiSubmitTransactionController.class.getName()) + // .log(Level.SEVERE, null, ex); + throw new ApiResourceNotFoundException("No stock found for item " + + t.getItem()); + } catch (DatabaseUpdateException ex) { + Logger.getLogger(ApiSubmitTransactionController.class.getName()) + .log(Level.SEVERE, ex.getMessage(), ex); + throw new ApiServerErrorException("Database update failure."); + } + return new ResponseEntity<>(t, HttpStatus.CREATED); } diff --git a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseConnectionException.java b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseConnectionException.java index 1b1598f32054dbcdf412b0c0d9dfda6c4ec24ed2..0ace8fd2c553a69ee9647ba0a2913a7d35441173 100644 --- a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseConnectionException.java +++ b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseConnectionException.java @@ -5,9 +5,24 @@ package fr.unistra.sil.erp.back.db; /** - * - * @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr> + * General database error. + * + * This is a custom Exception thrown when the database could not + * be used by the application, for whatever reason. + * + * This allows the program to handle error cases. + * + * @author BEAUVAIS ANTOINE */ public class DatabaseConnectionException extends Exception { + /** + * Class constructor. + * @param errMsg the error message. + */ + public DatabaseConnectionException(String errMsg) + { + super(errMsg); + } + } diff --git a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseInterface.java b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseInterface.java index 19f0dfa184e3569167a30fda0aa17b2c09ac0709..c71e0835beece9982d04d1291e4c075041593048 100644 --- a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseInterface.java +++ b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseInterface.java @@ -15,7 +15,7 @@ import java.util.List; * This interface allows the application to be able to use different * database systems (CSV, SQLite, MySQL...) by simply switching * implementations. - * @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr> + * @author BEAUVAIS ANTOINE */ public interface DatabaseInterface { @@ -46,4 +46,22 @@ public interface DatabaseInterface { */ public List<Stock> getStocks(); + /** + * Returns the stock entry of an item. + * @param id the item's ID. + * @return the stock line. + * @throws DatabaseResourceNotFoundException when the query fails. + */ + public Stock getStockForItem(int itemId) + throws DatabaseResourceNotFoundException; + + /** + * Updates the specified stock's quantity. + * @param id the stock entry's ID. + * @param quantity the new quantity. + * @throws DatabaseUpdateException when the query fails. + */ + public void updateStock(int id, int quantity) + throws DatabaseConnectionException, DatabaseUpdateException; + } diff --git a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseResourceNotFoundException.java b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseResourceNotFoundException.java new file mode 100644 index 0000000000000000000000000000000000000000..9a349196b20f067ac88dd802f66fc06a9aafe41b --- /dev/null +++ b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseResourceNotFoundException.java @@ -0,0 +1,26 @@ +/* + * CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B + * https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html + */ +package fr.unistra.sil.erp.back.db; + +/** + * Exception for non-existent database resources. + * + * If the information requested in a database doesn't exist, this Exception + * is thrown. + * + * @author BEAUVAIS ANTOINE + */ +public class DatabaseResourceNotFoundException extends Exception { + + /** + * Class constructor. + * @param errMsg the error message. + */ + public DatabaseResourceNotFoundException(String errMsg) + { + super(errMsg); + } + +} diff --git a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java index 6ba2ee98f936419de2e1056679f93269a65e6bda..5d94b89488c5f04e67685fe8761fbb1fe2ea8f72 100644 --- a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java +++ b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java @@ -20,27 +20,60 @@ import java.util.logging.Logger; /** * SQLite implementation of the Database interface. - * @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr> + * + * This allows the software to store and process information through + * a SQLite database file. + * + * @author BEAUVAIS ANTOINE */ public class DatabaseSQLiteImpl implements DatabaseInterface { + /** + * SQLite's connection string for the JDBC driver. + */ private static final String CONNECTION_URL = "jdbc:sqlite:dev.db"; + + /** + * Query used to select all items stored in the database. + */ private static final String SQL_GETALLITEMS = "SELECT ref, name, price, subscriberPrice, category FROM items"; + /** + * Query used to get all items stored in the database from a specific + * category. + */ private static final String SQL_GETITEMSFROMCATEGORY = SQL_GETALLITEMS + " WHERE category = ?"; + /** + * Query used to retrieve all categories stored in the database. + */ private static final String SQL_GETCATEGORIES = "SELECT id, name FROM categories"; + /** + * Query used to fetch all stocks stored in the database. + */ private static final String SQL_GETSTOCKS = "SELECT s.id AS id, s.item AS item, " + "i.name AS name, s.quantity AS quantity FROM stocks s " + "INNER JOIN items i ON s.item = i.ref"; + private static final String SQL_GETSTOCKFORITEM = SQL_GETSTOCKS + + " WHERE item = ?"; + + private static final String SQL_UPDATESTOCK = + "UPDATE stocks SET quantity = ? WHERE item = ?"; + + /** + * SQL Connection object. + */ private Connection conn; + /** + * Class constructor. + */ public DatabaseSQLiteImpl() { try { @@ -51,6 +84,11 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { } } + /** + * Executes a standard query, without parameters. + * @param query the string's query. + * @return the result set for this query. + */ private ResultSet query(String query) { Statement stmt; @@ -67,6 +105,10 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { return rs; } + /** + * Retrieves all items from inside the database. + * @return the list of items. + */ @Override public List<Item> getAllItems() { ResultSet rs = query(SQL_GETALLITEMS); @@ -92,6 +134,11 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { return res; } + /** + * Returns all items from the specified category. + * @param category the category to filter the results with. + * @return the list of items. + */ @Override public List<Item> getItemsFromCategory(int category) { @@ -136,11 +183,16 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { } catch (SQLException ex) { Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( Level.SEVERE, "Failed to parse results.", ex); + return null; } return res; } + /** + * Retrieves all categories from the database. + * @return the list of categories. + */ @Override public List<Category> getCategories() { ResultSet rs = this.query(SQL_GETCATEGORIES); @@ -163,6 +215,10 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { return res; } + /** + * Retrieves all stock entries from the database. + * @return the list of stock entries. + */ @Override public List<Stock> getStocks() { ResultSet rs = this.query(SQL_GETSTOCKS); @@ -186,5 +242,96 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { return res; } + + /** + * Returns the stock entry for the specified item. + * @param itemId the item's ID. + * @return the Stock entry. + * @throws DatabaseResourceNotFoundException if the stock doesn't exist. + */ + @Override + public Stock getStockForItem(int itemId) + throws DatabaseResourceNotFoundException { + PreparedStatement ps; + try { + ps = this.conn.prepareStatement(SQL_GETSTOCKFORITEM); + } catch (SQLException ex) { + Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( + Level.SEVERE, "Failed to connect to database.", ex); + return null; + } + + try { + ps.setInt(1, itemId); + } catch (SQLException ex) { + Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( + Level.SEVERE, "Failed to set query parameter.", ex); + return null; + } + + ResultSet rs; + try { + rs = ps.executeQuery(); + } catch (SQLException ex) { + Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( + Level.SEVERE, "Failed to fetch stock for item " + + itemId, ex); + return null; + } + + Stock s; + try { + if(rs.next()) + s = new Stock(rs.getInt("id"), rs.getInt("item"), + rs.getString("name"), + rs.getInt("quantity")); + else + throw new DatabaseResourceNotFoundException("Stock not found."); + } catch (SQLException ex) { + Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( + Level.SEVERE, "Failed to parse results.", ex); + return null; + } + + return s; + } + + /** + * Updates the specified stock entry with the new quantity. + * @param id the stock's ID. + * @param quantity the associated quantity. + * @throws DatabaseConnectionException when the connection to the DB fails. + * @throws DatabaseUpdateException when the update fails. + */ + @Override + public void updateStock(int id, int quantity) + throws DatabaseConnectionException, DatabaseUpdateException { + + PreparedStatement ps; + try { + ps = this.conn.prepareStatement(SQL_UPDATESTOCK); + } catch (SQLException ex) { + Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( + Level.SEVERE, "Failed to connect to database.", ex); + throw new DatabaseConnectionException("Failed to connect to DB."); + } + + try { + ps.setInt(1, quantity); + ps.setInt(2, id); + } catch (SQLException ex) { + Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( + Level.SEVERE, "Failed to set query parameter.", ex); + throw new DatabaseUpdateException("Failed to set query params."); + } + + try { + ps.execute(); + } catch (SQLException ex) { + Logger.getLogger(DatabaseSQLiteImpl.class.getName()) + .log(Level.SEVERE, "Failed to execute query.", ex); + throw new DatabaseUpdateException("Failed to execute query."); + } + } } diff --git a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseUpdateException.java b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseUpdateException.java new file mode 100644 index 0000000000000000000000000000000000000000..465912e1464ab18a3b3a8aa00149e101ccc0d310 --- /dev/null +++ b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseUpdateException.java @@ -0,0 +1,27 @@ +/* + * CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B + * https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html + */ +package fr.unistra.sil.erp.back.db; + +/** + * Exception for database update failures. + * + * When a database system fails to update specific rows + * from the database, then it throws this Exception so + * the application can handle it. + * + * @author BEAUVAIS ANTOINE + */ +public class DatabaseUpdateException extends Exception { + + /** + * Class constructor. + * @param errMsg the error message. + */ + public DatabaseUpdateException(String errMsg) + { + super(errMsg); + } + +} diff --git a/src/main/java/fr/unistra/sil/erp/back/model/ErrorMessage.java b/src/main/java/fr/unistra/sil/erp/back/model/ErrorMessage.java index c7fac546bdce415764dab989a3718df8bcdf7486..a5d4ba8b7d30831f3bad4eaf397fe7f5292d4b47 100644 --- a/src/main/java/fr/unistra/sil/erp/back/model/ErrorMessage.java +++ b/src/main/java/fr/unistra/sil/erp/back/model/ErrorMessage.java @@ -6,17 +6,35 @@ package fr.unistra.sil.erp.back.model; /** * JSON error message. + * + * When an error occurs, the application will send this type of message + * to the user. + * + * This object only provides a simple message to briefly inform the user + * of what went wrong with the query. + * * @author BEAUVAIS ANTOINE */ public class ErrorMessage { + /** + * The error message. + */ private final String message; + /** + * Class constructor. + * @param message the error message to display. + */ public ErrorMessage(String message) { this.message = message; } + /** + * Returns the error message. + * @return the message. + */ public String getMessage() { return this.message; diff --git a/src/main/java/fr/unistra/sil/erp/back/model/Item.java b/src/main/java/fr/unistra/sil/erp/back/model/Item.java index b37069dd569ff126f909c26bcde7feda0a1b6ecc..a7586d8deee48044b439f6e5637b6d0788c3019d 100644 --- a/src/main/java/fr/unistra/sil/erp/back/model/Item.java +++ b/src/main/java/fr/unistra/sil/erp/back/model/Item.java @@ -7,8 +7,16 @@ package fr.unistra.sil.erp.back.model; import java.math.BigDecimal; /** - * Item description. - * @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr> + * Item object. + * + * Items are products that can be bought and sold. + * + * Any transaction must be linked to an item. + * + * Items have specific quantities stored as "stocks" and cannot be sold + * if the quantity is depleted. + * + * @author BEAUVAIS ANTOINE */ public class Item { diff --git a/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java b/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java index 8cb06faaa8db023d4be7aa78248d2d69910e34ed..d3c10b20154d4d63c16c58216616a6b8929d2a90 100644 --- a/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java +++ b/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java @@ -8,25 +8,66 @@ import java.math.BigDecimal; import java.time.LocalDateTime; /** - * Transaction object. + * Entry in the Registry. + * + * The registry contains all credit and debit operations relative to + * the operating organization. + * * @author BEAUVAIS ANTOINE */ public class RegistryEntry { + /** + * The entry's ID. + */ private final int id; - private final int transactionId; + + /** + * The transaction's type. + */ + private final int transactionType; + + /** + * The date and time for this transaction. + */ private final LocalDateTime dt; + + /** + * The ID of the associated account. + */ private final int accountId; + + /** + * The amount of money, if it's a debit. + */ private final BigDecimal debit; + + /** + * The amount of money, if it's a credit. + */ private final BigDecimal credit; + + /** + * Arbitrary remarks. Optional. + */ private final String remarks; - public RegistryEntry(int id, int transactionId, LocalDateTime dt, + /** + * Class constructor. + * @param id the entry's ID. + * @param transactionType the transaction type. + * @param dt the date and time. + * @param accountId the associated account's ID. + * @param debit money for the debit. + * @param credit money for the credit. + * @param remarks arbitrary remarks. + */ + public RegistryEntry(int id, int transactionType, LocalDateTime dt, int accountId, BigDecimal debit, BigDecimal credit, String remarks) { this.id = id; - this.transactionId = transactionId; + this.transactionType = transactionType; this.dt = dt; this.accountId = accountId; this.debit = debit; @@ -34,36 +75,64 @@ public class RegistryEntry { this.remarks = remarks; } + /** + * Returns the entry's ID. + * @return the entry's ID. + */ public int getId() { return this.id; } - public int getTransactionId() + /** + * Returns the transaction's type. + * @return the transaction's type. + */ + public int getTransactionType() { - return this.transactionId; + return this.transactionType; } + /** + * Returns the transaction's date and time. + * @return the date and time. + */ public LocalDateTime getDatetime() { return this.dt; } + /** + * Returns the associated account's ID. + * @return the associated account's ID. + */ public int getAccountId() { return this.accountId; } + /** + * Returns the debit amount. + * @return the amount. + */ public BigDecimal getDebit() { return this.debit; } + /** + * Returns the credit amount. + * @return the amount. + */ public BigDecimal getCredit() { return this.credit; } + /** + * Returns the arbitrary remarks. + * @return the remarks. + */ public String getRemarks() { return this.remarks; diff --git a/src/main/java/fr/unistra/sil/erp/back/model/Stock.java b/src/main/java/fr/unistra/sil/erp/back/model/Stock.java index 05c950448c5e4a89406c296a2247d2177401cc36..b2c8dd22067b455f92783355e91017c8f84e1f8d 100644 --- a/src/main/java/fr/unistra/sil/erp/back/model/Stock.java +++ b/src/main/java/fr/unistra/sil/erp/back/model/Stock.java @@ -6,18 +6,42 @@ package fr.unistra.sil.erp.back.model; /** * Representation of a stock entry. + * + * Stocks simply provide the available quantity for each item. + * + * If an item's quantity is depleted, it cannot be sold. + * * @author BEAUVAIS ANTOINE */ public class Stock { + /** + * The stock entry's ID. + */ private final int id; + /** + * The stock's item ID. + */ private final int itemId; + /** + * The stock's item name. + */ private final String itemName; + /** + * The quantity for the item. + */ private final int quantity; + /** + * Class constructor. + * @param id the Stock's ID. + * @param itemId the item's ID. + * @param itemName the item's name. + * @param quantity the quantity. + */ public Stock(int id, int itemId, String itemName, int quantity) { this.id = id; @@ -26,21 +50,37 @@ public class Stock { this.quantity = quantity; } + /** + * Returns the stock ID. + * @return the stock's ID. + */ public int getId() { return this.id; } + /** + * Returns the item's ID. + * @return the item's ID. + */ public int getItemId() { return this.itemId; } + /** + * Returns the item's name. + * @return the item's name. + */ public String getItemName() { return this.itemName; } + /** + * Returns the quantity for the item. + * @return the quantity. + */ public int getQuantity() { return this.quantity; diff --git a/src/main/java/fr/unistra/sil/erp/back/model/Transaction.java b/src/main/java/fr/unistra/sil/erp/back/model/Transaction.java index b4eadecb89526c673987a594a6b23b2c5cf71c3e..fe41245790410c5d7e8b86ce8c29606df3e1570e 100644 --- a/src/main/java/fr/unistra/sil/erp/back/model/Transaction.java +++ b/src/main/java/fr/unistra/sil/erp/back/model/Transaction.java @@ -8,51 +8,81 @@ import java.math.BigDecimal; /** * Representation of a transaction. + * + * A Transaction is a query from the MONEY component that represents + * the information used for a proper transaction. + * + * Transactions automatically update stocks as well as the Registry + * when applicable. + * * @author BEAUVAIS ANTOINE */ public class Transaction { + /** + * Item ID. + */ private final Integer item; + /** + * Transaction type. + */ private final Integer type; - private final BigDecimal amount; - + /** + * Number of items. + */ private final Integer quantity; - public Transaction(Integer item, Integer type, BigDecimal amount, - Integer quantity) + /** + * Class constructor. + * @param item Item's identifier. + * @param type Transaction type. + * @param amount Transaction's amount. + * @param quantity Quantity of items involved. + */ + public Transaction(Integer item, Integer type, Integer quantity) { this.item = item; this.type = type; - this.amount = amount; this.quantity = quantity; } + /** + * Returns the Transaction's item ID. + * @return the item ID. + */ public Integer getItem() { return this.item; } + /** + * Returns the Transaction's type. + * @return the transaction's type. + */ public Integer getType() { return this.type; } - public BigDecimal getAmount() - { - return this.amount; - } - + /** + * Returns the number of items in the Transaction. + * @return the number of items. + */ public Integer getQuantity() { return this.quantity; } + /** + * Checks that the Transaction is valid. + * @return whether the Transaction is valid. + */ public boolean checkIfValid() { return (this.item != null && this.type != null - && this.amount != null && this.quantity != null); + && this.quantity != null); } }