diff --git a/mivan/src/main/java/mivan/model/Item.java b/mivan/src/main/java/mivan/model/Item.java new file mode 100644 index 0000000..856d073 --- /dev/null +++ b/mivan/src/main/java/mivan/model/Item.java @@ -0,0 +1,61 @@ +package mivan.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name="item") +public class Item implements Serializable{ + private static final long serialVersionUID = 1993L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", unique = true) + private long id; + + @ManyToOne(fetch=FetchType.LAZY) + @JoinColumn(name="isbn") + private Book book; + + @ManyToOne(fetch=FetchType.LAZY) + @JoinColumn(name="location") + private Location location; + + public Item () {} + public Item(Book book, Location location){ + super(); + this.book = book; + this.location = location; + } + + public long getId() { + return this.id; + } + + public Book getBook(){ + return this.book; + } + + public void setBook(Book book){ + this.book = book; + } + + public Location getLocation() { + return this.location; + } + + public void setLocation(Location location) { + this.location = location; + } + + +} \ No newline at end of file diff --git a/mivan/src/main/java/mivan/model/Loan.java b/mivan/src/main/java/mivan/model/Loan.java index 34f2e45..f4dade9 100644 --- a/mivan/src/main/java/mivan/model/Loan.java +++ b/mivan/src/main/java/mivan/model/Loan.java @@ -1,7 +1,10 @@ package mivan.model; import javax.persistence.Column; +import javax.persistence.FetchType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; public class Loan { @@ -19,6 +22,18 @@ public class Loan { @Column(name = "state", nullable = false) private String state; + + @ManyToOne(fetch=FetchType.LAZY) + @JoinColumn(name="item") + private Item item; + + @ManyToOne(fetch=FetchType.LAZY) + @JoinColumn(name="staff") + private Staff staff; + + @ManyToOne(fetch=FetchType.LAZY) + @JoinColumn(name="user") + private User user; public Loan(long id, String date_start, String date_end, String state) { this.id = id; @@ -59,5 +74,29 @@ public class Loan { this.state = state; } + public Item getItem() { + return item; + } + + public Staff getStaff() { + return staff; + } + + public User getUser() { + return user; + } + + public void setItem(Item item) { + this.item = item; + } + + public void setStaff(Staff staff) { + this.staff = staff; + } + + public void setUser(User user) { + this.user = user; + } + } diff --git a/mivan/src/main/java/mivan/repository/ItemRepository.java b/mivan/src/main/java/mivan/repository/ItemRepository.java new file mode 100644 index 0000000..e011ab2 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/ItemRepository.java @@ -0,0 +1,13 @@ +package mivan.repository; + +import mivan.model.Item; + +public interface ItemRepository extends Repository { + + public void deleteItemById(Long id); + + public void addItem(Item item); + + public int getSize(); + +} \ No newline at end of file diff --git a/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java b/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java new file mode 100644 index 0000000..6c89526 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java @@ -0,0 +1,81 @@ +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.Item; + +public class ItemRepositoryImpl implements ItemRepository { + private EntityManagerFactory entityManagerFactory; + + public ItemRepositoryImpl() { + this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); + } + @Override + public Optional findById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + Item item = entityManager.find(Item.class, id); + entityManager.close(); + return Optional.ofNullable(item); + } + + @Override + public Iterable findAll() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + List items = entityManager.createQuery("FROM Item", Item.class).getResultList(); + entityManager.close(); + return items; + } + + @Override + public void deleteItemById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Item item = entityManager.find(Item.class, id); + entityManager.remove(item); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void addItem(Item item) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + entityManager.persist(item); + entityManager.getTransaction().commit(); + entityManager.close(); + + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + + } + + @Override + public int getSize() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + int size = 0; + try { + size = entityManager.createQuery("FROM Item").getResultList().size(); + + entityManager.close(); + } catch (Exception ex) { + size = 0; + } + return size; + } + + +} \ No newline at end of file diff --git a/mivan/src/test/java/mivan/ItemTest.java b/mivan/src/test/java/mivan/ItemTest.java new file mode 100644 index 0000000..a3e5b8b --- /dev/null +++ b/mivan/src/test/java/mivan/ItemTest.java @@ -0,0 +1,58 @@ +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.Book; +import mivan.model.Item; +import mivan.model.Location; +import mivan.repository.BookRepository; +import mivan.repository.ItemRepository; +import mivan.repository.ItemRepositoryImpl; + +@TestMethodOrder(OrderAnnotation.class) +public class ItemTest { + private ItemRepository itemRepository = new ItemRepositoryImpl(); + + + @Test + @Order(1) + void testAddItem() { + Book book = null; + Location location = null; + Item item = new Item(book, location); + itemRepository.addItem(item); + int size = itemRepository.getSize(); + assertEquals(1, size); + } + + + @Test + @Order(2) + void testRemoveItem() { + int startSize = itemRepository.getSize(); + Book book = null; + Location location = null; + Item item = new Item(book, location); + itemRepository.addItem(item); + int size = itemRepository.getSize(); + assertEquals(startSize + 1, size); + + Optional optItem2 = itemRepository.findById(item.getId()); + + Item item2 = optItem2.get(); + long ID = item2.getId(); + itemRepository.deleteItemById(ID); + + int finalSize = itemRepository.getSize(); + assertEquals(startSize, finalSize); + + + } +} \ No newline at end of file