nl.enovation.addressbook.jpa.webui.controller.ContactsController.java Source code

Java tutorial

Introduction

Here is the source code for nl.enovation.addressbook.jpa.webui.controller.ContactsController.java

Source

/*
 * Copyright (c) 2010. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.enovation.addressbook.jpa.webui.controller;

import java.util.List;

import javax.validation.Valid;

import nl.enovation.addressbook.jpa.pojo.Contact;
import nl.enovation.addressbook.jpa.repository.ContactRepository;
import nl.enovation.addressbook.jpa.webui.pojo.SearchForm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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;

/**
 * Controller handling all actions regarding contacts.
 * 
 * @author Jettro Coenradie, Maarten van Meijeren
 */
@Controller
@RequestMapping("/contacts")
public class ContactsController {

    private ContactRepository contactRepository;

    private static final Logger LOGGER = LoggerFactory.getLogger(ContactsController.class);

    @RequestMapping(value = "{identifier}", method = RequestMethod.GET)
    public String details(@PathVariable Long identifier, Model model) {
        Contact contact = contactRepository.findOne(identifier);
        LOGGER.debug("Received request for command : {getContact(id)}");
        model.addAttribute("contact", contact);
        return "contacts/details";
    }

    @RequestMapping(value = "{identifier}/delete", method = RequestMethod.POST)
    public String formDelete(@ModelAttribute("contact") Contact contact, BindingResult bindingResult) {
        if (!bindingResult.hasErrors()) {
            contactRepository.delete(contact);
            LOGGER.debug("Received request for command : {removeContact(contact)}");
            return "redirect:/contacts";
        }
        return "contacts/delete";
    }

    @RequestMapping(value = "{identifier}/delete", method = RequestMethod.GET)
    public String formDelete(@PathVariable Long identifier, Model model) {
        Contact contact = contactRepository.findOne(identifier);
        model.addAttribute("contact", contact);
        return "contacts/delete";
    }

    @RequestMapping(value = "{identifier}/edit", method = RequestMethod.GET)
    public String formEdit(@PathVariable Long identifier, Model model) {
        Contact contact = contactRepository.findOne(identifier);
        model.addAttribute("contact", contact);
        return "contacts/edit";
    }

    @RequestMapping(value = "{identifier}/edit", method = RequestMethod.POST)
    public String formEditSubmit(@ModelAttribute("contact") @Valid Contact contact, BindingResult bindingResult) {
        LOGGER.debug("Received form submit for contact with identifier {}", contact.getIdentifier());
        if (bindingResult.hasErrors()) {
            return "contacts/edit";
        }
        contactRepository.save(contact);
        LOGGER.debug("Received request for command : {editContact(contact)}");
        return "redirect:/contacts/" + contact.getIdentifier();
    }

    @RequestMapping(value = "new", method = RequestMethod.GET)
    public String formNew(Model model) {
        Contact attributeValue = new Contact();
        model.addAttribute("contact", attributeValue);
        return "contacts/new";
    }

    @RequestMapping(value = "new", method = RequestMethod.POST)
    public String formNewSubmit(@Valid Contact contact, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "contacts/new";
        }
        contactRepository.save(contact);
        return "redirect:/contacts/" + contact.getIdentifier();
    }

    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        List<Contact> listContacts = contactRepository.findAll();
        SearchForm value = new SearchForm();
        LOGGER.debug("Received request for command : {getContacts}");
        model.addAttribute("contacts", listContacts);
        model.addAttribute("searchForm", value);
        return "contacts/list";
    }

    @RequestMapping(value = "search", method = RequestMethod.POST)
    public String search(@ModelAttribute("searchForm") SearchForm value, Model model, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "contacts/list";
        }
        List<Contact> listSearchContacts = contactRepository.findByName(value.getSearchValue());
        model.addAttribute("contacts", listSearchContacts);
        return "contacts/list";
    }

    public void setContactRepository(ContactRepository contactRepository) {
        this.contactRepository = contactRepository;
    }
}