Subversion Repositories SmartDukaan

Rev

Rev 23343 | Rev 23506 | 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;
23343 ashik.ali 35
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
22860 ashik.ali 36
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
23020 ashik.ali 37
import com.spice.profitmandi.service.inventory.InventoryService;
22860 ashik.ali 38
import com.spice.profitmandi.service.scheme.SchemeService;
39
import com.spice.profitmandi.web.model.LoginDetails;
40
import com.spice.profitmandi.web.util.CookiesProcessor;
41
 
42
@Controller
43
@Transactional(rollbackFor=Throwable.class)
44
public class SchemeController {
45
 
46
	private static final Logger LOGGER = LoggerFactory.getLogger(SchemeController.class);
47
 
48
	@Autowired
22927 ashik.ali 49
	private SchemeService schemeService;
22860 ashik.ali 50
 
51
	@Autowired
22927 ashik.ali 52
	private SchemeRepository schemeRepository;
22860 ashik.ali 53
 
54
	@Autowired
22927 ashik.ali 55
	private CookiesProcessor cookiesProcessor;
23020 ashik.ali 56
 
57
	@Autowired
58
	private InventoryService inventoryService;
22860 ashik.ali 59
 
60
	@RequestMapping(value = "/createScheme", method = RequestMethod.GET)
22927 ashik.ali 61
	public String createScheme(HttpServletRequest request, Model model){
23020 ashik.ali 62
		//Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllItemIdItemDescriptionMap();
63
		//model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
23419 ashik.ali 64
		model.addAttribute("brands", inventoryService.getAllTagListingBrands());
22860 ashik.ali 65
		return "create-scheme";
66
	}
67
 
23419 ashik.ali 68
	@RequestMapping(value = "/getTagListingItemsByBrand", method = RequestMethod.GET)
69
	public String getTagListingItemsByBrand(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.BRAND) String brand, Model model){
70
		Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllTagListingItemIdItemDescriptionMap(brand);
23020 ashik.ali 71
		model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
72
		//model.addAttribute("brands", inventoryService.getAllBrands());
23419 ashik.ali 73
		return "tag-listing-items-description";
23020 ashik.ali 74
	}
75
 
76
 
22860 ashik.ali 77
	@RequestMapping(value = "/createScheme", method = RequestMethod.POST)
22927 ashik.ali 78
	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{
79
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
22860 ashik.ali 80
		LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);
22927 ashik.ali 81
		schemeService.saveScheme(loginDetails.getFofoId(), createSchemeRequest);
82
		LOGGER.info("Scheme saved successfully");
23271 ashik.ali 83
		long size = schemeRepository.selectAllCount();
22927 ashik.ali 84
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
85
		model.addAttribute("schemes", schemes);
86
		model.addAttribute("start", offset + 1);
23271 ashik.ali 87
		model.addAttribute("size", size);
22927 ashik.ali 88
		if (schemes.size() < limit){
89
			model.addAttribute("end", offset + schemes.size());
22860 ashik.ali 90
		}
22927 ashik.ali 91
		else{
92
			model.addAttribute("end", offset + limit);
93
		}
94
		return "schemes";
95
 
22860 ashik.ali 96
	}
97
 
98
	@RequestMapping(value = "/getSchemes", method = RequestMethod.GET)
23343 ashik.ali 99
	public String getSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
100
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
101
		List<Scheme> schemes = null;
102
		long size = 0;
103
		if(loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)){
104
			schemes = schemeRepository.selectAll(offset, limit);
105
			size = schemeRepository.selectAllCount();
106
		}else{
107
			schemes = schemeRepository.selectActiveAll(offset, limit);
108
			size = schemeRepository.selectAllActiveCount();
109
		}
22860 ashik.ali 110
		model.addAttribute("schemes", schemes);
111
		model.addAttribute("start", offset + 1);
23271 ashik.ali 112
		model.addAttribute("size", size);
22860 ashik.ali 113
		if (schemes.size() < limit){
114
			model.addAttribute("end", offset + schemes.size());
115
		}
116
		else{
117
			model.addAttribute("end", offset + limit);
118
		}
23343 ashik.ali 119
		//model.addAttribute("roleTypes", loginDetails.getRoleTypes());
22860 ashik.ali 120
		return "schemes";
121
	}
122
 
123
	@RequestMapping(value = "/getPaginatedSchemes", method = RequestMethod.GET)
23343 ashik.ali 124
	public String getPaginatedSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
125
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
23271 ashik.ali 126
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
23343 ashik.ali 127
		List<Scheme> schemes = null;
128
		if(loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)){
129
			schemes = schemeRepository.selectAll(offset, limit);
130
		}else{
131
			schemes = schemeRepository.selectActiveAll(offset, limit);
132
		}
22860 ashik.ali 133
		model.addAttribute("schemes", schemes);
23343 ashik.ali 134
		model.addAttribute("roleTypes", loginDetails.getRoleTypes());
22860 ashik.ali 135
		return "schemes-paginated";
136
	}
137
 
23020 ashik.ali 138
	@RequestMapping(value = "/schemes/downloadPage", method = RequestMethod.GET)
139
	public String downloadPage(HttpServletRequest request, Model model){
140
		return "schemes-download";
141
	}
142
 
143
	@RequestMapping(value = "/schemes/download", method = RequestMethod.GET)
144
	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{
145
		LocalDateTime startDateTime = StringUtils.toDateTime(startDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
146
		LocalDateTime endDateTime = StringUtils.toDateTime(endDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
147
 
148
		List<SchemeModel> schemeModels = schemeService.getAllSchemeModels(startDateTime, endDateTime);
149
 
150
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
151
		ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
152
 
153
		final HttpHeaders headers=new HttpHeaders();
154
        headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
155
		headers.set("Content-disposition", "inline; filename=SchemesReport.xlsx");
156
        headers.setContentLength(byteArrayOutputStream.toByteArray().length);
157
        final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
158
        final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
159
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
160
 
161
		//return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
162
	}
163
 
22860 ashik.ali 164
	@RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)
22927 ashik.ali 165
	public String getSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model)  throws ProfitMandiBusinessException{
23343 ashik.ali 166
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
22860 ashik.ali 167
		Scheme scheme = schemeService.getSchemeById(schemeId);
168
		model.addAttribute("scheme", scheme);
23343 ashik.ali 169
		model.addAttribute("roleTypes", loginDetails.getRoleTypes());
22860 ashik.ali 170
		return "scheme-details";
171
	}
172
 
173
	@RequestMapping(value = "/activeSchemeById", method = RequestMethod.PUT)
22927 ashik.ali 174
	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 175
		schemeService.activeSchemeById(schemeId);
176
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
177
		model.addAttribute("schemes", schemes);
178
		return "schemes-paginated";
179
	}
180
 
181
 
182
	@RequestMapping(value = "/expireSchemeById", method = RequestMethod.PUT)
22927 ashik.ali 183
	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 184
		schemeService.expireSchemeById(schemeId);
185
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
186
		model.addAttribute("schemes", schemes);
187
		return "schemes-paginated";
188
	}
189
}