commit 24d8efc74b072093b88a1c43c1763f55b153ecb4 Author: Maksym Sierszen Date: Sat Jan 13 21:13:31 2024 +0100 final ver commit diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..f5dd5ee Binary files /dev/null and b/.DS_Store differ diff --git a/Biblioteka/.gitignore b/Biblioteka/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/Biblioteka/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Biblioteka/.idea/.gitignore b/Biblioteka/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Biblioteka/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Biblioteka/.idea/encodings.xml b/Biblioteka/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/Biblioteka/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Biblioteka/.idea/misc.xml b/Biblioteka/.idea/misc.xml new file mode 100644 index 0000000..ecfa09c --- /dev/null +++ b/Biblioteka/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Biblioteka/.idea/uiDesigner.xml b/Biblioteka/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/Biblioteka/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Biblioteka/pom.xml b/Biblioteka/pom.xml new file mode 100644 index 0000000..fce0ef7 --- /dev/null +++ b/Biblioteka/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + org.example + Library + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + + org.hibernate + hibernate-core + 5.6.9.Final + + + + + org.postgresql + postgresql + 42.4.0 + + + \ No newline at end of file diff --git a/Biblioteka/src/main/java/org/example/Author.java b/Biblioteka/src/main/java/org/example/Author.java new file mode 100644 index 0000000..af77582 --- /dev/null +++ b/Biblioteka/src/main/java/org/example/Author.java @@ -0,0 +1,67 @@ +package org.example; + +import javax.persistence.*; +import java.util.List; + +@Entity +@Table(name = "\"Authors\"") +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "author_id") + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "nationality") + private String nationality; + + @Column(name = "birth_year") + private Integer birthYear; + + @OneToMany(mappedBy = "author", cascade = CascadeType.ALL) + private List books; + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getNationality() { + return nationality; + } + + public void setNationality(String nationality) { + this.nationality = nationality; + } + + public Integer getBirthYear() { + return birthYear; + } + + public void setBirthYear(Integer birthYear) { + this.birthYear = birthYear; + } + + public List getBooks() { + return books; + } + + public void setBooks(List books) { + this.books = books; + } +} diff --git a/Biblioteka/src/main/java/org/example/Book.java b/Biblioteka/src/main/java/org/example/Book.java new file mode 100644 index 0000000..567b955 --- /dev/null +++ b/Biblioteka/src/main/java/org/example/Book.java @@ -0,0 +1,70 @@ +package org.example; + +import org.hibernate.annotations.Type; +import javax.persistence.*; +import java.time.ZonedDateTime; + +@Entity +@Table(name = "\"Books\"") +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "book_id") + private Long id; + + @Column(name = "title") + private String title; + + @Column(name = "publication_date") + @Type(type = "org.hibernate.type.ZonedDateTimeType") + private ZonedDateTime publicationDate; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "author_id") + private Author author; + + @Column(name = "availability_status") + private boolean availabilityStatus; + + // Getters and setters + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public ZonedDateTime getPublicationDate() { + return publicationDate; + } + + public void setPublicationDate(ZonedDateTime publicationDate) { + this.publicationDate = publicationDate; + } + + public Author getAuthor() { + return author; + } + + public void setAuthor(Author author) { + this.author = author; + } + + public boolean isAvailabilityStatus() { + return availabilityStatus; + } + + public void setAvailabilityStatus(boolean availabilityStatus) { + this.availabilityStatus = availabilityStatus; + } +} diff --git a/Biblioteka/src/main/java/org/example/Borrow.java b/Biblioteka/src/main/java/org/example/Borrow.java new file mode 100644 index 0000000..76c8465 --- /dev/null +++ b/Biblioteka/src/main/java/org/example/Borrow.java @@ -0,0 +1,71 @@ +package org.example; +import org.hibernate.annotations.Type; +import javax.persistence.*; +import java.time.ZonedDateTime; + +@Entity +@Table(name = "\"Borrows\"") +public class Borrow { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "borrow_id") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User user; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "book_id") + private Book book; + + @Column(name = "borrow_date") + @Type(type = "org.hibernate.type.ZonedDateTimeType") + private ZonedDateTime borrowDate; + + @Column(name = "return_date") + @Type(type = "org.hibernate.type.ZonedDateTimeType") + private ZonedDateTime returnDate; + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public Book getBook() { + return book; + } + + public void setBook(Book book) { + this.book = book; + } + + public ZonedDateTime getBorrowDate() { + return borrowDate; + } + + public void setBorrowDate(ZonedDateTime borrowDate) { + this.borrowDate = borrowDate; + } + + public ZonedDateTime getReturnDate() { + return returnDate; + } + + public void setReturnDate(ZonedDateTime returnDate) { + this.returnDate = returnDate; + } +} diff --git a/Biblioteka/src/main/java/org/example/LibraryService.java b/Biblioteka/src/main/java/org/example/LibraryService.java new file mode 100644 index 0000000..6f544a2 --- /dev/null +++ b/Biblioteka/src/main/java/org/example/LibraryService.java @@ -0,0 +1,100 @@ +package org.example; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.query.Query; +import java.util.List; + +public class LibraryService { + + private SessionFactory sessionFactory; + + public LibraryService(SessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + } + + public void printAllAuthors() { + try (Session session = sessionFactory.openSession()) { + Query query = session.createQuery("FROM Author", Author.class); + List authors = query.getResultList(); + + System.out.println("Dostępni autorzy:"); + for (Author author : authors) { + System.out.println("ID: " + author.getId() + author.getName()); + } + } + } + + public List findAllBooksByAuthorId(Long authorId) { + List books; + try (Session session = sessionFactory.openSession()) { + Query query = session.createQuery("SELECT b FROM Book b JOIN FETCH b.author WHERE b.author.id = :authorId", Book.class); + query.setParameter("authorId", authorId); + books = query.getResultList(); + } + return books; + } + + public void printBooksByAuthor(long authorId) { + List booksByAuthor = findAllBooksByAuthorId(authorId); + + if (!booksByAuthor.isEmpty()) { + Author author = booksByAuthor.get(0).getAuthor(); + System.out.println("Książki autora:\n " + author.getName()); + for (Book book : booksByAuthor) { + System.out.println("Tytuł: " + book.getTitle()); + System.out.println("Data publikacji: " + book.getPublicationDate()); + System.out.println("--------"); + } + } else { + System.out.println("Nie znaleziono książek dla autora o ID: " + authorId); + } + } + + public void printBorrowsByUserId(long userId, int pageNumber) { + int pageSize = 3; + try (Session session = sessionFactory.openSession()) { + Query query = session.createQuery("FROM Borrow b WHERE b.user.id = :userId", Borrow.class); + query.setParameter("userId", userId); + query.setFirstResult((pageNumber - 1) * pageSize); + query.setMaxResults(pageSize); + + List borrows = query.getResultList(); + + if (borrows.isEmpty()) { + System.out.println("Użytkownik o ID " + userId + " nie ma żadnych wypożyczeń."); + } else { + for (Borrow borrow : borrows) { + System.out.println("Wypożyczenie ID: " + borrow.getId()); + System.out.println("Książka: " + borrow.getBook().getTitle()); + System.out.println("Data wypożyczenia: " + borrow.getBorrowDate()); + System.out.println("Data zwrotu: " + borrow.getReturnDate()); + System.out.println("--------"); + } + } + } + } + + public boolean hasBorrows(long userId) { + try (Session session = sessionFactory.openSession()) { + Query query = session.createQuery("SELECT COUNT(b.id) FROM Borrow b WHERE b.user.id = :userId", Long.class); + query.setParameter("userId", userId); + return query.getSingleResult() > 0; + } + } + + public void printAvailableBooks() { + try (Session session = sessionFactory.openSession()) { + Query query = session.createQuery("FROM Book b WHERE b.availabilityStatus = true", Book.class); + List availableBooks = query.getResultList(); + + if (availableBooks.isEmpty()) { + System.out.println("Brak dostępnych książek."); + } else { + System.out.println("Dostępne książki:"); + for (Book book : availableBooks) { + System.out.println("ID: " + book.getId() + ", Tytuł: " + book.getTitle()); + } + } + } + } +} \ No newline at end of file diff --git a/Biblioteka/src/main/java/org/example/Main.java b/Biblioteka/src/main/java/org/example/Main.java new file mode 100644 index 0000000..787740f --- /dev/null +++ b/Biblioteka/src/main/java/org/example/Main.java @@ -0,0 +1,81 @@ +package org.example; + +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + // Konfiguracja Hibernate z pliku hibernate.cfg.xml + Configuration configuration = new Configuration(); + configuration.configure("hibernate.cfg.xml"); + + // Utworzenie sesji Hibernate + SessionFactory sessionFactory = configuration.buildSessionFactory(); + + // Utworzenie instancji LibraryService + LibraryService libraryService = new LibraryService(sessionFactory); + + Scanner scanner = new Scanner(System.in); + + while (true) { + System.out.println("-- BIBLIOTEKA --"); + System.out.println("1. Wypisz książki danego autora"); + System.out.println("2. Wypisz wypożyczenia użytkownika"); + System.out.println("3. Wypisz dostępne książki"); + System.out.println("4. Wyjście"); + + System.out.print("Wybierz opcję: "); + int choice = scanner.nextInt(); + + switch (choice) { + case 1: + libraryService.printAllAuthors(); + System.out.print("Podaj ID autora: "); + long authorId = scanner.nextLong(); + libraryService.printBooksByAuthor(authorId); + break; + case 2: + System.out.print("Podaj ID użytkownika: "); + long userId = scanner.nextLong(); + if (!libraryService.hasBorrows(userId)) { + System.out.println("Użytkownik o ID " + userId + " nie ma żadnych wypożyczeń."); + } else { + int currentPage = 1; + boolean browsing = true; + while (browsing) { + libraryService.printBorrowsByUserId(userId, currentPage); + System.out.println("Aktualna strona: " + currentPage); + System.out.println("1. Następna strona"); + System.out.println("2. Poprzednia strona"); + System.out.println("3. Wyjście ze stronicowania"); + System.out.print("Wybierz opcję: "); + int pageChoice = scanner.nextInt(); + switch (pageChoice) { + case 1: + currentPage++; + break; + case 2: + currentPage = Math.max(1, currentPage - 1); + break; + case 3: + browsing = false; + break; + } + } + } + break; + case 3: + libraryService.printAvailableBooks(); + break; + case 4: + System.out.println("> EXIT <"); + sessionFactory.close(); + System.exit(0); + break; + default: + System.out.println("Nieprawidłowa opcja, spróbuj ponownie."); + } + } + } +} \ No newline at end of file diff --git a/Biblioteka/src/main/java/org/example/User.java b/Biblioteka/src/main/java/org/example/User.java new file mode 100644 index 0000000..7cea6a7 --- /dev/null +++ b/Biblioteka/src/main/java/org/example/User.java @@ -0,0 +1,70 @@ +package org.example; + +import org.hibernate.annotations.Type; +import javax.persistence.*; +import java.time.ZonedDateTime; +import java.util.List; + +@Entity +@Table(name = "\"Users\"") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "user_id") + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "birth_date") + @Type(type = "org.hibernate.type.ZonedDateTimeType") + private ZonedDateTime birthDate; + + @Column(name = "email", unique = true) + private String email; + + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) + private List borrows; + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public ZonedDateTime getBirthDate() { + return birthDate; + } + + public void setBirthDate(ZonedDateTime birthDate) { + this.birthDate = birthDate; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public List getBorrows() { + return borrows; + } + + public void setBorrows(List borrows) { + this.borrows = borrows; + } +} diff --git a/Biblioteka/src/main/resources/hibernate.cfg.xml b/Biblioteka/src/main/resources/hibernate.cfg.xml new file mode 100644 index 0000000..c6bfe10 --- /dev/null +++ b/Biblioteka/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,25 @@ + + + + + org.postgresql.Driver + jdbc:postgresql://localhost:5432/Library + postgres + 1234 + + + org.hibernate.dialect.PostgreSQLDialect + + + false + true + + + + + + + + diff --git a/library_postgre_backup.sql b/library_postgre_backup.sql new file mode 100644 index 0000000..c8d8993 Binary files /dev/null and b/library_postgre_backup.sql differ