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