Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21615 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
25651 tejbeer 3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
25221 amit.gupta 6
import java.text.DecimalFormat;
24288 amit.gupta 7
import java.time.LocalDate;
24276 amit.gupta 8
import java.time.LocalDateTime;
24880 govind 9
import java.time.LocalTime;
25649 tejbeer 10
import java.time.ZoneOffset;
24339 amit.gupta 11
import java.util.ArrayList;
25136 amit.gupta 12
import java.util.LinkedHashMap;
23884 amit.gupta 13
import java.util.List;
24880 govind 14
import java.util.Map;
15
import java.util.stream.Collectors;
23568 govind 16
 
22086 amit.gupta 17
import javax.servlet.http.HttpServletRequest;
25221 amit.gupta 18
import javax.transaction.Transactional;
22086 amit.gupta 19
 
23786 amit.gupta 20
import org.apache.logging.log4j.LogManager;
23568 govind 21
import org.apache.logging.log4j.Logger;
22481 ashik.ali 22
import org.springframework.beans.factory.annotation.Autowired;
23379 ashik.ali 23
import org.springframework.beans.factory.annotation.Value;
25651 tejbeer 24
import org.springframework.core.io.InputStreamResource;
25
import org.springframework.http.HttpHeaders;
26
import org.springframework.http.HttpStatus;
25649 tejbeer 27
import org.springframework.http.MediaType;
28
import org.springframework.http.ResponseEntity;
21615 kshitij.so 29
import org.springframework.stereotype.Controller;
22073 ashik.ali 30
import org.springframework.ui.Model;
21615 kshitij.so 31
import org.springframework.web.bind.annotation.RequestMapping;
32
import org.springframework.web.bind.annotation.RequestMethod;
25649 tejbeer 33
import org.springframework.web.bind.annotation.RequestParam;
21615 kshitij.so 34
 
25649 tejbeer 35
import com.google.gson.Gson;
24996 amit.gupta 36
import com.mongodb.DBObject;
25651 tejbeer 37
import com.spice.profitmandi.common.enumuration.ContentType;
22481 ashik.ali 38
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24996 amit.gupta 39
import com.spice.profitmandi.common.model.BrandStockPrice;
25649 tejbeer 40
import com.spice.profitmandi.common.model.Notification;
24203 amit.gupta 41
import com.spice.profitmandi.common.model.ProfitMandiConstants;
25649 tejbeer 42
import com.spice.profitmandi.common.model.SendNotificationModel;
25651 tejbeer 43
import com.spice.profitmandi.common.web.util.ResponseSender;
25649 tejbeer 44
import com.spice.profitmandi.dao.Interface.Campaign;
25651 tejbeer 45
import com.spice.profitmandi.dao.entity.dtr.Document;
25649 tejbeer 46
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
25651 tejbeer 47
import com.spice.profitmandi.dao.entity.dtr.Retailer;
25649 tejbeer 48
import com.spice.profitmandi.dao.entity.dtr.UserCampaign;
24880 govind 49
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
22654 ashik.ali 50
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
25649 tejbeer 51
import com.spice.profitmandi.dao.entity.fofo.HygieneData;
24288 amit.gupta 52
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
25214 amit.gupta 53
import com.spice.profitmandi.dao.entity.fofo.PartnerTarget;
54
import com.spice.profitmandi.dao.entity.fofo.PartnerTargetDetails;
25566 tejbeer 55
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
25649 tejbeer 56
import com.spice.profitmandi.dao.model.SimpleCampaign;
57
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
24880 govind 58
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
25651 tejbeer 59
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
22481 ashik.ali 60
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24880 govind 61
import com.spice.profitmandi.dao.repository.dtr.InsurancePolicyRepository;
62
import com.spice.profitmandi.dao.repository.dtr.InsuranceProviderRepository;
24996 amit.gupta 63
import com.spice.profitmandi.dao.repository.dtr.Mongo;
25649 tejbeer 64
import com.spice.profitmandi.dao.repository.dtr.NotificationCampaignRepository;
24098 tejbeer 65
import com.spice.profitmandi.dao.repository.dtr.NotificationPanelRepository;
24880 govind 66
import com.spice.profitmandi.dao.repository.dtr.RechargeTransactionRepository;
25649 tejbeer 67
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
68
import com.spice.profitmandi.dao.repository.dtr.UserCampaignRepository;
24203 amit.gupta 69
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
24880 govind 70
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
25649 tejbeer 71
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
24288 amit.gupta 72
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
25214 amit.gupta 73
import com.spice.profitmandi.dao.repository.fofo.PartnerTargetRepository;
25566 tejbeer 74
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
25214 amit.gupta 75
import com.spice.profitmandi.dao.repository.fofo.TargetSlabRepository;
25649 tejbeer 76
import com.spice.profitmandi.dao.repository.user.UserRepository;
24336 amit.gupta 77
import com.spice.profitmandi.service.PartnerInvestmentService;
23844 amit.gupta 78
import com.spice.profitmandi.service.authentication.RoleManager;
23884 amit.gupta 79
import com.spice.profitmandi.service.inventory.InventoryService;
25214 amit.gupta 80
import com.spice.profitmandi.service.slab.TargetSlabService;
23884 amit.gupta 81
import com.spice.profitmandi.service.transaction.TransactionService;
22481 ashik.ali 82
import com.spice.profitmandi.web.model.LoginDetails;
83
import com.spice.profitmandi.web.util.CookiesProcessor;
24263 tejbeer 84
import com.spice.profitmandi.web.util.MVCResponseSender;
22481 ashik.ali 85
 
