com.pw.ism.controllers.AdminstratorController.java Source code

Java tutorial

Introduction

Here is the source code for com.pw.ism.controllers.AdminstratorController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pw.ism.controllers;

import com.pw.ism.heartbeat.Heartbeat;
import com.pw.ism.heartbeat.HeartbeatRepository;
import com.pw.ism.message.MessageRepository;
import com.pw.ism.users.User;
import com.pw.ism.users.UserProfile;
import com.pw.ism.users.UserRepository;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 *
 * @author NRS
 */
@Controller
@Secured("ROLE_ADMIN")
@RequestMapping("/admin")
public class AdminstratorController {

    private final HeartbeatRepository heartbeatRepository;
    private final UserRepository userRepository;
    private final MessageRepository messageRepo;
    private final static Logger LOGGER = LoggerFactory.getLogger(CommunicationController.class.getName());

    @Autowired
    private PasswordEncoder passwordEncoder;

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public AdminstratorController(HeartbeatRepository heartbeatRepository, UserRepository userRepository,
            MessageRepository messageRepo, JdbcTemplate jdbcTemplate) {
        this.heartbeatRepository = heartbeatRepository;
        this.userRepository = userRepository;
        this.messageRepo = messageRepo;
        this.jdbcTemplate = jdbcTemplate;
    }

    @RequestMapping
    public String adminPanel() {
        return "admin/adminpanel";
    }

    @RequestMapping("heartbeats")
    public ModelAndView heartbeatMenu() {
        return new ModelAndView("admin/hbmenu", "hbs", heartbeatRepository.getAllHeartbeats());
    }

    @RequestMapping("heartbeats/{name}/delete")
    public ModelAndView deleteHeartBeat(@PathVariable("name") String name, RedirectAttributes redirect) {
        String list[] = name.split("_");
        Heartbeat hb = new Heartbeat();
        hb.setCustomer(list[0]);
        hb.setNetwork(list[1]);
        heartbeatRepository.removeHeartbeat(hb);
        redirect.addFlashAttribute("globalMessage", "Heartbeat deleted: " + name);
        return new ModelAndView("redirect:/admin/heartbeats");
    }

    @RequestMapping("users")
    public ModelAndView usersMenu() {
        return new ModelAndView("admin/users", "users", userRepository.findAll());
    }

    @RequestMapping("userdetails/{id}")
    public ModelAndView viewUserDetails(@PathVariable("id") Long id, RedirectAttributes redirect) {
        User user = userRepository.findOne(id);
        List<UserProfile> userProfiles = jdbcTemplate.query("select ID, TYPE from USER_PROFILE",
                new BeanPropertyRowMapper(UserProfile.class));
        ModelAndView mvc = new ModelAndView("admin/userdetails");
        mvc.addObject("user", userRepository.findOne(id));
        mvc.addObject("profiles", userProfiles);
        return mvc;
    }

    @RequestMapping(value = "user/passchange", params = "form", method = RequestMethod.POST)
    public ModelAndView changePassword(User user, BindingResult result, RedirectAttributes redirect,
            final HttpServletRequest req) {
        System.out.println("empty? " + user.getPassword());
        if (result.hasErrors()) {
            System.out.println("true and empty? " + user.getPassword());
            ModelAndView mvc = new ModelAndView("redirect:/admin/userdetails/{user.id}", "user.id", user.getId());
            mvc.addObject("formErrors", result.getAllErrors());
            return mvc;
        }
        Long id = Long.parseLong(req.getParameter("removeId"));
        User userToChange = userRepository.findOne(id);
        String pass = passwordEncoder.encode(user.getPassword());
        userToChange.setPassword(pass);
        userRepository.save(userToChange);
        return new ModelAndView("redirect:/admin/userdetails/{user.id}", "user.id", userToChange.getId());
    }

    @RequestMapping(value = "user/setstate", params = "form", method = RequestMethod.POST)
    public ModelAndView setState(User user, BindingResult result, RedirectAttributes redirect) {
        User userToChange = userRepository.findOne(user.getId());
        if (userToChange != null) {
            userToChange.setState(user.getState());
            userRepository.save(userToChange);
        } else {
            LOGGER.warn("User ID was not found! Returning old ID.");
            return new ModelAndView("redirect:/admin/userdetails/{user.id}", "user.id", user.getId());
        }
        return new ModelAndView("redirect:/admin/userdetails/{user.id}", "user.id", userToChange.getId());
    }

    @RequestMapping(value = "user/setroles", params = "form", method = RequestMethod.POST)
    public ModelAndView setRoles(User user, UserProfile userProfile, BindingResult result,
            RedirectAttributes redirtect) {
        User userToSave = userRepository.findOne(user.getId());
        userToSave.setRoles(user.getRoles());
        userRepository.save(userToSave);
        return new ModelAndView("redirect:/admin/userdetails/{user.id}", "user.id", userToSave.getId());
    }

    @RequestMapping("stats")
    public ModelAndView showStats() {
        ModelAndView mvc = new ModelAndView();
        mvc.addObject("users", userRepository.findAll());
        mvc.addObject("heartbeats", heartbeatRepository.getAllHeartbeats());
        mvc.addObject("messages", messageRepo.findAll());
        return mvc;
    }
}