Subversion Repositories SmartDukaan

Rev

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

Rev 21760 Rev 21763
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import java.util.Collection;
3
import javax.servlet.http.HttpServletRequest;
4
import java.util.HashSet;
-
 
5
import java.util.List;
-
 
6
import java.util.Set;
4
 
7
 
-
 
8
import javax.servlet.http.HttpServletRequest;
5
 
9
 
-
 
10
import org.apache.commons.collections.CollectionUtils;
6
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.http.ResponseEntity;
12
import org.springframework.http.ResponseEntity;
8
import org.springframework.stereotype.Controller;
13
import org.springframework.stereotype.Controller;
9
import org.springframework.transaction.annotation.Transactional;
14
import org.springframework.transaction.annotation.Transactional;
10
import org.springframework.web.bind.annotation.RequestBody;
15
import org.springframework.web.bind.annotation.RequestBody;
Line 13... Line 18...
13
import org.springframework.web.bind.annotation.RequestParam;
18
import org.springframework.web.bind.annotation.RequestParam;
14
 
19
 
15
import com.spice.profitmandi.common.ResponseCodeHolder;
20
import com.spice.profitmandi.common.ResponseCodeHolder;
16
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
17
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
23
import com.spice.profitmandi.common.util.StringUtils;
18
import com.spice.profitmandi.common.web.util.ResponseSender;
24
import com.spice.profitmandi.common.web.util.ResponseSender;
-
 
25
import com.spice.profitmandi.dao.entity.dtr.Retailer;
-
 
26
import com.spice.profitmandi.dao.entity.fofo.RetailerTag;
-
 
27
import com.spice.profitmandi.dao.entity.fofo.PinCodeTag;
19
import com.spice.profitmandi.dao.entity.fofo.Tag;
28
import com.spice.profitmandi.dao.entity.fofo.Tag;
-
 
29
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
-
 
30
import com.spice.profitmandi.dao.repository.fofo.RetailerTagRepository;
-
 
31
import com.spice.profitmandi.dao.repository.fofo.PinCodeTagRepository;
20
import com.spice.profitmandi.dao.repository.fofo.TagRepository;
32
import com.spice.profitmandi.dao.repository.fofo.TagRepository;
21
import com.spice.profitmandi.web.request.CreatePinCodeTagRequest;
33
import com.spice.profitmandi.web.request.CreateTagRequest;
22
 
34
 
