Subversion Repositories SmartDukaan

Rev

Rev 21581 | Rev 21636 | 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.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomCurrentInventorySnapshot;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.FofoItemId;
import com.spice.profitmandi.dao.repository.CurrentInventorySnapshotRepository;
import com.spice.profitmandi.web.model.FofoDetails;
import com.spice.profitmandi.web.util.CookiesFetcher;
import com.spice.profitmandi.web.util.MVCResponseSender;

@Controller
public class InventoryController {
        
        private static final Logger LOGGER = LoggerFactory.getLogger(InventoryController.class);
        
        @Autowired
        CurrentInventorySnapshotRepository  currentInventorySnapshotRepository;
        
        @Autowired
        MVCResponseSender mvcResponseSender;
        
        @Autowired
        CookiesFetcher cookiesFetcher;
        
        @RequestMapping(value = "/checkCurrentAvailability")
        public String getCurrentAvailability(HttpServletRequest request, @ModelAttribute ModelMap model) throws Exception{
                FofoDetails fofoDetails;
                try {
                        fofoDetails = cookiesFetcher.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                List<Object[]> rows = currentInventorySnapshotRepository.selectCustomByFofoId(fofoDetails.getFofoId());
                
                model.addAttribute("currentInventorySnapshots", this.getCustomCurrentInventorySnapshots(rows));
                return "dashboard";
        }
        
        private Set<CustomCurrentInventorySnapshot> getCustomCurrentInventorySnapshots(List<Object[]> rows){
                Set<CustomCurrentInventorySnapshot> currentInventorySnapshots = new HashSet<>();
                for(Object[] row : rows){
                        currentInventorySnapshots.add(this.createCustomCurrentInventorySnapshop(row));
                }
                return currentInventorySnapshots;
        }
        
        private CustomCurrentInventorySnapshot createCustomCurrentInventorySnapshop(Object[] row)
        {
                CustomCurrentInventorySnapshot currentInventorySnapshot = new CustomCurrentInventorySnapshot();
                currentInventorySnapshot.setItemId((Integer)row[0]);
                currentInventorySnapshot.setAvailability((Integer)row[1]);
                currentInventorySnapshot.setBrand((String)row[2]);
                currentInventorySnapshot.setModelName((String)row[3]);
                currentInventorySnapshot.setModelNumber((String)row[4]);
                currentInventorySnapshot.setColor((String)row[5]);
                return currentInventorySnapshot;
        }
        @RequestMapping(value = "/checkItemAvailability")
        public String getItemAvailability(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ITEM_ID) int itemId, @ModelAttribute Model model) throws Exception{
                FofoDetails fofoDetails;
                try {
                        fofoDetails = cookiesFetcher.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                try{
                        FofoItemId fofoItemId = new FofoItemId();
                        fofoItemId.setFofoId(fofoDetails.getFofoId());
                        fofoItemId.setItemId(itemId);
                        Object[] row = currentInventorySnapshotRepository.selectCustomByFofoItemId(fofoItemId);
                        model.addAttribute("currentInventorySnapshots", this.createCustomCurrentInventorySnapshop(row));
                        return "dashboard";
                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("INVNTRY_SNPSHT", false, "/dashboard"));
                        return "response";
                }
        }
        
}