Subversion Repositories SmartDukaan

Rev

Rev 23528 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23505 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.util.HashMap;
4
 
5
import java.util.HashSet;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Set;
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;
15
import org.springframework.beans.factory.annotation.Value;
16
import org.springframework.stereotype.Controller;
17
import org.springframework.transaction.annotation.Transactional;
18
import org.springframework.ui.Model;
19
import org.springframework.web.bind.annotation.RequestBody;
20
import org.springframework.web.bind.annotation.RequestMapping;
21
import org.springframework.web.bind.annotation.RequestMethod;
22
import org.springframework.web.bind.annotation.RequestParam;
23
 
24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25
import com.spice.profitmandi.common.model.OperatorType;
26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
27
import com.spice.profitmandi.common.model.RechargeRequest;
28
import com.spice.profitmandi.dao.entity.dtr.RechargeOperator;
29
import com.spice.profitmandi.dao.entity.dtr.RechargeTransaction;
30
import com.spice.profitmandi.dao.enumuration.dtr.RechargeType;
31
import com.spice.profitmandi.dao.repository.dtr.RechargeOperatorRepository;
32
import com.spice.profitmandi.dao.repository.dtr.RechargeTransactionRepository;
33
import com.spice.profitmandi.service.recharge.RechargeService;
34
import com.spice.profitmandi.web.model.LoginDetails;
35
import com.spice.profitmandi.web.util.CookiesProcessor;
36
 
37
@Controller
38
@Transactional(rollbackFor=Throwable.class)
39
public class RechargeController {
40
 
41
	private static final Logger LOGGER = LoggerFactory.getLogger(RechargeController.class);
42
 
43
	@Value("${recharge.api.host}")
44
	private String rechargeApiHost;
45
 
46
	@Value("${recharge.api.port}")
47
	private int rechargeApiPort;
48
 
49
	@Value("${recharge.auth.key}")
50
	private String rechargeAuthKey;
51
 
52
	@Value("${recharge.validation.api.host}")
53
	private String rechargeValidationApiHost;
54
 
55
	@Value("${recharge.validation.api.port}")
56
	private int rechargeValidationApiPort;
57
 
58
	@Value("${recharge.validation.auth.key}")
59
	private String rechargeValidationAuthKey;
60
 
61
	@Autowired
62
	private RechargeService rechargeService;
63
 
64
	@Autowired
65
	private RechargeOperatorRepository rechargeOperatorRepository;
66
 
67
	@Autowired
68
	private RechargeTransactionRepository rechargeTransactionRepository;
69
 
70
	@Autowired
71
	private CookiesProcessor cookiesProcessor;
72
 
73
	@RequestMapping(value = "/createRecharge", method = RequestMethod.GET)
74
	public String createRecharge(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.RECHARGE_TYPE) String rechargeTypeString, Model model){
75
		//model.addAttribute("rechargeOperators", rechargeOperatorRepository.selectAllByRechargeType(RechargeType.MOBILE));
76
		//model.addAttribute("rechargeTypes", RechargeType.values());
77
		RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
78
		List<RechargeOperator> rechargeOperators = null;
79
		if(rechargeType == RechargeType.MOBILE) {
80
			model.addAttribute("operatorTypes", OperatorType.values());
81
			rechargeOperators = rechargeOperatorRepository.selectAllByOperatorType(OperatorType.PREPAID);
82
		}else {
83
			rechargeOperators = rechargeOperatorRepository.selectAllByRechargeType(rechargeType);
84
		}
85
		model.addAttribute("rechargeOperators", rechargeOperators);
86
		return "create-recharge";
87
	}
88
 
89
	private Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap(List<RechargeTransaction> rechargeTransactions){
90
		Map<Integer, String> rechargeOperatorIdrechargeOperatorNameMap = new HashMap<>();
91
		if(rechargeTransactions.isEmpty()) {
92
			return rechargeOperatorIdrechargeOperatorNameMap;
93
		}
94
		Set<Integer> operatorIds = new HashSet<>();
95
		for(RechargeTransaction rechargeTransaction : rechargeTransactions) {
96
			operatorIds.add(rechargeTransaction.getOperatorId());
97
		}
98
 
99
		List<RechargeOperator> rechargeOperators = rechargeOperatorRepository.selectAllByIds(operatorIds);
100
		for(RechargeOperator rechargeOperator : rechargeOperators) {
101
			rechargeOperatorIdrechargeOperatorNameMap.put(rechargeOperator.getId(), rechargeOperator.getName());
102
		}
103
		return rechargeOperatorIdrechargeOperatorNameMap;
104
	}
105
 
106
	@RequestMapping(value = "/createRecharge", method = RequestMethod.POST)
107
	public String createRecharge(HttpServletRequest request, @RequestBody RechargeRequest rechargeRequest, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
108
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
109
		rechargeService.doRecharge(rechargeApiHost, rechargeApiPort, rechargeAuthKey, rechargeValidationApiHost, rechargeValidationApiPort, rechargeValidationAuthKey, loginDetails.getFofoId(), rechargeRequest);
110
		RechargeType rechargeType = RechargeType.valueOf(rechargeRequest.getRechargeType());
111
		List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerIdAndType(loginDetails.getFofoId(), rechargeType, offset, limit);
112
		long size = rechargeTransactionRepository.selectCountByRetailerIdAndType(loginDetails.getFofoId(), rechargeType);
113
		Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap = this.rechargeOperatorIdRechargeOperatorNameMap(rechargeTransactions);
114
		model.addAttribute("rechargeTransactions", rechargeTransactions);
115
		model.addAttribute("rechargeOperatorIdRechargeOperatorNameMap", rechargeOperatorIdRechargeOperatorNameMap);
116
		model.addAttribute("start", offset + 1);
117
		model.addAttribute("size", size);
118
		if (rechargeTransactions.size() < limit){
119
			model.addAttribute("end", offset + rechargeTransactions.size());
120
		}else{
121
			model.addAttribute("end", offset + limit);
122
		}
123
		if(RechargeType.valueOf(rechargeRequest.getRechargeType()) == RechargeType.MOBILE) {
124
			return "mobile-recharges";
125
		}else {
126
			return "dth-recharges";
127
		}
128
	}