23
@Controller
35
@Controller
24
@Transactional
36
@Transactional
25
public class TagController {
37
public class TagController {
26
 
38
 
27
	@Autowired
39
	@Autowired
28
	TagRepository tagRepository;
40
	TagRepository tagRepository;
29
	
41
	
30
	@Autowired
42
	@Autowired
-
 
43
	PinCodeTagRepository pinCodeTagRepository;
-
 
44
	
-
 
45
	@Autowired
-
 
46
	RetailerRepository retailerRepository;
-
 
47
	
-
 
48
	@Autowired
-
 
49
	RetailerTagRepository fofoTagRepository;
-
 
50
	
-
 
51
	@Autowired
31
	ResponseSender<?> responseSender;
52
	ResponseSender<?> responseSender;
32
	
53
	
-
 
54
	@RequestMapping(value = ProfitMandiConstants.URL_PIN_CODE_TAG, method = RequestMethod.POST)
-
 
55
	public ResponseEntity<?> createPinCodeTag(HttpServletRequest request, @RequestBody CreateTagRequest createTagRequest) {
-
 
56
		Set<String> invalidPinCodes = new HashSet<>();
-
 
57
		for(String pinCode : createTagRequest.getRejectedPinCodes()){
-
 
58
			if(!StringUtils.isValidPinCode(pinCode)){
-
 
59
				invalidPinCodes.add(pinCode);
-
 
60
			}
-
 
61
		}
-
 
62
		if(!invalidPinCodes.isEmpty()){
-
 
63
			ProfitMandiBusinessException profitMandiBusinessException =  new ProfitMandiBusinessException(ProfitMandiConstants.PIN_CODE, invalidPinCodes, "PNCD_VE_1000");
-
 
64
			return responseSender.badRequest(profitMandiBusinessException);
-
 
65
		}
-
 
66
		
-
 
67
		Set<Integer> foundRetailerIds = new HashSet<>();
-
 
68
		List<Retailer> retailers = retailerRepository.selectByIds(createTagRequest.getRejectedRetailerIds());
-
 
69
		if(retailers.size() != createTagRequest.getRejectedRetailerIds().size()){
-
 
70
			for(Retailer retailer : retailers){
-
 
71
				foundRetailerIds.add(retailer.getId());
-
 
72
			}
-
 
73
			Collection<?> notFoundRetailerId = CollectionUtils.subtract(createTagRequest.getRejectedRetailerIds(), foundRetailerIds);
-
 
74
			ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, notFoundRetailerId, "RTLR_1000");
-
 
75
			return responseSender.badRequest(profitMandiBusinessException);
-
 
76
		}else{
-
 
77
			// given retailer ids are fofo user id's or not
-
 
78
			Set<Integer> notFofoRetailerIds = new HashSet<>();
-
 
79
			for(Retailer retailer : retailers){
-
 
80
				if(!retailer.isFofo()){
-
 
81
					notFofoRetailerIds.add(retailer.getId());
-
 
82
				}
-
 
83
			}
-
 
84
			if(!notFofoRetailerIds.isEmpty()){
-
 
85
				ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, notFofoRetailerIds, "RTLR_1010");
-
 
86
				return responseSender.badRequest(profitMandiBusinessException);
-
 
87
			}
-
 
88
		}
-
 
89
		Tag tag = null;
-
 
90
		try{
-
 
91
			tag = tagRepository.selectByLabelAndType(createTagRequest.getLabel(), createTagRequest.getTagType());
-
 
92
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
93
			tag = new Tag();
-
 
94
			tag.setActive(true);
-
 
95
			tag.setDescription(createTagRequest.getDescription());
-
 
96
			tag.setLabel(createTagRequest.getLabel());
-
 
97
			tag.setType(createTagRequest.getTagType());
-
 
98
			tag.setPinAll(createTagRequest.isPinAll());
-
 
99
			tag.setUserAll(createTagRequest.isUserAll());
-
 
100
			tag.setCreatedBy(1);
-
 
101
			tagRepository.persist(tag);
-
 
102
			//return responseSender.badRequest(profitMandiBusinessException);
-
 
103
		}
-
 
104
		for(int retailerId : createTagRequest.getRejectedRetailerIds()){
-
 
105
			RetailerTag fofoTag = new RetailerTag();
-
 
106
			fofoTag.setRetailerId(retailerId);
-
 
107
			fofoTag.setTagId(tag.getId());
-
 
108
			fofoTag.setActive(true);
-
 
109
			fofoTagRepository.persist(fofoTag);
-
 
110
		}
-
 
111
		for(String pinCode : createTagRequest.getRejectedPinCodes()){
-
 
112
			PinCodeTag pinCodeTag = new PinCodeTag();
-
 
113
			pinCodeTag.setPinCode(pinCode);
-
 
114
			pinCodeTag.setTagId(tag.getId());
-
 
115
			pinCodeTag.setActive(true);
-
 
116
			pinCodeTagRepository.persist(pinCodeTag);
-
 
117
		}
-
 
118
		return responseSender.ok(ResponseCodeHolder.getMessage("PNCDTG_OK_1000"));
-
 
119
	}
33
	
120
	
34
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_ID, method = RequestMethod.GET)
121
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_ID, method = RequestMethod.GET)
35
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
122
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
36
		try{
123
		try{
37
			return responseSender.ok(tagRepository.selectById(id));
124
			return responseSender.ok(tagRepository.selectById(id));