Subversion Repositories SmartDukaan

Rev

Rev 23366 | Rev 34861 | 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 javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spice.profitmandi.dao.model.FofoForm;
import com.spice.profitmandi.dao.repository.dtr.Mongo;
import com.spice.profitmandi.dao.util.FofoDocumentsGenerator;

@Controller
@Transactional
public class FofoController {
        
        private static final Logger LOGGER = LogManager.getLogger(FofoController.class);

        @Autowired
        private Mongo mongoClient;
        
        @Autowired
        private FofoDocumentsGenerator generator;
        
        @RequestMapping(value = "/fofo", method = RequestMethod.GET)
        public String getAll(HttpServletRequest request, Model model) throws Throwable {
                model.addAttribute("fofoForms", mongoClient.getFofoForms(0, 50));
                return "fofo-index";
        }
        
        @RequestMapping(value = "/fofo/{fofoId}/file-display", method = RequestMethod.GET)
        public ResponseEntity<byte[]> displayDocs(HttpServletRequest request, @PathVariable(name = "fofoId") int fofoId) throws Throwable{
                HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("application/pdf"));
            String filename = "output.pdf";
            headers.setContentDispositionFormData(filename, filename);
            headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
            byte[] contents = generator.getDocumentStream(fofoId);
                ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
            return response;
        }
        
        @RequestMapping(value = "/fofo/{fofoId}/edit", method = RequestMethod.GET)
        public String editFofoForm(HttpServletRequest request, @PathVariable(name = "fofoId") int fofoId, Model model) throws Exception{
                FofoForm ff = mongoClient.getFofoForm(fofoId);
                model.addAttribute("fofoForm", mongoClient.getFofoFormJsonStringByFofoId(fofoId));
                model.addAttribute("email", ff.getRegisteredEmail1());
                return "fofo-form";
        }
        
}