21615 kshitij.so 86
@Controller
25222 amit.gupta 87
@Transactional(rollbackOn = Throwable.class)
21615 kshitij.so 88
public class DashboardController {
23923 amit.gupta 89
 
23379 ashik.ali 90
	@Value("${web.api.host}")
91
	private String webApiHost;
23923 amit.gupta 92
 
24072 amit.gupta 93
	@Value("${web.api.scheme}")
94
	private String webApiScheme;
24288 amit.gupta 95
 
24078 amit.gupta 96
	@Value("${web.api.root}")
97
	private String webApiRoot;
98
 
23379 ashik.ali 99
	@Value("${web.api.port}")
100
	private int webApiPort;
21615 kshitij.so 101
 
22481 ashik.ali 102
	@Autowired
22927 ashik.ali 103
	private CookiesProcessor cookiesProcessor;
23923 amit.gupta 104
 
23568 govind 105
	@Autowired
25651 tejbeer 106
	private ResponseSender<?> responseSender;
107
 
108
	@Autowired
24880 govind 109
	private ItemRepository itemRepository;
110
 
111
	@Autowired
25214 amit.gupta 112
	private TargetSlabService targetSlabService;
113
 
114
	@Autowired
115
	private PartnerTargetRepository partnerTargetRepository;
116
 
117
	@Autowired
118
	private TargetSlabRepository targetSlabRepository;
119
 
120
	@Autowired
23786 amit.gupta 121
	private RoleManager roleManager;
23923 amit.gupta 122
 
23838 ashik.ali 123
	@Autowired
124
	private FofoStoreRepository fofoStoreRepository;
23884 amit.gupta 125
 
126
	@Autowired
24880 govind 127
	private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
23923 amit.gupta 128
 
23884 amit.gupta 129
	@Autowired
24880 govind 130
	private PartnerInvestmentService partnerInvestmentService;
24288 amit.gupta 131
 
23884 amit.gupta 132
	@Autowired
24880 govind 133
	private InsurancePolicyRepository insurancePolicyRepository;
23923 amit.gupta 134
	/*
135
	 * @Autowired private ScanRepository scanRepository;
136
	 */
24880 govind 137
	@Autowired
138
	private RechargeTransactionRepository rechargeTransactionRepository;
23923 amit.gupta 139
 
23884 amit.gupta 140
	@Autowired
141
	private TransactionService transactionService;
25136 amit.gupta 142
 
24996 amit.gupta 143
	@Autowired
144
	private InventoryService inventoryService;
24263 tejbeer 145
 
24098 tejbeer 146
	@Autowired
24263 tejbeer 147
	private MVCResponseSender mvcResponseSender;
148
 
149
	@Autowired
24098 tejbeer 150
	private NotificationPanelRepository notificationPanelRepository;
23923 amit.gupta 151
 
24203 amit.gupta 152
	@Autowired
153
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
154
 
24880 govind 155
	@Autowired
156
	private FofoOrderItemRepository fofoOrderItemRepository;
157
 
158
	@Autowired
159
	private InsuranceProviderRepository insuranceProviderRepository;
25566 tejbeer 160
	@Autowired
161
	private PartnerTypeChangeService partnerTypeChangeService;
25136 amit.gupta 162
 
24996 amit.gupta 163
	@Autowired
25649 tejbeer 164
	private HygieneDataRepository hygieneDataRepository;
165
 
166
	@Autowired
167
	private UserCampaignRepository userCampaignRepository;
168
 
169
	@Autowired
170
	private UserAccountRepository userAccountRepository;
171
 
172
	@Autowired
173
	private NotificationCampaignRepository notificationCampaignRepository;
174
 
175
	@Autowired
25651 tejbeer 176
	private DocumentRepository documentRepository;
177
 
178
	@Autowired
24996 amit.gupta 179
	private Mongo mongoClient;
24880 govind 180
 
25649 tejbeer 181
	@Autowired
182
	private Gson gson;
183
 
23568 govind 184
	private static final Logger LOGGER = LogManager.getLogger(DashboardController.class);
23923 amit.gupta 185
 
25136 amit.gupta 186
	@RequestMapping(value = "/12dashboard34", method = RequestMethod.GET)
25140 amit.gupta 187
	public String dashboard1(HttpServletRequest request, Model model) throws Exception {
25136 amit.gupta 188
		LOGGER.info("In Dashboard1");
189
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
25179 amit.gupta 190
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
191
		model.addAttribute("isAdmin", isAdmin);
25180 amit.gupta 192
		if (!isAdmin) {
25649 tejbeer 193
			// model.addAttribute("brandStockPrices",
194
			// this.getBrandStockPrices(loginDetails.getFofoId()));
25179 amit.gupta 195
			model.addAttribute("salesMap", this.getSales(loginDetails.getFofoId()));
196
			model.addAttribute("investments", this.getInvestments(loginDetails.getFofoId()));
197
			return "dashboard1";
198
		}
25136 amit.gupta 199
		return "dashboard1";
200
	}
201
 
202
	private Map<String, Object> getInvestments(int fofoId) throws Exception {
203
		Map<String, Object> investments = new LinkedHashMap<>();
204
		PartnerDailyInvestment investment = partnerInvestmentService.getInvestment(fofoId, 1);
25649 tejbeer 205
		LocalDate currentMonthStart = LocalDate.now().withDayOfMonth(1);
25136 amit.gupta 206
		LocalDate yesterDate = LocalDate.now().minusDays(1);
207
		PartnerDailyInvestment yesterdayInvestment = partnerDailyInvestmentRepository.select(fofoId, yesterDate);
208
		if (yesterdayInvestment == null) {
209
			yesterdayInvestment = new PartnerDailyInvestment();
210
		}
211
 
212
		List<PartnerDailyInvestment> currentMonthInvestments = partnerDailyInvestmentRepository.selectAll(fofoId,
213
				currentMonthStart, currentMonthStart.withDayOfMonth(currentMonthStart.lengthOfMonth()));
214
 
25140 amit.gupta 215
		long okInvestmentDays = currentMonthInvestments.stream().filter(x -> x.getShortPercentage() <= 10)
25136 amit.gupta 216
				.collect(Collectors.counting());
25140 amit.gupta 217
		investments.put("today", investment.getTotalInvestment());
218
		investments.put("investment", investment);
219
		investments.put("inStock", investment.getInStockAmount());
25182 amit.gupta 220
		investments.put("minimum", investment.getMinInvestmentString());
25140 amit.gupta 221
		investments.put("short", investment.getShortPercentage());
25649 tejbeer 222
		investments.put("okDays", okInvestmentDays);
25136 amit.gupta 223
		return investments;
224
	}
225
 
25221 amit.gupta 226
	private Map<String, Object> getSales(int fofoId) {
227
		Map<String, Object> salesMap = new LinkedHashMap<>();
25140 amit.gupta 228
		LocalDateTime curDate = LocalDate.now().atStartOfDay();
25214 amit.gupta 229
		int monthLength = LocalDate.now().lengthOfMonth();
25140 amit.gupta 230
		List<FofoOrderItem> fofoOrderItems = fofoOrderItemRepository.selectBetweenCreatedTime(fofoId, curDate,
231
				curDate.with(LocalTime.MAX));
232
		List<FofoOrderItem> mtdFofoOrderItems = fofoOrderItemRepository.selectBetweenCreatedTime(fofoId,
233
				curDate.withDayOfMonth(1), curDate.with(LocalTime.MAX));
234
		double todaySale = fofoOrderItems.stream()
235
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
236
		double mtdSale = mtdFofoOrderItems.stream()
237
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
25214 amit.gupta 238
 
239
		List<PartnerTargetDetails> partnerTargetDetails = partnerTargetRepository
240
				.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now());
25544 amit.gupta 241
		if (partnerTargetDetails.isEmpty()) {
25372 tejbeer 242
			partnerTargetDetails = partnerTargetRepository
25633 amit.gupta 243
					.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now().minusMonths(2));
25372 tejbeer 244
		}
