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.saveOrUpdate;
021
022import java.util.Collection;
023
024import javax.persistence.EntityManager;
025import javax.persistence.PersistenceContext;
026import javax.persistence.TypedQuery;
027
028import org.mitre.openid.connect.model.ApprovedSite;
029import org.mitre.openid.connect.repository.ApprovedSiteRepository;
030import org.springframework.stereotype.Repository;
031import org.springframework.transaction.annotation.Transactional;
032
033/**
034 * JPA ApprovedSite repository implementation
035 *
036 * @author Michael Joseph Walsh, aanganes
037 *
038 */
039@Repository
040public class JpaApprovedSiteRepository implements ApprovedSiteRepository {
041
042        @PersistenceContext(unitName="defaultPersistenceUnit")
043        private EntityManager manager;
044
045        @Override
046        @Transactional(value="defaultTransactionManager")
047        public Collection<ApprovedSite> getAll() {
048                TypedQuery<ApprovedSite> query = manager.createNamedQuery(ApprovedSite.QUERY_ALL, ApprovedSite.class);
049                return query.getResultList();
050        }
051
052        @Override
053        @Transactional(value="defaultTransactionManager")
054        public ApprovedSite getById(Long id) {
055                return manager.find(ApprovedSite.class, id);
056        }
057
058        @Override
059        @Transactional(value="defaultTransactionManager")
060        public void remove(ApprovedSite approvedSite) {
061                ApprovedSite found = manager.find(ApprovedSite.class, approvedSite.getId());
062
063                if (found != null) {
064                        manager.remove(found);
065                } else {
066                        throw new IllegalArgumentException();
067                }
068        }
069
070        @Override
071        @Transactional(value="defaultTransactionManager")
072        public ApprovedSite save(ApprovedSite approvedSite) {
073                return saveOrUpdate(approvedSite.getId(), manager, approvedSite);
074        }
075
076        @Override
077        public Collection<ApprovedSite> getByClientIdAndUserId(String clientId, String userId) {
078
079                TypedQuery<ApprovedSite> query = manager.createNamedQuery(ApprovedSite.QUERY_BY_CLIENT_ID_AND_USER_ID, ApprovedSite.class);
080                query.setParameter(ApprovedSite.PARAM_USER_ID, userId);
081                query.setParameter(ApprovedSite.PARAM_CLIENT_ID, clientId);
082
083                return query.getResultList();
084        }
085
086        @Override
087        @Transactional(value="defaultTransactionManager")
088        public Collection<ApprovedSite> getByUserId(String userId) {
089                TypedQuery<ApprovedSite> query = manager.createNamedQuery(ApprovedSite.QUERY_BY_USER_ID, ApprovedSite.class);
090                query.setParameter(ApprovedSite.PARAM_USER_ID, userId);
091
092                return query.getResultList();
093
094        }
095
096        @Override
097        @Transactional(value="defaultTransactionManager")
098        public Collection<ApprovedSite> getByClientId(String clientId) {
099                TypedQuery<ApprovedSite> query = manager.createNamedQuery(ApprovedSite.QUERY_BY_CLIENT_ID, ApprovedSite.class);
100                query.setParameter(ApprovedSite.PARAM_CLIENT_ID, clientId);
101
102                return query.getResultList();
103        }
104}