JpaSystemScopeRepository.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. /**
  19.  *
  20.  */
  21. package org.mitre.oauth2.repository.impl;

  22. import static org.mitre.util.jpa.JpaUtil.getSingleResult;
  23. import static org.mitre.util.jpa.JpaUtil.saveOrUpdate;

  24. import java.util.LinkedHashSet;
  25. import java.util.Set;

  26. import javax.persistence.EntityManager;
  27. import javax.persistence.PersistenceContext;
  28. import javax.persistence.TypedQuery;

  29. import org.mitre.oauth2.model.SystemScope;
  30. import org.mitre.oauth2.repository.SystemScopeRepository;
  31. import org.springframework.stereotype.Repository;
  32. import org.springframework.transaction.annotation.Transactional;

  33. /**
  34.  * @author jricher
  35.  *
  36.  */
  37. @Repository("jpaSystemScopeRepository")
  38. public class JpaSystemScopeRepository implements SystemScopeRepository {

  39.     @PersistenceContext(unitName="defaultPersistenceUnit")
  40.     private EntityManager em;

  41.     /* (non-Javadoc)
  42.      * @see org.mitre.oauth2.repository.SystemScopeRepository#getAll()
  43.      */
  44.     @Override
  45.     @Transactional(value="defaultTransactionManager")
  46.     public Set<SystemScope> getAll() {
  47.         TypedQuery<SystemScope> query = em.createNamedQuery(SystemScope.QUERY_ALL, SystemScope.class);

  48.         return new LinkedHashSet<>(query.getResultList());
  49.     }

  50.     /* (non-Javadoc)
  51.      * @see org.mitre.oauth2.repository.SystemScopeRepository#getById(java.lang.Long)
  52.      */
  53.     @Override
  54.     @Transactional(value="defaultTransactionManager")
  55.     public SystemScope getById(Long id) {
  56.         return em.find(SystemScope.class, id);
  57.     }

  58.     /* (non-Javadoc)
  59.      * @see org.mitre.oauth2.repository.SystemScopeRepository#getByValue(java.lang.String)
  60.      */
  61.     @Override
  62.     @Transactional(value="defaultTransactionManager")
  63.     public SystemScope getByValue(String value) {
  64.         TypedQuery<SystemScope> query = em.createNamedQuery(SystemScope.QUERY_BY_VALUE, SystemScope.class);
  65.         query.setParameter(SystemScope.PARAM_VALUE, value);
  66.         return getSingleResult(query.getResultList());
  67.     }

  68.     /* (non-Javadoc)
  69.      * @see org.mitre.oauth2.repository.SystemScopeRepository#remove(org.mitre.oauth2.model.SystemScope)
  70.      */
  71.     @Override
  72.     @Transactional(value="defaultTransactionManager")
  73.     public void remove(SystemScope scope) {
  74.         SystemScope found = getById(scope.getId());

  75.         if (found != null) {
  76.             em.remove(found);
  77.         }

  78.     }

  79.     /* (non-Javadoc)
  80.      * @see org.mitre.oauth2.repository.SystemScopeRepository#save(org.mitre.oauth2.model.SystemScope)
  81.      */
  82.     @Override
  83.     @Transactional(value="defaultTransactionManager")
  84.     public SystemScope save(SystemScope scope) {
  85.         return saveOrUpdate(scope.getId(), em, scope);
  86.     }

  87. }