25214 amit.gupta 245
 
246
		List<Integer> targetIds = partnerTargetRepository
247
				.selectAllPartnerTargetForPartner(fofoId,
248
						partnerTargetDetails.stream().map(x -> x.getId()).collect(Collectors.toList()))
249
				.stream().map(x -> x.getTargetId()).collect(Collectors.toList());
250
		if (targetIds.size() > 0) {
251
			PartnerTarget target = partnerTargetRepository.selectAllPartnerTargetForPartner(fofoId, targetIds).get(0);
25544 amit.gupta 252
			double targetValue = (double) target.getTargetValue();
25214 amit.gupta 253
			int remainingDays = monthLength - curDate.getDayOfMonth() + 1;
25544 amit.gupta 254
			// Should not consider today's sale while evaluating today's target.
255
			double todayTargetValue = Math.round((targetValue - (mtdSale - todaySale)) / remainingDays);
25221 amit.gupta 256
			DecimalFormat df = new DecimalFormat("#.##");
25544 amit.gupta 257
			String targetValueString = df.format(targetValue / 100000) + "Lac";
25214 amit.gupta 258
			salesMap.put("todayTarget", todayTargetValue);
25223 amit.gupta 259
			salesMap.put("monthTarget", targetValueString);
25214 amit.gupta 260
		} else {
261
			salesMap.put("todayTarget", 0d);
262
			salesMap.put("monthTarget", 0d);
263
 
264
		}
