Subversion Repositories SmartDukaan

Rev

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