Subversion Repositories SmartDukaan

Rev

Rev 23822 | Rev 26063 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23612 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.io.IOException;
4
 
5
import javax.servlet.http.HttpServletRequest;
6
 
7
import org.apache.http.HttpResponse;
23654 amit.gupta 8
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.Logger;
23637 amit.gupta 10
import org.springframework.beans.factory.annotation.Autowired;
23612 amit.gupta 11
import org.springframework.core.io.InputStreamResource;
12
import org.springframework.http.HttpHeaders;
13
import org.springframework.http.HttpStatus;
14
import org.springframework.http.ResponseEntity;
15
import org.springframework.stereotype.Controller;
23637 amit.gupta 16
import org.springframework.transaction.annotation.Transactional;
17
import org.springframework.ui.Model;
23612 amit.gupta 18
import org.springframework.web.bind.annotation.PathVariable;
19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestMethod;
21
 
22
import com.spice.profitmandi.common.enumuration.ReporticoProject;
23
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24
import com.spice.profitmandi.common.services.ReporticoService;
23784 ashik.ali 25
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
23798 amit.gupta 26
import com.spice.profitmandi.service.authentication.RoleManager;
23612 amit.gupta 27
import com.spice.profitmandi.web.model.LoginDetails;
28
import com.spice.profitmandi.web.util.CookiesProcessor;
29
 
30
@Controller
23784 ashik.ali 31
@Transactional(rollbackFor=Throwable.class)
23612 amit.gupta 32
public class ReportsController {
23784 ashik.ali 33
 
23637 amit.gupta 34
	@Autowired
23784 ashik.ali 35
	private RoleRepository roleRepository;
36
 
23764 amit.gupta 37
	@Autowired
23784 ashik.ali 38
	private CookiesProcessor cookiesProcessor; 
39
 
40
	@Autowired
23652 amit.gupta 41
	private ReporticoService reporticoService;
23764 amit.gupta 42
 
23798 amit.gupta 43
	@Autowired
44
	private RoleManager roleManager;
45
 
23654 amit.gupta 46
	private static final Logger log = LogManager.getLogger(OrderController.class);
23612 amit.gupta 47
 
48
	@RequestMapping(value = "/reports/{projectName}/{fileName}", method = RequestMethod.GET)
23764 amit.gupta 49
	public ResponseEntity<?> fetchReport(HttpServletRequest request, @PathVariable String fileName,
50
			@PathVariable ReporticoProject projectName)
51
			throws ProfitMandiBusinessException, UnsupportedOperationException, IOException {
23612 amit.gupta 52
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
53
		HttpResponse response;
23798 amit.gupta 54
	    if (roleManager.isAdmin(loginDetails.getRoleIds())) {
23764 amit.gupta 55
			response = reporticoService.getReportFile(projectName, fileName + ".xml");
23612 amit.gupta 56
		} else {
23764 amit.gupta 57
			response = reporticoService.getReportFile(loginDetails.getFofoId(), projectName, fileName + ".xml");
23612 amit.gupta 58
		}
59
		HttpHeaders headers = new HttpHeaders();
23764 amit.gupta 60
		InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
23765 amit.gupta 61
		headers.set("Content-Type", "application/vnd.ms-excel");
62
		headers.set("Content-disposition",
23766 amit.gupta 63
				"inline; filename=report-" + fileName + ".csv");
64
		headers.setContentLength(response.getEntity().getContentLength());
23764 amit.gupta 65
		return new ResponseEntity<InputStreamResource>(is, headers, HttpStatus.OK);
23612 amit.gupta 66
	}
23637 amit.gupta 67
 
23822 amit.gupta 68
	@RequestMapping(value = "/reports", method = RequestMethod.GET)
23764 amit.gupta 69
	public String reports(Model model) throws ProfitMandiBusinessException, UnsupportedOperationException, IOException {
23637 amit.gupta 70
		model.addAttribute("reporticoProjectMap", ReporticoProject.reporticoProjectMap);
71
		return "reports";
72
	}
23612 amit.gupta 73
}