| 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.UserRoleRepository;
|
| 21763 |
ashik.ali |
37 |
import com.spice.profitmandi.web.request.CreateTagRequest;
|
| 21541 |
ashik.ali |
38 |
|
|
|
39 |
@Controller
|
| 22037 |
amit.gupta |
40 |
@Transactional(rollbackFor=Throwable.class)
|
| 21749 |
ashik.ali |
41 |
public class TagController {
|
| 21541 |
ashik.ali |
42 |
|
| 21749 |
ashik.ali |
43 |
@Autowired
|
| 22923 |
ashik.ali |
44 |
private TagRepository tagRepository;
|
| 21749 |
ashik.ali |
45 |
|
|
|
46 |
@Autowired
|
| 22923 |
ashik.ali |
47 |
private PinCodeTagRepository pinCodeTagRepository;
|
| 21763 |
ashik.ali |
48 |
|
|
|
49 |
@Autowired
|
| 22923 |
ashik.ali |
50 |
private RetailerRepository retailerRepository;
|
| 22548 |
amit.gupta |
51 |
|
|
|
52 |
@Autowired
|
| 22923 |
ashik.ali |
53 |
private UserAccountRepository userAccountRepoitory;
|
| 21763 |
ashik.ali |
54 |
|
|
|
55 |
@Autowired
|
| 22923 |
ashik.ali |
56 |
private UserRoleRepository userRoleRepository;
|
| 22548 |
amit.gupta |
57 |
|
|
|
58 |
@Autowired
|
| 22923 |
ashik.ali |
59 |
private RetailerTagRepository fofoTagRepository;
|
| 22548 |
amit.gupta |
60 |
|
|
|
61 |
@Autowired
|
| 22923 |
ashik.ali |
62 |
private ResponseSender<?> responseSender;
|
| 21763 |
ashik.ali |
63 |
|
| 21777 |
ashik.ali |
64 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG, method = RequestMethod.POST)
|
| 22548 |
amit.gupta |
65 |
public ResponseEntity<?> createPinCodeTag(HttpServletRequest request, @RequestBody CreateTagRequest createTagRequest) throws Throwable {
|
| 21763 |
ashik.ali |
66 |
Set<String> invalidPinCodes = new HashSet<>();
|
| 21777 |
ashik.ali |
67 |
for(String pinCode : createTagRequest.getPinCodes()){
|
| 21763 |
ashik.ali |
68 |
if(!StringUtils.isValidPinCode(pinCode)){
|
|
|
69 |
invalidPinCodes.add(pinCode);
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
if(!invalidPinCodes.isEmpty()){
|
|
|
73 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.PIN_CODE, invalidPinCodes, "PNCD_VE_1000");
|
|
|
74 |
return responseSender.badRequest(profitMandiBusinessException);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
Set<Integer> foundRetailerIds = new HashSet<>();
|
| 21779 |
ashik.ali |
78 |
if(!createTagRequest.getRetailerIds().isEmpty()){
|
|
|
79 |
List<Retailer> retailers = retailerRepository.selectByIds(createTagRequest.getRetailerIds());
|
|
|
80 |
if(retailers.size() != createTagRequest.getRetailerIds().size()){
|
|
|
81 |
for(Retailer retailer : retailers){
|
|
|
82 |
foundRetailerIds.add(retailer.getId());
|
| 21763 |
ashik.ali |
83 |
}
|
| 21779 |
ashik.ali |
84 |
Collection<?> notFoundRetailerId = CollectionUtils.subtract(createTagRequest.getRetailerIds(), foundRetailerIds);
|
|
|
85 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, notFoundRetailerId, "RTLR_1000");
|
| 21763 |
ashik.ali |
86 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21779 |
ashik.ali |
87 |
}else{
|
| 21783 |
ashik.ali |
88 |
if(createTagRequest.getTagType() == TagType.FOFO){
|
|
|
89 |
// given retailer ids are fofo user id's or not
|
|
|
90 |
for(Retailer retailer : retailers){
|
| 22548 |
amit.gupta |
91 |
int userId = userAccountRepoitory.selectUserIdByRetailerId(retailer.getId());
|
|
|
92 |
List<RoleType> roleTypes = userRoleRepository.selectRoleTypesByUserId(userId);
|
|
|
93 |
if(!roleTypes.contains(RoleType.FOFO)){
|
|
|
94 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, retailer.getId(), "RTLR_1010");
|
|
|
95 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21783 |
ashik.ali |
96 |
}
|
| 21779 |
ashik.ali |
97 |
}
|
|
|
98 |
}
|
| 21763 |
ashik.ali |
99 |
}
|
|
|
100 |
}
|
|
|
101 |
Tag tag = null;
|
| 21804 |
amit.gupta |
102 |
/*try{
|
| 21763 |
ashik.ali |
103 |
tag = tagRepository.selectByLabelAndType(createTagRequest.getLabel(), createTagRequest.getTagType());
|
| 21804 |
amit.gupta |
104 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){*/
|
|
|
105 |
|
|
|
106 |
tag = new Tag();
|
|
|
107 |
tag.setActive(true);
|
|
|
108 |
tag.setDescription(createTagRequest.getDescription());
|
|
|
109 |
tag.setLabel(createTagRequest.getLabel());
|
|
|
110 |
tag.setType(createTagRequest.getTagType());
|
|
|
111 |
tag.setPinAll(createTagRequest.isPinAll());
|
|
|
112 |
tag.setUserAll(createTagRequest.isUserAll());
|
|
|
113 |
tag.setCreatedBy(1);
|
|
|
114 |
tagRepository.persist(tag);
|
| 21763 |
ashik.ali |
115 |
//return responseSender.badRequest(profitMandiBusinessException);
|
| 21804 |
amit.gupta |
116 |
for(int retailerId : new HashSet<>(createTagRequest.getRetailerIds())){
|
| 21763 |
ashik.ali |
117 |
RetailerTag fofoTag = new RetailerTag();
|
|
|
118 |
fofoTag.setRetailerId(retailerId);
|
|
|
119 |
fofoTag.setTagId(tag.getId());
|
|
|
120 |
fofoTag.setActive(true);
|
|
|
121 |
fofoTagRepository.persist(fofoTag);
|
|
|
122 |
}
|
| 21804 |
amit.gupta |
123 |
for(String pinCode : new HashSet<>(createTagRequest.getPinCodes())){
|
| 21763 |
ashik.ali |
124 |
PinCodeTag pinCodeTag = new PinCodeTag();
|
|
|
125 |
pinCodeTag.setPinCode(pinCode);
|
|
|
126 |
pinCodeTag.setTagId(tag.getId());
|
|
|
127 |
pinCodeTag.setActive(true);
|
|
|
128 |
pinCodeTagRepository.persist(pinCodeTag);
|
|
|
129 |
}
|
|
|
130 |
return responseSender.ok(ResponseCodeHolder.getMessage("PNCDTG_OK_1000"));
|
|
|
131 |
}
|
| 21541 |
ashik.ali |
132 |
|
| 21755 |
ashik.ali |
133 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG_ID, method = RequestMethod.GET)
|
| 22923 |
ashik.ali |
134 |
public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) throws ProfitMandiBusinessException {
|
|
|
135 |
return responseSender.ok(tagRepository.selectById(id));
|
| 21541 |
ashik.ali |
136 |
}
|
| 21749 |
ashik.ali |
137 |
|
| 21755 |
ashik.ali |
138 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG_ALL, method = RequestMethod.GET)
|
| 21749 |
ashik.ali |
139 |
public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
|
|
|
140 |
return responseSender.ok(tagRepository.selectAll(pageNumber, pageSize));
|
|
|
141 |
}
|
|
|
142 |
|
| 21755 |
ashik.ali |
143 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG_ACTIVE, method = RequestMethod.PUT)
|
| 22923 |
ashik.ali |
144 |
public ResponseEntity<?> setActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) throws ProfitMandiBusinessException{
|
|
|
145 |
tagRepository.updateActiveById(id, true);
|
| 21755 |
ashik.ali |
146 |
return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1001"));
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
@RequestMapping(value = ProfitMandiConstants.URL_TAG_INACTIVE, method = RequestMethod.PUT)
|
| 22923 |
ashik.ali |
150 |
public ResponseEntity<?> setInActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) throws ProfitMandiBusinessException{
|
|
|
151 |
tagRepository.updateActiveById(id, false);
|
| 21755 |
ashik.ali |
152 |
return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1002"));
|
|
|
153 |
}
|
|
|
154 |
|
| 21541 |
ashik.ali |
155 |
}
|