Skip to content

Commit

Permalink
Add JavaDocs in Crud package
Browse files Browse the repository at this point in the history
  • Loading branch information
TatuJLund committed Jun 30, 2024
1 parent 96d3968 commit 9948c21
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class BookForm extends Composite implements HasI18N {

// Localization constants
private static final String AVAILABILITY_MISMATCH = "availability-mismatch";
private static final String DISCARD_CHANGES = "discard-changes";
private static final String SAVE = "save";
private static final String CANCEL = "cancel";
private static final String CANNOT_CONVERT = "cannot-convert";
Expand Down Expand Up @@ -99,6 +98,15 @@ public Result<Integer> convertToModel(String value,

}

/**
* Represents a form for creating or editing a book. This form is used in
* the Vaadin Create application. It allows users to enter information about
* a book, such as its price, stock count, and category. The form includes
* validation for the entered data and provides buttons for saving,
* discarding, canceling, and deleting the book. The form is bound to a
* presenter, which handles the business logic for saving, editing, and
* deleting books.
*/
public BookForm(BooksPresenter presenter) {
setCompositionRoot(layout);
buildForm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
* data sets.
*/
@SuppressWarnings("serial")
public class BookGrid extends Grid<Product>
implements HasI18N {
public class BookGrid extends Grid<Product> implements HasI18N {

private static final String CATEGORIES = "categories";
private static final String IN_STOCK = "in-stock";
Expand All @@ -50,6 +49,13 @@ public class BookGrid extends Grid<Product>
private int edited;
private DecimalFormat decimalFormat;

/**
* The BookGrid class represents a grid component that displays a list of
* books. It provides various columns to display different properties of the
* books, such as id, name, price, availability, stock count, and
* categories. The grid also supports highlighting the last edited row and
* showing a traffic light icon for availability.
*/
public BookGrid() {
setId("book-grid");
setSizeFull();
Expand Down Expand Up @@ -183,6 +189,13 @@ public void attach() {
decimalFormat.setMinimumFractionDigits(2);
}

/**
* Adjusts the visibility and width of columns in the book grid based on the
* specified width.
*
* @param width
* the width of the grid
*/
private void adjustColumns(int width) {
setDescriptionGenerator(book -> {
var user = lockedBooks.isLocked(Product.class, book.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ private CompletableFuture<Collection<Category>> loadCategoriesAsync() {
executor);
}

/**
* Requests an update of the products and updates the view asynchronously.
*/
public void requestUpdateProducts() {
future = loadProductsAsync().thenAccept(products -> {
logger.info("Fetching products complete");
Expand All @@ -62,6 +65,11 @@ public void requestUpdateProducts() {
});
}

/**
* Cancels the update of products and unlocks the book. If there is a future
* task running, it will be cancelled and the future reference will be set
* to null.
*/
public void cancelUpdateProducts() {
unlockBook();
if (future != null) {
Expand All @@ -71,6 +79,15 @@ public void cancelUpdateProducts() {
}
}

/**
* Initializes the BooksPresenter.
*
* This method is responsible for initializing the presenter and setting up
* the initial state of the view. It calls the editProduct method with a
* null parameter to ensure that the view is in an editable state. If the
* current user is not an admin, it disables the ability to create new
* products.
*/
public void init() {
editProduct(null);
// Hide and disable if not admin
Expand All @@ -79,18 +96,35 @@ public void init() {
}
}

/**
* Cancels the current product operation. This method calls the
* `cancelProduct` method of the view and unlocks the book.
*/
public void cancelProduct() {
view.cancelProduct();
unlockBook();
}

/**
* Unlocks the currently editing book. If there is a book currently being
* edited, it will be unlocked by calling the `unlock` method of the
* `lockedBooks` object. After unlocking the book, the `editing` variable is
* set to null.
*/
public void unlockBook() {
if (editing != null) {
lockedBooks.unlock(Product.class, editing);
editing = null;
}
}

/**
* Locks a book with the specified ID for editing. If there is already a
* book being edited, it will be unlocked first.
*
* @param id
* the ID of the book to lock for editing
*/
private void lockBook(Integer id) {
if (editing != null) {
unlockBook();
Expand All @@ -99,6 +133,16 @@ private void lockBook(Integer id) {
editing = id;
}

/**
* Handles the navigation to the view with the specified product ID. If the
* product ID is "new", it creates a new product. Otherwise, it attempts to
* find the product with the given ID and selects it in the view. If the
* product ID is not valid or cannot be parsed as an integer, an error
* message is shown in the view.
*
* @param productId
* the ID of the product to navigate to
*/
public void enter(String productId) {
if (productId != null && !productId.isEmpty()) {
if (productId.equals("new")) {
Expand All @@ -121,11 +165,24 @@ public void enter(String productId) {
}
}

/**
* Finds a product by its ID.
*
* @param productId
* the ID of the product to find
* @return the product with the specified ID, or null if not found
*/
public Product findProduct(int productId) {
logger.info("Fetching product {}", productId);
return service.getProductById(productId);
}

/**
* Saves the given product.
*
* @param product
* The product to be saved.
*/
public void saveProduct(Product product) {
view.showSaveNotification(product.getProductName());
view.clearSelection();
Expand All @@ -141,6 +198,12 @@ public void saveProduct(Product product) {
view.setFragmentParameter("");
}

/**
* Deletes a product from the system.
*
* @param product
* the product to be deleted
*/
public void deleteProduct(Product product) {
view.showDeleteNotification(product.getProductName());
view.clearSelection();
Expand All @@ -151,6 +214,15 @@ public void deleteProduct(Product product) {
view.setFragmentParameter("");
}

/**
* Edits the specified product. If the product is null, it sets the fragment
* parameter to an empty string and unlocks the book. If the product's ID is
* -1, it sets the fragment parameter to "new". Otherwise, it sets the
* fragment parameter to the product's ID and locks the book.
*
* @param product
* the product to be edited
*/
public void editProduct(Product product) {
if (product == null) {
view.setFragmentParameter("");
Expand All @@ -166,13 +238,26 @@ public void editProduct(Product product) {
view.editProduct(product);
}

/**
* Creates a new product. This method clears the selection, sets the
* fragment parameter to "new", logs an info message, and edits a new
* product in the view.
*/
public void newProduct() {
view.clearSelection();
view.setFragmentParameter("new");
logger.info("New product");
view.editProduct(new Product());
}

/**
* Handles the event when a row is selected in the UI. If the user has the
* role of ADMIN and the selected product is not locked, the product is
* edited. Otherwise, the selection is cleared.
*
* @param product
* the selected product
*/
public void rowSelected(Product product) {
if (accessControl.isUserInRole(Role.ADMIN)) {
if (product != null && lockedBooks.isLocked(Product.class,
Expand Down
Loading

0 comments on commit 9948c21

Please sign in to comment.