From e9d766a54b054f2e7c6f775e30024ad4e94875ea Mon Sep 17 00:00:00 2001 From: meliurwen Date: Sun, 12 Jan 2020 21:53:14 +0100 Subject: [PATCH 1/3] Huge refactoring --- .gitignore | 4 + mivan/.editorconfig | 14 ++ {multibook => mivan}/.gitignore | 0 .../.mvn/wrapper/MavenWrapperDownloader.java | 4 +- .../.mvn/wrapper/maven-wrapper.jar | Bin 50710 -> 50710 bytes .../.mvn/wrapper/maven-wrapper.properties | 4 +- {multibook => mivan}/mvnw | 8 +- {multibook => mivan}/mvnw.cmd | 8 +- {multibook => mivan}/pom.xml | 36 ++-- mivan/src/main/java/mivan/Mivan.java | 37 +++++ mivan/src/main/java/mivan/model/Author.java | 74 +++++++++ mivan/src/main/java/mivan/model/Book.java | 93 +++++++++++ mivan/src/main/java/mivan/model/Location.java | 83 +++++++++ .../src/main/java/mivan}/model/Person.java | 17 +- mivan/src/main/java/mivan/model/Staff.java | 71 ++++++++ mivan/src/main/java/mivan/model/User.java | 90 ++++++++++ .../mivan/repository/AuthorRepository.java | 20 +++ .../repository/AuthorRepositoryImpl.java | 156 +++++++++++++++++ .../java/mivan/repository/BookRepository.java | 24 +++ .../mivan/repository/BookRepositoryImpl.java | 157 ++++++++++++++++++ .../mivan/repository/LocationRepository.java | 20 +++ .../repository/LocationRepositoryImpl.java | 114 +++++++++++++ .../java/mivan/repository/Repository.java | 8 + .../mivan/repository/StaffRepository.java | 21 +++ .../mivan/repository/StaffRepositoryImpl.java | 118 +++++++++++++ .../java/mivan/repository/UserRepository.java | 21 +++ .../mivan/repository/UserRepositoryImpl.java | 133 +++++++++++++++ .../main/resources/META-INF/persistence.xml | 27 +++ .../src/main/resources/application.properties | 2 + mivan/src/test/java/mivan/AuthorBookTest.java | 30 ++++ mivan/src/test/java/mivan/AuthorTest.java | 77 +++++++++ mivan/src/test/java/mivan/BookTest.java | 41 +++++ mivan/src/test/java/mivan/LocationTest.java | 116 +++++++++++++ mivan/src/test/java/mivan/StaffTest.java | 146 ++++++++++++++++ mivan/src/test/java/mivan/UserTest.java | 109 ++++++++++++ multibook/database.mv.db | Bin 32768 -> 0 bytes .../mivan/multibook/MultibookApplication.java | 13 -- .../multibook/controller/UserController.java | 24 --- .../disco/mivan/multibook/model/Author.java | 55 ------ .../disco/mivan/multibook/model/Book.java | 68 -------- .../disco/mivan/multibook/model/Item.java | 65 -------- .../disco/mivan/multibook/model/Loan.java | 101 ----------- .../disco/mivan/multibook/model/Location.java | 36 ---- .../disco/mivan/multibook/model/Staff.java | 26 --- .../disco/mivan/multibook/model/User.java | 67 -------- .../repository/PersonRepository.java | 17 -- .../multibook/repository/UserRepository.java | 19 --- .../mivan/multibook/service/UserService.java | 37 ----- .../src/main/resources/application.properties | 23 --- .../multibook/MultibookApplicationTests.java | 13 -- .../disco/mivan/multibook/UserTests.java | 33 ---- 51 files changed, 1855 insertions(+), 625 deletions(-) create mode 100644 mivan/.editorconfig rename {multibook => mivan}/.gitignore (100%) rename {multibook => mivan}/.mvn/wrapper/MavenWrapperDownloader.java (97%) rename {multibook => mivan}/.mvn/wrapper/maven-wrapper.jar (91%) rename {multibook => mivan}/.mvn/wrapper/maven-wrapper.properties (58%) rename {multibook => mivan}/mvnw (98%) rename {multibook => mivan}/mvnw.cmd (97%) rename {multibook => mivan}/pom.xml (56%) create mode 100644 mivan/src/main/java/mivan/Mivan.java create mode 100644 mivan/src/main/java/mivan/model/Author.java create mode 100644 mivan/src/main/java/mivan/model/Book.java create mode 100644 mivan/src/main/java/mivan/model/Location.java rename {multibook/src/main/java/com/unimib/disco/mivan/multibook => mivan/src/main/java/mivan}/model/Person.java (70%) create mode 100644 mivan/src/main/java/mivan/model/Staff.java create mode 100644 mivan/src/main/java/mivan/model/User.java create mode 100644 mivan/src/main/java/mivan/repository/AuthorRepository.java create mode 100644 mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java create mode 100644 mivan/src/main/java/mivan/repository/BookRepository.java create mode 100644 mivan/src/main/java/mivan/repository/BookRepositoryImpl.java create mode 100644 mivan/src/main/java/mivan/repository/LocationRepository.java create mode 100644 mivan/src/main/java/mivan/repository/LocationRepositoryImpl.java create mode 100644 mivan/src/main/java/mivan/repository/Repository.java create mode 100644 mivan/src/main/java/mivan/repository/StaffRepository.java create mode 100644 mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java create mode 100644 mivan/src/main/java/mivan/repository/UserRepository.java create mode 100644 mivan/src/main/java/mivan/repository/UserRepositoryImpl.java create mode 100644 mivan/src/main/resources/META-INF/persistence.xml create mode 100644 mivan/src/main/resources/application.properties create mode 100644 mivan/src/test/java/mivan/AuthorBookTest.java create mode 100644 mivan/src/test/java/mivan/AuthorTest.java create mode 100644 mivan/src/test/java/mivan/BookTest.java create mode 100644 mivan/src/test/java/mivan/LocationTest.java create mode 100644 mivan/src/test/java/mivan/StaffTest.java create mode 100644 mivan/src/test/java/mivan/UserTest.java delete mode 100644 multibook/database.mv.db delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/MultibookApplication.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/controller/UserController.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Author.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Book.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Item.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Loan.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Location.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Staff.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/model/User.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/PersonRepository.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/UserRepository.java delete mode 100644 multibook/src/main/java/com/unimib/disco/mivan/multibook/service/UserService.java delete mode 100644 multibook/src/main/resources/application.properties delete mode 100644 multibook/src/test/java/com/unimib/disco/mivan/multibook/MultibookApplicationTests.java delete mode 100644 multibook/src/test/java/com/unimib/disco/mivan/multibook/UserTests.java diff --git a/.gitignore b/.gitignore index df19b1b..0c01375 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ *.bib~ *.pdf + + +mivan/target +mivan/bin diff --git a/mivan/.editorconfig b/mivan/.editorconfig new file mode 100644 index 0000000..2eccc8b --- /dev/null +++ b/mivan/.editorconfig @@ -0,0 +1,14 @@ +# http://editorconfig.org + +root = true + +[*] +indent_style = space +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true +end_of_line = lf +charset = utf-8 + +[*.{yml,yaml,json}] +insert_final_newline = false diff --git a/multibook/.gitignore b/mivan/.gitignore similarity index 100% rename from multibook/.gitignore rename to mivan/.gitignore diff --git a/multibook/.mvn/wrapper/MavenWrapperDownloader.java b/mivan/.mvn/wrapper/MavenWrapperDownloader.java similarity index 97% rename from multibook/.mvn/wrapper/MavenWrapperDownloader.java rename to mivan/.mvn/wrapper/MavenWrapperDownloader.java index e76d1f3..2281075 100644 --- a/multibook/.mvn/wrapper/MavenWrapperDownloader.java +++ b/mivan/.mvn/wrapper/MavenWrapperDownloader.java @@ -1,5 +1,5 @@ /* - * Copyright 2007-present the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import java.util.Properties; public class MavenWrapperDownloader { - private static final String WRAPPER_VERSION = "0.5.6"; + private static final String WRAPPER_VERSION = "0.5.5"; /** * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. */ diff --git a/multibook/.mvn/wrapper/maven-wrapper.jar b/mivan/.mvn/wrapper/maven-wrapper.jar similarity index 91% rename from multibook/.mvn/wrapper/maven-wrapper.jar rename to mivan/.mvn/wrapper/maven-wrapper.jar index 2cc7d4a55c0cd0092912bf49ae38b3a9e3fd0054..0d5e649888a4843c1520054d9672f80c62ebbb48 100644 G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similarity index 58% rename from multibook/.mvn/wrapper/maven-wrapper.properties rename to mivan/.mvn/wrapper/maven-wrapper.properties index 642d572..7d59a01 100644 --- a/multibook/.mvn/wrapper/maven-wrapper.properties +++ b/mivan/.mvn/wrapper/maven-wrapper.properties @@ -1,2 +1,2 @@ -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip -wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.2/apache-maven-3.6.2-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar diff --git a/multibook/mvnw b/mivan/mvnw similarity index 98% rename from multibook/mvnw rename to mivan/mvnw index a16b543..21d3ee8 100755 --- a/multibook/mvnw +++ b/mivan/mvnw @@ -19,7 +19,7 @@ # ---------------------------------------------------------------------------- # ---------------------------------------------------------------------------- -# Maven Start Up Batch script +# Maven2 Start Up Batch script # # Required ENV vars: # ------------------ @@ -212,9 +212,9 @@ else echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." fi if [ -n "$MVNW_REPOURL" ]; then - jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar" else - jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar" fi while IFS="=" read key value; do case "$key" in (wrapperUrl) jarUrl="$value"; break ;; @@ -246,7 +246,7 @@ else else curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f fi - + else if [ "$MVNW_VERBOSE" = true ]; then echo "Falling back to using Java to download" diff --git a/multibook/mvnw.cmd b/mivan/mvnw.cmd similarity index 97% rename from multibook/mvnw.cmd rename to mivan/mvnw.cmd index c8d4337..84d60ab 100644 --- a/multibook/mvnw.cmd +++ b/mivan/mvnw.cmd @@ -18,7 +18,7 @@ @REM ---------------------------------------------------------------------------- @REM ---------------------------------------------------------------------------- -@REM Maven Start Up Batch script +@REM Maven2 Start Up Batch script @REM @REM Required ENV vars: @REM JAVA_HOME - location of a JDK home dir @@ -26,7 +26,7 @@ @REM Optional ENV vars @REM M2_HOME - location of maven2's installed home dir @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands -@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven @REM e.g. to debug Maven itself, use @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 @@ -120,7 +120,7 @@ SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain -set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar" FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B @@ -134,7 +134,7 @@ if exist %WRAPPER_JAR% ( ) ) else ( if not "%MVNW_REPOURL%" == "" ( - SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar" ) if "%MVNW_VERBOSE%" == "true" ( echo Couldn't find %WRAPPER_JAR%, downloading it ... diff --git a/multibook/pom.xml b/mivan/pom.xml similarity index 56% rename from multibook/pom.xml rename to mivan/pom.xml index 4160597..4b75c2b 100644 --- a/multibook/pom.xml +++ b/mivan/pom.xml @@ -1,35 +1,43 @@ - 4.0.0 org.springframework.boot spring-boot-starter-parent - 2.2.2.RELEASE - + 2.2.1.RELEASE + - com.unimib.disco.mivan - multibook + mivan 0.0.1-SNAPSHOT - multibook - Project for book loan + mivan + Spring MVC and Hibernate project + UTF-8 + UTF-8 1.8 org.springframework.boot - spring-boot-starter-data-jpa + spring-boot-starter-thymeleaf - org.springframework.boot - spring-boot-devtools - runtime - true + spring-boot-starter-web + + org.hibernate + hibernate-core + + + org.hibernate + hibernate-entitymanager + + com.h2database h2 @@ -53,6 +61,10 @@ org.springframework.boot spring-boot-maven-plugin + + true + ${project.artifactId} + diff --git a/mivan/src/main/java/mivan/Mivan.java b/mivan/src/main/java/mivan/Mivan.java new file mode 100644 index 0000000..f8b851a --- /dev/null +++ b/mivan/src/main/java/mivan/Mivan.java @@ -0,0 +1,37 @@ +package mivan; + +import java.io.Console; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import mivan.model.User; +import mivan.repository.UserRepository; +import mivan.repository.UserRepositoryImpl; + +@SpringBootApplication +public class Mivan { + + public static void main(String[] args) { + + String firstName = "Paolo"; + String lastName = "Verdi"; + String city = "Milano"; + String username = "p.verdi1"; + String email = "p.verdi1@campus.unimib.it"; + String password = "pverdipassword"; + + User user = new User(firstName, lastName, city, username, email, password); + + + UserRepository userRepository = new UserRepositoryImpl(); + userRepository.addUser(user); + + int size = userRepository.getSize(); + + System.out.print(size); + + SpringApplication.run(Mivan.class, args); + } + +} diff --git a/mivan/src/main/java/mivan/model/Author.java b/mivan/src/main/java/mivan/model/Author.java new file mode 100644 index 0000000..3df1f6d --- /dev/null +++ b/mivan/src/main/java/mivan/model/Author.java @@ -0,0 +1,74 @@ +package mivan.model; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.Table; + +@Entity +@Table(name = "author") +public class Author implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", unique = true) + private long id; + + @Column(name = "name", nullable=false) + + private String name; + + @ManyToMany(mappedBy="authors") + private List books; + + public Author() {} + public Author(String name) { + super(); + this.name = name; + this.books = new ArrayList (); + } + + 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 List getBooks() { + return this.books; + } + + public void addBook(Book book) { + if (!this.books.contains(book)) { + this.books.add(book); + book.addAuthor(this); + } + } + + public void removeBook(Book book) { + if (this.books.contains(book)) + this.books.remove(book); + book.removeAuthor(this); + } + + +} diff --git a/mivan/src/main/java/mivan/model/Book.java b/mivan/src/main/java/mivan/model/Book.java new file mode 100644 index 0000000..3c75b49 --- /dev/null +++ b/mivan/src/main/java/mivan/model/Book.java @@ -0,0 +1,93 @@ +package mivan.model; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToMany; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "book") +public class Book implements Serializable { + + private static final long serialVersionUID = -5124436115031696628L; + + @Id + @Column(name = "isbn", nullable=false) + private int isbn; + + @Column(name = "title", nullable=false) + private String title; + + @OneToOne + private Book prequel; + + @ManyToMany + @JoinTable( + name = "book_has_author", + joinColumns=@JoinColumn(name="isbn", referencedColumnName="isbn"), + inverseJoinColumns=@JoinColumn(name="id", referencedColumnName="id") ) + private List authors; + + public Book() { + } + + public Book(int isbn, String title, Book prequel) { + this.isbn = isbn; + this.title = title; + this.prequel = prequel; + this.authors = new ArrayList(); + } + + public int getIsbn() { + return isbn; + } + + public String getTitolo() { + return title; + } + + public Book getPrequel() { + return prequel; + } + + public void setIsbn(int isbn) { + this.isbn = isbn; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setPrequel(Book prequel) { + this.prequel = prequel; + } + + public List getAuthors() { + return this.authors; + } + + public void addAuthor(Author author) { + + if (!this.authors.contains(author)) { + this.authors.add(author); + author.addBook(this); + } + } + + public void removeAuthor(Author author) { + if (this.authors.contains(author)) { + this.authors.remove(author); + author.removeBook(this); + } + + } + +} diff --git a/mivan/src/main/java/mivan/model/Location.java b/mivan/src/main/java/mivan/model/Location.java new file mode 100644 index 0000000..58246d9 --- /dev/null +++ b/mivan/src/main/java/mivan/model/Location.java @@ -0,0 +1,83 @@ +package mivan.model; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +@Entity +@Table(name = "location") +public class Location implements Serializable { + + private static final long serialVersionUID = -5124436115031696628L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", unique = true) + private Long id; + + @Column(name = "name", nullable = false, unique = true) + private String name; + + @Column(name = "adress", nullable = false) + private String adress; + + @OneToMany(mappedBy = "location") + private List staffs; + + protected Location() { + } + + public Location(String name, String adress) { + this.name = name; + this.adress = adress; + this.staffs = new ArrayList(); + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getAdress() { + return adress; + } + + public List getStaffs() { + return staffs; + } + + public void setId(Long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public void setAdress(String adress) { + this.adress = adress; + } + + public void setStaffs(List staffs) { + this.staffs = staffs; + } + + public void addStaff(Staff staff) { + this.staffs.add(staff); + if (staff.getLocation() != this) { + staff.setLocation(this); + } + } + +} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Person.java b/mivan/src/main/java/mivan/model/Person.java similarity index 70% rename from multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Person.java rename to mivan/src/main/java/mivan/model/Person.java index 7dd3068..9c9d46a 100644 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Person.java +++ b/mivan/src/main/java/mivan/model/Person.java @@ -1,15 +1,19 @@ -package com.unimib.disco.mivan.multibook.model; +package mivan.model; +import java.io.Serializable; import javax.persistence.*; @MappedSuperclass -public class Person { +public class Person implements Serializable { + private static final long serialVersionUID = -5124436115031696628L; + + @Id - @Column(name = "id", nullable=false) - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", unique = true) private long id; @Column(name = "firstName", nullable=false) @@ -63,6 +67,11 @@ public class Person { this.city = city; } + @Override + public String toString() { + return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", city=" + city + "]"; + } + } diff --git a/mivan/src/main/java/mivan/model/Staff.java b/mivan/src/main/java/mivan/model/Staff.java new file mode 100644 index 0000000..cbec79c --- /dev/null +++ b/mivan/src/main/java/mivan/model/Staff.java @@ -0,0 +1,71 @@ +package mivan.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "staff") +public class Staff extends Person { + + private static final long serialVersionUID = -5124436115031696628L; + + @Column(name = "idka", nullable = false, unique = true) + private String idka; + + @Column(name = "ruolo", nullable = false) + private String ruolo; + + @ManyToOne + @JoinColumn(name = "location_id", referencedColumnName = "id") + private Location location; + + public Staff() { + super(); + // TODO Auto-generated constructor stub + } + + public Staff(String firstName, String lastName, String city, String idka, String ruolo, Location location) { + super(firstName, lastName, city); + this.idka = idka; + this.ruolo = ruolo; + setLocation(location); + } + + public Staff(String firstName, String lastName, String city, String idka, String ruolo) { + super(firstName, lastName, city); + this.idka = idka; + this.ruolo = ruolo; + + } + + public String getIdka() { + return idka; + } + + public String getRuolo() { + return ruolo; + } + + public Location getLocation() { + return location; + } + + public void setIdka(String idka) { + this.idka = idka; + } + + public void setRuolo(String ruolo) { + this.ruolo = ruolo; + } + + public void setLocation(Location location) { + this.location = location; + if (!location.getStaffs().contains(this)) { + location.getStaffs().add(this); + } + } + +} diff --git a/mivan/src/main/java/mivan/model/User.java b/mivan/src/main/java/mivan/model/User.java new file mode 100644 index 0000000..3b4bdec --- /dev/null +++ b/mivan/src/main/java/mivan/model/User.java @@ -0,0 +1,90 @@ +package mivan.model; + + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +@Entity +@Table(name = "user") +public class User extends Person { + + private static final long serialVersionUID = -5124436115031696628L; + + @Column(name = "username", nullable=false,unique=true) + private String username; + + @Column(name = "email", nullable=false,unique=true) + private String email; + + @Column(name = "password", nullable=false) + private String password; + + + protected User() { + } + + + +public User(String firstName, String lastName, String city,String username, String email, String password) { + super(firstName,lastName,city); + this.username = username; + this.email = email; + this.password = password; +} + + + +public String getUsername() { + return username; +} + + + +public void setUsername(String username) { + this.username = username; +} + + + +public String getEmail() { + return email; +} + + + +public void setEmail(String email) { + this.email = email; +} + + + +public String getPassword() { + return password; +} + + + +public void setPassword(String password) { + this.password = password; +} + + + +public static long getSerialversionuid() { + return serialVersionUID; +} + + + +@Override +public String toString() { + return "User [username=" + username + ", email=" + email + ", password=" + password + "]"; +} + + + + + + +} diff --git a/mivan/src/main/java/mivan/repository/AuthorRepository.java b/mivan/src/main/java/mivan/repository/AuthorRepository.java new file mode 100644 index 0000000..ab2b72a --- /dev/null +++ b/mivan/src/main/java/mivan/repository/AuthorRepository.java @@ -0,0 +1,20 @@ +package mivan.repository; + +import mivan.model.Author; + +public interface AuthorRepository extends Repository { + + public void deleteAuthorById(Long id); + + public void addAuthor(Author author); + + public void updateAuthor(Long id, String name); + + public Author searchAuthorByName(String name); + + public void addBook(int isbn, long id); + + public void removeBook(int isbn, long id); + + public int getSize(); +} diff --git a/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java b/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java new file mode 100644 index 0000000..0e5086a --- /dev/null +++ b/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java @@ -0,0 +1,156 @@ +package mivan.repository; + +import java.util.List; +import java.util.Optional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import mivan.model.Author; +import mivan.model.Book; + +public class AuthorRepositoryImpl implements AuthorRepository { + + private EntityManagerFactory entityManagerFactory; + + public AuthorRepositoryImpl() { + this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); + } + + @Override + public Optional findById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + Author author = entityManager.find(Author.class, id); + entityManager.close(); + return Optional.ofNullable(author); + } + + @Override + public Iterable findAll() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + List author = entityManager.createQuery("FROM Author", Author.class).getResultList(); + entityManager.close(); + return author; + } + + @Override + public void deleteAuthorById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Author author = entityManager.find(Author.class, id); + entityManager.remove(author); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void addAuthor(Author author) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + entityManager.persist(author); + entityManager.getTransaction().commit(); + entityManager.close(); + + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void updateAuthor(Long id, String name) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Author author = entityManager.find(Author.class, id); + author.setName(name); + entityManager.persist(author); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public Author searchAuthorByName(String name) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + + Author author = null; + try { + author = (Author) entityManager.createQuery("FROM Author u WHERE lower(u.name) = '" + name.toLowerCase() + "'") + .getSingleResult(); + + entityManager.close(); + } catch (Exception ex) { + author = null; + } + + return author; + } + + @Override + public int getSize() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + int size = 0; + try { + size = entityManager.createQuery("FROM Author").getResultList().size(); + + entityManager.close(); + } catch (Exception ex) { + size = 0; + } + return size; + } + + @Override + public void addBook(int isbn, long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Book book = entityManager.find(Book.class, isbn); + Author author = entityManager.find(Author.class, id); + book.addAuthor(author); + entityManager.persist(book); + entityManager.persist(author); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void removeBook(int isbn, long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Book book = entityManager.find(Book.class, isbn); + Author author = entityManager.find(Author.class, id); + book.removeAuthor(author); + entityManager.persist(book); + entityManager.persist(author); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + +} diff --git a/mivan/src/main/java/mivan/repository/BookRepository.java b/mivan/src/main/java/mivan/repository/BookRepository.java new file mode 100644 index 0000000..63f0465 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/BookRepository.java @@ -0,0 +1,24 @@ +package mivan.repository; + +import mivan.model.Author; +import mivan.model.Book; + +public interface BookRepository extends Repository{ + + //public Optional findUserAddressById(Long id); + + public void deleteBookById(int isbn); + + public void addBook(Book book); + + public void updateBook(int isbn, String title, Book prequel); + + public Book searchBookByTitle(String title); + + public void addAuthor(int isbn, long id); + + public void removeAuthor(int isbn, long id); + + public int getSize(); +} + diff --git a/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java b/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java new file mode 100644 index 0000000..72cd1b7 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java @@ -0,0 +1,157 @@ +package mivan.repository; + +import java.util.List; +import java.util.Optional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import mivan.model.Author; +import mivan.model.Book; + +public class BookRepositoryImpl implements BookRepository { + + private EntityManagerFactory entityManagerFactory; + + @Override + public Optional findById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + Book book = entityManager.find(Book.class, id); + entityManager.close(); + return Optional.ofNullable(book); + } + + @Override + public Iterable findAll() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + List book = entityManager.createQuery("FROM Book", Book.class).getResultList(); + entityManager.close(); + return book; + } + + @Override + public void deleteBookById(int isbn) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Book book = entityManager.find(Book.class, isbn); + entityManager.remove(book); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void addBook(Book book) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + entityManager.persist(book); + entityManager.getTransaction().commit(); + entityManager.close(); + + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void updateBook(int isbn, String title, Book prequel) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Book book = entityManager.find(Book.class, isbn); + book.setTitle(title); + entityManager.persist(book); + if (prequel != null) { + book.setPrequel(prequel); + } + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public Book searchBookByTitle(String title) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + + Book book = null; + try { + book = (Book) entityManager.createQuery("FROM Book u WHERE lower(u.title) = '" + title.toLowerCase() + "'") + .getSingleResult(); + + entityManager.close(); + } catch (Exception ex) { + book = null; + } + + return book; + } + + @Override + public int getSize() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + int size = 0; + try { + size = entityManager.createQuery("FROM Book").getResultList().size(); + + entityManager.close(); + } catch (Exception ex) { + size = 0; + } + return size; + } + + + + @Override + public void addAuthor(int isbn, long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Book book = entityManager.find(Book.class, isbn); + Author author = entityManager.find(Author.class, id); + book.addAuthor(author); + entityManager.persist(book); + entityManager.persist(author); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void removeAuthor(int isbn, long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Book book = entityManager.find(Book.class, isbn); + Author author = entityManager.find(Author.class, id); + book.removeAuthor(author); + entityManager.persist(book); + entityManager.persist(author); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + + +} diff --git a/mivan/src/main/java/mivan/repository/LocationRepository.java b/mivan/src/main/java/mivan/repository/LocationRepository.java new file mode 100644 index 0000000..2980213 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/LocationRepository.java @@ -0,0 +1,20 @@ +package mivan.repository; + +import java.util.List; + +import mivan.model.Location; +import mivan.model.Staff; + +public interface LocationRepository extends Repository { + + public void deleteLocationById(Long id); + + public void addLocation(Location user); + + public void updateLocation(Long id, String name, String adress, List staffs); + + public Location searchLocationByName(String name); + + public int getSize(); + +} diff --git a/mivan/src/main/java/mivan/repository/LocationRepositoryImpl.java b/mivan/src/main/java/mivan/repository/LocationRepositoryImpl.java new file mode 100644 index 0000000..a60cadc --- /dev/null +++ b/mivan/src/main/java/mivan/repository/LocationRepositoryImpl.java @@ -0,0 +1,114 @@ +package mivan.repository; + +import java.util.List; +import java.util.Optional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import mivan.model.Location; +import mivan.model.Staff; + +public class LocationRepositoryImpl implements LocationRepository { + + private EntityManagerFactory entityManagerFactory; + + public LocationRepositoryImpl() { + this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); + } + + @Override + public Optional findById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + Location location = entityManager.find(Location.class, id); + entityManager.close(); + return Optional.ofNullable(location); + } + + @Override + public Iterable findAll() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + List location = entityManager.createQuery("FROM Location", Location.class).getResultList(); + entityManager.close(); + return location; + } + + public void deleteLocationById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Location location = entityManager.find(Location.class, id); + entityManager.remove(location); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + public void addLocation(Location location) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + entityManager.persist(location); + entityManager.getTransaction().commit(); + entityManager.close(); + + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + public void updateLocation(Long id, String name, String adress, List staffs) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Location location = entityManager.find(Location.class, id); + location.setName(name); + location.setAdress(adress); + location.setStaffs(staffs); + entityManager.persist(location); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + public Location searchLocationByName(String name) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + + Location location = null; + try { + location = (Location) entityManager + .createQuery("FROM Location u WHERE lower(u.name) = '" + name.toLowerCase() + "'") + .getSingleResult(); + + entityManager.close(); + } catch (Exception ex) { + location = null; + } + + return location; + } + + public int getSize() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + int size = 0; + try { + size = entityManager.createQuery("FROM Location").getResultList().size(); + + entityManager.close(); + } catch (Exception ex) { + size = 0; + } + return size; + } +} diff --git a/mivan/src/main/java/mivan/repository/Repository.java b/mivan/src/main/java/mivan/repository/Repository.java new file mode 100644 index 0000000..8c14a0f --- /dev/null +++ b/mivan/src/main/java/mivan/repository/Repository.java @@ -0,0 +1,8 @@ +package mivan.repository; + +import java.util.Optional; + +public interface Repository { + Optional findById(ID id); + Iterable findAll(); +} diff --git a/mivan/src/main/java/mivan/repository/StaffRepository.java b/mivan/src/main/java/mivan/repository/StaffRepository.java new file mode 100644 index 0000000..651b5c6 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/StaffRepository.java @@ -0,0 +1,21 @@ +package mivan.repository; + +import mivan.model.Location; +import mivan.model.Staff; + + +public interface StaffRepository extends Repository{ + + public void deleteStaffById(Long id); + + public void addStaff(Staff user); + + public void updateStaff(Long id, String firstname, String lastname, String city, String idka, String ruolo, Location location); + + public Staff searchStaffByidka(String idka); + + //public List searchStaffByRole(String role); + + + public int getSize(); +} diff --git a/mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java b/mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java new file mode 100644 index 0000000..ae8df0c --- /dev/null +++ b/mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java @@ -0,0 +1,118 @@ +package mivan.repository; + +import java.util.List; +import java.util.Optional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import mivan.model.Location; +import mivan.model.Staff; + +public class StaffRepositoryImpl implements StaffRepository{ + + private EntityManagerFactory entityManagerFactory; + + public StaffRepositoryImpl() { + this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); + } + + @Override + public Optional findById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + Staff staff = entityManager.find(Staff.class, id); + entityManager.close(); + return Optional.ofNullable(staff); + } + + + @Override + public Iterable findAll() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + List staffs = entityManager.createQuery("FROM Staff", Staff.class).getResultList(); + entityManager.close(); + return staffs; + } + + public void deleteStaffById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Staff staff = entityManager.find(Staff.class, id); + entityManager.remove(staff); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + public void addStaff(Staff staff) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + entityManager.persist(staff); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + + public void updateStaff(Long id, String firstname, String lastname, String city,String idka,String ruolo,Location location) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Staff staff = entityManager.find(Staff.class, id); + staff.setFirstName(firstname); + staff.setLastName(lastname); + staff.setCity(city); + staff.setIdka(idka); + staff.setRuolo(ruolo); + staff.setLocation(location); + + entityManager.persist(staff); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + public Staff searchStaffByidka(String idka) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + + Staff staff = null; + try { + staff = (Staff) entityManager.createQuery("FROM Staff u WHERE lower(u.idka) = '" + idka.toLowerCase() + "'") + .getSingleResult(); + + entityManager.close(); + } catch (Exception ex) { + staff = null; + } + + return staff; + } + + public int getSize() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + int size = 0; + try { + size = entityManager.createQuery("FROM Staff").getResultList().size(); + + entityManager.close(); + } catch (Exception ex) { + size = 0; + } + return size; + } +} diff --git a/mivan/src/main/java/mivan/repository/UserRepository.java b/mivan/src/main/java/mivan/repository/UserRepository.java new file mode 100644 index 0000000..f040321 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/UserRepository.java @@ -0,0 +1,21 @@ +package mivan.repository; + +import mivan.model.User; + +public interface UserRepository extends Repository{ + + //public Optional findUserAddressById(Long id); + + public void deleteUserById(Long id); + + public void addUser(User user); + + public void updateUser(Long id, String firstname, String lastname, String city,String username, String email, String password); + + public User searchUserByName(String username); + + public User searchUserByEmail(String email); + + + public int getSize(); +} diff --git a/mivan/src/main/java/mivan/repository/UserRepositoryImpl.java b/mivan/src/main/java/mivan/repository/UserRepositoryImpl.java new file mode 100644 index 0000000..6ce5cd8 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/UserRepositoryImpl.java @@ -0,0 +1,133 @@ +package mivan.repository; + +import java.util.List; +import java.util.Optional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + + +import mivan.model.User; + +public class UserRepositoryImpl implements UserRepository { + + private EntityManagerFactory entityManagerFactory; + + public UserRepositoryImpl() { + this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); + } + + @Override + public Optional findById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + User user = entityManager.find(User.class, id); + entityManager.close(); + return Optional.ofNullable(user); + } + + @Override + public Iterable findAll() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + List users = entityManager.createQuery("FROM User", User.class).getResultList(); + entityManager.close(); + return users; + } + + public void deleteUserById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + User user = entityManager.find(User.class, id); + entityManager.remove(user); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + public void addUser(User user) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + entityManager.persist(user); + entityManager.getTransaction().commit(); + entityManager.close(); + + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + + public void updateUser(Long id, String firstname, String lastname, String city,String username, String email, String password) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + User user = entityManager.find(User.class, id); + user.setFirstName(firstname); + user.setLastName(lastname); + user.setCity(city); + user.setUsername(username); + user.setEmail(email); + user.setPassword(password); + entityManager.persist(user); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + public User searchUserByName(String username) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + + User user = null; + try { + user = (User) entityManager.createQuery("FROM User u WHERE lower(u.username) = '" + username.toLowerCase() + "'") + .getSingleResult(); + + entityManager.close(); + } catch (Exception ex) { + user = null; + } + + return user; + } + + public User searchUserByEmail(String email) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + + User user = null; + try { + user = (User) entityManager.createQuery("FROM User u WHERE lower(u.email) = '" + email.toLowerCase() + "'") + .getSingleResult(); + + entityManager.close(); + } catch (Exception ex) { + user = null; + } + + return user; + } + + public int getSize() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + int size = 0; + try { + size = entityManager.createQuery("FROM User").getResultList().size(); + + entityManager.close(); + } catch (Exception ex) { + size = 0; + } + return size; + } +} diff --git a/mivan/src/main/resources/META-INF/persistence.xml b/mivan/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..fec0853 --- /dev/null +++ b/mivan/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,27 @@ + + + org.hibernate.jpa.HibernatePersistenceProvider + + + + + + + + + + + + + + + diff --git a/mivan/src/main/resources/application.properties b/mivan/src/main/resources/application.properties new file mode 100644 index 0000000..a860a0d --- /dev/null +++ b/mivan/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.application.name=Mivan +spring.datasource.url=jdbc:h2:file:./data/demo \ No newline at end of file diff --git a/mivan/src/test/java/mivan/AuthorBookTest.java b/mivan/src/test/java/mivan/AuthorBookTest.java new file mode 100644 index 0000000..3500557 --- /dev/null +++ b/mivan/src/test/java/mivan/AuthorBookTest.java @@ -0,0 +1,30 @@ +package mivan; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Optional; + +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; + +import mivan.model.Author; +import mivan.repository.AuthorRepository; +import mivan.repository.AuthorRepositoryImpl; +import mivan.repository.BookRepository; +import mivan.repository.BookRepositoryImpl; + +@TestMethodOrder(OrderAnnotation.class) +public class AuthorBookTest { + + private AuthorRepository authorRepository = new AuthorRepositoryImpl(); + + private BookRepository bookRepository = new BookRepositoryImpl(); + + + + + + +} diff --git a/mivan/src/test/java/mivan/AuthorTest.java b/mivan/src/test/java/mivan/AuthorTest.java new file mode 100644 index 0000000..197309a --- /dev/null +++ b/mivan/src/test/java/mivan/AuthorTest.java @@ -0,0 +1,77 @@ +package mivan; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Optional; + +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; + +import mivan.model.Author; +import mivan.repository.AuthorRepository; +import mivan.repository.AuthorRepositoryImpl; + +@TestMethodOrder(OrderAnnotation.class) +public class AuthorTest { + + private AuthorRepository authorRepository = new AuthorRepositoryImpl(); + + + @Test + @Order(1) + void testAddAuthor() { + + + String name = "Ivan"; + + Author author = new Author(name); + + authorRepository.addAuthor(author); + + int size = authorRepository.getSize(); + + + assertEquals(size, 1); + } + + @Test + @Order(2) + void testUpdateAuthor() { + + String name = "Ivan"; + + Author author = new Author(name); + + authorRepository.addAuthor(author); + + String updatedName ="Javoso"; + + authorRepository.updateAuthor(author.getId(), updatedName); + Optional updated_author = authorRepository.findById(author.getId()); + updated_author.ifPresent(a -> {assertEquals(a.getName(), updatedName);}); + + } + + @Test + @Order(3) + void testDeleteUser() { + String name = "ivan"; + + Author author = new Author(name); + + authorRepository.addAuthor(author); + + Author editAuthor = authorRepository.searchAuthorByName(name); + + long ID = editAuthor.getId(); + + authorRepository.deleteAuthorById(ID); + + int size = authorRepository.getSize(); + assertEquals(size, 0); + } + + +} diff --git a/mivan/src/test/java/mivan/BookTest.java b/mivan/src/test/java/mivan/BookTest.java new file mode 100644 index 0000000..c127605 --- /dev/null +++ b/mivan/src/test/java/mivan/BookTest.java @@ -0,0 +1,41 @@ +package mivan; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import mivan.model.Author; +import mivan.model.Book; +import mivan.model.User; +import mivan.repository.BookRepository; +import mivan.repository.BookRepositoryImpl; + +public class BookTest { + + + private BookRepository bookRepository = new BookRepositoryImpl(); + + + @Test + @Order(1) + void testAddBook() { + + int isbn = 1234; + String title = "illiade"; + Book prequel = null; + + Book book = new Book(isbn, title, prequel); + + bookRepository.addBook(book); + + int size = bookRepository.getSize(); + + + assertEquals(size, 1); + + } + + + +} diff --git a/mivan/src/test/java/mivan/LocationTest.java b/mivan/src/test/java/mivan/LocationTest.java new file mode 100644 index 0000000..4202fa8 --- /dev/null +++ b/mivan/src/test/java/mivan/LocationTest.java @@ -0,0 +1,116 @@ +package mivan; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; + +import mivan.model.Location; +import mivan.model.Staff; +import mivan.repository.LocationRepository; +import mivan.repository.LocationRepositoryImpl; +import mivan.repository.StaffRepository; +import mivan.repository.StaffRepositoryImpl; + +@TestMethodOrder(OrderAnnotation.class) +public class LocationTest { + + private StaffRepository staffRepository = new StaffRepositoryImpl(); + private LocationRepository locationRepository = new LocationRepositoryImpl(); + + @Test + @Order(1) + void testAddLocation() { + + String lname = "Library bria 2"; + String ladress = "via chissa dove2"; + Location location = new Location(lname, ladress); + + int size = locationRepository.getSize(); + + locationRepository.addLocation(location); + + int newsize = locationRepository.getSize(); + + assertEquals(newsize, size + 1); + } + + @Test + @Order(2) + void testUpdateLocation() { + + // creo nuovo menìmbro dello staff + String firstname = "Ivan"; + String lastname = "Donati"; + String city = "Milano"; + String idka = "lihwelhqluchwlir8"; + String ruolo = "frontoffice"; + Staff newstaff1 = new Staff(firstname, lastname, city, idka, ruolo); + + String firstname2 = "Rossi"; + String lastname2 = "Donati"; + String city2 = "Milano"; + String idka2 = "lihwelhsfgdghhwlir8"; + String ruolo2 = "manager"; + Staff newstaff2 = new Staff(firstname2, lastname2, city2, idka2, ruolo2); + + staffRepository.addStaff(newstaff1); + staffRepository.addStaff(newstaff2); + + List staffs = new ArrayList(); + staffs.add(newstaff1); + staffs.add(newstaff2); + + // creo una locazione + String lname = "Library cosa 2"; + String ladress = "via chissa dove"; + Location newlocation = new Location(lname, ladress); + locationRepository.addLocation(newlocation); + + Location editLocation = locationRepository.searchLocationByName(lname); + + long id = editLocation.getId(); + // nuovi dati per modificare i vecchi + String newlName = "Library bria 5"; + String newlAdress = "via prossima stella"; + + // aggungo i membri dello staff / modifico i dati della location + locationRepository.updateLocation(id, newlName, newlAdress, staffs); + + Optional location = locationRepository.findById(id); + location.ifPresent(a -> { + assertEquals(a.getName(), newlName); + assertEquals(a.getAdress(), newlAdress); + assertEquals(a.getStaffs(), staffs); + }); + } + + @Test + @Order(3) + void testDeleteLocation() { + + String lname = "Library bria 9"; + String ladress = "via chissa dove9"; + Location location = new Location(lname, ladress); + locationRepository.addLocation(location); + + Location removeLocation = locationRepository.searchLocationByName(lname); + + long ID = removeLocation.getId(); + + int size = locationRepository.getSize(); + + locationRepository.deleteLocationById(ID); + + int newsize = locationRepository.getSize(); + + assertEquals(newsize, size -1); + } + +} diff --git a/mivan/src/test/java/mivan/StaffTest.java b/mivan/src/test/java/mivan/StaffTest.java new file mode 100644 index 0000000..5920453 --- /dev/null +++ b/mivan/src/test/java/mivan/StaffTest.java @@ -0,0 +1,146 @@ +package mivan; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Optional; + +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; + +import mivan.model.Location; +import mivan.model.Staff; +import mivan.repository.LocationRepository; +import mivan.repository.LocationRepositoryImpl; +import mivan.repository.StaffRepository; +import mivan.repository.StaffRepositoryImpl; + +@TestMethodOrder(OrderAnnotation.class) +public class StaffTest { + + private StaffRepository staffRepository = new StaffRepositoryImpl(); + private LocationRepository locationRepository = new LocationRepositoryImpl(); + + @Test + @Order(1) + void testAddStaff() { + + String firstname = "Ivan"; + String lastname = "Donati"; + String city = "Milano"; + String idka = "lihwelhqluchwlir8"; + String ruolo = "frontoffice"; + + String lname = "Library bria 2"; + String ladress = "via chissa dove"; + Location location = new Location(lname, ladress); + locationRepository.addLocation(location); + + Staff newstaff = new Staff(firstname, lastname, city, idka, ruolo, location); + + int size = staffRepository.getSize(); + + staffRepository.addStaff(newstaff); + + int newsize = staffRepository.getSize(); + + assertEquals(newsize, size + 1); + + Iterable sad2 = staffRepository.findAll(); + } + + @Test + @Order(2) + void testUpdateStaff() { + + // creo una locazione + String lname = "Library cosa 2"; + String ladress = "via chissa dove"; + Location location = new Location(lname, ladress); + + // creo una seconda locazione + String newlName = "Library destra 5"; + String newlAdress = "via prossima stella"; + Location newlocation = new Location(newlName, newlAdress); + + int losize = locationRepository.getSize(); + + locationRepository.addLocation(location); + locationRepository.addLocation(newlocation); + + int newlosize = locationRepository.getSize(); + + assertEquals(newlosize, losize + 2); // ci sono 2 locazioni aggunte + + // creo un membro dello staff + String firstname = "Maccio"; + String lastname = "Donati"; + String city = "Milano"; + String idka = "lihwelhqluchwlir8"; + String ruolo = "frontoffice"; + Staff newstaff = new Staff(firstname, lastname, city, idka, ruolo); + + int size = staffRepository.getSize(); + + staffRepository.addStaff(newstaff); + + int newsize = staffRepository.getSize(); + + assertEquals(newsize, size + 1); // c'è 1 nuovo membro dello staff + + Staff editStaff = staffRepository.searchStaffByidka(idka); + + long id = editStaff.getId(); + String newfirstname = "Ivans"; + String newlastname = "Donatis"; + String newcity = "Milanos"; + String newidka = "lihwelhkluchwlir5"; + String newruolo = "spazzino"; + + staffRepository.updateStaff(id, newfirstname, newlastname, newcity, newidka, newruolo, newlocation); + + Optional staff = staffRepository.findById(id); + staff.ifPresent(a -> { + assertEquals(a.getFirstName(), newfirstname); + assertEquals(a.getLastName(), newlastname); + assertEquals(a.getCity(), newcity); + assertEquals(a.getIdka(), newidka); + assertEquals(a.getRuolo(), newruolo); + assertEquals(a.getLocation().getName(), newlName); + assertEquals(a.getLocation().getAdress(), newlAdress); + + }); + } + + @Test + @Order(3) + void testDeleteStaff() { + + String firstname = "John"; + String lastname = "Donati"; + String city = "Milano"; + String idka = "lihwelhsfghhwlir8"; + String ruolo = "manager"; + + String lname = "Library siniz 2"; + String ladress = "via sassa dove"; + Location location = new Location(lname, ladress); + locationRepository.addLocation(location); + + Staff newstaff = new Staff(firstname, lastname, city, idka, ruolo, location); + + staffRepository.addStaff(newstaff); + + Staff editStaff = staffRepository.searchStaffByidka(idka); + + long ID = editStaff.getId(); + + staffRepository.deleteStaffById(ID); + ; + + int size = staffRepository.getSize(); + assertEquals(size, 0); + } + +} diff --git a/mivan/src/test/java/mivan/UserTest.java b/mivan/src/test/java/mivan/UserTest.java new file mode 100644 index 0000000..d7efa13 --- /dev/null +++ b/mivan/src/test/java/mivan/UserTest.java @@ -0,0 +1,109 @@ +package mivan; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Optional; + +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; + +import mivan.model.User; +import mivan.repository.UserRepository; +import mivan.repository.UserRepositoryImpl; + +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +@TestMethodOrder(OrderAnnotation.class) +class UserTest { + + private UserRepository userRepository = new UserRepositoryImpl(); + + @Test + @Order(1) + void testAddUser() { + + String firstname = "Ivan"; + String lastname = "Donati"; + String city = "Milano"; + String username = "p.donati"; + String email = "p.donati@campus.unimib.it"; + String password = "pdonatipassword"; + User user = new User(firstname, lastname, city, username, email, password); + + int size = userRepository.getSize(); + + userRepository.addUser(user); + + int newsize = userRepository.getSize(); + + assertEquals(newsize, size + 1); + } + + @Test + @Order(2) + void testUpdateCompany() { + + String firstname = "Simone"; + String lastname = "Donati"; + String city = "Milano"; + String username = "i.donati"; + String email = "i.donati@campus.unimib.it"; + String password = "passwordsicura"; + + User newuser = new User(firstname, lastname, city, username, email, password); + + userRepository.addUser(newuser); + + User editUser = userRepository.searchUserByName(username); + + long id = editUser.getId(); + + String newfirstname = "Ivans"; + String newlastname = "Donatis"; + String newcity = "Milanos"; + String newusername = "asd.donati1s"; + String newemail = "asd.donati@campus.unimib.com"; + String newpassword = "newpdonatipassword"; + + userRepository.updateUser(id, newfirstname, newlastname, newcity, newusername, newemail, newpassword); + + Optional user = userRepository.findById(id); + user.ifPresent(a -> { + assertEquals(a.getFirstName(), newfirstname); + assertEquals(a.getLastName(), newlastname); + assertEquals(a.getCity(), newcity); + assertEquals(a.getUsername(), newusername); + assertEquals(a.getEmail(), newemail); + assertEquals(a.getPassword(), newpassword); + + }); + } + + @Test + @Order(3) + void testDeleteUser() { + + String firstname = "Rossi"; + String lastname = "Donati"; + String city = "Milano"; + String username = "r.donati"; + String email = "r.donati@campus.unimib.it"; + String password = "rdonatipassword"; + + User newuser = new User(firstname, lastname, city, username, email, password); + + userRepository.addUser(newuser); + + User editUser = userRepository.searchUserByName(username); + + long ID = editUser.getId(); + + int size = userRepository.getSize(); + + userRepository.deleteUserById(ID); + + int newsize = userRepository.getSize(); + assertEquals(newsize, size - 1); + } +} diff --git a/multibook/database.mv.db b/multibook/database.mv.db deleted file mode 100644 index c151f91106df7a8dad945c7742b38deda1813853..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeHPTXW*b6*d`2ZJ71WvO9T5Rd!3O$*w&KA-Z396CjKN21W>bsH8#(5WBANfXl|4 zsXXR2Z+S>kwUvL6%0J0N{!l7Mty?1yCR3i7OmcW=OKPe6^mkhAbIx~Kt*TI-TrQ_C zs#5TGaPd2@%5xl7o_=$?cv0om^7PVk7gfGIz4qLdH>>i>T$emin=4#-zPx_vuAuT; z@A~Fq3Ad@``K7m-e&bzNMQ$ccRZb`#;13Z%1P}p401-e05CKF05kLeG0Ym^1Km^_) z0xk6a-y! zUsWZ@1Xy^lR*)ZX<$mqmK;`vvxvI(<f$Bj3rF?a_YSQX{2KN|%%qQcg)Z zBjx;X;lA`L7mJzqs=^CvXVTi4x^||2NS?b~P8#mYeHfjbx>xbB?ahBkt}m~=g>&sL zZrte#vM6qXM_2CT(yMUviA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ABWRp1jKX@ZSb0P_FH|0Dl@SMCDx z|49Y|c!$XUBmbY7Z`5pO68G<#|DQa1LQzvmYHCDH1zD&_O|4N=SvjivQ&U04Vah9s zscS-HgO9S25UM0R`+JnH8Z&3<^zFUV0QOAk4)EFsJv!?f$*TJ%tKuvT_nsW;VwfzR z%ITFdb9%jp;IIa%BE0){wkTjPhyWsh2p|H803v`0AOeU0B7g`W0*JtSk3h)(C;WTN z|5M-5*A;1^^X}xHoPMgADE~hlcuVpBdu;-bC8r~3RA2Xe81VlR^8c_M5&3`Q|6xbC zzq1?p|M<%Qo0%85|3921xc@(6tM(lmjxqlq^Z#-Ge~`_;gM3eR&2Rh42>C;R_JQPVv4zLNf_5JK_arbjMN diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/MultibookApplication.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/MultibookApplication.java deleted file mode 100644 index 34fb121..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/MultibookApplication.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.unimib.disco.mivan.multibook; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class MultibookApplication { - - public static void main(String[] args) { - SpringApplication.run(MultibookApplication.class, args); - } - -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/controller/UserController.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/controller/UserController.java deleted file mode 100644 index b6cb4d1..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/controller/UserController.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.unimib.disco.mivan.multibook.controller; - -import org.springframework.beans.factory.annotation.Autowired; - -import com.unimib.disco.mivan.multibook.model.User; -import com.unimib.disco.mivan.multibook.repository.UserRepository; - -public class UserController { - - @Autowired - UserRepository userRepository; - - - public void createUser(String firstName, String lastName, String city,String username, String email, String password) { - - User user = new User(firstName, lastName, city,username,email,password); - - userRepository.save(user); - - - - - } -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Author.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Author.java deleted file mode 100644 index 25010b9..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Author.java +++ /dev/null @@ -1,55 +0,0 @@ -/*package com.unimib.disco.mivan.multibook.model; - -import java.util.List; - -import javax.persistence.*; - -public class Author { - - - @Id - @Column(name = "id", nullable=false) - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - @Column(name = "firstName", nullable=false) - private String artName; - - @ManyToMany - List publications; - - public Author() { - // TODO Auto-generated constructor stub - } - - public Author(String artName, List publications) { - super(); - this.artName = artName; - this.publications = publications; - } - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public String getArtName() { - return artName; - } - - public void setArtName(String artName) { - this.artName = artName; - } - - public List getPublications() { - return publications; - } - - public void setPublications(List publications) { - this.publications = publications; - } - -}*/ diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Book.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Book.java deleted file mode 100644 index af7820e..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Book.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.unimib.disco.mivan.multibook.model; - -import java.util.List; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.ManyToMany; -import javax.persistence.OneToOne; - -@Entity -public class Book { - - @Id - @Column(name = "isbn", nullable=false) - private int isbn; - - @Column(name = "titolo", nullable=false) - private String titolo; - - @OneToOne - private Book prequel; - /* - @ManyToMany - List authors; - */ - - public Book() { - // TODO Auto-generated constructor stub - } - - public Book(int isbn, String titolo, Book prequel/*, List authors*/) { - super(); - this.isbn = isbn; - this.titolo = titolo; - this.prequel = prequel; - //this.authors = authors; - } - - public int getIsbn() { - return isbn; - } - - public void setIsbn(int isbn) { - this.isbn = isbn; - } - - public String getTitolo() { - return titolo; - } - - public void setTitolo(String titolo) { - this.titolo = titolo; - } - - public Book getPrequel() { - return prequel; - } - - public void setPrequel(Book prequel) { - this.prequel = prequel; - } - - - - - -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Item.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Item.java deleted file mode 100644 index e020d43..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Item.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.unimib.disco.mivan.multibook.model; - -import javax.persistence.Column; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToOne; - -public class Item{ - - - @Id - @Column(name = "id", nullable=false) - @GeneratedValue(strategy = GenerationType.AUTO) - private int id; - - @ManyToOne - private Location location; - - @ManyToOne - private Book libro; - - - public Item() { - // TODO Auto-generated constructor stub - } - - - public Item(Book libro,Location location) { - this.libro = libro; - this.location=location; - // TODO Auto-generated constructor stub - } - - - public int getId() { - return id; - } - - - public void setId(int id) { - this.id = id; - } - - - public Location getLocation() { - return location; - } - - - public void setLocation(Location location) { - this.location = location; - } - - - public Book getLibro() { - return libro; - } - - - public void setLibro(Book libro) { - this.libro = libro; - } - -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Loan.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Loan.java deleted file mode 100644 index daa82e3..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Loan.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.unimib.disco.mivan.multibook.model; - -import java.util.Date; - -import javax.persistence.Column; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToOne; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; - -public class Loan { - - @Id - @Column(name = "id", nullable=false) - @GeneratedValue(strategy = GenerationType.AUTO) - private int id; - - - @ManyToOne - private Item item; - - @ManyToOne - private Staff staff; - - @ManyToOne - private User user; - - @Column(name = "start") - @Temporal(TemporalType.TIMESTAMP) - Date start; - - @Column(name = "end") - @Temporal(TemporalType.TIMESTAMP) - Date end; - - public Loan() { - // TODO Auto-generated constructor stub - } - - public Loan(int id, Item item, Staff staff, User user, Date start, Date end) { - super(); - this.item = item; - this.staff = staff; - this.user = user; - this.start = start; - this.end = end; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public Item getItem() { - return item; - } - - public void setItem(Item item) { - this.item = item; - } - - public Staff getStaff() { - return staff; - } - - public void setStaff(Staff staff) { - this.staff = staff; - } - - public User getUser() { - return user; - } - - public void setUser(User user) { - this.user = user; - } - - public Date getStart() { - return start; - } - - public void setStart(Date start) { - this.start = start; - } - - public Date getEnd() { - return end; - } - - public void setEnd(Date end) { - this.end = end; - } - - - -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Location.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Location.java deleted file mode 100644 index 4c6cd8c..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Location.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.unimib.disco.mivan.multibook.model; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -@Entity -public class Location { - - @Id - @Column(name = "id", nullable=false) - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - @Column(name = "name", nullable=false) - private String name; - - @Column(name = "address", nullable=false) - private String address; - - @Column(name = "city", nullable=false) - private String city; - - public Location() { - // TODO Auto-generated constructor stub - } - - public Location(String name, String address, String city) { - super(); - this.name = name; - this.address = address; - this.city = city; - } -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Staff.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Staff.java deleted file mode 100644 index e35f3b2..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Staff.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.unimib.disco.mivan.multibook.model; - -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.ManyToOne; - -@Entity -public class Staff extends User { - - @ManyToOne(fetch = FetchType.LAZY) - private Location library; - - - public Staff() { - // TODO Auto-generated constructor stub - } - - public Staff(String firstName, String lastName, String city, String username, String email, - String password, Location library) { - super(firstName, lastName, city, username, email, password); - this.library=library; - - // TODO Auto-generated constructor stub - } - -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/User.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/User.java deleted file mode 100644 index 3a46bcc..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/model/User.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.unimib.disco.mivan.multibook.model; - - -import javax.persistence.*; - -@Entity -public class User extends Person { - - - @Column(name = "username", nullable=false,unique=true) - private String username; - - @Column(name = "email", nullable=false,unique=true) - private String email; - - @Column(name = "password", nullable=false) - private String password; - - - public User() { - super(); - // TODO Auto-generated constructor stub - } - - public User(String firstName, String lastName, String city, String username, String email, String password) { - super(firstName, lastName, city); - this.username = username; - this.email = email; - this.password = password; - } - - - public String getUsername() { - return username; - } - - - public void setUsername(String username) { - this.username = username; - } - - - public String getEmail() { - return email; - } - - - public void setEmail(String email) { - this.email = email; - } - - - public String getPassword() { - return password; - } - - - public void setPassword(String password) { - this.password = password; - } - - - - - - -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/PersonRepository.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/PersonRepository.java deleted file mode 100644 index 6274ec5..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/PersonRepository.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.unimib.disco.mivan.multibook.repository; - -import java.util.List; - -import org.springframework.data.repository.CrudRepository; - -import com.unimib.disco.mivan.multibook.model.Person; - -public interface PersonRepository extends CrudRepository { - - List findByFirstNameAllIgnoreCase(String firstName); - - List findByLastNameAllIgnoreCase(String lastName); - - List findByirstNameAndLastNameAllIgnoreCase(String firstName, String lastName); - -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/UserRepository.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/UserRepository.java deleted file mode 100644 index d11e05a..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/UserRepository.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.unimib.disco.mivan.multibook.repository; - -import com.unimib.disco.mivan.multibook.model.User; - -import java.util.Optional; - - -public interface UserRepository extends PersonRepository { - - - public Optional findById(Long id); - - public User findByEmail(String email); - - public User findByUsername(String username); - - - -} diff --git a/multibook/src/main/java/com/unimib/disco/mivan/multibook/service/UserService.java b/multibook/src/main/java/com/unimib/disco/mivan/multibook/service/UserService.java deleted file mode 100644 index 5c2e712..0000000 --- a/multibook/src/main/java/com/unimib/disco/mivan/multibook/service/UserService.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.unimib.disco.mivan.multibook.service; - -import java.util.List; -import java.util.Optional; - -import com.unimib.disco.mivan.multibook.model.User; -import com.unimib.disco.mivan.multibook.repository.UserRepository; - -public class UserService { - - private UserRepository userRepository; - - public UserService(UserRepository userRepository) { - this.userRepository = userRepository; - } - /* - public List getClients(){ - return Lists.newArrayList(userRepository.findAll()); - }*/ - - public Optional getClient(Long id) { - return userRepository.findById(id); - } - - public List getClientsByNameAndSurname(String name, String surname) { - return userRepository.findByirstNameAndLastNameAllIgnoreCase(name, surname); - } - - - public void deleteClient(User client) { - userRepository.delete(client); - } - - public User insertOrUpdateClient(User client) { - return userRepository.save(client); - } -} diff --git a/multibook/src/main/resources/application.properties b/multibook/src/main/resources/application.properties deleted file mode 100644 index 5054168..0000000 --- a/multibook/src/main/resources/application.properties +++ /dev/null @@ -1,23 +0,0 @@ - - -spring.datasource.url=jdbc:h2:./database; -spring.datasource.driverClassName=org.h2.Driver -spring.datasource.username=librarymenagment -spring.datasource.password=password123! -spring.datasource.initialization-mode=always - -spring.h2.console.enabled=true -spring.h2.console.path=/h2 - - -spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=update -spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect - - - - -# Used for initialization of db. -# !!! Remove comment for initialize db first time. !!! -# spring.datasource.initialization-mode=always - diff --git a/multibook/src/test/java/com/unimib/disco/mivan/multibook/MultibookApplicationTests.java b/multibook/src/test/java/com/unimib/disco/mivan/multibook/MultibookApplicationTests.java deleted file mode 100644 index cdeb7a8..0000000 --- a/multibook/src/test/java/com/unimib/disco/mivan/multibook/MultibookApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.unimib.disco.mivan.multibook; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class MultibookApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/multibook/src/test/java/com/unimib/disco/mivan/multibook/UserTests.java b/multibook/src/test/java/com/unimib/disco/mivan/multibook/UserTests.java deleted file mode 100644 index 0aac3cb..0000000 --- a/multibook/src/test/java/com/unimib/disco/mivan/multibook/UserTests.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.unimib.disco.mivan.multibook; - - -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.springframework.boot.test.context.SpringBootTest; - -import com.unimib.disco.mivan.multibook.model.User; -import com.unimib.disco.mivan.multibook.service.UserService; - - - -@SpringBootTest -public class UserTests { - - - - //private UserRepository userRepository = new UserRepository(); - private UserService userRepository; - - @Test - void userCreation() { - - User asd = new User("Ivan", "Donati", "Novara", "territory", "ciao.iva@gmail.com", "sonounapassword"); - - userRepository.insertOrUpdateClient(asd); - - //userRepository.insertOrUpdateClient("Ivan", "Donati", "Novara", "territory", "ciao.iva@gmail.com", "sonounapassword"); - - - } - -} From 708184146f4bd4ebdedb89a76dd0bd25702dcd03 Mon Sep 17 00:00:00 2001 From: meliurwen Date: Sun, 12 Jan 2020 22:58:54 +0100 Subject: [PATCH 2/3] Test book fix --- mivan/src/main/java/mivan/model/Book.java | 12 ++++++------ .../java/mivan/repository/AuthorRepository.java | 4 ++-- .../mivan/repository/AuthorRepositoryImpl.java | 4 ++-- .../java/mivan/repository/BookRepository.java | 9 ++++----- .../java/mivan/repository/BookRepositoryImpl.java | 15 ++++++++++----- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/mivan/src/main/java/mivan/model/Book.java b/mivan/src/main/java/mivan/model/Book.java index 3c75b49..96e14b7 100644 --- a/mivan/src/main/java/mivan/model/Book.java +++ b/mivan/src/main/java/mivan/model/Book.java @@ -17,11 +17,11 @@ import javax.persistence.Table; @Table(name = "book") public class Book implements Serializable { - private static final long serialVersionUID = -5124436115031696628L; + private static final long serialVersionUID = 19L; @Id - @Column(name = "isbn", nullable=false) - private int isbn; + @Column(name = "isbn", nullable=false, unique=true) + private long isbn; @Column(name = "title", nullable=false) private String title; @@ -39,14 +39,14 @@ public class Book implements Serializable { public Book() { } - public Book(int isbn, String title, Book prequel) { + public Book(long isbn, String title, Book prequel) { this.isbn = isbn; this.title = title; this.prequel = prequel; this.authors = new ArrayList(); } - public int getIsbn() { + public long getIsbn() { return isbn; } @@ -58,7 +58,7 @@ public class Book implements Serializable { return prequel; } - public void setIsbn(int isbn) { + public void setIsbn(long isbn) { this.isbn = isbn; } diff --git a/mivan/src/main/java/mivan/repository/AuthorRepository.java b/mivan/src/main/java/mivan/repository/AuthorRepository.java index ab2b72a..b5fc0d2 100644 --- a/mivan/src/main/java/mivan/repository/AuthorRepository.java +++ b/mivan/src/main/java/mivan/repository/AuthorRepository.java @@ -12,9 +12,9 @@ public interface AuthorRepository extends Repository { public Author searchAuthorByName(String name); - public void addBook(int isbn, long id); + public void addBook(Long isbn, Long id); - public void removeBook(int isbn, long id); + public void removeBook(Long isbn, Long id); public int getSize(); } diff --git a/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java b/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java index 0e5086a..ea0d85b 100644 --- a/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java +++ b/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java @@ -115,7 +115,7 @@ public class AuthorRepositoryImpl implements AuthorRepository { } @Override - public void addBook(int isbn, long id) { + public void addBook(Long isbn, Long id) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { @@ -134,7 +134,7 @@ public class AuthorRepositoryImpl implements AuthorRepository { } @Override - public void removeBook(int isbn, long id) { + public void removeBook(Long isbn, Long id) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { diff --git a/mivan/src/main/java/mivan/repository/BookRepository.java b/mivan/src/main/java/mivan/repository/BookRepository.java index 63f0465..ba772ae 100644 --- a/mivan/src/main/java/mivan/repository/BookRepository.java +++ b/mivan/src/main/java/mivan/repository/BookRepository.java @@ -1,23 +1,22 @@ package mivan.repository; -import mivan.model.Author; import mivan.model.Book; public interface BookRepository extends Repository{ //public Optional findUserAddressById(Long id); - public void deleteBookById(int isbn); + public void deleteBookById(Long isbn); public void addBook(Book book); - public void updateBook(int isbn, String title, Book prequel); + public void updateBook(Long isbn, String title, Book prequel); public Book searchBookByTitle(String title); - public void addAuthor(int isbn, long id); + public void addAuthor(Long isbn, Long id); - public void removeAuthor(int isbn, long id); + public void removeAuthor(Long isbn, Long id); public int getSize(); } diff --git a/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java b/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java index 72cd1b7..4c0006e 100644 --- a/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java +++ b/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java @@ -5,6 +5,7 @@ import java.util.Optional; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; import mivan.model.Author; import mivan.model.Book; @@ -13,6 +14,10 @@ public class BookRepositoryImpl implements BookRepository { private EntityManagerFactory entityManagerFactory; + public BookRepositoryImpl(){ + this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); + } + @Override public Optional findById(Long id) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); @@ -30,7 +35,7 @@ public class BookRepositoryImpl implements BookRepository { } @Override - public void deleteBookById(int isbn) { + public void deleteBookById(Long isbn) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { @@ -46,7 +51,7 @@ public class BookRepositoryImpl implements BookRepository { } @Override - public void addBook(Book book) { + public void addBook(Book book) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { @@ -62,7 +67,7 @@ public class BookRepositoryImpl implements BookRepository { } @Override - public void updateBook(int isbn, String title, Book prequel) { + public void updateBook(Long isbn, String title, Book prequel) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { @@ -115,7 +120,7 @@ public class BookRepositoryImpl implements BookRepository { @Override - public void addAuthor(int isbn, long id) { + public void addAuthor(Long isbn, Long id) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { @@ -134,7 +139,7 @@ public class BookRepositoryImpl implements BookRepository { } @Override - public void removeAuthor(int isbn, long id) { + public void removeAuthor(Long isbn, Long id) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { From a62040d5f7aa104e6c919e26e9080ab27fa9ba5a Mon Sep 17 00:00:00 2001 From: meliurwen Date: Sun, 12 Jan 2020 23:23:45 +0100 Subject: [PATCH 3/3] Aggiornamento test Book e Author --- mivan/src/main/java/mivan/model/Book.java | 2 +- mivan/src/test/java/mivan/AuthorTest.java | 7 ++-- mivan/src/test/java/mivan/BookTest.java | 49 ++++++++++++++++++++++- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/mivan/src/main/java/mivan/model/Book.java b/mivan/src/main/java/mivan/model/Book.java index 96e14b7..0c00668 100644 --- a/mivan/src/main/java/mivan/model/Book.java +++ b/mivan/src/main/java/mivan/model/Book.java @@ -50,7 +50,7 @@ public class Book implements Serializable { return isbn; } - public String getTitolo() { + public String getTitle() { return title; } diff --git a/mivan/src/test/java/mivan/AuthorTest.java b/mivan/src/test/java/mivan/AuthorTest.java index 197309a..59002f2 100644 --- a/mivan/src/test/java/mivan/AuthorTest.java +++ b/mivan/src/test/java/mivan/AuthorTest.java @@ -57,7 +57,8 @@ public class AuthorTest { @Test @Order(3) void testDeleteUser() { - String name = "ivan"; + int size = authorRepository.getSize(); + String name = "gollum"; Author author = new Author(name); @@ -69,8 +70,8 @@ public class AuthorTest { authorRepository.deleteAuthorById(ID); - int size = authorRepository.getSize(); - assertEquals(size, 0); + int end_size = authorRepository.getSize(); + assertEquals(end_size, size); } diff --git a/mivan/src/test/java/mivan/BookTest.java b/mivan/src/test/java/mivan/BookTest.java index c127605..9ff3abe 100644 --- a/mivan/src/test/java/mivan/BookTest.java +++ b/mivan/src/test/java/mivan/BookTest.java @@ -1,6 +1,11 @@ package mivan; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Optional; + +import javax.validation.constraints.AssertTrue; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -21,7 +26,7 @@ public class BookTest { @Order(1) void testAddBook() { - int isbn = 1234; + long isbn = 1234; String title = "illiade"; Book prequel = null; @@ -36,6 +41,48 @@ public class BookTest { } + @Test + @Order(2) + void testUpdateBook(){ + long isbn = 12348; + String title = "odissea"; + Book prequel = null; + + Book book = new Book(isbn, title, prequel); + + bookRepository.addBook(book); + String update_title = "eneide"; + bookRepository.updateBook(isbn, update_title, prequel); + + Optional opt_book2 = bookRepository.findById(isbn); + + Book book2 = opt_book2.get(); + assertTrue(update_title.equals(book2.getTitle())); + + } + + @Test + @Order(3) + void testDeleteBook() { + int start_size = bookRepository.getSize(); + long isbn = 12345; + String title = "illiade"; + Book prequel = null; + + Book book = new Book(isbn, title, prequel); + + bookRepository.addBook(book); + + Optional opt_book2 = bookRepository.findById(isbn); + + Book book2 = opt_book2.get(); + long ID = book2.getIsbn(); + + bookRepository.deleteBookById(ID); + + int size = bookRepository.getSize(); + assertEquals(start_size,size); + } }