Skip to content

Commit

Permalink
Important Update
Browse files Browse the repository at this point in the history
features added:
1. Added comments field
2. Added advanced database search

Note:
Project Finished!
Bookshelf project reaches its end
with the advanced search feature.
I am glad to support continuously
this project on my free time, but
I'm sure that I won't add important
features for the next months.
  • Loading branch information
Cristian committed Oct 29, 2022
1 parent a2294f3 commit b768d62
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 69 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.7'
version '1.1.0'

repositories {
mavenCentral()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/it/books/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Objects;

public class Main extends Application {
private final static String version = "1.0.7"; //App version
private final static String version = "1.1.0"; //App version

/** Return the current app version. **/
public static String getVersion(){
Expand All @@ -50,7 +50,7 @@ public void start(Stage stage) throws Exception {
});
stage.setScene(scene);
stage.setTitle("Catalogo");
stage.getIcons().add(new Image("https://aux.iconspalace.com/uploads/book-icon-256-2103632816.png"));
stage.getIcons().add(new Image(this.getClass().getResource("bsicon.png").toURI().toURL().openStream()));
stage.show();
}

Expand Down
74 changes: 56 additions & 18 deletions src/main/java/it/books/MicrosoftDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ public static ArrayList<Book> connectAndGet(File dbFile, int offset, int limit){
}
}

/** Connect and retrieve the entire list of books from a given database
* @param dbFile specify the database file to work on
* @return the list of specified book
* **/
public static ArrayList<Book> connectAndGet(File dbFile){
return connectAndGet(dbFile, 0, 0);
}

