diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/model/Component.java b/src/main/java/ru/ulstu/is/sbapp/repair/model/Component.java index 462cbbc..66de488 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/model/Component.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/model/Component.java @@ -18,9 +18,6 @@ public class Component { @Column(name = "amount") private Integer amount; - @ManyToMany(mappedBy = "component",fetch = FetchType.EAGER) - private List favors; - public Component() { } @@ -49,18 +46,6 @@ public class Component { this.amount = amount; } - public List getFavors() { - return favors; - } - - public void setFavors(Favor favors) { - - if(!favors.getComponents().contains(this)){ - favors.getComponents().add(this); - } - } - - @Override public boolean equals(Object o) { diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/model/Favor.java b/src/main/java/ru/ulstu/is/sbapp/repair/model/Favor.java index bd92497..e01d98a 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/model/Favor.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/model/Favor.java @@ -18,13 +18,6 @@ public class Favor { @Column(name = "price") private Integer price; - @ManyToMany(mappedBy = "favor", fetch = FetchType.EAGER) - private List components; - - @ManyToMany(mappedBy = "favor", fetch = FetchType.EAGER) - private List orders; - - public Favor() { } @@ -53,33 +46,7 @@ public class Favor { public void setPrice(Integer price) { this.price = price; } - - public List getComponents() { - return components; - } - - public void setComponents(Component components) { - if (! components.getFavors().contains(this)){ - components.getFavors().add(this); - } - } - - public void update(Favor favor){ - this.favorName = favor.favorName; - this.price = favor.price; - this.components = favor.getComponents(); - } - - - public List getOrders() { - return orders; - } - - public void setOrders(Order orders) { - if (! orders.getFavors().contains(this)){ - orders.getFavors().add(this); - } - } + @Override public boolean equals(Object o) { diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/model/Order.java b/src/main/java/ru/ulstu/is/sbapp/repair/model/Order.java index 32b64a1..afd1c3b 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/model/Order.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/model/Order.java @@ -16,12 +16,9 @@ public class Order { private Long id; @Column(name = "date") private Date date; - @ManyToMany(fetch = FetchType.EAGER) - private List favors; public Order(){ } - public Order(Date date) { this.date = date; } @@ -42,26 +39,6 @@ public class Order { this.date = date; } - - public List getFavors() { - return favors; - } - - public void setFavors(List favors) { - this.favors = favors; - } - - public void addFavor(Favor orderFavors){ - if (favors == null){ - this.favors = new ArrayList<>(); - } - favors.add(orderFavors); - if (!orderFavors.getOrders().contains(this)){ - orderFavors.setOrders(this); - } - } - - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/service/ComponentService.java b/src/main/java/ru/ulstu/is/sbapp/repair/service/ComponentService.java index 186d1f7..9d4c1dc 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/service/ComponentService.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/service/ComponentService.java @@ -58,7 +58,7 @@ public class ComponentService { @Transactional public Component deleteComponent(Long id) { final Component currentComponent = findComponent(id); - int size = currentComponent.getFavors().size(); + //int size = currentComponent.getFavors().size(); em.remove(currentComponent); return currentComponent; } diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/service/FavorService.java b/src/main/java/ru/ulstu/is/sbapp/repair/service/FavorService.java index 5929b50..423ebe0 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/service/FavorService.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/service/FavorService.java @@ -22,8 +22,8 @@ public class FavorService { private EntityManager em; @Transactional - public Favor addFavor(String favorName, Integer price, Integer[] count, List components) { - if (!StringUtils.hasText(favorName) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || components.size() == 0 || components.stream().filter(Objects::isNull).toList().size() != 0 || count.length != components.size()) { + public Favor addFavor(String favorName, Integer price) { + if (!StringUtils.hasText(favorName) || price == 0) { throw new IllegalArgumentException("Favor name is null or empty"); } final Favor favor = new Favor(favorName, price); @@ -48,77 +48,27 @@ public class FavorService { } @Transactional - public Favor updateFavor(Long id, String favorName, Integer price, Integer[] count, List components) { - if (!StringUtils.hasText(favorName) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || components.size() == 0 || components.stream().filter(Objects::isNull).toList().size() != 0 || count.length != components.size()) { + public Favor updateFavor(Long id, String favorName, Integer price) { + if (!StringUtils.hasText(favorName) || price == 0) { throw new IllegalArgumentException("Favor name is null or empty"); } final Favor currentFavor = findFavor(id); currentFavor.setFavorName(favorName); currentFavor.setPrice(price); em.merge(currentFavor); - List favorComponentsList = em.createQuery("select p from FavorComponents p where p.id.favorId = " + id, FavorComponents.class) - .getResultList(); - List component_id = new ArrayList<>(favorComponentsList.stream().map(p -> p.getId().getComponentId()).toList()); - for (int i = 0; i < components.size(); i++) { - final Long currentId = components.get(i).getId(); - if (component_id.contains(currentId)) { - final FavorComponents favorComponents = favorComponentsList.stream().filter(x -> Objects.equals(x.getId().getComponentId(), currentId)).toList().get(0); - favorComponentsList.remove(favorComponents); - component_id.remove(components.get(i).getId()); - favorComponents.setCount(count[i]); - em.merge(favorComponents); - } else { - final FavorComponents favorComponents = new FavorComponents(components.get(i), currentFavor, count[i]); - currentFavor.addComponent(favorComponents); - components.get(i).addFavor(favorComponents); - em.persist(favorComponents); - } - } - for (int i = 0; i < favorComponentsList.size(); i++) { - favorComponentsList.get(i).getComponent().removeFavor(favorComponentsList.get(i)); - favorComponentsList.get(i).getFavor().removeComponent(favorComponentsList.get(i)); - em.remove(favorComponentsList.get(i)); - } return currentFavor; } @Transactional public Favor deleteFavor(Long id) { final Favor currentFavor = findFavor(id); - int size = currentFavor.getComponents().size(); - for (int i = 0; i < size; i++) { - FavorComponents temp = currentFavor.getComponents().get(0); - temp.getComponent().removeFavor(temp); - temp.getFavor().removeComponent(temp); - em.remove(temp); - } - int ordSize = currentFavor.getOrders().size(); - for (int i = 0; i < ordSize; i++){ - OrderFavors temp = currentFavor.getOrders().get(0); - temp.getFavor().removeOrder(temp); - temp.getOrder().removeFavors(temp); - em.remove(temp); - } em.remove(currentFavor); return currentFavor; } @Transactional public void deleteAllFavor() { - em.createQuery("delete from FavorComponents").executeUpdate(); - em.createQuery("delete from OrderFavors ").executeUpdate(); em.createQuery("delete from Favor").executeUpdate(); } - @Transactional - public List findFiltredFavors(Long[] arr) { - if (arr.length == 0) { - throw new IllegalArgumentException("Array id is empty"); - } - List favorList = new ArrayList<>(); - for (int i = 0; i < arr.length; i++) { - favorList.add(em.find(Favor.class, arr[i])); - } - return favorList; - } } diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java b/src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java index 762be37..94dad6e 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java @@ -20,19 +20,13 @@ public class OrderService { private EntityManager em; @Transactional - public Order addOrder(String date, Integer price, Integer[] count, List favors) { - if (!StringUtils.hasText(date) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || favors.size() == 0 || favors.stream().filter(Objects::isNull).toList().size() != 0 || count.length != favors.size()) { + public Order addOrder(String date, Integer price) { + if (!StringUtils.hasText(date) || price == 0) { throw new IllegalArgumentException("Order is null or empty"); } Date correctDate = getDate(date); final Order order = new Order(correctDate); em.persist(order); - for (int i = 0; i < favors.size(); i++) { - final OrderFavors orderFavor = new OrderFavors(order, favors.get(i)); - order.addFavor(orderFavor); - favors.get(i).addOrder(orderFavor); - em.persist(orderFavor); - } return order; } @@ -71,46 +65,17 @@ public class OrderService { final Order currentOrder = findOrder(id); currentOrder.setDate(getDate(date)); em.merge(currentOrder); - List orderFavorsList = em.createQuery("select o from OrderFavors o where o.id.orderId = " + id, OrderFavors.class).getResultList(); - List favor_id = new ArrayList<>(orderFavorsList.stream().map(p -> p.getId().getFavorId()).toList()); - for (int i = 0; i < favors.size(); i++) { - final Long currentId = favors.get(i).getId(); - if (favor_id.contains(currentId)) { - final OrderFavors orderFavors = orderFavorsList.stream().filter(x -> Objects.equals(x.getId().getFavorId(), currentId)).toList().get(0); - orderFavorsList.remove(orderFavors); - favor_id.remove(favors.get(i).getId()); - em.merge(orderFavors); - } else { - final OrderFavors orderFavors = new OrderFavors(currentOrder, favors.get(i)); - currentOrder.addFavor(orderFavors); - favors.get(i).addOrder(orderFavors); - em.persist(orderFavors); - } - } - for (int i = 0; i < orderFavorsList.size(); i++) { - orderFavorsList.get(i).getFavor().removeOrder(orderFavorsList.get(i)); - orderFavorsList.get(i).getOrder().removeFavors(orderFavorsList.get(i)); - em.remove(orderFavorsList.get(i)); - } return currentOrder; } @Transactional public Order deleteOrder(Long id) { final Order currentOrder = findOrder(id); - int size = currentOrder.getFavors().size(); - for (int i = 0; i < size; i++) { - OrderFavors temp = currentOrder.getFavors().get(0); - temp.getFavor().removeOrder(temp); - temp.getOrder().removeFavors(temp); - em.remove(temp); - } em.remove(currentOrder); return currentOrder; } @Transactional public void deleteAllOrder() { - em.createQuery("delete from OrderFavors").executeUpdate(); em.createQuery("delete from Order").executeUpdate(); } } diff --git a/src/main/java/ru/ulstu/is/sbapp/service/TypeService.java b/src/main/java/ru/ulstu/is/sbapp/service/TypeService.java index 5e348f5..00aad69 100644 --- a/src/main/java/ru/ulstu/is/sbapp/service/TypeService.java +++ b/src/main/java/ru/ulstu/is/sbapp/service/TypeService.java @@ -50,21 +50,21 @@ public class TypeService { public Object Sum(Object value1, Object value2, String type){ ValidateParams(value1,value2,type); - return _type.Sum(_value1,_value2).toString(); + return _type.Sum(_value1,_value2); } public Object Min(Object value1, Object value2, String type){ ValidateParams(value1,value2,type); - return _type.Min(_value1,_value2).toString(); + return _type.Min(_value1,_value2); } public Object Mul(Object value1, Object value2, String type){ ValidateParams(value1,value2,type); - return _type.Mul(_value1,_value2).toString(); + return _type.Mul(_value1,_value2); } public Object Del(Object value1, Object value2, String type){ ValidateParams(value1,value2,type); - return _type.Del(_value1,_value2).toString(); + return _type.Del(_value1,_value2); } }