25140 amit.gupta 265
		salesMap.put("todaySale", todaySale);
266
		salesMap.put("mtdSale", mtdSale);
25136 amit.gupta 267
		return salesMap;
268
	}
269
 
25649 tejbeer 270
	/*
271
	 * private List<BrandStockPrice> getBrandStockPrices(int fofoId) throws
272
	 * Exception { Map<String, BrandStockPrice> brandStockPricesMap =
273
	 * inventoryService.getBrandWiseStockValue(fofoId);
274
	 * 
275
	 * List<DBObject> mobileBrands = mongoClient.getMongoBrands(fofoId, "", 3);
276
	 * List<BrandStockPrice> brandStockPrices = new ArrayList<>();
277
	 * 
278
	 * mobileBrands.stream().forEach(x -> { String brand = (String) x.get("name");
279
	 * if (brandStockPricesMap.containsKey(brand)) { BrandStockPrice brandStockPrice
280
	 * = brandStockPricesMap.get(brand); brandStockPrice.setBrandUrl((String)
281
	 * x.get("url")); brandStockPrice.setRank(((Double) x.get("rank")).intValue());
282
	 * brandStockPrices.add(brandStockPrice); } });
283
	 * 
284
	 * return brandStockPrices.stream().filter(x -> x.getTotalQty() > 0).sorted((x,
285
	 * y) -> x.getRank() - y.getRank()) .collect(Collectors.toList()); }
286
	 */
25175 amit.gupta 287
 
21615 kshitij.so 288
	@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
