Merge branch 'develop' of gitlab.com:meliurwen/2019_assignment3_MiVan into develop

develop
Meliurwen 5 years ago
commit 7a24c956b8
  1. 61
      mivan/src/main/java/mivan/model/Item.java
  2. 39
      mivan/src/main/java/mivan/model/Loan.java
  3. 13
      mivan/src/main/java/mivan/repository/ItemRepository.java
  4. 81
      mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java
  5. 58
      mivan/src/test/java/mivan/ItemTest.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;
}
}

@ -1,7 +1,10 @@
package mivan.model; package mivan.model;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
public class Loan { public class Loan {
@ -20,6 +23,18 @@ public class Loan {
@Column(name = "state", nullable = false) @Column(name = "state", nullable = false)
private String state; 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) { public Loan(long id, String date_start, String date_end, String state) {
this.id = id; this.id = id;
this.date_start = date_start; this.date_start = date_start;
@ -59,5 +74,29 @@ public class Loan {
this.state = state; 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;
}
} }

@ -0,0 +1,13 @@
package mivan.repository;
import mivan.model.Item;
public interface ItemRepository extends Repository<Item, Long> {
public void deleteItemById(Long id);
public void addItem(Item item);
public int getSize();
}

@ -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<Item> 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<Item> findAll() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
List<Item> 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;
}
}

@ -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<Item> optItem2 = itemRepository.findById(item.getId());
Item item2 = optItem2.get();
long ID = item2.getId();
itemRepository.deleteItemById(ID);
int finalSize = itemRepository.getSize();
assertEquals(startSize, finalSize);
}
}
Loading…
Cancel
Save