Skip to content

Commit

Permalink
Public Pre-Release Update
Browse files Browse the repository at this point in the history
- Bug fixed
- Add book / Remove book
- Requirements
  • Loading branch information
Cristian committed Sep 22, 2022
1 parent 2f7747f commit b03842f
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'it.books'
version '1.0.3'
version '1.0.4'

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/it/books/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
40 changes: 37 additions & 3 deletions src/main/java/it/books/MicrosoftDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
91 changes: 91 additions & 0 deletions src/main/java/it/books/gcon/AddBookFormG.java
Original file line number Diff line number Diff line change
@@ -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<EvBook> 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<EvBook> 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();
}

}
2 changes: 0 additions & 2 deletions src/main/java/it/books/gcon/DeskG.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* This is controller class of desk.fxml
* ======================================== */



package it.books.gcon;

import it.books.Main;
Expand Down
48 changes: 36 additions & 12 deletions src/main/java/it/books/gcon/NewDeskDetailG.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<EvBook>) 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<EvBook>) MainPane.getCenter()).getItems().clear();
((TableView<EvBook>) MainPane.getCenter()).getItems().addAll(MicrosoftDB.connectAndGetNew(workingDB)); //Reload db
});
stage.setOnShowing(ref -> {
AddBookBtn.setDisable(true);
});
}catch (IOException ignored){
AddBookBtn.setDisable(false);
};

}

Expand Down Expand Up @@ -142,9 +167,8 @@ public void DetailsButton(){
stage.setResizable(false);
stage.setMaximized(false);
stage.centerOnScreen();
stage.setAlwaysOnTop(true);
stage.setScene(scene);
stage.show();


}
}
Loading

0 comments on commit b03842f

Please sign in to comment.