/** Connect and search on the entire list of books from a given database
* @param dbFile specify the database file to work on
* @param author specify the book's author
Expand Down Expand Up @@ -178,6 +170,7 @@ public static ArrayList<EvBook> connectAndGetNew(File dbFile, int offset, int li
while(res.next()){
EvBook book = new EvBook(res.getInt("ID"), res.getString("Codice"), res.getString("Titolo"), res.getString("Autore"));
book.addDetails(res.getString("Titolo_Originale"), res.getString("Genere"), res.getString("Anno_Pubblicazione"), res.getString("Edizione"), res.getString("Editore"), res.getString("Collezione"), res.getInt("Pagine"), res.getString("Formato_Pagine"), res.getString("Nazione"), res.getString("Scaffale"), res.getString("Posseduto_Dal"));
book.setComments(res.getString("Commento"));
try{
ResultSet pre = query.executeQuery("SELECT * FROM PRESTITI WHERE Codice=\""+book.getCode()+"\"");
if(pre.next()){
Expand Down Expand Up @@ -205,12 +198,11 @@ public static ArrayList<EvBook> connectAndGetNew(File dbFile){
/** [*** NEW DATABASE] Used to delete an entry in the specified book database.
* Use at least a param to ensure the book deletion;
* @param dbFile necessary to work on the specified db
* @param id the book id required to remove the book instantly (could be null)
* @param title the book title required to remove the book instantly (could be null)
* @param code required to remove the specified book (it could be null)
* @return true whether the action was correctly applied. False otherwise.
* **/
public static boolean DeleteEntry(File dbFile, int id, String code, String title) {
public static boolean DeleteEntry(File dbFile, String code, String title) {
try(Database db = DatabaseBuilder.open(dbFile)){
Iterator<Row> rows = db.getTable("LIBRI").iterator();
Iterator<Row> prestitiRows = db.getTable("PRESTITI").iterator();
Expand Down Expand Up @@ -260,7 +252,7 @@ public static int connectAndGetLastID(File dbFile){
* **/
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.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(), book.getComments(), book.getPagesFormat(), null, book.getOriginal());
db.flush();
if(book.isLeasing()){
//Codice, is in leasing, data inzio, data fine, a...
Expand All @@ -286,9 +278,10 @@ public static EvBook connectAndSearch(File dbFile, String code){
try(Connection db = DriverManager.getConnection("jdbc:ucanaccess://"+path+";memory=true")){
Statement st = db.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM LIBRI WHERE Codice = '"+code+"'");
while (res.next()){
if (res.next()){
EvBook book = new EvBook(res.getInt("ID"), res.getString("Codice"), res.getString("Titolo"), res.getString("Autore"));
book.addDetails(res.getString("Titolo_Originale"), res.getString("Genere"), res.getString("Anno_Pubblicazione"), res.getString("Edizione"), res.getString("Editore"), res.getString("Collezione"), res.getInt("Pagine"), res.getString("Formato_Pagine"), res.getString("Nazione"), res.getString("Scaffale"), res.getString("Posseduto_Dal"));
book.setComments(res.getString("Commento"));
return book;
}
}catch (Exception e){
Expand All @@ -297,6 +290,54 @@ public static EvBook connectAndSearch(File dbFile, String code){
return null;
}

/** Advanced database search to catch multiple filters (on newest database)
* This method use UNION to add results found on multiple queries.
* @param dbFile specify the database which work on;
* @param code specify the book code;
* @param title specify book title;
* @param author specify book author;
* @param year specify book released year;
* @return an arraylist of books found thanks to the specified query.
* **/
public static ArrayList<EvBook> connectAndSearch(String code, String title, String year, String author, File dbFile){
String path = dbFile.getAbsolutePath();
path = path.replace("\\", "/"); //Solve a bug
try(Connection db = DriverManager.getConnection("jdbc:ucanaccess://"+path+";memory=true")){
Statement statement = db.createStatement();
StringBuilder SearchQuery = new StringBuilder(); String[] values = new String[4];
if((!Objects.equals(code, ""))) values[0] = "SELECT * FROM LIBRI WHERE Codice LIKE '%"+code+"%' ";
if(!Objects.equals(title, "")) values[1] = "SELECT * FROM LIBRI WHERE Titolo LIKE '%"+title+"%' ";
if(!Objects.equals(year, "")) values[2] = "SELECT * FROM LIBRI WHERE Anno_Pubblicazione LIKE '%"+year+"%' ";
if(!Objects.equals(author, "")) values[3] = "SELECT * FROM LIBRI WHERE Autore LIKE '%"+author+"%' ";
boolean multiple = false;
for(String i : values){
if(i == null) continue;
SearchQuery.append(multiple? "UNION " : "").append(i);
multiple = true;
}
String SearchFinal;

//String rewrite - to match query
if(SearchQuery.toString().endsWith("UNION ")){
SearchFinal = SearchQuery.substring(0, SearchQuery.length()-8);
}else SearchFinal = SearchQuery.substring(0, SearchQuery.length()-1);

System.out.println(SearchFinal);

ResultSet res = statement.executeQuery(SearchFinal);
ArrayList<EvBook> books = new ArrayList<>();
while (res.next()){
EvBook book = new EvBook(res.getInt("ID"), res.getString("Codice"), res.getString("Titolo"), res.getString("Autore"));
book.addDetails(res.getString("Titolo_Originale"), res.getString("Genere"), res.getString("Anno_Pubblicazione"), res.getString("Edizione"), res.getString("Editore"), res.getString("Collezione"), res.getInt("Pagine"), res.getString("Formato_Pagine"), res.getString("Nazione"), res.getString("Scaffale"), res.getString("Posseduto_Dal"));
book.setComments(res.getString("Commento"));
books.add(book);
}
return books;
}catch (SQLException exc){
return null;
}
}

/** Connect to the database and add a new lease entry
* @param dbFile specify the database which work on;
* @param book the book with specified leasing (book must already exist)
Expand All @@ -310,9 +351,7 @@ public static void connectAndAddLease(File dbFile, EvBook book){
String[] personData = book.leasedTo().split(";");
db.getTable("PRESTITI").addRow(book.getCode(), book.isLeasing(), book.getBeginDate(), book.getEndDate(), personData[0]+";"+personData[1]+";"+personData[2]);
//Reset here
}catch (IOException | NoSuchElementException e){
return;
}
}catch (IOException | NoSuchElementException ignored){ }
}

/** Connect to the database and delete an existing lease information
Expand All @@ -333,8 +372,7 @@ public static void connectAndDeleteLease(File dbFile, EvBook book){
db.getTable("PRESTITI").deleteRow(row1);
}
}
}catch (IOException | NoSuchElementException e){
return;
}
}catch (IOException | NoSuchElementException ignored){ }
}

}
13 changes: 2 additions & 11 deletions src/main/java/it/books/base/EvBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class EvBook {
//Lease structure
private static class Lease{
public Lease(){}; //Init (not in lease)
public Lease(){} //Init (not in lease)

public void setLease(String LeaseBegin, String LeaseExpectedEnd, String Name, String Surname, String Tel){
InLeasing = true;
Expand Down Expand Up @@ -98,7 +98,7 @@ public String toString(){
private String shelf;

//Other
private String comments;
private String comments = "";

private String ownDate;

Expand All @@ -125,10 +125,6 @@ public void setComments(String comments){
this.comments = comments;
}

public Lease getLease() {
return lease;
}

public String getComments() {
return comments;
}
Expand All @@ -151,16 +147,11 @@ public void addDetails(String originalTitle, String genre, String year, String e
this.ownDate = ownDate;
}


@Override
public String toString(){
return "EvBook ID: " + id + "\nTitle: " + title + "/" + original + "\n";
}

public long getId() {
return id;
}

public String getTitle() {
return title;
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/it/books/gcon/AddBookFormG.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public void initialize(){
//FXML Components
@FXML
TextField IDBookData, CodeData, TitleData, AuthorsData, OriginalData, GenreData, YearData, EditionData, EditorData, SeriesData, PagesData, FormatData, CountryData, ShelfData;

@FXML
TextArea CommentBox;

@FXML
DatePicker DatePickerData;
@FXML
Expand All @@ -59,17 +63,18 @@ public void initialize(){
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")));
newBook.setComments(CommentBox.getText());
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());
bookFinalized.setComments(newBook.getComments());
parent.getItems().add(bookFinalized);
new Alert(Alert.AlertType.INFORMATION, "Libro aggiunto al database.", ButtonType.OK).showAndWait();
MainPane.getScene().getWindow().hide(); //Hide Window
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/it/books/gcon/DeskG.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class DeskG {

Expand Down Expand Up @@ -70,7 +69,7 @@ private void CloseBtn() {
private void AboutBtn() {
//Generate a new window from code
BorderPane pane = new BorderPane();
Label label = new Label("Software realizzato per la Biblioteca di Divignano.\nSoftware libero - con licenza MIT\nVersione "+Main.getVersion()+" - 2022\nProgramma realizzato da:\nCristian Capraro");
Label label = new Label("Software bibliotecario\nLicenza MIT\nVersione "+Main.getVersion()+" - 2022\nCristian Capraro");
label.idProperty().setValue("About");
pane.setCenter(label);

Expand All @@ -83,7 +82,9 @@ private void AboutBtn() {
stage.setMinWidth(240);
stage.setResizable(false);
stage.setTitle("Informazioni Catalogo");
stage.getIcons().add(new Image("https://www.ngmadv.it/wp-content/uploads/2017/06/User-yellow-icon.png"));
try {
stage.getIcons().add(new Image(this.getClass().getResource("usericon.png").toURI().toURL().openStream()));
}catch (Exception ignored){ }
//Actions (disable multiple instances)
stage.setOnHiding(i -> AboutButton.setDisable(false));
stage.setOnShowing(i -> AboutButton.setDisable(true));
Expand Down Expand Up @@ -218,7 +219,7 @@ private void OpenBtn() throws IOException {
}catch (Exception notFound){
//Debug notFound.printStackTrace();
//Loading and operating with database files
ArrayList<Book> book = new ArrayList<>();
ArrayList<Book> book;
//Share info
OldDeskDetailG.getDBFile(dbFile);

Expand Down
Loading

0 comments on commit b768d62

Please sign in to comment.