| 21749 |
ashik.ali |
1 |
package com.spice.profitmandi.web.controller;
|
| 21541 |
ashik.ali |
2 |
|
| 21763 |
ashik.ali |
3 |
import java.util.Collection;
|
|
|
4 |
import java.util.HashSet;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
import java.util.Set;
|
|
|
7 |
|
| 21749 |
ashik.ali |
8 |
import javax.servlet.http.HttpServletRequest;
|
| 21541 |
ashik.ali |
9 |
|
| 21763 |
ashik.ali |
10 |
import org.apache.commons.collections.CollectionUtils;
|
| 21749 |
ashik.ali |
11 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
12 |
import org.springframework.http.ResponseEntity;
|
| 21541 |
ashik.ali |
13 |
import org.springframework.stereotype.Controller;
|
| 21749 |
ashik.ali |
14 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
15 |
import org.springframework.web.bind.annotation.RequestBody;
|
| 21541 |
ashik.ali |
16 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
17 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 21749 |
ashik.ali |
18 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 21541 |
ashik.ali |
19 |
|
| 21749 |
ashik.ali |
20 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
|
|
21 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
22 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21763 |
ashik.ali |
23 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 21749 |
ashik.ali |
24 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 22037 |
amit.gupta |
25 |
import com.spice.profitmandi.dao.entity.catalog.PinCodeTag;
|
|
|
26 |
import com.spice.profitmandi.dao.entity.catalog.RetailerTag;
|
|
|
27 |
import com.spice.profitmandi.dao.entity.catalog.Tag;
|
| 21763 |
ashik.ali |
28 |
import com.spice.profitmandi.dao.entity.dtr.Retailer;
|
| 22658 |
amit.gupta |
29 |
import com.spice.profitmandi.dao.enumuration.catalog.TagType;
|
| 22548 |
amit.gupta |
30 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
| 22037 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.repository.catalog.PinCodeTagRepository;
|
|
|
32 |
import com.spice.profitmandi.dao.repository.catalog.RetailerTagRepository;
|
|
|
33 |
import com.spice.profitmandi.dao.repository.catalog.TagRepository;
|
| 21763 |
ashik.ali |
34 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 22548 |
amit.gupta |
35 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
36 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
|
|
37 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| 21763 |
ashik.ali |
38 |
import com.spice.profitmandi.web.request.CreateTagRequest;
|
| 21541 |
ashik.ali |
39 |
|
|
|
40 |
@Controller
|
| 22037 |
amit.gupta |
41 |
@Transactional(rollbackFor=Throwable.class)
|
| 21749 |
ashik.ali |
42 |
public class TagController {
|
| 21541 |
ashik.ali |
43 |
|
| 21749 |
ashik.ali |
44 |
@Autowired
|
|
|
45 |
TagRepository tagRepository;
|
|
|
46 |
|
|
|
47 |
@Autowired
|
| 21763 |
ashik.ali |
48 |
PinCodeTagRepository pinCodeTagRepository;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
|
|
51 |
RetailerRepository retailerRepository;
|
| 22548 |
amit.gupta |
52 |
|
|
|
53 |
@Autowired
|
|
|
54 |
UserAccountRepository userAccountRepoitory;
|
| 21763 |
ashik.ali |
55 |
|
| 22548 |
amit.gupta |
56 |
|
| 21763 |
ashik.ali |
57 |
@Autowired
|
| 22548 |
amit.gupta |
58 |
UserRoleRepository userRoleRepository;
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
@Autowired
|
|
|
62 |
UserRepository userRepoitory;
|
|
|
63 |
|
|
|
64 |
@Autowired
|
| 21763 |
ashik.ali |
65 |
RetailerTagRepository fofoTagRepository;
|
|
|
66 |
|
|
|
67 |
@Autowired
|
| 21749 |
ashik.ali |
68 |
ResponseSender<?> responseSender;
|
|
|
69 |
|
| 21777 |
ashik.ali |
70 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG, method = RequestMethod.POST)
|
| 22548 |
amit.gupta |
71 |
public ResponseEntity<?> createPinCodeTag(HttpServletRequest request, @RequestBody CreateTagRequest createTagRequest) throws Throwable {
|
| 21763 |
ashik.ali |
72 |
Set<String> invalidPinCodes = new HashSet<>();
|
| 21777 |
ashik.ali |
73 |
for(String pinCode : createTagRequest.getPinCodes()){
|
| 21763 |
ashik.ali |
74 |
if(!StringUtils.isValidPinCode(pinCode)){
|
|
|
75 |
invalidPinCodes.add(pinCode);
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
if(!invalidPinCodes.isEmpty()){
|
|
|
79 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.PIN_CODE, invalidPinCodes, "PNCD_VE_1000");
|
|
|
80 |
return responseSender.badRequest(profitMandiBusinessException);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
Set<Integer> foundRetailerIds = new HashSet<>();
|
| 21779 |
ashik.ali |
84 |
if(!createTagRequest.getRetailerIds().isEmpty()){
|
|
|
85 |
List<Retailer> retailers = retailerRepository.selectByIds(createTagRequest.getRetailerIds());
|
|
|
86 |
if(retailers.size() != createTagRequest.getRetailerIds().size()){
|
|
|
87 |
for(Retailer retailer : retailers){
|
|
|
88 |
foundRetailerIds.add(retailer.getId());
|
| 21763 |
ashik.ali |
89 |
}
|
| 21779 |
ashik.ali |
90 |
Collection<?> notFoundRetailerId = CollectionUtils.subtract(createTagRequest.getRetailerIds(), foundRetailerIds);
|
|
|
91 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, notFoundRetailerId, "RTLR_1000");
|
| 21763 |
ashik.ali |
92 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21779 |
ashik.ali |
93 |
}else{
|
| 21783 |
ashik.ali |
94 |
if(createTagRequest.getTagType() == TagType.FOFO){
|
|
|
95 |
// given retailer ids are fofo user id's or not
|
|
|
96 |
for(Retailer retailer : retailers){
|
| 22548 |
amit.gupta |
97 |
int userId = userAccountRepoitory.selectUserIdByRetailerId(retailer.getId());
|
|
|
98 |
List<RoleType> roleTypes = userRoleRepository.selectRoleTypesByUserId(userId);
|
|
|
99 |
if(!roleTypes.contains(RoleType.FOFO)){
|
|
|
100 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, retailer.getId(), "RTLR_1010");
|
|
|
101 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21783 |
ashik.ali |
102 |
}
|
| 21779 |
ashik.ali |
103 |
}
|
|
|
104 |
}
|
| 21763 |
ashik.ali |
105 |
}
|
|
|
106 |
}
|
|
|
107 |
Tag tag = null;
|
| 21804 |
amit.gupta |
108 |
/*try{
|
| 21763 |
ashik.ali |
109 |
tag = tagRepository.selectByLabelAndType(createTagRequest.getLabel(), createTagRequest.getTagType());
|
| 21804 |
amit.gupta |
110 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){*/
|
|
|
111 |
|
|
|
112 |
tag = new Tag();
|
|
|
113 |
tag.setActive(true);
|
|
|
114 |
tag.setDescription(createTagRequest.getDescription());
|
|
|
115 |
tag.setLabel(createTagRequest.getLabel());
|
|
|
116 |
tag.setType(createTagRequest.getTagType());
|
|
|
117 |
tag.setPinAll(createTagRequest.isPinAll());
|
|
|
118 |
tag.setUserAll(createTagRequest.isUserAll());
|
|
|
119 |
tag.setCreatedBy(1);
|
|
|
120 |
tagRepository.persist(tag);
|
| 21763 |
ashik.ali |
121 |
//return responseSender.badRequest(profitMandiBusinessException);
|
| 21804 |
amit.gupta |
122 |
for(int retailerId : new HashSet<>(createTagRequest.getRetailerIds())){
|
| 21763 |
ashik.ali |
123 |
RetailerTag fofoTag = new RetailerTag();
|
|
|
124 |
fofoTag.setRetailerId(retailerId);
|
|
|
125 |
fofoTag.setTagId(tag.getId());
|
|
|
126 |
fofoTag.setActive(true);
|
|
|
127 |
fofoTagRepository.persist(fofoTag);
|
|
|
128 |
}
|
| 21804 |
amit.gupta |
129 |
for(String pinCode : new HashSet<>(createTagRequest.getPinCodes())){
|
| 21763 |
ashik.ali |
130 |
PinCodeTag pinCodeTag = new PinCodeTag();
|
|
|
131 |
pinCodeTag.setPinCode(pinCode);
|
|
|
132 |
pinCodeTag.setTagId(tag.getId());
|
|
|
133 |
pinCodeTag.setActive(true);
|
|
|
134 |
pinCodeTagRepository.persist(pinCodeTag);
|
|
|
135 |
}
|
|
|
136 |
return responseSender.ok(ResponseCodeHolder.getMessage("PNCDTG_OK_1000"));
|
|
|
137 |
}
|
| 21541 |
ashik.ali |
138 |
|
| 21755 |
ashik.ali |
139 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG_ID, method = RequestMethod.GET)
|
| 21749 |
ashik.ali |
140 |
public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
|
|
|
141 |
try{
|
|
|
142 |
return responseSender.ok(tagRepository.selectById(id));
|
|
|
143 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
144 |
return responseSender.badRequest(profitMandiBusinessException);
|
|
|
145 |
}
|
| 21541 |
ashik.ali |
146 |
}
|
| 21749 |
ashik.ali |
147 |
|
| 21755 |
ashik.ali |
148 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG_ALL, method = RequestMethod.GET)
|
| 21749 |
ashik.ali |
149 |
public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
|
|
|
150 |
return responseSender.ok(tagRepository.selectAll(pageNumber, pageSize));
|
|
|
151 |
}
|
|
|
152 |
|
| 21755 |
ashik.ali |
153 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG_ACTIVE, method = RequestMethod.PUT)
|
|
|
154 |
public ResponseEntity<?> setActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
|
|
|
155 |
try {
|
|
|
156 |
tagRepository.updateActiveById(id, true);
|
|
|
157 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
158 |
return responseSender.badRequest(profitMandiBusinessException);
|
|
|
159 |
}
|
|
|
160 |
return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1001"));
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG_INACTIVE, method = RequestMethod.PUT)
|
|
|
164 |
public ResponseEntity<?> setInActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
|
|
|
165 |
try {
|
|
|
166 |
tagRepository.updateActiveById(id, false);
|
|
|
167 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
168 |
return responseSender.badRequest(profitMandiBusinessException);
|
|
|
169 |
}
|
|
|
170 |
return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1002"));
|
|
|
171 |
}
|
|
|
172 |
|
| 21541 |
ashik.ali |
173 |
}
|