Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
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;
21763 ashik.ali 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;
21749 ashik.ali 28
import com.spice.profitmandi.dao.entity.fofo.Tag;
21763 ashik.ali 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;
21749 ashik.ali 32
import com.spice.profitmandi.dao.repository.fofo.TagRepository;
21763 ashik.ali 33
import com.spice.profitmandi.web.request.CreateTagRequest;
21541 ashik.ali 34
 
35
@Controller
21749 ashik.ali 36
@Transactional
37
public class TagController {
21541 ashik.ali 38
 
21749 ashik.ali 39
	@Autowired
40
	TagRepository tagRepository;
41
 
42
	@Autowired
21763 ashik.ali 43
	PinCodeTagRepository pinCodeTagRepository;
44
 
45
	@Autowired
46
	RetailerRepository retailerRepository;
47
 
48
	@Autowired
49
	RetailerTagRepository fofoTagRepository;
50
 
51
	@Autowired
21749 ashik.ali 52
	ResponseSender<?> responseSender;
53
 
21777 ashik.ali 54
	@RequestMapping(value = ProfitMandiConstants.URL_TAG, method = RequestMethod.POST)
21763 ashik.ali 55
	public ResponseEntity<?> createPinCodeTag(HttpServletRequest request, @RequestBody CreateTagRequest createTagRequest) {
56
		Set<String> invalidPinCodes = new HashSet<>();
21777 ashik.ali 57
		for(String pinCode : createTagRequest.getPinCodes()){
21763 ashik.ali 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<>();
21777 ashik.ali 68
		List<Retailer> retailers = retailerRepository.selectByIds(createTagRequest.getRetailerIds());
69
		if(retailers.size() != createTagRequest.getRetailerIds().size()){
21763 ashik.ali 70
			for(Retailer retailer : retailers){
71
				foundRetailerIds.add(retailer.getId());
72
			}
21777 ashik.ali 73
			Collection<?> notFoundRetailerId = CollectionUtils.subtract(createTagRequest.getRetailerIds(), foundRetailerIds);
21763 ashik.ali 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
		}
21777 ashik.ali 104
		for(int retailerId : createTagRequest.getRetailerIds()){
21763 ashik.ali 105
			RetailerTag fofoTag = new RetailerTag();
106
			fofoTag.setRetailerId(retailerId);
107
			fofoTag.setTagId(tag.getId());
108
			fofoTag.setActive(true);
109
			fofoTagRepository.persist(fofoTag);
110
		}
21777 ashik.ali 111
		for(String pinCode : createTagRequest.getPinCodes()){
21763 ashik.ali 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
	}
21541 ashik.ali 120
 
21755 ashik.ali 121
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_ID, method = RequestMethod.GET)
21749 ashik.ali 122
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
123
		try{
124
			return responseSender.ok(tagRepository.selectById(id));
125
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
126
			return responseSender.badRequest(profitMandiBusinessException);
127
		}
21541 ashik.ali 128
	}
21749 ashik.ali 129
 
21755 ashik.ali 130
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_ALL, method = RequestMethod.GET)
21749 ashik.ali 131
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
132
		return responseSender.ok(tagRepository.selectAll(pageNumber, pageSize));
133
	}
134
 
21755 ashik.ali 135
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_ACTIVE, method = RequestMethod.PUT)
136
	public ResponseEntity<?> setActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
137
		try {
138
			tagRepository.updateActiveById(id, true);
139
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
140
			return responseSender.badRequest(profitMandiBusinessException);
141
		}
142
		return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1001"));
143
	}
144
 
145
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_INACTIVE, method = RequestMethod.PUT)
146
	public ResponseEntity<?> setInActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
147
		try {
148
			tagRepository.updateActiveById(id, false);
149
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
150
			return responseSender.badRequest(profitMandiBusinessException);
151
		}
152
		return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1002"));
153
	}
154
 
21541 ashik.ali 155
}