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.ResourceSet;
026import org.mitre.uma.repository.ResourceSetRepository;
027import org.mitre.util.jpa.JpaUtil;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.springframework.stereotype.Repository;
031import org.springframework.transaction.annotation.Transactional;
032
033/**
034 * @author jricher
035 *
036 */
037@Repository
038public class JpaResourceSetRepository implements ResourceSetRepository {
039
040        @PersistenceContext(unitName="defaultPersistenceUnit")
041        private EntityManager em;
042        private static Logger logger = LoggerFactory.getLogger(JpaResourceSetRepository.class);
043
044        @Override
045        @Transactional(value="defaultTransactionManager")
046        public ResourceSet save(ResourceSet rs) {
047                return JpaUtil.saveOrUpdate(rs.getId(), em, rs);
048        }
049
050        @Override
051        public ResourceSet getById(Long id) {
052                return em.find(ResourceSet.class, id);
053        }
054
055        @Override
056        @Transactional(value="defaultTransactionManager")
057        public void remove(ResourceSet rs) {
058                ResourceSet found = getById(rs.getId());
059                if (found != null) {
060                        em.remove(found);
061                } else {
062                        logger.info("Tried to remove unknown resource set: " + rs.getId());
063                }
064        }
065
066        @Override
067        public Collection<ResourceSet> getAllForOwner(String owner) {
068                TypedQuery<ResourceSet> query = em.createNamedQuery(ResourceSet.QUERY_BY_OWNER, ResourceSet.class);
069                query.setParameter(ResourceSet.PARAM_OWNER, owner);
070                return query.getResultList();
071        }
072
073        @Override
074        public Collection<ResourceSet> getAllForOwnerAndClient(String owner, String clientId) {
075                TypedQuery<ResourceSet> query = em.createNamedQuery(ResourceSet.QUERY_BY_OWNER_AND_CLIENT, ResourceSet.class);
076                query.setParameter(ResourceSet.PARAM_OWNER, owner);
077                query.setParameter(ResourceSet.PARAM_CLIENTID, clientId);
078                return query.getResultList();
079        }
080
081        @Override
082        public Collection<ResourceSet> getAll() {
083                TypedQuery<ResourceSet> query = em.createNamedQuery(ResourceSet.QUERY_ALL, ResourceSet.class);
084                return query.getResultList();
085        }
086
087        /* (non-Javadoc)
088         * @see org.mitre.uma.repository.ResourceSetRepository#getAllForClient(org.mitre.oauth2.model.ClientDetailsEntity)
089         */
090        @Override
091        public Collection<ResourceSet> getAllForClient(String clientId) {
092                TypedQuery<ResourceSet> query = em.createNamedQuery(ResourceSet.QUERY_BY_CLIENT, ResourceSet.class);
093                query.setParameter(ResourceSet.PARAM_CLIENTID, clientId);
094                return query.getResultList();
095        }
096
097}