25649 tejbeer 289
	public String dashboard(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset,
290
			@RequestParam(name = "limit", defaultValue = "20") int limit, Model model) throws Exception {
25140 amit.gupta 291
 
22927 ashik.ali 292
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
25180 amit.gupta 293
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
294
		model.addAttribute("isAdmin", isAdmin);
23923 amit.gupta 295
 
22481 ashik.ali 296
		FofoStore fofoStore = null;
25274 amit.gupta 297
		model.addAttribute("webApiHost", webApiHost);
298
		model.addAttribute("webApiPort", webApiPort);
299
		model.addAttribute("webApiScheme", webApiScheme);
25544 amit.gupta 300
		model.addAttribute("webApiRoot", webApiRoot);
25183 amit.gupta 301
		if (isAdmin) {
25180 amit.gupta 302
			return "dashboard1";
303
		} else {
304
			try {
305
				fofoStore = fofoStoreRepository.selectByRetailerId(loginDetails.getFofoId());
25566 tejbeer 306
				PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoStore.getId(), LocalDate.now());
25544 amit.gupta 307
				if (!fofoStore.isActive()) {
308
					return "redirect:/login";
309
				}
25649 tejbeer 310
 
25566 tejbeer 311
				LOGGER.info("partnerType {}", partnerType);
312
 
313
				model.addAttribute("partnerType", partnerType);
25180 amit.gupta 314
				model.addAttribute("fofoStore", fofoStore);
25566 tejbeer 315
				model.addAttribute("partnerType", partnerType);
25180 amit.gupta 316
				model.addAttribute("hasGift", hasGift(loginDetails.getFofoId()));
317
				model.addAttribute("giftItemId", ProfitMandiConstants.GIFT_ID);
318
 
25649 tejbeer 319
				// model.addAttribute("brandStockPrices",
320
				// this.getBrandStockPrices(loginDetails.getFofoId()));
25180 amit.gupta 321
				model.addAttribute("salesMap", this.getSales(loginDetails.getFofoId()));
25544 amit.gupta 322
				// this.setInvestments
323
				//
25180 amit.gupta 324
				model.addAttribute("investments", this.getInvestments(loginDetails.getFofoId()));
25544 amit.gupta 325
				model.addAttribute("isInvestmentOk", partnerInvestmentService.isInvestmentOk(loginDetails.getFofoId(),
326
						10, ProfitMandiConstants.CUTOFF_INVESTMENT));
25180 amit.gupta 327
			} catch (ProfitMandiBusinessException e) {
328
				LOGGER.error("FofoStore Code not found of fofoId {}", loginDetails.getFofoId());
329
 
330
			}
22481 ashik.ali 331
		}
25649 tejbeer 332
 
333
		LocalDateTime currentMonthStart = LocalDateTime.now().withDayOfMonth(1);
334
		List<HygieneData> hygieneData = hygieneDataRepository
335
				.selectAll(loginDetails.getFofoId(), currentMonthStart,
336
						LocalDateTime.now().plusMonths(1).withDayOfMonth(1))
337
				.stream().filter(x -> x.getDisposedTimestamp() != null).collect(Collectors.toList());
338
 
339
		LOGGER.info("hygieneData" + hygieneData);
340
		int successHygieneData = hygieneData.stream().filter(x -> x.getHygieneRating() == true)
341
				.collect(Collectors.toList()).size();
342
		int unsuccessfullHygieneData = hygieneData.stream().filter(x -> x.getHygieneRating() == false)
343
				.collect(Collectors.toList()).size();
25651 tejbeer 344
 
25649 tejbeer 345
		model.addAttribute("successHygieneData", successHygieneData);
346
		model.addAttribute("fullHygieneData", successHygieneData + unsuccessfullHygieneData);
25651 tejbeer 347
 
25197 amit.gupta 348
		model.addAttribute("monthDays", LocalDate.now().minusDays(1).lengthOfMonth());
349
		model.addAttribute("dayOfMonth", LocalDate.now().minusDays(1).getDayOfMonth());
25140 amit.gupta 350
		return "dashboard1";
21615 kshitij.so 351
	}
23923 amit.gupta 352
 
24288 amit.gupta 353
	// This method is currently hardcoded to faciliate watches sold as gift.
24203 amit.gupta 354
	private boolean hasGift(int fofoId) {
355
		try {
24288 amit.gupta 356
			return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId)
357
					.getAvailability() > 0;
24203 amit.gupta 358
		} catch (ProfitMandiBusinessException e) {
359
			return false;
360
		}
361
	}
24288 amit.gupta 362
 
22354 ashik.ali 363
	@RequestMapping(value = "/contactUs", method = RequestMethod.GET)
