Subversion Repositories SmartDukaan

Rev

Rev 21804 | Rev 22548 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.ResponseCodeHolder;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.util.StringUtils;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.catalog.PinCodeTag;
import com.spice.profitmandi.dao.entity.catalog.RetailerTag;
import com.spice.profitmandi.dao.entity.catalog.Tag;
import com.spice.profitmandi.dao.entity.dtr.Retailer;
import com.spice.profitmandi.dao.enumuration.fofo.TagType;
import com.spice.profitmandi.dao.repository.catalog.PinCodeTagRepository;
import com.spice.profitmandi.dao.repository.catalog.RetailerTagRepository;
import com.spice.profitmandi.dao.repository.catalog.TagRepository;
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
import com.spice.profitmandi.web.request.CreateTagRequest;

@Controller
@Transactional(rollbackFor=Throwable.class)
public class TagController {

        @Autowired
        TagRepository tagRepository;
        
        @Autowired
        PinCodeTagRepository pinCodeTagRepository;
        
        @Autowired
        RetailerRepository retailerRepository;
        
        @Autowired
        RetailerTagRepository fofoTagRepository;
        
        @Autowired
        ResponseSender<?> responseSender;
        
        @RequestMapping(value = ProfitMandiConstants.URL_TAG, method = RequestMethod.POST)
        public ResponseEntity<?> createPinCodeTag(HttpServletRequest request, @RequestBody CreateTagRequest createTagRequest) {
                Set<String> invalidPinCodes = new HashSet<>();
                for(String pinCode : createTagRequest.getPinCodes()){
                        if(!StringUtils.isValidPinCode(pinCode)){
                                invalidPinCodes.add(pinCode);
                        }
                }
                if(!invalidPinCodes.isEmpty()){
                        ProfitMandiBusinessException profitMandiBusinessException =  new ProfitMandiBusinessException(ProfitMandiConstants.PIN_CODE, invalidPinCodes, "PNCD_VE_1000");
                        return responseSender.badRequest(profitMandiBusinessException);
                }
                
                Set<Integer> foundRetailerIds = new HashSet<>();
                if(!createTagRequest.getRetailerIds().isEmpty()){
                        List<Retailer> retailers = retailerRepository.selectByIds(createTagRequest.getRetailerIds());
                        if(retailers.size() != createTagRequest.getRetailerIds().size()){
                                for(Retailer retailer : retailers){
                                        foundRetailerIds.add(retailer.getId());
                                }
                                Collection<?> notFoundRetailerId = CollectionUtils.subtract(createTagRequest.getRetailerIds(), foundRetailerIds);
                                ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, notFoundRetailerId, "RTLR_1000");
                                return responseSender.badRequest(profitMandiBusinessException);
                        }else{
                                if(createTagRequest.getTagType() == TagType.FOFO){
                                        // given retailer ids are fofo user id's or not
                                        Set<Integer> notFofoRetailerIds = new HashSet<>();
                                        for(Retailer retailer : retailers){
                                                if(!retailer.isFofo()){
                                                        notFofoRetailerIds.add(retailer.getId());
                                                }
                                        }
                                        if(!notFofoRetailerIds.isEmpty()){
                                                ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.REJECTED_USER_IDS, notFofoRetailerIds, "RTLR_1010");
                                                return responseSender.badRequest(profitMandiBusinessException);
                                        }
                                }
                        }
                }
                Tag tag = null;
                /*try{
                        tag = tagRepository.selectByLabelAndType(createTagRequest.getLabel(), createTagRequest.getTagType());
                }catch(ProfitMandiBusinessException profitMandiBusinessException){*/

                tag = new Tag();
                tag.setActive(true);
                tag.setDescription(createTagRequest.getDescription());
                tag.setLabel(createTagRequest.getLabel());
                tag.setType(createTagRequest.getTagType());
                tag.setPinAll(createTagRequest.isPinAll());
                tag.setUserAll(createTagRequest.isUserAll());
                tag.setCreatedBy(1);
                tagRepository.persist(tag);
                        //return responseSender.badRequest(profitMandiBusinessException);
                for(int retailerId : new HashSet<>(createTagRequest.getRetailerIds())){
                        RetailerTag fofoTag = new RetailerTag();
                        fofoTag.setRetailerId(retailerId);
                        fofoTag.setTagId(tag.getId());
                        fofoTag.setActive(true);
                        fofoTagRepository.persist(fofoTag);
                }
                for(String pinCode : new HashSet<>(createTagRequest.getPinCodes())){
                        PinCodeTag pinCodeTag = new PinCodeTag();
                        pinCodeTag.setPinCode(pinCode);
                        pinCodeTag.setTagId(tag.getId());
                        pinCodeTag.setActive(true);
                        pinCodeTagRepository.persist(pinCodeTag);
                }
                return responseSender.ok(ResponseCodeHolder.getMessage("PNCDTG_OK_1000"));
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_TAG_ID, method = RequestMethod.GET)
        public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
                try{
                        return responseSender.ok(tagRepository.selectById(id));
                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_TAG_ALL, method = RequestMethod.GET)
        public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
                return responseSender.ok(tagRepository.selectAll(pageNumber, pageSize));
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_TAG_ACTIVE, method = RequestMethod.PUT)
        public ResponseEntity<?> setActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
                try {
                        tagRepository.updateActiveById(id, true);
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        return responseSender.badRequest(profitMandiBusinessException);
                }
                return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1001"));
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_TAG_INACTIVE, method = RequestMethod.PUT)
        public ResponseEntity<?> setInActive(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ID) int id) {
                try {
                        tagRepository.updateActiveById(id, false);
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        return responseSender.badRequest(profitMandiBusinessException);
                }
                return responseSender.ok(ResponseCodeHolder.getMessage("TG_OK_1002"));
        }
        
}