From b03842f29178a2a6a0ed245177bb10192237ed72 Mon Sep 17 00:00:00 2001 From: Cristian Date: Thu, 22 Sep 2022 21:41:22 +0200 Subject: [PATCH] Public Pre-Release Update - Bug fixed - Add book / Remove book - Requirements --- build.gradle | 2 +- src/main/java/it/books/Main.java | 2 +- src/main/java/it/books/MicrosoftDB.java | 40 +++- src/main/java/it/books/gcon/AddBookFormG.java | 91 ++++++++ src/main/java/it/books/gcon/DeskG.java | 2 - .../java/it/books/gcon/NewDeskDetailG.java | 48 +++- .../resources/it/books/gcon/addBookForm.fxml | 220 ++++++++++++++++++ .../it/books/gcon/newDeskDetail.fxml | 5 - 8 files changed, 386 insertions(+), 24 deletions(-) create mode 100644 src/main/java/it/books/gcon/AddBookFormG.java create mode 100644 src/main/resources/it/books/gcon/addBookForm.fxml diff --git a/build.gradle b/build.gradle index c710b2d..b0da327 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'it.books' -version '1.0.3' +version '1.0.4' repositories { mavenCentral() diff --git a/src/main/java/it/books/Main.java b/src/main/java/it/books/Main.java index 9d192bb..788f793 100644 --- a/src/main/java/it/books/Main.java +++ b/src/main/java/it/books/Main.java @@ -24,7 +24,7 @@ import java.util.Objects; public class Main extends Application { - private final static String version = "1.0.3"; //App version + private final static String version = "1.0.4"; //App version /** Return the current app version. **/ public static String getVersion(){ diff --git a/src/main/java/it/books/MicrosoftDB.java b/src/main/java/it/books/MicrosoftDB.java index 3f4c85b..d39026b 100644 --- a/src/main/java/it/books/MicrosoftDB.java +++ b/src/main/java/it/books/MicrosoftDB.java @@ -11,15 +11,14 @@ import com.healthmarketscience.jackcess.*; import it.books.base.Book; import it.books.base.EvBook; -import org.hsqldb.types.Charset; import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.sql.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; public class MicrosoftDB { /** Connect and retrieve a list of books from a given database @@ -212,7 +211,7 @@ public static boolean DeleteEntry(File dbFile, int id, String code, String title Row row; while(rows.hasNext()){ row = rows.next(); - if((row.getShort("ID") == id) || (row.getString("Codice").equals("code")) || (row.getString("Titolo").equals(title))){ + if((row.getString("Codice").equals("code")) || (row.getString("Titolo").equals(title))){ db.getTable("LIBRI").deleteRow(row); db.flush(); return true; @@ -223,4 +222,39 @@ public static boolean DeleteEntry(File dbFile, int id, String code, String title return false; } } + + /** + * This method is used to connect to local db and retrieve the last id number. + * @param dbFile specify the database file to work on + * @return integer result of the last registered id. Return -1 if an error occurred. + **/ + public static int connectAndGetLastID(File dbFile){ + String path = dbFile.getAbsolutePath(); + path = path.replace("\\", "/"); //Solve a bug + try(Connection db = DriverManager.getConnection("jdbc:ucanaccess://"+path+";memory=true")){ + ResultSet res = db.createStatement().executeQuery("SELECT * FROM LIBRI ORDER BY ID DESC LIMIT 1"); + res.next(); //Skip and read the first row + return res.getInt("ID"); + }catch (SQLException exc){ + //Debug exc.printStackTrace(); + return -1; + } + } + + /** Connect to the database file and add a new book entry + * @param dbFile specify the database which work on; + * @param book the adv. book element to add to the database; + * @return true whether the procedure was successful. Otherwise, if error occurred, it returns false. + * **/ + public static int connectAndUploadANewBook(File dbFile, EvBook book){ + try(Database db = DatabaseBuilder.open(dbFile)){ + db.getTable("LIBRI").addRow(null, book.getCode(), book.getTitle(), book.getAuthors(), book.getGenre(), book.getPublisher(), book.getEdition(), book.getSeries(), book.getOwnDate(), Short.parseShort(Integer.toString(book.getPages())), Short.parseShort(book.getYear()), book.getCountry(), book.getShelf(), null, book.getPagesFormat(), null, book.getOriginal()); + db.flush(); + }catch (IOException e){ + System.out.println("Impossibile aggiungere il libro al database."); + //Debug e.printStackTrace(); + return -1; + } + return 0; + } } diff --git a/src/main/java/it/books/gcon/AddBookFormG.java b/src/main/java/it/books/gcon/AddBookFormG.java new file mode 100644 index 0000000..0fa5f96 --- /dev/null +++ b/src/main/java/it/books/gcon/AddBookFormG.java @@ -0,0 +1,91 @@ +/* ================================================= + * Author: Cristian Capraro + * Personal Project under MIT Licence + * This class is the graphic controller to work on + * new Microsoft database specific format - + * This is the controller of add book form. + * ================================================= */ + + +package it.books.gcon; + +import it.books.MicrosoftDB; +import it.books.base.EvBook; +import javafx.fxml.FXML; +import javafx.scene.control.*; +import javafx.scene.layout.AnchorPane; + +import java.io.File; +import java.time.LocalDate; +import java.time.chrono.Chronology; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +public class AddBookFormG { + private static File workingDB = null; + public static TableView parent; + + /** Method used to share data between classes, it will be used to + * get the working database path **/ + public static void setWorkingDB(File dbFile) { + workingDB = dbFile; + } + + /** Method used to share data between classes, it will be used to + * get the parent tableview to work on **/ + public static void setParent(TableView p){ + parent = p; + } + + //Graphics Init + @FXML + public void initialize(){ + //Set value - locale + DatePickerData.chronologyProperty().set(Chronology.ofLocale(Locale.ITALIAN)); + DatePickerData.setValue(LocalDate.now()); + IDBookData.setText(""+(MicrosoftDB.connectAndGetLastID(workingDB)+1)); + } + + //FXML Components + @FXML + TextField IDBookData, CodeData, TitleData, AuthorsData, OriginalData, GenreData, YearData, EditionData, EditorData, SeriesData, PagesData, FormatData, CountryData, ShelfData; + @FXML + DatePicker DatePickerData; + @FXML + AnchorPane MainPane; + //FXML Actions + + //Add + public void AddToDBBtn(){ + if(TitleData.getText().isEmpty()||CodeData.getText().isEmpty()){ + new Alert(Alert.AlertType.ERROR, "Per registrare un nuovo libro bisogna inserire almeno il TITOLO e il CODICE (o ISBN)", ButtonType.OK).showAndWait(); + return; + }else{ + EvBook newBook = new EvBook(Integer.parseInt(IDBookData.getText()), CodeData.getText(), TitleData.getText(), AuthorsData.getText()); + if(PagesData.getText().isEmpty()) PagesData.setText("0"); + if(YearData.getText().isEmpty()) YearData.setText("0"); + try{ + newBook.addDetails(OriginalData.getText(), GenreData.getText(), YearData.getText(), EditionData.getText(), EditorData.getText() ,SeriesData.getText(), Integer.parseInt(PagesData.getText()), FormatData.getText(), CountryData.getText(), ShelfData.getText(), DatePickerData.getValue().format(DateTimeFormatter.ofPattern("dd-MM-yyyy"))); + int lastID; + if((lastID = MicrosoftDB.connectAndUploadANewBook(workingDB, newBook)) != -1){ + EvBook bookFinalized = new EvBook(lastID, newBook.getCode(), newBook.getTitle(), newBook.getAuthors()); + bookFinalized.addDetails(newBook.getOriginal(), newBook.getGenre(), newBook.getYear(), newBook.getEdition(), newBook.getPublisher(), newBook.getSeries(), newBook.getPages(), newBook.getPagesFormat(), newBook.getCountry(), newBook.getShelf(), newBook.getOwnDate()); + parent.getItems().add(bookFinalized); + new Alert(Alert.AlertType.INFORMATION, "Libro aggiunto al database.", ButtonType.OK).showAndWait(); + MainPane.getScene().getWindow().hide(); //Hide Window + }else{ + new Alert(Alert.AlertType.ERROR, "Errore con il database.", ButtonType.OK).showAndWait(); + } + + }catch (NumberFormatException e){ + new Alert(Alert.AlertType.ERROR, "Errore!\nSi prega di controllare l'anno di pubblicazione e il numero di pagine.", ButtonType.OK).showAndWait(); + } + } + } + + //Abort + public void CloseBtn(){ + MainPane.getScene().getWindow().hide(); + } + +} diff --git a/src/main/java/it/books/gcon/DeskG.java b/src/main/java/it/books/gcon/DeskG.java index 26d7fef..fecf15e 100644 --- a/src/main/java/it/books/gcon/DeskG.java +++ b/src/main/java/it/books/gcon/DeskG.java @@ -4,8 +4,6 @@ * This is controller class of desk.fxml * ======================================== */ - - package it.books.gcon; import it.books.Main; diff --git a/src/main/java/it/books/gcon/NewDeskDetailG.java b/src/main/java/it/books/gcon/NewDeskDetailG.java index 4b7b8ba..00113ed 100644 --- a/src/main/java/it/books/gcon/NewDeskDetailG.java +++ b/src/main/java/it/books/gcon/NewDeskDetailG.java @@ -1,11 +1,17 @@ +/* ================================================= + * Author: Cristian Capraro + * Personal Project under MIT Licence + * This class is the graphic controller to work on + * new Microsoft database specific format. + * ================================================= */ + package it.books.gcon; import it.books.MicrosoftDB; import it.books.base.EvBook; import javafx.concurrent.Task; -import javafx.concurrent.Worker; -import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Node; @@ -21,6 +27,8 @@ import javafx.stage.StageStyle; import java.io.File; +import java.io.IOException; +import java.util.Objects; import java.util.Optional; public class NewDeskDetailG { @@ -31,6 +39,7 @@ public class NewDeskDetailG { * get the working database path **/ public static void setWorkingDB(File dbFile) { workingDB = dbFile; + AddBookFormG.setWorkingDB(workingDB); } //On init @@ -65,7 +74,7 @@ protected Void call() throws Exception { @FXML BorderPane MainPane; @FXML - Button AddBookBtn, RemBookBtn, ModBookBtn, DetailBtn, LeaseBtn; + Button AddBookBtn, RemBookBtn, DetailBtn, LeaseBtn; @FXML TextField TitleTextBox, AuthorsTextBox, GenreTextBox, YearTextBox, EditionTextBox, PageTextBox, CodeTextBox; @FXML @@ -84,17 +93,33 @@ public void RemoveBookButton(){ alert.showAndWait().ifPresent(btn -> { if(btn.equals(ButtonType.YES)){ table.getItems().remove(selected); - MicrosoftDB.DeleteEntry(workingDB, (int) selected.getId(), selected.getCode(), selected.getTitle()); - }else return; + MicrosoftDB.DeleteEntry(workingDB, -1, selected.getCode(), selected.getTitle()); + } }); } - @FXML - public void ModifyBookButton(){ - - } - + @SuppressWarnings("unchecked") public void AddBookButton() { + AddBookFormG.setParent((TableView) MainPane.getCenter()); + try{ + Parent addBk = FXMLLoader.load(Objects.requireNonNull(AddBookFormG.class.getResource("addBookForm.fxml"))); + Scene scene = new Scene(addBk); + Stage stage = new Stage(); + stage.setScene(scene); + stage.setResizable(false); + stage.setIconified(false); + stage.initStyle(StageStyle.UNDECORATED); + stage.show(); + stage.setOnHiding(ref -> { + ((TableView) MainPane.getCenter()).getItems().clear(); + ((TableView) MainPane.getCenter()).getItems().addAll(MicrosoftDB.connectAndGetNew(workingDB)); //Reload db + }); + stage.setOnShowing(ref -> { + AddBookBtn.setDisable(true); + }); + }catch (IOException ignored){ + AddBookBtn.setDisable(false); + }; } @@ -142,9 +167,8 @@ public void DetailsButton(){ stage.setResizable(false); stage.setMaximized(false); stage.centerOnScreen(); + stage.setAlwaysOnTop(true); stage.setScene(scene); stage.show(); - - } } diff --git a/src/main/resources/it/books/gcon/addBookForm.fxml b/src/main/resources/it/books/gcon/addBookForm.fxml new file mode 100644 index 0000000..bc0fbb6 --- /dev/null +++ b/src/main/resources/it/books/gcon/addBookForm.fxml @@ -0,0 +1,220 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/it/books/gcon/newDeskDetail.fxml b/src/main/resources/it/books/gcon/newDeskDetail.fxml index 6b41e3b..9decf91 100644 --- a/src/main/resources/it/books/gcon/newDeskDetail.fxml +++ b/src/main/resources/it/books/gcon/newDeskDetail.fxml @@ -149,11 +149,6 @@ -