23923 amit.gupta 364
	public String contactUs(HttpServletRequest request, Model model) throws Throwable {
22354 ashik.ali 365
		model.addAttribute("appContextPath", request.getContextPath());
366
		return "contact-us";
367
	}
23923 amit.gupta 368
 
25649 tejbeer 369
	@RequestMapping(value = "/notifications", method = RequestMethod.GET)
370
	public String getNotificationsWithType(HttpServletRequest request, @RequestParam(value = "") String notifyType,
371
			@RequestParam(name = "offset", defaultValue = "0") int offset,
372
			@RequestParam(name = "limit", defaultValue = "20") int limit, Model model) throws Exception {
373
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
374
		int userId = userAccountRepository.selectUserIdByRetailerId(loginDetails.getFofoId());
375
		List<UserCampaign> userCampaigns = userCampaignRepository.selectByUserId(userId);
376
		List<Notification> notifications = new ArrayList<>();
25651 tejbeer 377
		Document document = null;
25649 tejbeer 378
		if (!userCampaigns.isEmpty()) {
379
			List<Integer> campaignIds = new ArrayList<>();
380
			for (UserCampaign userCampaign : userCampaigns) {
381
				campaignIds.add(userCampaign.getCampaignId());
382
			}
383
			List<NotificationCampaign> notificationCampaigns = notificationCampaignRepository.selectbyCids(campaignIds);
384
			if (notifyType.equals("annoucement")) {
385
				for (NotificationCampaign notificationCampaign : notificationCampaigns) {
386
					if (notificationCampaign.isAnnoucement() == true) {
387
						Notification ns = new Notification();
388
						SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
389
								SimpleCampaignParams.class);
390
						Campaign campaign = new SimpleCampaign(scp);
391
						LocalDateTime expire = campaign.getExpireTimestamp();
392
						ns.setCid(Integer.toString(notificationCampaign.getId()));
393
						ns.setType(campaign.getType());
394
						ns.setMessage(campaign.getMessage());
395
						ns.setTitle(campaign.getTitle());
25651 tejbeer 396
						if (notificationCampaign.getDocumentId() != null) {
397
							document = documentRepository.selectById(notificationCampaign.getDocumentId());
398
							ns.setDocumentName(document.getDisplayName());
399
						}
25649 tejbeer 400
						ns.setUrl(campaign.getUrl());
401
						ns.setShowImage(campaign.getShowImage());
402
						ns.setImageUrl(campaign.getImageUrl());
25651 tejbeer 403
						ns.setDocumentId(notificationCampaign.getDocumentId());
25649 tejbeer 404
						ns.setAnnoucement(notificationCampaign.isAnnoucement());
405
						ns.setCreated(notificationCampaign.getCreatedTimestamp()
406
								.toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30)) * 1000);
407
						if (LocalDateTime.now().isAfter(expire)) {
408
							ns.setExpired(true);
409
						} else {
410
							ns.setExpired(false);
411
						}
412
						notifications.add(ns);
413
					}
414
				}
415
			} else if (notifyType.equals("notification")) {
416
				for (NotificationCampaign notificationCampaign : notificationCampaigns) {
417
					if (notificationCampaign.isAnnoucement() == false) {
418
						Notification ns = new Notification();
419
						SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
420
								SimpleCampaignParams.class);
421
						Campaign campaign = new SimpleCampaign(scp);
422
						LocalDateTime expire = campaign.getExpireTimestamp();
423
						ns.setCid(Integer.toString(notificationCampaign.getId()));
424
						ns.setType(campaign.getType());
425
						ns.setMessage(campaign.getMessage());
426
						ns.setTitle(campaign.getTitle());
427
						ns.setUrl(campaign.getUrl());
25651 tejbeer 428
						if (notificationCampaign.getDocumentId() != null) {
429
							document = documentRepository.selectById(notificationCampaign.getDocumentId());
430
							ns.setDocumentName(document.getDisplayName());
431
						}
25649 tejbeer 432
						ns.setShowImage(campaign.getShowImage());
433
						ns.setImageUrl(campaign.getImageUrl());
25651 tejbeer 434
						ns.setDocumentId(notificationCampaign.getDocumentId());
25649 tejbeer 435
						ns.setAnnoucement(notificationCampaign.isAnnoucement());
436
						ns.setCreated(notificationCampaign.getCreatedTimestamp()
437
								.toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30)) * 1000);
