ua.com.codefire.web.ContactController.java Source code

Java tutorial

Introduction

Here is the source code for ua.com.codefire.web.ContactController.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 ua.com.codefire.web;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
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.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import ua.com.codefire.entity.Contact;
import ua.com.codefire.service.ContactService;
import ua.com.codefire.entity.Users;
import ua.com.codefire.service.UsersServise;

@Controller
public class ContactController {

    private static final String PHONE_REGEX = "^\\+?([0-9]{3})?\\(?[0-9]{2}\\)?[0-9]{3}\\-?[0-9]{2}\\-?[0-9]{2}$";
    private static final String LOGIN_REGEX = "^[a-zA-Z0-9]+([._]?[a-zA-Z0-9]+)*$";
    @Autowired
    private ContactService contactService;

    @Autowired
    private UsersServise usersServise;

    @RequestMapping("/index")
    public String listContacts(Map<String, Object> map) {
        map.put("contact", new Contact());
        map.put("contactList", contactService.listContact());
        return "contact";
    }

    @RequestMapping("/")
    public String home() {
        return "redirect:/index";
    }

    @RequestMapping(value = "/registration", method = RequestMethod.GET)
    public String registration(Model model) {
        model.addAttribute("userForm", new Users());

        return "registration";
    }

    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    public ModelAndView registration(@ModelAttribute("userForm") Users user) {

        ModelAndView model = new ModelAndView();

        if (isEmpty(user.getUsername())) {
            model.addObject("msg_username", "Username is empty!");
        } else if (user.getUsername().length() < 3) {
            model.addObject("msg_username", "Surname length should be more than 3");
        } else if (!CorrectParam(user.getUsername(), LOGIN_REGEX)) {
            model.addObject("msg_username", "Input VALID value!");
        }

        if (isEmpty(user.getPassword())) {
            model.addObject("msg_password", "Password is empty!");
        } else if (user.getPassword().length() < 5) {
            model.addObject("msg_password", "Password length should be more than 5");
        }

        if (isEmpty(user.getFIO())) {
            model.addObject("msg_FIO", "FIO is empty!");
        } else if (user.getFIO().length() < 5) {
            model.addObject("msg_FIO", "FIO length should be more than 5");
        }

        if (model.isEmpty()) {
            usersServise.addUser(user);
            model.setViewName("redirect:/index");
        } else {
            model.setViewName("registration");
        }

        return model;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView addContact(@ModelAttribute("contact") Contact contact) {

        ModelAndView model = new ModelAndView();

        if (isEmpty(contact.getSurname())) {
            model.addObject("msg_surname", "Surname is empty!");
        } else if (contact.getSurname().length() < 4) {
            model.addObject("msg_surname", "Surname length should be more than 3");
        }

        if (isEmpty(contact.getFirstname())) {
            model.addObject("msg_firstname", "First name is empty!");
        } else if (contact.getFirstname().length() < 4) {
            model.addObject("msg_firstname", "Firstname length should be more than 3");
        }

        if (isEmpty(contact.getLastname())) {
            model.addObject("msg_lastname", "Last name is empty!");
        } else if (contact.getLastname().length() < 4) {
            model.addObject("msg_lastname", "Lastname length should be more than 3");
        }

        if (isEmpty(contact.getMobile())) {
            model.addObject("msg_mobile", "Mobile is empty!");
        } else if (!CorrectParam(contact.getMobile(), PHONE_REGEX)) {
            model.addObject("msg_mobile", "Input VALID value!");
        }

        if (!isEmpty(contact.getTelephonehome()) && !CorrectParam(contact.getTelephonehome(), PHONE_REGEX)) {
            model.addObject("msg_telephonehome", "Input VALID value!");
        }

        if (model.isEmpty()) {
            contact.setUsername(contactService.CurrentUser());
            contactService.addContact(contact);
            model.setViewName("redirect:/index");
        } else {
            model.setViewName("contact");
        }

        return model;

    }

    @RequestMapping("/delete/{contactId}")
    public String deleteContact(@PathVariable("contactId") Integer contactId) {
        contactService.removeContact(contactId);
        return "redirect:/index";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(@RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "logout", required = false) String logout) {

        ModelAndView model = new ModelAndView();
        if (error != null) {
            model.addObject("error", "Invalid username and password!");
        }

        if (logout != null) {

            model.addObject("msg", "You've been logged out successfully.");
        }
        model.setViewName("login");

        return model;

    }

    //for 403 access denied page
    @RequestMapping(value = "/403", method = RequestMethod.GET)
    public ModelAndView accesssDenied() {

        ModelAndView model = new ModelAndView();

        //check if user is login
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (!(auth instanceof AnonymousAuthenticationToken)) {
            UserDetails userDetail = (UserDetails) auth.getPrincipal();
            System.out.println(userDetail);

            model.addObject("username", userDetail.getUsername());

        }

        model.setViewName("403");
        return model;

    }

    //logout
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ModelAndView logout() {

        ModelAndView model = new ModelAndView();

        model.setViewName("logout");
        return model;

    }

    private Boolean isEmpty(String param) {
        if (param == null || param.trim().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    private Boolean CorrectParam(String param, String regex) {
        if (param.matches(regex)) {
            return true;
        } else {
            return false;
        }
    }

}