001/******************************************************************************* 002 * Copyright 2017 The MIT Internet Trust Consortium 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 *******************************************************************************/ 016/** 017 * 018 */ 019package org.mitre.oauth2.repository.impl; 020 021import static org.mitre.util.jpa.JpaUtil.getSingleResult; 022import static org.mitre.util.jpa.JpaUtil.saveOrUpdate; 023 024import java.util.Collection; 025import java.util.Date; 026 027import javax.persistence.EntityManager; 028import javax.persistence.PersistenceContext; 029import javax.persistence.TypedQuery; 030 031import org.mitre.oauth2.model.DeviceCode; 032import org.springframework.stereotype.Repository; 033import org.springframework.transaction.annotation.Transactional; 034 035/** 036 * @author jricher 037 * 038 */ 039@Repository("jpaDeviceCodeRepository") 040public class JpaDeviceCodeRepository implements DeviceCodeRepository { 041 042 @PersistenceContext(unitName="defaultPersistenceUnit") 043 private EntityManager em; 044 045 /* (non-Javadoc) 046 */ 047 @Override 048 @Transactional(value="defaultTransactionManager") 049 public DeviceCode getById(Long id) { 050 return em.find(DeviceCode.class, id); 051 } 052 053 /* (non-Javadoc) 054 */ 055 @Override 056 @Transactional(value="defaultTransactionManager") 057 public DeviceCode getByUserCode(String value) { 058 TypedQuery<DeviceCode> query = em.createNamedQuery(DeviceCode.QUERY_BY_USER_CODE, DeviceCode.class); 059 query.setParameter(DeviceCode.PARAM_USER_CODE, value); 060 return getSingleResult(query.getResultList()); 061 } 062 063 /* (non-Javadoc) 064 */ 065 @Override 066 @Transactional(value="defaultTransactionManager") 067 public DeviceCode getByDeviceCode(String value) { 068 TypedQuery<DeviceCode> query = em.createNamedQuery(DeviceCode.QUERY_BY_DEVICE_CODE, DeviceCode.class); 069 query.setParameter(DeviceCode.PARAM_DEVICE_CODE, value); 070 return getSingleResult(query.getResultList()); 071 } 072 073 /* (non-Javadoc) 074 */ 075 @Override 076 @Transactional(value="defaultTransactionManager") 077 public void remove(DeviceCode scope) { 078 DeviceCode found = getById(scope.getId()); 079 080 if (found != null) { 081 em.remove(found); 082 } 083 084 } 085 086 /* (non-Javadoc) 087 * @see org.mitre.oauth2.repository.SystemScopeRepository#save(org.mitre.oauth2.model.SystemScope) 088 */ 089 @Override 090 @Transactional(value="defaultTransactionManager") 091 public DeviceCode save(DeviceCode scope) { 092 return saveOrUpdate(scope.getId(), em, scope); 093 } 094 095 /* (non-Javadoc) 096 * @see org.mitre.oauth2.repository.impl.DeviceCodeRepository#getExpiredCodes() 097 */ 098 @Override 099 @Transactional(value="defaultTransactionManager") 100 public Collection<DeviceCode> getExpiredCodes() { 101 TypedQuery<DeviceCode> query = em.createNamedQuery(DeviceCode.QUERY_EXPIRED_BY_DATE, DeviceCode.class); 102 query.setParameter(DeviceCode.PARAM_DATE, new Date()); 103 return query.getResultList(); 104 } 105 106}