diff --git a/mivan/src/main/java/mivan/model/Loan.java b/mivan/src/main/java/mivan/model/Loan.java new file mode 100644 index 0000000..34f2e45 --- /dev/null +++ b/mivan/src/main/java/mivan/model/Loan.java @@ -0,0 +1,63 @@ +package mivan.model; + +import javax.persistence.Column; +import javax.persistence.Id; + +public class Loan { + + private static final long serialVersionUID = -5124436115031696628L; + + @Id + @Column(name = "id", nullable=false, unique=true) + private long id; + + @Column(name = "date_start", nullable = false) + private String date_start; + + @Column(name = "date_end", nullable = false) + private String date_end; + + @Column(name = "state", nullable = false) + private String state; + + public Loan(long id, String date_start, String date_end, String state) { + this.id = id; + this.date_start = date_start; + this.date_end = date_end; + this.state = state; + } + + public long getId() { + return id; + } + + public String getDate_start() { + return date_start; + } + + public String getDate_end() { + return date_end; + } + + public String getState() { + return state; + } + + public void setId(long id) { + this.id = id; + } + + public void setDate_start(String date_start) { + this.date_start = date_start; + } + + public void setDate_end(String date_end) { + this.date_end = date_end; + } + + public void setState(String state) { + this.state = state; + } + + +} diff --git a/mivan/src/main/java/mivan/repository/LoanRepository.java b/mivan/src/main/java/mivan/repository/LoanRepository.java new file mode 100644 index 0000000..4898199 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/LoanRepository.java @@ -0,0 +1,16 @@ +package mivan.repository; + + +import mivan.model.Loan; + +public interface LoanRepository extends Repository { + + public void deleteLoanById(Long id); + + public void addLoan(Loan loan); + + public void updateLoan(Long id, String stato); + + public int getSize(); + +} diff --git a/mivan/src/main/java/mivan/repository/LoanRepositoryImpl.java b/mivan/src/main/java/mivan/repository/LoanRepositoryImpl.java new file mode 100644 index 0000000..0dec829 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/LoanRepositoryImpl.java @@ -0,0 +1,98 @@ +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.Loan; + +public class LoanRepositoryImpl implements LoanRepository{ + + private EntityManagerFactory entityManagerFactory; + + public LoanRepositoryImpl() { + this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); + } + + public Optional findById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + Loan loan = entityManager.find(Loan.class, id); + entityManager.close(); + return Optional.ofNullable(loan); + } + + @Override + public Iterable findAll() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + List loan = entityManager.createQuery("FROM Loan", Loan.class).getResultList(); + entityManager.close(); + return loan; + } + + @Override + public void deleteLoanById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Loan loan = entityManager.find(Loan.class, id); + entityManager.remove(loan); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void addLoan(Loan loan) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + entityManager.persist(loan); + entityManager.getTransaction().commit(); + entityManager.close(); + + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void updateLoan(Long id, String state) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Loan loan = entityManager.find(Loan.class, id); + loan.setState(state); + entityManager.persist(loan); + 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 Loan").getResultList().size(); + + entityManager.close(); + } catch (Exception ex) { + size = 0; + } + return size; + } +}