com.rambird.miles.repository.jdbc.JdbcUserRepositoryImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.rambird.miles.repository.jdbc.JdbcUserRepositoryImpl.java

Source

/*
 * Copyright 2002-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.rambird.miles.repository.jdbc;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.stereotype.Repository;

import com.rambird.miles.model.Owner;
import com.rambird.miles.model.Pet;
import com.rambird.miles.model.User;
import com.rambird.miles.model.Visit;
import com.rambird.miles.repository.OwnerRepository;
import com.rambird.miles.repository.UserRepository;

/**
 * A simple JDBC-based implementation of the {@link OwnerRepository} interface.
 *
 * @author Ken Krebs
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @author Sam Brannen
 * @author Thomas Risberg
 * @author Mark Fisher
 */
@Repository
public class JdbcUserRepositoryImpl implements UserRepository {

    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    public JdbcUserRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {

        this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    }

    /**
     * Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link Pet Pets} and {@link Visit Visits}
     * for the corresponding owner, if not already loaded.
     */
    @Override
    public User authUser(String userName, String password) throws DataAccessException {
        User user;
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("userName", userName);
            params.put("password", password);

            user = this.namedParameterJdbcTemplate.queryForObject(
                    "SELECT user_name, full_name, home FROM myoath WHERE user_name= :userName", params,
                    ParameterizedBeanPropertyRowMapper.newInstance(User.class));
        } catch (EmptyResultDataAccessException ex) {
            throw new ObjectRetrievalFailureException(User.class, userName);
        }
        return user;
    }

}