Subversion Repositories SmartDukaan

Rev

Rev 31497 | 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.HashSet;
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.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
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.mongodb.DBObject;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.catalog.BrandCatalog;
import com.spice.profitmandi.dao.entity.catalog.BrandCategory;
import com.spice.profitmandi.dao.entity.dtr.Api;
import com.spice.profitmandi.dao.entity.dtr.Role;
import com.spice.profitmandi.dao.entity.dtr.User;
import com.spice.profitmandi.dao.enumuration.dtr.Method;
import com.spice.profitmandi.dao.repository.catalog.BrandCategoryRepository;

import com.spice.profitmandi.dao.repository.catalog.BrandsRepository;
import com.spice.profitmandi.dao.repository.dtr.ApiRepository;
import com.spice.profitmandi.dao.repository.dtr.Mongo;
import com.spice.profitmandi.dao.repository.dtr.RoleApiRepository;
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
import com.spice.profitmandi.service.authentication.RoleService;

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

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

        @Autowired
        @Qualifier("userRepository")
        private UserRepository userRepository;

        @Autowired
        private UserRoleRepository userRoleRepository;

        @Autowired
        private RoleRepository roleRepository;

        @Autowired
        private RoleApiRepository roleApiRepository;

        @Autowired
        private ApiRepository apiRepository;

        @Autowired
        private RoleService roleService;

        @RequestMapping(value = "/createRole", method = RequestMethod.GET)
        public String createRole(HttpServletRequest request, Model model) throws Exception {
                return "create-role";
        }

        @RequestMapping(value = "/createRole", method = RequestMethod.POST)
        public String createRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.NAME) String name,
                        Model model) throws Exception {
                roleService.createRole(name);
                return "create-role";
        }

        @RequestMapping(value = "/createDuplicateRole", method = RequestMethod.GET)
        public String createDuplicateRole(HttpServletRequest request, Model model) throws Exception {
                List<Role> roles = roleRepository.selectAll();
                model.addAttribute("roles", roles);
                return "create-duplicate-role";
        }

        @RequestMapping(value = "/createDuplicateRole", method = RequestMethod.POST)
        public String createDuplicateRole(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId,
                        @RequestParam(name = ProfitMandiConstants.NAME) String name, Model model) throws Exception {
                roleService.createDuplicateRole(roleId, name);
                List<Role> roles = roleRepository.selectAll();
                model.addAttribute("roles", roles);
                return "create-duplicate-role";
        }

        @RequestMapping(value = "/deleteRole", method = RequestMethod.GET)
        public String deleteRole(HttpServletRequest request, Model model) throws Exception {
                List<Role> roles = roleRepository.selectAll();
                model.addAttribute("roles", roles);
                return "delete-role";
        }

        @RequestMapping(value = "/deleteRole", method = RequestMethod.DELETE)
        public String deleteRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId,
                        Model model) throws Exception {
                roleService.deleteRole(roleId);
                List<Role> roles = roleRepository.selectAll();
                model.addAttribute("roles", roles);
                return "delete-role";
        }

        @RequestMapping(value = "/renameRole", method = RequestMethod.GET)
        public String renameRole(HttpServletRequest request, Model model) throws Exception {
                List<Role> roles = roleRepository.selectAll();
                model.addAttribute("roles", roles);
                return "rename-role";
        }

        @RequestMapping(value = "/renameRole", method = RequestMethod.PUT)
        public String renameRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId,
                        @RequestParam(name = ProfitMandiConstants.NAME) String name, Model model) throws Exception {
                roleService.renameRole(roleId, name);
                List<Role> roles = roleRepository.selectAll();
                model.addAttribute("roles", roles);
                return "rename-role";
        }

        @RequestMapping(value = "/createApi", method = RequestMethod.GET)
        public String createApi(HttpServletRequest request, Model model) throws Exception {
                model.addAttribute("methods", Method.values());
                return "create-api";
        }

        @RequestMapping(value = "/createApi", method = RequestMethod.POST)
        public String createApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.NAME) String name,
                        @RequestParam(name = ProfitMandiConstants.URI) String uri,
                        @RequestParam(name = ProfitMandiConstants.METHOD) Method method, Model model) throws Exception {
                roleService.createApi(name, uri, method);
                return "create-api";
        }

        @RequestMapping(value = "/editApi", method = RequestMethod.GET)
        public String editApi(HttpServletRequest request, Model model) throws Exception {
                List<Api> apis = apiRepository.selectAll();
                model.addAttribute("apis", apis);
                model.addAttribute("methods", Method.values());
                return "edit-api";
        }

        @RequestMapping(value = "/editApi", method = RequestMethod.PUT)
        public String editApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.API_ID) int apiId,
                        @RequestParam(name = ProfitMandiConstants.NAME) String name,
                        @RequestParam(name = ProfitMandiConstants.URI) String uri,
                        @RequestParam(name = ProfitMandiConstants.METHOD) Method method, Model model) throws Exception {
                roleService.editApi(apiId, name, uri, method);
                List<Api> apis = apiRepository.selectAll();
                model.addAttribute("apis", apis);
                model.addAttribute("methods", Method.values());
                return "edit-api";
        }

        @RequestMapping(value = "/deleteApi", method = RequestMethod.GET)
        public String deleteApi(HttpServletRequest request, Model model) throws Exception {
                List<Api> apis = apiRepository.selectAll();
                model.addAttribute("apis", apis);
                model.addAttribute("methods", Method.values());
                return "delete-api";
        }

        @RequestMapping(value = "/deleteApi", method = RequestMethod.DELETE)
        public String deleteApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.API_ID) int apiId,
                        Model model) throws Exception {
                roleService.deleteApi(apiId);
                List<Api> apis = apiRepository.selectAll();
                model.addAttribute("apis", apis);
                model.addAttribute("methods", Method.values());
                return "delete-api";
        }

        @RequestMapping(value = "/addRemoveRole", method = RequestMethod.GET)
        public String addRemoveRole(HttpServletRequest request, Model model) throws Exception {
                return "add-remove-role";
        }

        @RequestMapping(value = "/userRoles", method = RequestMethod.GET)
        public String userRoles(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber,
                        Model model) throws Exception {
                User user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
                List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
                List<Role> roles = roleRepository.selectAll();
                model.addAttribute("userRoleIds", roleIds);
                model.addAttribute("roles", roles);
                return "user-roles";
        }

        @RequestMapping(value = "/addRemoveRoles", method = RequestMethod.POST)
        public String addRemoveRoles(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber,
                        @RequestBody List<Integer> roleIds, Model model) throws Exception {
                roleService.addRemoveRoles(emailIdOrMobileNumber, new HashSet<>(roleIds));
                return "add-remove-role";
        }

        @RequestMapping(value = "/addRemoveApi", method = RequestMethod.GET)
        public String addRemoveApi(HttpServletRequest request, Model model) throws Exception {
                List<Role> roles = roleRepository.selectAll();
                model.addAttribute("roles", roles);
                return "add-remove-api";
        }

        @RequestMapping(value = "/roleApis", method = RequestMethod.GET)
        public String roleApis(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId,
                        Model model) throws Exception {
                Map<String, Object> map = roleService.getApisByRoleId(roleId);
                model.addAllAttributes(map);
                return "role-apis";
        }

        @RequestMapping(value = "/addRemoveApis", method = RequestMethod.POST)
        public String addRemoveApi(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId, @RequestBody List<Integer> apiIds,
                        Model model) throws Exception {
                roleService.addRemoveApis(roleId, new HashSet<>(apiIds));
                List<Role> roles = roleRepository.selectAll();
                model.addAttribute("roles", roles);
                return "add-remove-api";
        }

        @Autowired
        private Mongo mongoClient;

        @Autowired
        private BrandsRepository brandsRepository;

        @Autowired
        private BrandCategoryRepository brandCategoryRepository;

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

                List<DBObject> mobileBrands = mongoClient.getAllBrandsToDisplay(6);
                for (DBObject mobileBrand : mobileBrands) {
                        String brandName = (String) mobileBrand.get("name");
                        BrandCatalog brand = brandsRepository.selectByBrand(brandName);

                        if (brand == null) {
                                brand = new BrandCatalog();
                                brand.setName(brandName);
                                brand.setLogoUrl((String) mobileBrand.get("url"));
                                brandsRepository.persist(brand);
                        }

                        LOGGER.info("brand {}", brand);

                        Double category = (Double) mobileBrand.get("categoryId");

                        Double rank = (Double) mobileBrand.get("rank");

                        BrandCategory brandCategory = new BrandCategory();
                        brandCategory.setActive((boolean) mobileBrand.get("active"));
                        brandCategory.setCategoryId(category.intValue());
                        brandCategory.setBrandId(brand.getId());
                        brandCategory.setRank(rank.intValue());
                        brandCategoryRepository.persist(brandCategory);

                        LOGGER.info("brandCategory {}", brandCategory);

                }

                return "add-remove-api";
        }

}