Subversion Repositories SmartDukaan

Rev

Rev 23570 | Rev 23752 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23570 Rev 23715
Line 46... Line 46...
46
import com.spice.profitmandi.web.model.LoginDetails;
46
import com.spice.profitmandi.web.model.LoginDetails;
47
import com.spice.profitmandi.web.util.CookiesProcessor;
47
import com.spice.profitmandi.web.util.CookiesProcessor;
48
import com.spice.profitmandi.web.util.MVCResponseSender;
48
import com.spice.profitmandi.web.util.MVCResponseSender;
49
 
49
 
50
@Controller
50
@Controller
51
@Transactional(rollbackFor=Throwable.class)
51
@Transactional(rollbackFor = Throwable.class)
52
public class SchemeController {
52
public class SchemeController {
53
 
53
 
54
	private static final Logger LOGGER = LogManager.getLogger(SchemeController.class);
54
	private static final Logger LOGGER = LogManager.getLogger(SchemeController.class);
55
 
55
 
56
	@Autowired
56
	@Autowired
57
	private SchemeService schemeService;
57
	private SchemeService schemeService;
58
	
58
 
59
	@Autowired
59
	@Autowired
60
	private SchemeRepository schemeRepository;
60
	private SchemeRepository schemeRepository;
61
 
61
 
62
	@Autowired
62
	@Autowired
63
	private SchemeItemRepository schemeItemRepository;
63
	private SchemeItemRepository schemeItemRepository;
64
	
64
 
65
	@Autowired
65
	@Autowired
66
	private MVCResponseSender mvcResponseSender;
66
	private MVCResponseSender mvcResponseSender;
67
	
67
 
68
	@Autowired
68
	@Autowired
69
	private CookiesProcessor cookiesProcessor;
69
	private CookiesProcessor cookiesProcessor;
70
	
70
 
71
	@Autowired
71
	@Autowired
72
	private InventoryService inventoryService;
72
	private InventoryService inventoryService;
73
 
73
 
74
	@Autowired
74
	@Autowired
75
	private TagListingRepository tagListingRepository;
75
	private TagListingRepository tagListingRepository;
76
	
76
 
77
	@Autowired
77
	@Autowired
78
	PurchaseRepository purchaseRepository;
78
	PurchaseRepository purchaseRepository;
79
 
79
 
80
	@RequestMapping(value = "/createScheme", method = RequestMethod.GET)
80
	@RequestMapping(value = "/createScheme", method = RequestMethod.GET)
81
	public String createScheme(HttpServletRequest request, Model model){
81
	public String createScheme(HttpServletRequest request, Model model) {
-
 
82
		// Map<Integer, String> itemIdItemDescriptionMap =
82
		//Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllItemIdItemDescriptionMap();
83
		// inventoryService.getAllItemIdItemDescriptionMap();
83
		//model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
84
		// model.addAttribute("itemIdItemDescriptionMap",
-
 
85
		// itemIdItemDescriptionMap);
84
		model.addAttribute("brands", inventoryService.getAllTagListingBrands());
86
		model.addAttribute("brands", inventoryService.getAllTagListingBrands());
85
		return "create-scheme";
87
		return "create-scheme";
86
	}
88
	}
87
	
89
 
88
	@RequestMapping(value = "/getTagListingItemsByBrand", method = RequestMethod.GET)
90
	@RequestMapping(value = "/getTagListingItemsByBrand", method = RequestMethod.GET)
-
 
91
	public String getTagListingItemsByBrand(HttpServletRequest request,
89
	public String getTagListingItemsByBrand(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.BRAND) String brand, Model model){
92
			@RequestParam(name = ProfitMandiConstants.BRAND) String brand, Model model) {
90
		Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllTagListingItemIdItemDescriptionMap(brand);
93
		Map<Integer, String> itemIdItemDescriptionMap = inventoryService
-
 
94
				.getAllTagListingItemIdItemDescriptionMap(brand);
91
		model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
95
		model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
92
		//model.addAttribute("brands", inventoryService.getAllBrands());
96
		// model.addAttribute("brands", inventoryService.getAllBrands());
93
		return "tag-listing-items-description";
97
		return "tag-listing-items-description";
94
	}
98
	}
95
 
99
 
96
	@RequestMapping(value = "/schemes/update-schemes-page", method = RequestMethod.GET)
100
	@RequestMapping(value = "/schemes/update-schemes-page", method = RequestMethod.GET)
97
	public String updateShcemes(HttpServletRequest request) throws ProfitMandiBusinessException {
101
	public String updateShcemes(HttpServletRequest request) throws ProfitMandiBusinessException {
98
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
102
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
99
		if(!loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)) {
103
		if (!loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)) {
100
			throw new ProfitMandiBusinessException("User", loginDetails.getEmailId(), "Unauthorised access");
104
			throw new ProfitMandiBusinessException("User", loginDetails.getEmailId(), "Unauthorised access");
101
		}
105
		}
102
		return "update-schemes-page";
106
		return "update-schemes-page";
103
	}
