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.Collection; 021 022import javax.persistence.EntityManager; 023import javax.persistence.PersistenceContext; 024import javax.persistence.TypedQuery; 025 026import org.mitre.oauth2.model.ClientDetailsEntity; 027import org.mitre.oauth2.repository.OAuth2ClientRepository; 028import org.mitre.util.jpa.JpaUtil; 029import org.springframework.stereotype.Repository; 030import org.springframework.transaction.annotation.Transactional; 031 032/** 033 * @author jricher 034 * 035 */ 036@Repository 037@Transactional(value="defaultTransactionManager") 038public class JpaOAuth2ClientRepository implements OAuth2ClientRepository { 039 040 @PersistenceContext(unitName="defaultPersistenceUnit") 041 private EntityManager manager; 042 043 public JpaOAuth2ClientRepository() { 044 045 } 046 047 public JpaOAuth2ClientRepository(EntityManager manager) { 048 this.manager = manager; 049 } 050 051 @Override 052 public ClientDetailsEntity getById(Long id) { 053 return manager.find(ClientDetailsEntity.class, id); 054 } 055 056 /* (non-Javadoc) 057 * @see org.mitre.oauth2.repository.OAuth2ClientRepository#getClientById(java.lang.String) 058 */ 059 @Override 060 public ClientDetailsEntity getClientByClientId(String clientId) { 061 TypedQuery<ClientDetailsEntity> query = manager.createNamedQuery(ClientDetailsEntity.QUERY_BY_CLIENT_ID, ClientDetailsEntity.class); 062 query.setParameter(ClientDetailsEntity.PARAM_CLIENT_ID, clientId); 063 return JpaUtil.getSingleResult(query.getResultList()); 064 } 065 066 /* (non-Javadoc) 067 * @see org.mitre.oauth2.repository.OAuth2ClientRepository#saveClient(org.mitre.oauth2.model.ClientDetailsEntity) 068 */ 069 @Override 070 public ClientDetailsEntity saveClient(ClientDetailsEntity client) { 071 return JpaUtil.saveOrUpdate(client.getClientId(), manager, client); 072 } 073 074 /* (non-Javadoc) 075 * @see org.mitre.oauth2.repository.OAuth2ClientRepository#deleteClient(org.mitre.oauth2.model.ClientDetailsEntity) 076 */ 077 @Override 078 public void deleteClient(ClientDetailsEntity client) { 079 ClientDetailsEntity found = getById(client.getId()); 080 if (found != null) { 081 manager.remove(found); 082 } else { 083 throw new IllegalArgumentException("Client not found: " + client); 084 } 085 } 086 087 @Override 088 public ClientDetailsEntity updateClient(Long id, ClientDetailsEntity client) { 089 // sanity check 090 client.setId(id); 091 092 return JpaUtil.saveOrUpdate(id, manager, client); 093 } 094 095 @Override 096 public Collection<ClientDetailsEntity> getAllClients() { 097 TypedQuery<ClientDetailsEntity> query = manager.createNamedQuery(ClientDetailsEntity.QUERY_ALL, ClientDetailsEntity.class); 098 return query.getResultList(); 099 } 100 101}