}
}
@Override
public void unlinkProduct(Long eshopId, Long productId, Long linkedEshopId, Long linkedProductId) throws NotExistingEntityException {
Eshop eshop = eshopDAO.findEagerlyById(Eshop.class, eshopId);
if (eshop == null) {
throw new NotExistingEntityException("E-shop with ID " + eshopId + " does not exist.");
}
Product product = productDAO.findByComplexId(Product.class, productId, Eshop.class, eshopId);
if (product == null) {
throw new NotExistingEntityException("Product with ID " + productId + " does not exist.");
}
Product linkedProduct = productDAO.findByComplexId(Product.class, linkedProductId, Eshop.class, linkedEshopId);
if (linkedProduct == null) {
throw new NotExistingEntityException("Product with ID " + linkedProductId + " does not exist.");
}
if (eshopId.equals(linkedEshopId)) {
for (Product productItem : eshop.getProducts()) {
if (productItem.equals(product)) {
productItem.removeSimilarProduct(linkedProduct);
} else if (productItem.equals(linkedProduct)) {
productItem.removeSimilarProduct(product);
}
}
eshopDAO.update(eshop);
} else {
Eshop linkedEshop = eshopDAO.findEagerlyById(Eshop.class, linkedEshopId);
if (linkedEshop == null) {
throw new NotExistingEntityException("E-shop with ID " + linkedEshopId + " does not exist.");
}
for (Product productItem : eshop.getProducts()) {
if (productItem.equals(product)) {
productItem.removeSimilarProduct(linkedProduct);
break;
}
}
for (Product productItem : linkedEshop.getProducts()) {
if (productItem.equals(linkedProduct)) {
productItem.removeSimilarProduct(product);
break;
}
}