107
	}
104
	
108
 
105
	@RequestMapping(value = "/schemes/update", method = RequestMethod.POST)
109
	@RequestMapping(value = "/schemes/update", method = RequestMethod.POST)
106
	public String updateShcemes(HttpServletRequest request, @RequestBody SchemeItems schemeItems, Model model) throws Exception {
110
	public String updateShcemes(HttpServletRequest request, @RequestBody SchemeItems schemeItems, Model model)
-
 
111
			throws Exception {
107
		for(int schemeId : schemeItems.getSchemeIds()) {
112
		for (int schemeId : schemeItems.getSchemeIds()) {
108
			if(schemeRepository.selectById(schemeId) != null)
113
			if (schemeRepository.selectById(schemeId) != null)
109
			for(int itemId : schemeItems.getItemIds()) {
114
				for (int itemId : schemeItems.getItemIds()) {
110
				if(tagListingRepository.selectByItemIdsAndTagIds(new HashSet<>(Arrays.asList(itemId)), new HashSet<>(Arrays.asList(4,7))).size() > 0) {
115
					if (tagListingRepository.selectByItemIdsAndTagIds(new HashSet<>(Arrays.asList(itemId)),
-
 
116
							new HashSet<>(Arrays.asList(4, 7))).size() > 0) {
111
					SchemeItem si = new SchemeItem();
117
						SchemeItem si = new SchemeItem();
112
					si.setItemId(itemId);
118
						si.setItemId(itemId);
113
					si.setSchemeId(schemeId);
119
						si.setSchemeId(schemeId);
114
					try {
120
						try {
115
						schemeItemRepository.persist(si);
121
							schemeItemRepository.persist(si);
116
					} catch (Exception e) {
122
						} catch (Exception e) {
117
						LOGGER.info("Scheme aleady exist");
123
							LOGGER.info("Scheme aleady exist");
-
 
124
						}
-
 
125
						model.addAttribute("response", mvcResponseSender.createResponseString(true));
-
 
126
					} else {
-
 
127
						model.addAttribute("response", mvcResponseSender.createResponseString(false));
-
 
128
						throw new ProfitMandiBusinessException("ItemId", itemId, "Invalid Item Id");
118
					}
129
					}
-
 
130
				}
-
 
131
		}
-
 
132
		return "response";
-
 
133
	}
-
 
134
	@RequestMapping(value = "/schemes/delete", method = RequestMethod.POST)
-
 
135
	public String deleteShcemes(HttpServletRequest request, @RequestBody SchemeItems schemeItems, Model model)
-
 
136
			throws Exception {
-
 
137
		if(schemeItemRepository.selectBySchemeIdsAndItemIds(new HashSet<>(schemeItems.getSchemeIds()), new HashSet<>(schemeItems.getItemIds())).size()>0)
-
 
138
		{
-
 
139
			for(int schemeId:schemeItems.getSchemeIds())
-
 
140
			{
-
 
141
				for(int itemId:schemeItems.getItemIds())
-
 
142
				{
-
 
143
					schemeItemRepository.deletebyItemIdsandSchemeIds(itemId, schemeId);
119
					model.addAttribute("response", mvcResponseSender.createResponseString(true));
144
					model.addAttribute("response", mvcResponseSender.createResponseString(true));
120
				} else {
-
 
121
					model.addAttribute("response", mvcResponseSender.createResponseString(false));
-
 
122
					throw new ProfitMandiBusinessException("ItemId", itemId,"Invalid Item Id");
-
 
123
				}
145
				}
124
			}
146
			}
125
		}
147
		}
-
 
148
		else {
-
 
149
			throw new ProfitMandiBusinessException("ItemId", schemeItems.getItemIds(),"invalid Item Id");
-
 
150
		}
126
		return "response";
151
		return "response";
127
	}
152
	}
128
	
-
 
129
	
-
 
130
	@RequestMapping(value = "/createScheme", method = RequestMethod.POST)
153
	@RequestMapping(value = "/createScheme", method = RequestMethod.POST)