129
 
130
	@RequestMapping(value = "/getRechargeById", method = RequestMethod.GET)
131
	public String getRechargeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.RECHARGE_TRANSACTION_ID) int rechargeTransactionId, Model model)  throws ProfitMandiBusinessException{
132
		RechargeTransaction rechargeTransaction = rechargeTransactionRepository.selectById(rechargeTransactionId);
133
		model.addAttribute("rechargeTransaction", rechargeTransaction);
134
		return "recharge-details";
135
	}
136
 
137
	@RequestMapping(value = "/checkStatus", method = RequestMethod.GET)
138
	public String checkStatus(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.REQUEST_ID) String requestId, @RequestParam(name = ProfitMandiConstants.RECHARGE_TYPE) String rechargeTypeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
139
		LOGGER.info("RequestId [{}], rechargeType [{}]", requestId, rechargeTypeString);
140
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
141
		rechargeService.checkStatus(rechargeApiHost, rechargeApiPort, rechargeAuthKey, loginDetails.getFofoId(), requestId);
142
		RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
143
		List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerIdAndType(loginDetails.getFofoId(), rechargeType, offset, limit);
144
		//long size = rechargeTransactionRepository.selectCountByRetailerId(loginDetails.getFofoId());
145
		Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap = this.rechargeOperatorIdRechargeOperatorNameMap(rechargeTransactions);
146
		model.addAttribute("rechargeTransactions", rechargeTransactions);
147
		model.addAttribute("rechargeOperatorIdRechargeOperatorNameMap", rechargeOperatorIdRechargeOperatorNameMap);
148
 
149
		if(RechargeType.valueOf(rechargeTypeString) == RechargeType.MOBILE) {
150
			return "mobile-recharges-paginated";
151
		}else {
152
			return "dth-recharges-paginated";
153
		}
154
	}
155
 
156
	@RequestMapping(value = "/getRecharges", method = RequestMethod.GET)
157
	public String getRecharges(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.RECHARGE_TYPE) String rechargeTypeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
158
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
159
		RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
160
		List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerIdAndType(loginDetails.getFofoId(), rechargeType, offset, limit);
161
		long size = rechargeTransactionRepository.selectCountByRetailerIdAndType(loginDetails.getFofoId(), rechargeType);
162
		Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap = this.rechargeOperatorIdRechargeOperatorNameMap(rechargeTransactions);
163
		model.addAttribute("rechargeTransactions", rechargeTransactions);
164
		model.addAttribute("rechargeOperatorIdRechargeOperatorNameMap", rechargeOperatorIdRechargeOperatorNameMap);
165
		model.addAttribute("start", offset + 1);
166
		model.addAttribute("size", size);
167
		if (rechargeTransactions.size() < limit){
168
			model.addAttribute("end", offset + rechargeTransactions.size());
169
		}else{
170
			model.addAttribute("end", offset + limit);
171
		}
172
		if(RechargeType.valueOf(rechargeTypeString) == RechargeType.MOBILE) {
173
			return "mobile-recharges";
174
		}else {
175
			return "dth-recharges";
176
		}
177
	}
178
 
179
	@RequestMapping(value = "/getPaginatedRecharges", method = RequestMethod.GET)
180
	public String getPaginatedRecharges(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.RECHARGE_TYPE) String rechargeTypeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
181
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
182
		RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
183
		List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerIdAndType(loginDetails.getFofoId(), rechargeType, offset, limit);
184
		Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap = this.rechargeOperatorIdRechargeOperatorNameMap(rechargeTransactions);
185
		model.addAttribute("rechargeTransactions", rechargeTransactions);
186
		model.addAttribute("rechargeOperatorIdRechargeOperatorNameMap", rechargeOperatorIdRechargeOperatorNameMap);
187
		if(RechargeType.valueOf(rechargeTypeString) == RechargeType.MOBILE) {
188
			return "mobile-recharges-paginated";
189
		}else {
190
			return "dth-recharges-paginated";
191
		}
192
	}
193
 
194
	@RequestMapping(value = "/getOperators", method = RequestMethod.GET)
195
	public String getOperators(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.OPERATOR_TYPE) String operatorTypeString, Model model){
196
		//model.addAttribute("rechargeOperators", rechargeOperatorRepository.selectAllByRechargeType(RechargeType.MOBILE));
197
		//model.addAttribute("rechargeTypes", RechargeType.values());
198
		OperatorType operatorType = OperatorType.valueOf(operatorTypeString);
199
		List<RechargeOperator> rechargeOperators = rechargeOperatorRepository.selectAllByOperatorType(operatorType);
200
		model.addAttribute("rechargeOperators", rechargeOperators);
201
		return "recharge-operators";
202
	}
203
 
204
 
205
}