Subversion Repositories SmartDukaan

Rev

Rev 31599 | 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.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.logistics.PostOffice;
import com.spice.profitmandi.dao.repository.logistics.PostOfficeRepository;

@Controller
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public class PostOfficeController {

        @Autowired
        private ResponseSender<?> responseSender;

        private static final Logger LOGGER = LogManager.getLogger(PostOfficeController.class);

        @Autowired
        private PostOfficeRepository postOfficeRepository;

        @RequestMapping(value = ProfitMandiConstants.URL_POST_OFFICE, method = RequestMethod.GET)
        public ResponseEntity<?> getByPin(HttpServletRequest request, @RequestParam(name = "pinCode") int pinCode) {
                LOGGER.info("requested url : " + request.getRequestURL().toString());
                List<PostOffice> postOffices = postOfficeRepository.selectByPinCode(pinCode);
                Set<String> cities = new HashSet<>();
                Map<String, Object> map = new HashMap<>(3);

                map.put(ProfitMandiConstants.PIN_CODE, pinCode);
                for (PostOffice postOffice : postOffices) {
                        String city = postOffice.getCity();
                        if (city.equals("NA")) {
                                cities.add(postOffice.getDistrict());
        
                        } else {
                                cities.add(postOffice.getCity());
                        }
                        map.put(ProfitMandiConstants.STATE, postOffice.getState());
                        String[] nameArr = postOffice.getCodeName().split( " ");
                        if(nameArr.length > 2)
                        cities.add(postOffice.getCodeName().split( " ")[0]);
                }
                map.put(ProfitMandiConstants.CITIES, cities);
                return responseSender.ok(map);

        }
}