Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kursovoy_proekt Переделаный.docx
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
724 Кб
Скачать

Слой доступа к данным

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package DAL;

import Entities.Employee;

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.EntityTransaction;

import javax.persistence.Query;

/**

*

* @author Humanity

*/

public class EmployeeRepository {

private EntityManager entityManager;

private String GetAllEmployeesQuery = "select e from Employee e";

public EmployeeRepository(EntityManager entityManager) {

this.entityManager = entityManager;

}

public List<Employee> GetAllEmployees() {

Query query = entityManager.createQuery(GetAllEmployeesQuery);

return query.getResultList();

}

public Employee GetEmployeeById(int employeeId) {

Employee employee = entityManager.find(Employee.class, employeeId);

return employee;

}

public void InsertEmployee(Employee employee) {

EntityTransaction t = entityManager.getTransaction();

t.begin();

try {

entityManager.persist(employee);

entityManager.flush();

t.commit();

} catch(Exception ex) {

t.rollback();

}

}

public void UpdateEmployee(Employee employee) {

EntityTransaction t = entityManager.getTransaction();

t.begin();

try {

entityManager.refresh(employee);

entityManager.flush();

t.commit();

} catch(Exception ex) {

t.rollback();

}

}

public void DeleteEmployee(int employeeId) {

EntityTransaction t = entityManager.getTransaction();

t.begin();

try {

Employee Employee = GetEmployeeById(employeeId);

entityManager.remove(Employee);

entityManager.flush();

t.commit();

} catch(Exception ex) {

t.rollback();

}

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package DAL;

import Entities.Product;

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.EntityTransaction;

import javax.persistence.Query;

/**

*

* @author Humanity

*/

public class ProductRepository {

private EntityManager entityManager;

private String GetAllProductsQuery = "select p from Product p";

public ProductRepository(EntityManager entityManager) {

this.entityManager = entityManager;

}

public List<Product> GetAllProducts() {

Query query = entityManager.createQuery(GetAllProductsQuery);

return query.getResultList();

}

public Product GetProductById(int productId) {

Product product = entityManager.find(Product.class, productId);

return product;

}

public boolean InsertProduct(Product product) {

EntityTransaction t = entityManager.getTransaction();

t.begin();

try {

entityManager.merge(product);

//entityManager.persist(product);

t.commit();

return true;

} catch(Exception ex) {

t.rollback();

return false;

}

}

public void UpdateProduct(Product product) {

EntityTransaction t = entityManager.getTransaction();

t.begin();

try {

entityManager.refresh(product);

entityManager.flush();

t.commit();

} catch(Exception ex) {

t.rollback();

}

}

public void DeleteProduct(int productId) {

EntityTransaction t = entityManager.getTransaction();

t.begin();

try {

Product Product = GetProductById(productId);

entityManager.remove(Product);

entityManager.flush();

t.commit();

} catch(Exception ex) {

t.rollback();

}

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package DAL;

import Entities.Product;

import Entities.Shipment;

import java.lang.Math;

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.EntityTransaction;

import javax.persistence.Query;

/**

*

* @author Humanity

*/

public class ShipmentRepository {

private EntityManager entityManager;

private String GetAllShipmentsQuery = "select s from Shipment s";

private String GetShipmentsForProductQuery = "select s from Shipment s where s.productid.id = %1$d";

private String GetShipmentsForPersonQuery = "select s from Shipment s where s.responsiblepersonid.id = %1$d";

private String GetShipmentsForPersonAndProductQuery =

"select s from Shipment s where s.responsiblepersonid.id = %1$d and s.productid.id = %2$d";

public ShipmentRepository(EntityManager entityManager) {

this.entityManager = entityManager;

}

public List<Shipment> GetAllShipments() {

Query query = entityManager.createQuery(GetAllShipmentsQuery);

return query.getResultList();

}

public Shipment GetShipmentById(int shipmentId) {

Shipment shipment = entityManager.find(Shipment.class, shipmentId);

return shipment;

}

public List<Shipment> GetShipmentForProduct(int productId) {

Query query = entityManager.createQuery(String.format(GetShipmentsForProductQuery, productId));

return query.getResultList();

}

public List<Shipment> GetShipmentForPerson(int personId) {

Query query = entityManager.createQuery(String.format(GetShipmentsForPersonQuery, personId));

return query.getResultList();

}

public List<Shipment> GetShipmentList(int personId, int productId) {

if (productId == 0){

if (personId == 0) {

return GetAllShipments();

} else {

return GetShipmentForPerson(personId);

}

} else {

if (personId == 0) {

return GetShipmentForProduct(productId);

} else {

Query query = entityManager.createQuery(String.format(GetShipmentsForPersonAndProductQuery, personId, productId));

return query.getResultList();

}

}

}

public boolean InsertShipment(Shipment shipment) {

Product p = StockDatabase.getInstance().getProducts().GetProductById(shipment.getProductid().getId());

if (shipment.getQuantity() < 0 &&

p.getQuantityinstock() < Math.abs(shipment.getQuantity())) {

// trying to take TOO MUCH from stock...

return false;

}

int finalQuantity = p.getQuantityinstock() + shipment.getQuantity();

p.setQuantityinstock(finalQuantity);

EntityTransaction t = entityManager.getTransaction();

try {

t.begin();

entityManager.persist(shipment);

entityManager.persist(p);

//entityManager.flush();

t.commit();

return true;

} catch(Exception ex) {

t.rollback();

return false;

}

}

public void UpdateShipment(Shipment shipment) {

EntityTransaction t = entityManager.getTransaction();

t.begin();

try {

entityManager.refresh(shipment);

entityManager.flush();

t.commit();

} catch(Exception ex) {

t.rollback();

}

}

public void DeleteShipment(int shipmentId) {

EntityTransaction t = entityManager.getTransaction();

t.begin();

try {

Shipment Shipment = GetShipmentById(shipmentId);

entityManager.remove(Shipment);

entityManager.flush();

t.commit();

} catch(Exception ex) {

t.rollback();

}

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package DAL;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

/**

*

* @author Humanity

*/

public class StockDatabase {

private static class SingletonHolder {

public static StockDatabase instance = new StockDatabase();

}

public static StockDatabase getInstance() {

return SingletonHolder.instance;

}

private String connectionName = "StockDb";

private EntityManager entityManager;

EmployeeRepository employee;

ProductRepository products;

ShipmentRepository shipments;

protected StockDatabase() {

try {

EntityManagerFactory factory = Persistence.createEntityManagerFactory(connectionName);

entityManager = factory.createEntityManager();

employee = new EmployeeRepository(entityManager);

products = new ProductRepository(entityManager);

shipments = new ShipmentRepository(entityManager);

} catch (java.lang.ExceptionInInitializerError ex) {

Throwable a = ex.getException();

Throwable b = ex.getCause();

}

}

public EmployeeRepository getEmployee() {

return employee;

}

public ProductRepository getProducts() {

return products;

}

public ShipmentRepository getShipments() {

return shipments;

}

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]