Subversion Repositories SmartDukaan

Rev

Rev 24123 | Rev 24465 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomCurrentInventorySnapshot;
import com.spice.profitmandi.common.model.CustomRetailer;
import com.spice.profitmandi.common.model.InventoryItemAgingModel;
import com.spice.profitmandi.common.model.ItemFeatureDataModel;
import com.spice.profitmandi.common.model.NotificationDataModel;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.util.ExcelUtils;
import com.spice.profitmandi.common.util.Utils;
import com.spice.profitmandi.dao.entity.catalog.Item;
import com.spice.profitmandi.dao.entity.catalog.TagRanking;
import com.spice.profitmandi.dao.entity.dtr.NotificationData;
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
import com.spice.profitmandi.dao.repository.catalog.TagRankingRepository;
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
import com.spice.profitmandi.service.inventory.InventoryService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;
import com.spice.profitmandi.web.util.MVCResponseSender;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class InventoryController {

        private static final Logger LOGGER = LogManager.getLogger(InventoryController.class);

        @Autowired
        private CookiesProcessor cookiesProcessor;

        @Autowired
        @Qualifier("fofoInventoryService")
        private InventoryService inventoryService;
        
        @Autowired
        private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;

        @Autowired
        private TagRankingRepository tagRankingRepository;

        @Autowired
        private ItemRepository itemRepository;

        @Autowired
        private MVCResponseSender mvcResponseSender;

        @Value("${saholic.api.host}")
        private String host;

        @Value("${saholic.api.port}")
        private int port;

        @Value("${saholic.api.webapp}")
        private String webapp;

        @RequestMapping(value = "/getCurrentInventorySnapshot")
        public String getCurrentAvailability(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getCurrentInventorySnapshot(loginDetails.getFofoId(), offset, limit,
                                searchTerm);
                model.addAllAttributes(map);
                return "inventory-snapshot";
        }

        @RequestMapping(value = "/getBadInventorySnapshot")
        public String getBadAvailability(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
                        throws ProfitMandiBusinessException {
                if (searchTerm == null) {
                        searchTerm = "";
                }
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getBadInventorySnapshot(loginDetails.getFofoId(), offset, limit,
                                searchTerm);
                model.addAllAttributes(map);
                return "bad-inventory-snapshot";
        }

        @RequestMapping(value = "/getPaginatedCurrentInventorySnapshot")
        public String getPaginatedCurrentInventorySnapshot(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
                        throws ProfitMandiBusinessException {
                if (searchTerm == null) {
                        searchTerm = "";
                }
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getPaginatedCurrentInventorySnapshot(loginDetails.getFofoId(),
                                offset, limit, searchTerm);
                model.addAllAttributes(map);
                return "inventory-snapshot-paginated";
        }

        @RequestMapping(value = "/getCatalog")
        public String getCatalog(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                if (searchTerm == null) {
                        searchTerm = "";
                }
                
                Map<String, Object> map = inventoryService.getCatalog(loginDetails.getFofoId(), offset, limit, searchTerm);
                model.addAllAttributes(map);
                model.addAttribute("hasGift", hasGift(loginDetails.getFofoId()));
                return "catalog";
        }
        

        //This method is currently hardcoded to faciliate watches sold as gift.
        private boolean hasGift(int fofoId) {
                try {
                        return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId).getAvailability() > 0;
                } catch (ProfitMandiBusinessException e) {
                        return false;
                }
        }

        @RequestMapping(value = "/getPaginatedCatalog")
        public String getCatalogPaginated(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                if (searchTerm == null) {
                        searchTerm = "";
                }
                Map<String, Object> map = inventoryService.getPaginatedCatalog(loginDetails.getFofoId(), offset, limit,
                                searchTerm);
                model.addAllAttributes(map);
                return "catalog-paginated";
        }

        @RequestMapping(value = "/checkItemAvailability")
        public String getItemAvailability(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.ITEM_ID) int itemId, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                CustomCurrentInventorySnapshot customCurrentInventorySnapshot = inventoryService.checkItemAvailability(itemId,
                                loginDetails.getFofoId());
                customCurrentInventorySnapshot
                                .setIconUrl(Utils.getIconUrl(customCurrentInventorySnapshot.getCatalogItemId(), host, port, webapp));
                model.addAttribute("currentInventorySnapshot", new Gson().toJson(customCurrentInventorySnapshot));
                return "current-item-availability";

        }

        @RequestMapping(value = "/cart", method = RequestMethod.POST)
        public String addToCart(HttpServletRequest request, @RequestParam(name = "cartData") String cartData, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);

                Map<String, Object> map = inventoryService.addToCart(cartData, loginDetails.getFofoId());
                model.addAllAttributes(map);
                return "cart";
        }

        @RequestMapping(value = "/validate-cart", method = RequestMethod.POST)
        public String validateCart(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam(name = "cartData") String cartData, Model model) throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);

                Map<String, Object> map = inventoryService.validateCart(cartData, loginDetails.getFofoId());
                model.addAllAttributes(map);
                return "validate-cart";
        }

        @RequestMapping(value = "/grnHistory")
        public String getGrnHistory(HttpServletRequest request, @RequestParam(required = false) LocalDateTime startTime,
                        @RequestParam(required = false) LocalDateTime endTime,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = ProfitMandiConstants.PURCHASE_REFERENCE, defaultValue = "") String purchaseReference,
                        @RequestParam(name = "searchType", defaultValue = "") String searchType, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getGrnHistory(loginDetails.getFofoId(), startTime, endTime, offset,
                                limit, purchaseReference, searchType);
                model.addAllAttributes(map);
                return "grn-history";
        }

        @RequestMapping(value = "/getPaginatedGrnHistory")
        public String getPaginatedGrnHistory(HttpServletRequest request,
                        @RequestParam(required = false) LocalDateTime startTime,
                        @RequestParam(required = false) LocalDateTime endTime,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = ProfitMandiConstants.PURCHASE_REFERENCE, defaultValue = "") String purchaseReference,
                        @RequestParam(name = "searchType", defaultValue = "") String searchType, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getPaginatedGrnHistory(loginDetails.getFofoId(), startTime, endTime,
                                offset, limit);
                model.addAllAttributes(map);
                return "grn-history-paginated";
        }

        @RequestMapping(value = "/grnHistoryDetailByPurchaseId")
        public String grnHistoryByPurchaseId(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.PURCHASE_ID) int purchaseId, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getGrnHistoryDetail(loginDetails.getFofoId(), purchaseId, host, port,
                                webapp);
                model.addAllAttributes(map);
                return "grn-details";
        }

        @RequestMapping(value = "/grnHistoryDetailByPurchaseReference")
        public String grnHistoryByPurchaseReference(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.PURCHASE_REFERENCE) String purchaseReference, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getGrnHistoryDetail(loginDetails.getFofoId(), purchaseReference,
                                host, port, webapp);
                model.addAllAttributes(map);
                return "grn-details";
        }

        @RequestMapping(value = "/getInventoryItemAgingByInterval", method = RequestMethod.POST)
        public String getInventoryItemAgingByInterval(HttpServletRequest request, @RequestBody List<Integer> intervals,
                        Model model, @RequestParam(name = "searchContent", defaultValue = "") String searchContent,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getPaginatedItemAgingByInterval(loginDetails.getFofoId(), intervals,
                                searchContent, offset, limit);
                model.addAllAttributes(map);
                return "item-aging";
        }

        @RequestMapping(value = "/downloadInventoryItemAgingByInterval", method = RequestMethod.POST)
        public ResponseEntity<?> downloadInventoryItemAgingByInterval(HttpServletRequest request,
                        @RequestBody List<Integer> intervals, Model model) throws ProfitMandiBusinessException {
                LOGGER.info("Request received at url{} with body {}", request.getRequestURI(), intervals);
                LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);

                List<InventoryItemAgingModel> inventoryItemAgingModels = inventoryService
                                .getItemAgingByInterval(fofoDetails.getFofoId(), intervals);

                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ExcelUtils.writeInventoryItemAgingModels(inventoryItemAgingModels, intervals, byteArrayOutputStream);

                final HttpHeaders headers = new HttpHeaders();
                // private static final String CONTENT_TYPE_XLSX =
                // "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                // headers.set("Content-Type", "application/vnd.ms-excel");
                headers.set("Content-disposition", "inline; filename=InventoryItemAging.xlsx");
                headers.setContentLength(byteArrayOutputStream.toByteArray().length);
                final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
                return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);

                // return
                // responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
        }

        @RequestMapping(value = "/featurePanel", method = RequestMethod.GET)
        public String FeaturePanel(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {

                List<TagRanking> tagRanking = null;

                long size = 0;
                tagRanking = tagRankingRepository.selectAllTagRanking(offset, limit);
                size = tagRankingRepository.selectAllCount();
                LOGGER.info("tagRanking" + tagRanking);
                if (!tagRanking.isEmpty()) {
            model.addAttribute("tagRanking", tagRanking);
                        model.addAttribute("start", offset + 1);
                        model.addAttribute("size", size);
                        model.addAttribute("url", "/getPaginatedfeature");

                        if (tagRanking.size() < limit) {
                                model.addAttribute("end", offset + tagRanking.size());
                        } else {
                                model.addAttribute("end", offset + limit);
                        }
                } else {
                        model.addAttribute("tagRanking", tagRanking);
                        model.addAttribute("size", size);
                }

                return "feature";

        }

        @RequestMapping(value = "/getPaginatedfeature", method = RequestMethod.GET)
        public String getPaginatedFeaturePanel(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
                List<TagRanking> tagRanking = null;

                long size = 0;
                tagRanking = tagRankingRepository.selectAllTagRanking(offset, limit);
                if (!tagRanking.isEmpty()) {

                        model.addAttribute("tagRanking", tagRanking);
                        model.addAttribute("start", offset + 1);
                        model.addAttribute("size", size);
                        model.addAttribute("url", "/getPaginatedfeature");

                } else {
                        model.addAttribute("tagRanking", tagRanking);
                }
                return "feature-paginated";
        }

        @RequestMapping(value = "/itemfeature", method = RequestMethod.POST)
        public String Itemfeature(HttpServletRequest request, @RequestBody ItemFeatureDataModel itemFeatureDatatModel,
                        Model model) throws Exception {

                TagRanking tagRanking = new TagRanking();
                tagRanking.setRankPoints(itemFeatureDatatModel.getRankPoints());
                tagRanking.setCatalogItemId(itemFeatureDatatModel.getCatalogItemId());
                tagRanking.setFeature(itemFeatureDatatModel.getFeature());

                LOGGER.info("tagRanking" + tagRanking);
                tagRankingRepository.persist(tagRanking);
                model.addAttribute("response", mvcResponseSender.createResponseString(true));
                return "response";
        }

}