com.simon.server.controller.HomeController.java Source code

Java tutorial

Introduction

Here is the source code for com.simon.server.controller.HomeController.java

Source

/*
 * Copyright 2012-2013 the original author or authors.
 *
 * 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 com.simon.server.controller;

import com.simon.server.activemq.MessageConsumer;
import com.simon.server.model.People;
import com.simon.server.service.FamilyRepository;
import com.simon.server.service.PeopleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.jms.MessageProducer;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Controller
@EnableAutoConfiguration
@Configuration
public class HomeController {
    private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class);
    private static final String TAG = HomeController.class.getSimpleName();
    @Autowired
    private PeopleService peopleService;

    @Autowired
    private FamilyRepository familyRepository;

    @Autowired
    private MessageConsumer messageConsumer;

    @Autowired
    MessageProducer messageProducer;

    // initialization

    /**
     * Simply selects the home view to render by returning its name.
     *
     * @throws Exception
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView home(Locale locale, Model model) throws Exception {

        LOGGER.info(TAG, "Welcome home! The client locale is {}.", locale);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("home");

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate);

        model.addAttribute("peoples", peopleService.getEntityList());

        return mav;
    }

    @RequestMapping(value = "/peoples", method = RequestMethod.GET)
    public @ResponseBody List<People> getPeoples() throws Exception {
        return peopleService.getEntityList();
    }

    @RequestMapping(value = "/peoples", method = RequestMethod.POST)
    @ResponseBody
    public void addPeople(@RequestBody People people) throws Exception {
        peopleService.addEntity(people);
    }

    @RequestMapping(value = "/peoples/{ID}", method = RequestMethod.GET)
    public @ResponseBody People getPeople(@PathVariable int ID) throws Exception {
        return peopleService.getEntityById(ID);
    }

    @RequestMapping(value = "/peoples/{ID}", method = RequestMethod.PUT)
    @ResponseBody
    public void putPeople(@PathVariable int ID, @RequestBody People people) throws Exception {
        peopleService.updateEntity(ID, people);
    }

    @RequestMapping(value = "/peoples/{ID}", method = RequestMethod.DELETE)
    @ResponseBody
    public void deletePeople(@PathVariable int ID) throws Exception {
        LOGGER.info(TAG, " to delete a people");
        peopleService.deleteEntity(ID);
    }
}