438
						if (LocalDateTime.now().isAfter(expire)) {
439
							ns.setExpired(true);
440
						} else {
441
							ns.setExpired(false);
442
						}
443
						notifications.add(ns);
444
					}
445
				}
446
			} else {
447
				for (NotificationCampaign notificationCampaign : notificationCampaigns) {
448
					Notification ns = new Notification();
449
					SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
450
							SimpleCampaignParams.class);
451
					Campaign campaign = new SimpleCampaign(scp);
452
					LocalDateTime expire = campaign.getExpireTimestamp();
453
					ns.setCid(Integer.toString(notificationCampaign.getId()));
454
					ns.setType(campaign.getType());
455
					ns.setMessage(campaign.getMessage());
456
					ns.setTitle(campaign.getTitle());
457
					ns.setUrl(campaign.getUrl());
25651 tejbeer 458
					if (notificationCampaign.getDocumentId() != null) {
459
						document = documentRepository.selectById(notificationCampaign.getDocumentId());
460
						ns.setDocumentName(document.getDisplayName());
461
					}
462
					ns.setDocumentId(notificationCampaign.getDocumentId());
25649 tejbeer 463
					ns.setShowImage(campaign.getShowImage());
464
					ns.setImageUrl(campaign.getImageUrl());
465
					ns.setAnnoucement(notificationCampaign.isAnnoucement());
466
					ns.setCreated(
467
							notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30))
468
									* 1000);
469
					if (LocalDateTime.now().isAfter(expire)) {
470
						ns.setExpired(true);
471
					} else {
472
						ns.setExpired(false);
473
					}
474
					notifications.add(ns);
475
				}
476
			}
477
		}
25651 tejbeer 478
 
25649 tejbeer 479
		model.addAttribute("notifications", notifications);
25651 tejbeer 480
		return "notification-template";
481
	}
25649 tejbeer 482
 
25651 tejbeer 483
	@RequestMapping(value = "/notifyDocument/documentId", method = RequestMethod.GET)
484
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
485
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId, @RequestParam int cid)
486
			throws ProfitMandiBusinessException {
487
		Document document = documentRepository.selectById(documentId);
488
		NotificationCampaign nc = notificationCampaignRepository.selectById(cid);
489
		if (nc.getDocumentId() == null) {
490
			throw new ProfitMandiBusinessException("cid", nc.getId(), "not available");
491
		}
492
		if (nc.getDocumentId() != documentId) {
493
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
494
		}
495
		return responseSender.ok(document);
25649 tejbeer 496
	}
497
 
25651 tejbeer 498
	@RequestMapping(value = "/notifyDocument/download", method = RequestMethod.GET)
499
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request, @RequestParam int cid, Model model)
500
			throws ProfitMandiBusinessException {
501
 
502
		NotificationCampaign nc = notificationCampaignRepository.selectById(cid);
503
 
504
		if (nc.getDocumentId() == null) {
505
			throw new ProfitMandiBusinessException("cid", nc.getId(), "not available");
506
		}
507
 
508
		Document document = documentRepository.selectById(nc.getDocumentId());
509
 
510
		FileInputStream file = null;
511
		try {
512
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
513
		} catch (FileNotFoundException e) {
514
			LOGGER.error("Retailer Document file not found : ", e);
515
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
516
		}
517
		// ByteArrayOutputStream byteArrayOutputStream = new
518
		// ByteArrayOutputStream();
519
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
520
 
521
		final HttpHeaders headers = new HttpHeaders();
522
		String contentType = "";
523
		if (document.getContentType() == ContentType.JPEG) {
524
			contentType = "image/jpeg";
525
		} else if (document.getContentType() == ContentType.PNG) {
526
			contentType = "image/png";
527
		} else if (document.getContentType() == ContentType.PDF) {
528
			contentType = "application/pdf";
529
		}
530
		headers.set("Content-Type", contentType);
531
		headers.set("Content-disposition", "inline; filename=" + document.getName());
532
		headers.setContentLength(document.getSize());
533
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
534
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
535
 
536
		// return
537
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
538
	}
539
 
21615 kshitij.so 540
}