131
	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{
154
	public String createScheme(HttpServletRequest request, @RequestBody CreateSchemeRequest createSchemeRequest,
-
 
155
			@RequestParam(name = "offset", defaultValue = "0") int offset,
-
 
156
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
-
 
157
			throws ProfitMandiBusinessException {
132
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
158
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
133
		LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);
159
		LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);
134
		schemeService.saveScheme(loginDetails.getFofoId(), createSchemeRequest);
160
		schemeService.saveScheme(loginDetails.getFofoId(), createSchemeRequest);
135
		LOGGER.info("Scheme saved successfully");
161
		LOGGER.info("Scheme saved successfully");
136
		long size = schemeRepository.selectAllCount();
162
		long size = schemeRepository.selectAllCount();
137
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
163
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
138
		model.addAttribute("schemes", schemes);
164
		model.addAttribute("schemes", schemes);
139
		model.addAttribute("start", offset + 1);
165
		model.addAttribute("start", offset + 1);
140
		model.addAttribute("size", size);
166
		model.addAttribute("size", size);
141
		if (schemes.size() < limit){
167
		if (schemes.size() < limit) {
142
			model.addAttribute("end", offset + schemes.size());
168
			model.addAttribute("end", offset + schemes.size());
143
		}
-
 
144
		else{
169
		} else {
145
			model.addAttribute("end", offset + limit);
170
			model.addAttribute("end", offset + limit);
146
		}
171
		}
147
		return "schemes";
172
		return "schemes";
148
		
173
 
149
	}
174
	}
150
	
175
 
151
	@RequestMapping(value = "/getSchemes", method = RequestMethod.GET)
176
	@RequestMapping(value = "/getSchemes", method = RequestMethod.GET)
152
	public String getSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
177
	public String getSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset,
-
 
178
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
-
 
179
			throws ProfitMandiBusinessException {
153
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
180
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
154
		List<Scheme> schemes = null;
181
		List<Scheme> schemes = null;
155
		long size = 0;
182
		long size = 0;
156
		if(loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)){
183
		if (loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)) {
157
			schemes = schemeRepository.selectAll(offset, limit);
184
			schemes = schemeRepository.selectAll(offset, limit);
158
			size = schemeRepository.selectAllCount();
185
			size = schemeRepository.selectAllCount();
159
		}else{
186
		} else {
160
			schemes = schemeRepository.selectActiveAll(offset, limit);
187
			schemes = schemeRepository.selectActiveAll(offset, limit);
161
			size = schemeRepository.selectAllActiveCount();
188
			size = schemeRepository.selectAllActiveCount();
162
		}
189
		}
163
		model.addAttribute("schemes", schemes);
190
		model.addAttribute("schemes", schemes);
164
		model.addAttribute("start", offset + 1);
191
		model.addAttribute("start", offset + 1);
165
		model.addAttribute("size", size);
192
		model.addAttribute("size", size);
166
		if (schemes.size() < limit){
193
		if (schemes.size() < limit) {
167
			model.addAttribute("end", offset + schemes.size());
194
			model.addAttribute("end", offset + schemes.size());
168
		}
-
 
169
		else{
195
		} else {
170
			model.addAttribute("end", offset + limit);
196
			model.addAttribute("end", offset + limit);
171
		}
197
		}
172
		//model.addAttribute("roleTypes", loginDetails.getRoleTypes());
198
		// model.addAttribute("roleTypes", loginDetails.getRoleTypes());
173
		return "schemes";
199
		return "schemes";
174
	}
200
	}
175
	
201
 
176
	@RequestMapping(value = "/getPaginatedSchemes", method = RequestMethod.GET)
202
	@RequestMapping(value = "/getPaginatedSchemes", method = RequestMethod.GET)
-
 
203
	public String getPaginatedSchemes(HttpServletRequest request,
-
 
204
			@RequestParam(name = "offset", defaultValue = "0") int offset,
177
	public String getPaginatedSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
205
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
-
 
206
			throws ProfitMandiBusinessException {
178
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
207
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
179
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
208
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
180
		List<Scheme> schemes = null;
209
		List<Scheme> schemes = null;
181
		if(loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)){
210
		if (loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)) {
182
			schemes = schemeRepository.selectAll(offset, limit);
211
			schemes = schemeRepository.selectAll(offset, limit);
183
		}else{
212
		} else {
184
			schemes = schemeRepository.selectActiveAll(offset, limit);
213
			schemes = schemeRepository.selectActiveAll(offset, limit);
185
		}
214
		}
186
		model.addAttribute("schemes", schemes);
215
		model.addAttribute("schemes", schemes);
187
		model.addAttribute("roleTypes", loginDetails.getRoleTypes());
216
		model.addAttribute("roleTypes", loginDetails.getRoleTypes());
188
		return "schemes-paginated";
217
		return "schemes-paginated";
189
	}
218
	}
190
	
219
 
191
	@RequestMapping(value = "/schemes/downloadPage", method = RequestMethod.GET)
