Subversion Repositories SmartDukaan

Rev

Rev 21777 | Rev 21783 | 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<>();
21779 ashik.ali 68
		if(!createTagRequest.getRetailerIds().isEmpty()){
69
			List<Retailer> retailers = retailerRepository.selectByIds(createTagRequest.getRetailerIds());
70
			if(retailers.size() != createTagRequest.getRetailerIds().size()){
71
				for(Retailer retailer : retailers){
72
					foundRetailerIds.add(retailer.getId());
21763 ashik.ali 73
				}
21779 ashik.ali 74
				Collection<?> notFoundRetailerId = CollectionUtils.subtract(createTagRequest.getRetailerIds(), foundRetailerIds);
75
				ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, notFoundRetailerId, "RTLR_1000");
21763 ashik.ali 76
				return responseSender.badRequest(profitMandiBusinessException);
21779 ashik.ali 77
			}else{
78
				// given retailer ids are fofo user id's or not
79
				Set<Integer> notFofoRetailerIds = new HashSet<>();
80
				for(Retailer retailer : retailers){
81
					if(!retailer.isFofo()){
82
						notFofoRetailerIds.add(retailer.getId());
83
					}
84
				}
85
				if(!notFofoRetailerIds.isEmpty()){
86
					ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, notFofoRetailerIds, "RTLR_1010");
87
					return responseSender.badRequest(profitMandiBusinessException);
88
				}
21763 ashik.ali 89
			}
90
		}
91
		Tag tag = null;
92
		try{
93
			tag = tagRepository.selectByLabelAndType(createTagRequest.getLabel(), createTagRequest.getTagType());
94
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
95
			tag = new Tag();
96
			tag.setActive(true);
97
			tag.setDescription(createTagRequest.getDescription());
98
			tag.setLabel(createTagRequest.getLabel());
99
			tag.setType(createTagRequest.getTagType());
100
			tag.setPinAll(createTagRequest.isPinAll());
101
			tag.setUserAll(createTagRequest.isUserAll());
102
			tag.setCreatedBy(1);
103
			tagRepository.persist(tag);
104
			//return responseSender.badRequest(profitMandiBusinessException);
105
		}
21777 ashik.ali 106
		for(int retailerId : createTagRequest.getRetailerIds()){
21763 ashik.ali 107
			RetailerTag fofoTag = new RetailerTag();
108
			fofoTag.setRetailerId(retailerId);
109
			fofoTag.setTagId(tag.getId());
110
			fofoTag.setActive(true);
111
			fofoTagRepository.persist(fofoTag);
112
		}
21777 ashik.ali 113
		for(String pinCode : createTagRequest.getPinCodes()){
21763 ashik.ali 114
			PinCodeTag pinCodeTag = new PinCodeTag();
115
			pinCodeTag.setPinCode(pinCode);
116
			pinCodeTag.setTagId(tag.getId());
117
			pinCodeTag.setActive(true);
118
			pinCodeTagRepository.persist(pinCodeTag);
119
		}
120
		return responseSender.ok(ResponseCodeHolder.getMessage("PNCDTG_OK_1000"));
121
	}
21541 ashik.ali 122
 
21755 ashik.ali 123
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_ID, method = RequestMethod.GET)
21749 ashik.ali 124
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
125
		try{
126
			return responseSender.ok(tagRepository.selectById(id));
127
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
128
			return responseSender.badRequest(profitMandiBusinessException);
129
		}
21541 ashik.ali 130
	}
21749 ashik.ali 131
 
21755 ashik.ali 132
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_ALL, method = RequestMethod.GET)
21749 ashik.ali 133
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
134
		return responseSender.ok(tagRepository.selectAll(pageNumber, pageSize));
135
	}
136
 
21755 ashik.ali 137
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_ACTIVE, method = RequestMethod.PUT)
138
	public ResponseEntity<?> setActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
139
		try {
140
			tagRepository.updateActiveById(id, true);
141
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
142
			return responseSender.badRequest(profitMandiBusinessException);
143
		}
144
		return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1001"));
145
	}
146
 
147
	@RequestMapping(value = ProfitMandiConstants.URL_TAG_INACTIVE, method = RequestMethod.PUT)
148
	public ResponseEntity<?> setInActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
149
		try {
150
			tagRepository.updateActiveById(id, false);
151
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
152
			return responseSender.badRequest(profitMandiBusinessException);
153
		}
154
		return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1002"));
155
	}
156
 
21541 ashik.ali 157
}