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 *******************************************************************************/
018/**
019 *
020 */
021package org.mitre.oauth2.repository.impl;
022
023import static org.mitre.util.jpa.JpaUtil.getSingleResult;
024import static org.mitre.util.jpa.JpaUtil.saveOrUpdate;
025
026import java.util.LinkedHashSet;
027import java.util.Set;
028
029import javax.persistence.EntityManager;
030import javax.persistence.PersistenceContext;
031import javax.persistence.TypedQuery;
032
033import org.mitre.oauth2.model.SystemScope;
034import org.mitre.oauth2.repository.SystemScopeRepository;
035import org.springframework.stereotype.Repository;
036import org.springframework.transaction.annotation.Transactional;
037
038/**
039 * @author jricher
040 *
041 */
042@Repository("jpaSystemScopeRepository")
043public class JpaSystemScopeRepository implements SystemScopeRepository {
044
045        @PersistenceContext(unitName="defaultPersistenceUnit")
046        private EntityManager em;
047
048        /* (non-Javadoc)
049         * @see org.mitre.oauth2.repository.SystemScopeRepository#getAll()
050         */
051        @Override
052        @Transactional(value="defaultTransactionManager")
053        public Set<SystemScope> getAll() {
054                TypedQuery<SystemScope> query = em.createNamedQuery(SystemScope.QUERY_ALL, SystemScope.class);
055
056                return new LinkedHashSet<>(query.getResultList());
057        }
058
059        /* (non-Javadoc)
060         * @see org.mitre.oauth2.repository.SystemScopeRepository#getById(java.lang.Long)
061         */
062        @Override
063        @Transactional(value="defaultTransactionManager")
064        public SystemScope getById(Long id) {
065                return em.find(SystemScope.class, id);
066        }
067
068        /* (non-Javadoc)
069         * @see org.mitre.oauth2.repository.SystemScopeRepository#getByValue(java.lang.String)
070         */
071        @Override
072        @Transactional(value="defaultTransactionManager")
073        public SystemScope getByValue(String value) {
074                TypedQuery<SystemScope> query = em.createNamedQuery(SystemScope.QUERY_BY_VALUE, SystemScope.class);
075                query.setParameter(SystemScope.PARAM_VALUE, value);
076                return getSingleResult(query.getResultList());
077        }
078
079        /* (non-Javadoc)
080         * @see org.mitre.oauth2.repository.SystemScopeRepository#remove(org.mitre.oauth2.model.SystemScope)
081         */
082        @Override
083        @Transactional(value="defaultTransactionManager")
084        public void remove(SystemScope scope) {
085                SystemScope found = getById(scope.getId());
086
087                if (found != null) {
088                        em.remove(found);
089                }
090
091        }
092
093        /* (non-Javadoc)
094         * @see org.mitre.oauth2.repository.SystemScopeRepository#save(org.mitre.oauth2.model.SystemScope)
095         */
096        @Override
097        @Transactional(value="defaultTransactionManager")
098        public SystemScope save(SystemScope scope) {
099                return saveOrUpdate(scope.getId(), em, scope);
100        }
101
102}