Skip to content

Commit

Permalink
Huge Features Update
Browse files Browse the repository at this point in the history
- Database Creation
- New database structure
- Removed deprecated functionalities
- Created the new Graphical workspace
- Bug fixed
- Size bug fixed
- Delete entries
- View details on new database
  • Loading branch information
Cristian committed Sep 20, 2022
1 parent c0af7b3 commit 2f7747f
Show file tree
Hide file tree
Showing 15 changed files with 1,026 additions and 143 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.2'
version '1.0.3'

repositories {
mavenCentral()
Expand Down
Binary file modified example/asimov.accdb
Binary file not shown.
19 changes: 11 additions & 8 deletions src/main/java/it/books/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* --------------------------------------------------------------------
* ==================================================================== */



package it.books;

import it.books.gcon.DeskG;
Expand All @@ -23,18 +21,23 @@
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.util.Objects;

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

/** Return the current app version. **/
public static String getVersion(){
return version;
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(Objects.requireNonNull(DeskG.class.getResource("desk.fxml")));
Scene scene = new Scene(root, 640, 480);
//scene.getStylesheets().add(DeskG.class.getResource("dark.css").toExternalForm());
//Setting minimals (Aspect ratio 4:3)
stage.setMinWidth(640);
stage.setMinWidth(480);
Scene scene = new Scene(root);
scene.getStylesheets().add(Objects.requireNonNull(this.getClass().getResource("defStyle.css")).toExternalForm());
//Setting minimals
stage.setMinWidth(800);
stage.setMinHeight(600);
//Override exit button
stage.setOnCloseRequest(i -> {
i.consume();
Expand Down
141 changes: 114 additions & 27 deletions src/main/java/it/books/MicrosoftDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@

package it.books;

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;

public class MicrosoftDB {
/** Connect and retrieve a list of books from a given database
Expand Down Expand Up @@ -67,33 +74,7 @@ public static ArrayList<Book> connectAndGet(File dbFile, int offset, int limit){
* @return the list of specified book
* **/
public static ArrayList<Book> connectAndGet(File dbFile){
// Supply bug
String db = dbFile.getAbsolutePath().replace("\\", "/");
try(Connection conn= DriverManager.getConnection("jdbc:ucanaccess://"+db+";memory=true")){
//Connect and get raw results
Statement query = conn.createStatement();
ResultSet res = query.executeQuery("SELECT * FROM LIBRI");
ArrayList<Book> bookList = new ArrayList<>();
//Book setup
while(res.next()){
Book book = new Book(res.getString("codice"), res.getString("titolo"));
book.setDetails(res.getString("autore"), res.getBoolean("unico_autore"), res.getString("collana"), res.getString("genere"), res.getString("editore"), res.getString("edizione"), res.getString("anno"));
book.setDetails(res.getString("scaffale"), (res.getString("pro_dal_gg") +"/" + (res.getString("pro_dal_mm") + "/" + res.getString("pro_dal_aa"))), res.getString("commento"));
book.setLocale(res.getString("nazione"), res.getString("traduttore"), res.getString("titolo_orig"));
book.setPages(res.getInt("pagine"), res.getString("formato"));
if(res.getBoolean("prestito")){
book.setLeasing(true, (res.getString("pre_dal_gg") + "/" + res.getString("pre_dal_mm") + "/" + res.getString("pre_dal_aa")), res.getString("prestitario"));
}else{
book.setLeasing(false, "", "");
}
bookList.add(book);
}
return bookList;

}catch (SQLException e){
return null;
}

return connectAndGet(dbFile, 0, 0);
}

/** Connect and search on the entire list of books from a given database
Expand Down Expand Up @@ -136,4 +117,110 @@ public static ArrayList<Book> connectAndSearch(File dbFile, String title, String
}

}

/** This method must be used to create a new BookShelf Database.
* @param dbFile specify the database file to init
* @return true if the operation will end successfully, false
* otherwise.
* **/
public static boolean CreateAndInit(File dbFile){
try(Database db = DatabaseBuilder.create(Database.FileFormat.V2016, dbFile)){
TableBuilder table1 = new TableBuilder("LIBRI");

table1.addColumns(List.of(
new ColumnBuilder("ID", DataType.LONG).setAutoNumber(true),
new ColumnBuilder("Codice", DataType.TEXT),
new ColumnBuilder("Titolo", DataType.TEXT),
new ColumnBuilder("Autore", DataType.TEXT),
new ColumnBuilder("Genere", DataType.TEXT),
new ColumnBuilder("Editore", DataType.TEXT),
new ColumnBuilder("Edizione", DataType.TEXT),
new ColumnBuilder("Collezione", DataType.TEXT),
new ColumnBuilder("Posseduto_Dal", DataType.TEXT),
new ColumnBuilder("Pagine", DataType.INT),
new ColumnBuilder("Anno_Pubblicazione", DataType.INT),
new ColumnBuilder("Nazione", DataType.TEXT),
new ColumnBuilder("Scaffale", DataType.TEXT),
new ColumnBuilder("Commento", DataType.TEXT),
new ColumnBuilder("Formato_Pagine", DataType.TEXT),
new ColumnBuilder("Traduttore", DataType.TEXT),
new ColumnBuilder("Titolo_Originale", DataType.TEXT)
)).toTable(db);

TableBuilder table2 = new TableBuilder("PRESTITI");
table2.addColumns(List.of(
new ColumnBuilder("Codice", DataType.TEXT),
new ColumnBuilder("In_Prestito", DataType.BOOLEAN),
new ColumnBuilder("Data_Prestito", DataType.SHORT_DATE_TIME),
new ColumnBuilder("Prestato_A", DataType.TEXT)
)).toTable(db);

System.out.println(db.getTable("LIBRI").getColumns());

System.out.println("OK");
return true;
}catch (IOException exc){
exc.printStackTrace();
return false;
}
}

/** [*** NEW DATABASE] Connect and retrieve a list of books from a given database
* @param dbFile specify the database file to work on
* @param limit specify the maximum number of book given (set to 0 for infinity)
* @param offset specify the offset from the first row in the table
* @return the list of specified book
* **/

public static ArrayList<EvBook> connectAndGetNew(File dbFile, int offset, int limit){
String db = dbFile.getAbsolutePath().replace("\\", "/"); //Solve bugs
try(Connection conn= DriverManager.getConnection("jdbc:ucanaccess://"+db+";memory=true")){
Statement query = conn.createStatement();
ResultSet res = query.executeQuery((limit == 0)? ("SELECT * FROM LIBRI OFFSET " + offset + " ROWS") : ("SELECT * FROM LIBRI LIMIT " + offset + ", " + limit));
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"));
books.add(book);
}
return books;
}catch (SQLException e){
e.printStackTrace();
return null;
}
}

/** [*** NEW DATABASE] 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<EvBook> connectAndGetNew(File dbFile){
return connectAndGetNew(dbFile, 0, 0);
}

/** [*** 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) {
try(Database db = DatabaseBuilder.open(dbFile)){
Iterator<Row> rows = db.getTable("LIBRI").iterator();
Row row;
while(rows.hasNext()){
row = rows.next();
if((row.getShort("ID") == id) || (row.getString("Codice").equals("code")) || (row.getString("Titolo").equals(title))){
db.getTable("LIBRI").deleteRow(row);
db.flush();
return true;
}
}
return false; //Not found
}catch (IOException e){
return false;
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/it/books/base/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package it.books.base;
import java.util.List;

public class Book extends MetaBook {
public final class Book extends MetaBook {
private String code;
private String title;
private String author;
Expand Down Expand Up @@ -45,7 +45,6 @@ public static List<String> getLocale(){

//Non-static


public void setLeasing(boolean lease, String leasingDate, String leasedTo) {
this.leasing = (lease)? "Si" : "No";
this.leasingDate = leasingDate;
Expand Down
Loading

0 comments on commit 2f7747f

Please sign in to comment.