without connections

This commit is contained in:
VictoriaPresnyakova 2023-04-09 19:20:33 +04:00
parent f9ffc7799c
commit 943027b803
7 changed files with 12 additions and 168 deletions

View File

@ -18,9 +18,6 @@ public class Component {
@Column(name = "amount")
private Integer amount;
@ManyToMany(mappedBy = "component",fetch = FetchType.EAGER)
private List<Favor> favors;
public Component() {
}
@ -49,18 +46,6 @@ public class Component {
this.amount = amount;
}
public List<Favor> getFavors() {
return favors;
}
public void setFavors(Favor favors) {
if(!favors.getComponents().contains(this)){
favors.getComponents().add(this);
}
}
@Override
public boolean equals(Object o) {

View File

@ -18,13 +18,6 @@ public class Favor {
@Column(name = "price")
private Integer price;
@ManyToMany(mappedBy = "favor", fetch = FetchType.EAGER)
private List<Component> components;
@ManyToMany(mappedBy = "favor", fetch = FetchType.EAGER)
private List<Order> orders;
public Favor() {
}
@ -54,32 +47,6 @@ public class Favor {
this.price = price;
}
public List<Component> 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<Order> getOrders() {
return orders;
}
public void setOrders(Order orders) {
if (! orders.getFavors().contains(this)){
orders.getFavors().add(this);
}
}
@Override
public boolean equals(Object o) {

View File

@ -16,12 +16,9 @@ public class Order {
private Long id;
@Column(name = "date")
private Date date;
@ManyToMany(fetch = FetchType.EAGER)
private List<Favor> favors;
public Order(){
}
public Order(Date date) {
this.date = date;
}
@ -42,26 +39,6 @@ public class Order {
this.date = date;
}
public List<Favor> getFavors() {
return favors;
}
public void setFavors(List<Favor> 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;

View File

@ -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;
}

View File

@ -22,8 +22,8 @@ public class FavorService {
private EntityManager em;
@Transactional
public Favor addFavor(String favorName, Integer price, Integer[] count, List<Component> 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<Component> 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<FavorComponents> favorComponentsList = em.createQuery("select p from FavorComponents p where p.id.favorId = " + id, FavorComponents.class)
.getResultList();
List<Long> 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<Favor> findFiltredFavors(Long[] arr) {
if (arr.length == 0) {
throw new IllegalArgumentException("Array id is empty");
}
List<Favor> favorList = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
favorList.add(em.find(Favor.class, arr[i]));
}
return favorList;
}
}

View File

@ -20,19 +20,13 @@ public class OrderService {
private EntityManager em;
@Transactional
public Order addOrder(String date, Integer price, Integer[] count, List<Favor> 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<OrderFavors> orderFavorsList = em.createQuery("select o from OrderFavors o where o.id.orderId = " + id, OrderFavors.class).getResultList();
List<Long> 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();
}
}

View File

@ -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);
}
}