/**
 * Created by Djidjelly Siclait on 11/26/2016.
 */
package com.evapps.Service.CRUD;

import com.evapps.Entity.History;
import com.evapps.Entity.Product;
import com.evapps.Repository.HistoryRepository;
import com.evapps.Repository.ProductRepository;
import freemarker.template.utility.NullArgumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.PersistenceException;
import java.util.Set;

@Service
public class DeleteDataService {

    // Repositories
    @Autowired
    private HistoryRepository historyRepository;
    @Autowired
    private ProductRepository productRepository;

    // Deletes
    public void deleteRegisteredProduct(Integer productId) throws Exception{

        if (!doesProductIdExist(productId))
            throw new IllegalArgumentException("This product does not exists");

        try {
            Product product = productRepository.findByProductId(productId);

            // Applying Cascade
            for (History history:
                 historyRepository.findAll()) {
                // Fetching sets
                Set<Product> browsingHistory = history.getBrowsingHistory();
                Set<Product> shoppingCart = history.getShoppingCart();

                // Removing the item
                browsingHistory.remove(product);
                shoppingCart.remove(product);

                // Update History
                history.setBrowsingHistory(browsingHistory);
                history.setShoppingCart(shoppingCart);
                historyRepository.save(history);
            }

            // Deleting Product
            productRepository.delete(product);
        } catch (PersistenceException exp){
            throw new PersistenceException("Persistence Error --> " + exp.getMessage());
        } catch (NullArgumentException exp){
            throw new NullArgumentException("Null Argument Error --> " + exp.getMessage());
        } catch (Exception exp){
            throw new Exception("General Error --> " + exp.getMessage());
        }
    }

    // Auxiliary Functions
    private boolean doesProductIdExist(Integer productId){
        Product product = productRepository.findByProductId(productId);
        return (product != null);
    }
}

@@ -150,16 +150,50 @@
 roduct;%0A
+import com.evapps.Entity.Receipt;%0A
 import c
@@ -276,24 +276,72 @@
 Repository;%0A
+import com.evapps.Repository.ReceiptRepository;%0A
 import freem
@@ -757,16 +757,80 @@
 ository;
+%0A    @Autowired%0A    private ReceiptRepository receiptRepository;
 %0A%0A    //
@@ -2244,24 +2244,571 @@
    %7D%0A    %7D%0A%0A
+    public void deleteRegisteredPendingTransaction(String fiscalCode) throws Exception%7B%0A%0A        try %7B%0A            receiptRepository.delete(fiscalCode);%0A        %7D catch (PersistenceException exp)%7B%0A            throw new PersistenceException(%22Persistence Error --%3E %22 + exp.getMessage());%0A        %7D catch (NullArgumentException exp)%7B%0A            throw new NullArgumentException(%22Null Argument Error --%3E %22 + exp.getMessage());%0A        %7D catch (Exception exp)%7B%0A            throw new Exception(%22General Error --%3E %22 + exp.getMessage());%0A        %7D%0A    %7D%0A%0A
     // Auxil

Add deleteRegisteredPendingTransaction in DeleteDataService