Subversion Repositories SmartDukaan

Rev

Rev 26158 | Rev 26298 | 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.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spice.profitmandi.common.enumuration.ReporticoProject;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.services.ReporticoService;
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
import com.spice.profitmandi.service.authentication.RoleManager;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;

@Controller
@Transactional(rollbackFor=Throwable.class)
public class ReportsController {
        
        @Autowired
        private RoleRepository roleRepository;
        
        @Autowired
        private CookiesProcessor cookiesProcessor; 
        
        @Autowired
        private ReporticoService reporticoService;

        @Autowired
        private RoleManager roleManager;
        
        private static final Logger log = LogManager.getLogger(OrderController.class);

        @RequestMapping(value = "/reports/{projectName}/{fileName}")
        public ResponseEntity<?> fetchReport(HttpServletRequest request, @PathVariable String fileName,
                        @PathVariable ReporticoProject projectName, @RequestBody(required=false) Map<String, String> paramsMap)
                        throws ProfitMandiBusinessException, UnsupportedOperationException, IOException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                HttpResponse response;
            if (roleManager.isAdmin(loginDetails.getRoleIds())) {
                } else {
                        if(paramsMap==null) {
                                paramsMap = new HashMap<String, String>();
                        }
                        paramsMap.put("MANUAL_fofoId", loginDetails.getFofoId() + "");
                }
            response = getAdminReportFile(loginDetails.getEmailId(), projectName, fileName + ".xml", paramsMap);
                HttpHeaders headers = new HttpHeaders();
                InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
                headers.set("Content-Type", "application/vnd.ms-excel");
                headers.set("Content-disposition",
                                "inline; filename=report-" + fileName + ".csv");
                headers.setContentLength(response.getEntity().getContentLength());
                return new ResponseEntity<InputStreamResource>(is, headers, HttpStatus.OK);
        }

        private HttpResponse getAdminReportFile(String email, ReporticoProject projectName, String fileName, Map<String, String> reportParams) throws ProfitMandiBusinessException,  IOException{
                return reporticoService.getReportFile(projectName, fileName, reportParams);
        }

        @RequestMapping(value = "/reports", method = RequestMethod.GET)
        public String reports(Model model) throws ProfitMandiBusinessException, UnsupportedOperationException, IOException {
                model.addAttribute("reporticoProjectMap", ReporticoProject.reporticoProjectMap);
                return "reports";
        }
}