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.oauth2.repository.impl;
019
020import java.util.List;
021
022import javax.persistence.EntityManager;
023import javax.persistence.PersistenceContext;
024import javax.persistence.TypedQuery;
025
026import org.mitre.data.DefaultPageCriteria;
027import org.mitre.data.PageCriteria;
028import org.mitre.oauth2.model.AuthenticationHolderEntity;
029import org.mitre.oauth2.repository.AuthenticationHolderRepository;
030import org.mitre.util.jpa.JpaUtil;
031import org.springframework.stereotype.Repository;
032import org.springframework.transaction.annotation.Transactional;
033
034@Repository
035@Transactional(value="defaultTransactionManager")
036public class JpaAuthenticationHolderRepository implements AuthenticationHolderRepository {
037
038        private static final int MAXEXPIREDRESULTS = 1000;
039
040        @PersistenceContext(unitName="defaultPersistenceUnit")
041        private EntityManager manager;
042
043        @Override
044        public List<AuthenticationHolderEntity> getAll() {
045                TypedQuery<AuthenticationHolderEntity> query = manager.createNamedQuery(AuthenticationHolderEntity.QUERY_ALL, AuthenticationHolderEntity.class);
046                return query.getResultList();
047        }
048
049        @Override
050        public AuthenticationHolderEntity getById(Long id) {
051                return manager.find(AuthenticationHolderEntity.class, id);
052        }
053
054        @Override
055        @Transactional(value="defaultTransactionManager")
056        public void remove(AuthenticationHolderEntity a) {
057                AuthenticationHolderEntity found = getById(a.getId());
058                if (found != null) {
059                        manager.remove(found);
060                } else {
061                        throw new IllegalArgumentException("AuthenticationHolderEntity not found: " + a);
062                }
063        }
064
065        @Override
066        @Transactional(value="defaultTransactionManager")
067        public AuthenticationHolderEntity save(AuthenticationHolderEntity a) {
068                return JpaUtil.saveOrUpdate(a.getId(), manager, a);
069        }
070
071        @Override
072        @Transactional(value="defaultTransactionManager")
073        public List<AuthenticationHolderEntity> getOrphanedAuthenticationHolders() {
074                DefaultPageCriteria pageCriteria = new DefaultPageCriteria(0,MAXEXPIREDRESULTS);
075                return getOrphanedAuthenticationHolders(pageCriteria);
076        }
077
078        @Override
079        @Transactional(value="defaultTransactionManager")
080        public List<AuthenticationHolderEntity> getOrphanedAuthenticationHolders(PageCriteria pageCriteria) {
081                TypedQuery<AuthenticationHolderEntity> query = manager.createNamedQuery(AuthenticationHolderEntity.QUERY_GET_UNUSED, AuthenticationHolderEntity.class);
082                return JpaUtil.getResultPage(query, pageCriteria);
083        }
084
085}