220
	@RequestMapping(value = "/schemes/downloadPage", method = RequestMethod.GET)
192
	public String downloadPage(HttpServletRequest request, Model model){
221
	public String downloadPage(HttpServletRequest request, Model model) {
193
		return "schemes-download";
222
		return "schemes-download";
194
	}
223
	}
195
	
224
 
196
	@RequestMapping(value = "/schemes/download", method = RequestMethod.GET)
225
	@RequestMapping(value = "/schemes/download", method = RequestMethod.GET)
-
 
226
	public ResponseEntity<?> downloadInventoryItemAgingByInterval(HttpServletRequest request,
-
 
227
			@RequestParam(name = ProfitMandiConstants.START_DATE_TIME) String startDateTimeString,
197
	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{
228
			@RequestParam(name = ProfitMandiConstants.END_DATE_TIME) String endDateTimeString, Model model)
-
 
229
			throws ProfitMandiBusinessException {
198
		LocalDateTime startDateTime = StringUtils.toDateTime(startDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
230
		LocalDateTime startDateTime = StringUtils.toDateTime(startDateTimeString,
-
 
231
				DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
199
		LocalDateTime endDateTime = StringUtils.toDateTime(endDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
232
		LocalDateTime endDateTime = StringUtils.toDateTime(endDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
200
		
233
 
201
		List<SchemeModel> schemeModels = schemeService.getAllSchemeModels(startDateTime, endDateTime);
234
		List<SchemeModel> schemeModels = schemeService.getAllSchemeModels(startDateTime, endDateTime);
202
		
235
 
203
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
236
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
204
		ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
237
		ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
205
		
238
 
206
		final HttpHeaders headers=new HttpHeaders();
239
		final HttpHeaders headers = new HttpHeaders();
207
        headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
240
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
208
		headers.set("Content-disposition", "inline; filename=SchemesReport.xlsx");
241
		headers.set("Content-disposition", "inline; filename=SchemesReport.xlsx");
209
        headers.setContentLength(byteArrayOutputStream.toByteArray().length);
242
		headers.setContentLength(byteArrayOutputStream.toByteArray().length);
210
        final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
243
		final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
211
        final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
244
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
212
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
245
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
213
		
246
 
-
 
247
		// return
214
		//return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
248
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
215
	}
249
	}
216
	
250
 
217
	@RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)
251
	@RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)
-
 
252
	public String getSchemeById(HttpServletRequest request,
218
	public String getSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model)  throws ProfitMandiBusinessException{
253
			@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model)
-
 
254
			throws ProfitMandiBusinessException {
219
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
255
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
220
		Scheme scheme = schemeService.getSchemeById(schemeId);
256
		Scheme scheme = schemeService.getSchemeById(schemeId);
221
		model.addAttribute("scheme", scheme);
257
		model.addAttribute("scheme", scheme);
222
		model.addAttribute("roleTypes", loginDetails.getRoleTypes());
258
		model.addAttribute("roleTypes", loginDetails.getRoleTypes());
223
		return "scheme-details";
259
		return "scheme-details";
224
	}
260
	}
225
	
261
 
226
	@RequestMapping(value = "/activeSchemeById", method = RequestMethod.PUT)
262
	@RequestMapping(value = "/activeSchemeById", method = RequestMethod.PUT)
-
 
263
	public String activeSchemeById(HttpServletRequest request,
-
 
264
			@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId,
-
 
265
			@RequestParam(name = "offset", defaultValue = "0") int offset,
227
	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{
266
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
-
 
267
			throws ProfitMandiBusinessException {
228
		schemeService.activeSchemeById(schemeId);
268
		schemeService.activeSchemeById(schemeId);
229
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
269
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
230
		model.addAttribute("schemes", schemes);
270
		model.addAttribute("schemes", schemes);
231
		return "schemes-paginated";
271
		return "schemes-paginated";
232
	}
272
	}
233
	
-
 
234
	
273
 
235
	@RequestMapping(value = "/expireSchemeById", method = RequestMethod.PUT)
274
	@RequestMapping(value = "/expireSchemeById", method = RequestMethod.PUT)
-
 
275
	public String expireSchemeById(HttpServletRequest request,
-
 
276
			@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId,
-
 
277
			@RequestParam(name = "offset", defaultValue = "0") int offset,
236
	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{
278
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
-
 
279
			throws ProfitMandiBusinessException {
237
		schemeService.expireSchemeById(schemeId);
280
		schemeService.expireSchemeById(schemeId);
238
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
281
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
239
		model.addAttribute("schemes", schemes);
282
		model.addAttribute("schemes", schemes);
240
		return "schemes-paginated";
283
		return "schemes-paginated";
241
	}
284
	}