Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
22860 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
23020 ashik.ali 3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.InputStream;
6
import java.time.LocalDateTime;
22860 ashik.ali 7
import java.util.List;
23020 ashik.ali 8
import java.util.Map;
22860 ashik.ali 9
 
10
import javax.servlet.http.HttpServletRequest;
11
 
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14
import org.springframework.beans.factory.annotation.Autowired;
23020 ashik.ali 15
import org.springframework.core.io.InputStreamResource;
16
import org.springframework.http.HttpHeaders;
17
import org.springframework.http.HttpStatus;
18
import org.springframework.http.ResponseEntity;
22860 ashik.ali 19
import org.springframework.stereotype.Controller;
20
import org.springframework.transaction.annotation.Transactional;
21
import org.springframework.ui.Model;
22
import org.springframework.web.bind.annotation.RequestBody;
23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
25
import org.springframework.web.bind.annotation.RequestParam;
26
 
23020 ashik.ali 27
import com.spice.profitmandi.common.enumuration.DateTimePattern;
22860 ashik.ali 28
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
29
import com.spice.profitmandi.common.model.CreateSchemeRequest;
30
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23020 ashik.ali 31
import com.spice.profitmandi.common.model.SchemeModel;
32
import com.spice.profitmandi.common.util.ExcelUtils;
33
import com.spice.profitmandi.common.util.StringUtils;
22860 ashik.ali 34
import com.spice.profitmandi.dao.entity.catalog.Scheme;
35
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
23020 ashik.ali 36
import com.spice.profitmandi.service.inventory.InventoryService;
22860 ashik.ali 37
import com.spice.profitmandi.service.scheme.SchemeService;
38
import com.spice.profitmandi.web.model.LoginDetails;
39
import com.spice.profitmandi.web.util.CookiesProcessor;
40
 
41
@Controller
42
@Transactional(rollbackFor=Throwable.class)
43
public class SchemeController {
44
 
45
	private static final Logger LOGGER = LoggerFactory.getLogger(SchemeController.class);
46
 
47
	@Autowired
22927 ashik.ali 48
	private SchemeService schemeService;
22860 ashik.ali 49
 
50
	@Autowired
22927 ashik.ali 51
	private SchemeRepository schemeRepository;
22860 ashik.ali 52
 
53
	@Autowired
22927 ashik.ali 54
	private CookiesProcessor cookiesProcessor;
23020 ashik.ali 55
 
56
	@Autowired
57
	private InventoryService inventoryService;
22860 ashik.ali 58
 
59
	@RequestMapping(value = "/createScheme", method = RequestMethod.GET)
22927 ashik.ali 60
	public String createScheme(HttpServletRequest request, Model model){
23020 ashik.ali 61
		//Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllItemIdItemDescriptionMap();
62
		//model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
63
		model.addAttribute("brands", inventoryService.getAllBrands());
22860 ashik.ali 64
		return "create-scheme";
65
	}
66
 
23020 ashik.ali 67
	@RequestMapping(value = "/getItemsByBrand", method = RequestMethod.GET)
68
	public String getItemsByBrand(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.BRAND) String brand, Model model){
69
		Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllItemIdItemDescriptionMap(brand);
70
		model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
71
		//model.addAttribute("brands", inventoryService.getAllBrands());
72
		return "items-description";
73
	}
74
 
75
 
22860 ashik.ali 76
	@RequestMapping(value = "/createScheme", method = RequestMethod.POST)
22927 ashik.ali 77
	public String createScheme(HttpServletRequest request, @RequestBody CreateSchemeRequest createSchemeRequest, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)  throws ProfitMandiBusinessException{
78
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
22860 ashik.ali 79
		LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);
22927 ashik.ali 80
		schemeService.saveScheme(loginDetails.getFofoId(), createSchemeRequest);
81
		LOGGER.info("Scheme saved successfully");
23271 ashik.ali 82
		long size = schemeRepository.selectAllCount();
