JpaAuthenticationHolderRepository.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Portions copyright 2011-2013 The MITRE Corporation
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License");
  7.  * you may not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  *******************************************************************************/
  18. package org.mitre.oauth2.repository.impl;

  19. import java.util.List;

  20. import javax.persistence.EntityManager;
  21. import javax.persistence.PersistenceContext;
  22. import javax.persistence.TypedQuery;

  23. import org.mitre.data.DefaultPageCriteria;
  24. import org.mitre.data.PageCriteria;
  25. import org.mitre.oauth2.model.AuthenticationHolderEntity;
  26. import org.mitre.oauth2.repository.AuthenticationHolderRepository;
  27. import org.mitre.util.jpa.JpaUtil;
  28. import org.springframework.stereotype.Repository;
  29. import org.springframework.transaction.annotation.Transactional;

  30. @Repository
  31. @Transactional(value="defaultTransactionManager")
  32. public class JpaAuthenticationHolderRepository implements AuthenticationHolderRepository {

  33.     private static final int MAXEXPIREDRESULTS = 1000;

  34.     @PersistenceContext(unitName="defaultPersistenceUnit")
  35.     private EntityManager manager;

  36.     @Override
  37.     public List<AuthenticationHolderEntity> getAll() {
  38.         TypedQuery<AuthenticationHolderEntity> query = manager.createNamedQuery(AuthenticationHolderEntity.QUERY_ALL, AuthenticationHolderEntity.class);
  39.         return query.getResultList();
  40.     }

  41.     @Override
  42.     public AuthenticationHolderEntity getById(Long id) {
  43.         return manager.find(AuthenticationHolderEntity.class, id);
  44.     }

  45.     @Override
  46.     @Transactional(value="defaultTransactionManager")
  47.     public void remove(AuthenticationHolderEntity a) {
  48.         AuthenticationHolderEntity found = getById(a.getId());
  49.         if (found != null) {
  50.             manager.remove(found);
  51.         } else {
  52.             throw new IllegalArgumentException("AuthenticationHolderEntity not found: " + a);
  53.         }
  54.     }

  55.     @Override
  56.     @Transactional(value="defaultTransactionManager")
  57.     public AuthenticationHolderEntity save(AuthenticationHolderEntity a) {
  58.         return JpaUtil.saveOrUpdate(a.getId(), manager, a);
  59.     }

  60.     @Override
  61.     @Transactional(value="defaultTransactionManager")
  62.     public List<AuthenticationHolderEntity> getOrphanedAuthenticationHolders() {
  63.         DefaultPageCriteria pageCriteria = new DefaultPageCriteria(0,MAXEXPIREDRESULTS);
  64.         return getOrphanedAuthenticationHolders(pageCriteria);
  65.     }

  66.     @Override
  67.     @Transactional(value="defaultTransactionManager")
  68.     public List<AuthenticationHolderEntity> getOrphanedAuthenticationHolders(PageCriteria pageCriteria) {
  69.         TypedQuery<AuthenticationHolderEntity> query = manager.createNamedQuery(AuthenticationHolderEntity.QUERY_GET_UNUSED, AuthenticationHolderEntity.class);
  70.         return JpaUtil.getResultPage(query, pageCriteria);
  71.     }

  72. }