Subversion Repositories SmartDukaan

Rev

Rev 27402 | 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.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.spice.profitmandi.dao.entity.auth.AuthUser;
import com.spice.profitmandi.dao.entity.dtr.GpsLocation;
import com.spice.profitmandi.dao.entity.dtr.User;
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
import com.spice.profitmandi.dao.repository.user.GpsLocationRepository;

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

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

        @Autowired
        GpsLocationRepository gpsLocationRepository;

        @Autowired
        UserRepository userRepository;

        @Autowired
        AuthRepository authRepository;

        @Autowired
        Gson gson;

        @RequestMapping(value = "/authUserMapInfo", method = RequestMethod.GET)
        public String AuthUserMapInfo(HttpServletRequest request, Model model) throws Exception {

                List<AuthUser> authUsers = authRepository.selectAllActiveUser();

                model.addAttribute("authUsers", authUsers);
                return "map-info";
        }

        @RequestMapping(value = "/map", method = RequestMethod.GET)
        public String getByPin(HttpServletRequest request, @RequestParam List<String> emails,
                        @RequestParam LocalDateTime date, Model model) throws Exception {
                Map<Integer, List<GpsLocation>> userIdAndLocationMap = new HashMap<>();
                List<User> users = new ArrayList<>();
                for (String email : emails) {
                        User dtrUser = userRepository.selectByEmailId(email);

                        List<GpsLocation> locations = gpsLocationRepository.selectAllByUserIdAndDate(dtrUser.getId(),
                                        date.toLocalDate());
                        if (!locations.isEmpty()) {
                                userIdAndLocationMap.put(dtrUser.getId(), locations);
                                users.add(dtrUser);
                        }
                }
                LOGGER.info("locations {}", gson.toJson(userIdAndLocationMap));
                model.addAttribute("userLocationMap", gson.toJson(userIdAndLocationMap));
                model.addAttribute("users", gson.toJson(users));
                return "map-index";
        }

}