Subversion Repositories SmartDukaan

Rev

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