001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Portions copyright 2011-2013 The MITRE Corporation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *******************************************************************************/
018package org.mitre.openid.connect.repository.impl;
019
020import static org.mitre.util.jpa.JpaUtil.getSingleResult;
021
022import javax.persistence.EntityManager;
023import javax.persistence.PersistenceContext;
024import javax.persistence.TypedQuery;
025
026import org.mitre.openid.connect.model.DefaultUserInfo;
027import org.mitre.openid.connect.model.UserInfo;
028import org.mitre.openid.connect.repository.UserInfoRepository;
029import org.springframework.stereotype.Repository;
030
031/**
032 * JPA UserInfo repository implementation
033 *
034 * @author Michael Joseph Walsh
035 *
036 */
037@Repository("jpaUserInfoRepository")
038public class JpaUserInfoRepository implements UserInfoRepository {
039
040        @PersistenceContext(unitName="defaultPersistenceUnit")
041        private EntityManager manager;
042
043        /**
044         * Get a single UserInfo object by its username
045         */
046        @Override
047        public UserInfo getByUsername(String username) {
048                TypedQuery<DefaultUserInfo> query = manager.createNamedQuery(DefaultUserInfo.QUERY_BY_USERNAME, DefaultUserInfo.class);
049                query.setParameter(DefaultUserInfo.PARAM_USERNAME, username);
050
051                return getSingleResult(query.getResultList());
052
053        }
054
055        /**
056         * Get a single UserInfo object by its email address
057         */
058        @Override
059        public UserInfo getByEmailAddress(String email) {
060                TypedQuery<DefaultUserInfo> query = manager.createNamedQuery(DefaultUserInfo.QUERY_BY_EMAIL, DefaultUserInfo.class);
061                query.setParameter(DefaultUserInfo.PARAM_EMAIL, email);
062
063                return getSingleResult(query.getResultList());
064        }
065
066}