org.stockwatcher.web.UserController.java Source code

Java tutorial

Introduction

Here is the source code for org.stockwatcher.web.UserController.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.stockwatcher.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.stockwatcher.data.UserDAO;
import org.stockwatcher.domain.User;

/**
 * Controller class for all User-related web requests. Uses DAO classes to 
 * interact with the database.
 * 
 * @author Tony Piazza
 */
@Controller
@RequestMapping("/users")
public class UserController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserDAO dao;

    @RequestMapping(method = RequestMethod.GET)
    public String allUsers(Model model) {
        model.addAttribute("users", dao.getUsers());
        return "users";
    }

    @RequestMapping(value = "/select/{userId}", method = RequestMethod.GET)
    public String selectUser(@PathVariable String userId, Model model, HttpServletRequest request,
            HttpServletResponse response) {
        String view = "redirect:/main/users";
        // first we need to validate the user
        User user = getUser(userId);
        if (user != null) {
            LOGGER.info("User selected with id = {}", userId);
            request.getSession().setAttribute("user", user);
        } else {
            String path = request.getQueryString();
            view = path.substring(0, path.indexOf("/select/"));
        }
        return view;
    }
}