22927 ashik.ali 83
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
84
		model.addAttribute("schemes", schemes);
85
		model.addAttribute("start", offset + 1);
23271 ashik.ali 86
		model.addAttribute("size", size);
22927 ashik.ali 87
		if (schemes.size() < limit){
88
			model.addAttribute("end", offset + schemes.size());
22860 ashik.ali 89
		}
22927 ashik.ali 90
		else{
91
			model.addAttribute("end", offset + limit);
92
		}
93
		return "schemes";
94
 
22860 ashik.ali 95
	}
96
 
97
	@RequestMapping(value = "/getSchemes", method = RequestMethod.GET)
22927 ashik.ali 98
	public String getSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model){
22860 ashik.ali 99
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
23271 ashik.ali 100
		long size = schemeRepository.selectAllCount();
22860 ashik.ali 101
		model.addAttribute("schemes", schemes);
102
		model.addAttribute("start", offset + 1);
23271 ashik.ali 103
		model.addAttribute("size", size);
22860 ashik.ali 104
		if (schemes.size() < limit){
105
			model.addAttribute("end", offset + schemes.size());
106
		}
107
		else{
108
			model.addAttribute("end", offset + limit);
109
		}
110
		return "schemes";
111
	}
112
 
113
	@RequestMapping(value = "/getPaginatedSchemes", method = RequestMethod.GET)
22927 ashik.ali 114
	public String getPaginatedSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model){
23271 ashik.ali 115
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
22860 ashik.ali 116
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
117
		model.addAttribute("schemes", schemes);
118
		return "schemes-paginated";
119
	}
120
 
23020 ashik.ali 121
	@RequestMapping(value = "/schemes/downloadPage", method = RequestMethod.GET)
122
	public String downloadPage(HttpServletRequest request, Model model){
123
		return "schemes-download";
124
	}
125
 
126
	@RequestMapping(value = "/schemes/download", method = RequestMethod.GET)
127
	public ResponseEntity<?> downloadInventoryItemAgingByInterval(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.START_DATE_TIME) String startDateTimeString, @RequestParam(name = ProfitMandiConstants.END_DATE_TIME) String endDateTimeString, Model model) throws ProfitMandiBusinessException{
128
		LocalDateTime startDateTime = StringUtils.toDateTime(startDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
129
		LocalDateTime endDateTime = StringUtils.toDateTime(endDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
130
 
131
		List<SchemeModel> schemeModels = schemeService.getAllSchemeModels(startDateTime, endDateTime);
132
 
133
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
134
		ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
135
 
136
		final HttpHeaders headers=new HttpHeaders();
137
        headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
138
		headers.set("Content-disposition", "inline; filename=SchemesReport.xlsx");
139
        headers.setContentLength(byteArrayOutputStream.toByteArray().length);
140
        final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
141
        final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
142
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
143
 
144
		//return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
145
	}
146
 
22860 ashik.ali 147
	@RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)
22927 ashik.ali 148
	public String getSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model)  throws ProfitMandiBusinessException{
22860 ashik.ali 149
		Scheme scheme = schemeService.getSchemeById(schemeId);
150
		model.addAttribute("scheme", scheme);
151
		return "scheme-details";
152
	}
153
 
154
	@RequestMapping(value = "/activeSchemeById", method = RequestMethod.PUT)
22927 ashik.ali 155
	public String activeSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)  throws ProfitMandiBusinessException{
22860 ashik.ali 156
		schemeService.activeSchemeById(schemeId);
157
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
158
		model.addAttribute("schemes", schemes);
159
		return "schemes-paginated";
160
	}
161
 
162
 
163
	@RequestMapping(value = "/expireSchemeById", method = RequestMethod.PUT)
22927 ashik.ali 164
	public String expireSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)  throws ProfitMandiBusinessException{
22860 ashik.ali 165
		schemeService.expireSchemeById(schemeId);
166
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
167
		model.addAttribute("schemes", schemes);
168
		return "schemes-paginated";
169
	}
170
}