/**
 * Created by Djidjelly Siclait on 12/3/2016.
 */
package com.evapps.Service.Auxiliary;

import com.evapps.Repository.HistoryRepository;
import com.evapps.Repository.ReceiptRepository;
import com.evapps.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StatisticService {

    // Repositories
    @Autowired
    private HistoryRepository historyRepository;
    @Autowired
    private ReceiptRepository receiptRepository;
    @Autowired
    private UserRepository userRepository;

    // Functions
}

@@ -108,26 +108,142 @@
 pps.
-Repository.History
+Entity.History;%0Aimport com.evapps.Entity.Product;%0Aimport com.evapps.Repository.HistoryRepository;%0Aimport com.evapps.Repository.Product
 Repo
@@ -454,16 +454,90 @@
 rvice;%0A%0A
+import java.util.Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 @Service
@@ -661,32 +661,96 @@
 %0A    @Autowired%0A
+    private ProductRepository productRepository;%0A    @Autowired%0A
     private Rece
@@ -841,16 +841,16 @@
 itory;%0A%0A
-
     // F
@@ -858,10 +858,1125 @@
 nctions%0A
+    // History related Statistics%0A    public Product mostViewedProduct()%7B%0A%0A        Map%3CInteger, Integer%3E statistic = fetchProductLegend();%0A%0A        try %7B%0A            for (History history: historyRepository.findAll())%0A                for (Product product: history.getBrowsingHistory())%0A                    statistic.replace(product.getProductId(), statistic.get(product.getProductId()) + 1);%0A%0A            return productRepository.findByProductId(getMaxValue((Integer%5B%5D) statistic.values().toArray()));%0A%0A        %7D catch (Exception exp) %7B%0A            //%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A    // Auxiliary Functions%0A    private Map%3CInteger, Integer%3E fetchProductLegend()%7B%0A        Map%3CInteger, Integer%3E legend = new HashMap%3C%3E();%0A%0A        for (Product p:%0A             productRepository.findAll()) %7B%0A            legend.put(p.getProductId(), 0);%0A        %7D%0A%0A        return legend;%0A    %7D%0A%0A    private Integer getMaxValue(Integer%5B%5D list)%7B%0A        Arrays.sort(list);%0A%0A        return list%5Blist.length - 1%5D;%0A    %7D%0A%0A    private Integer getMinValue(Integer%5B%5D list)%7B%0A        Arrays.sort(list);%0A%0A        return list%5B0%5D;%0A    %7D%0A%0A%0A
 %7D%0A

Add mostViewedProduct