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.openid.connect.repository.impl;
022
023import static org.mitre.util.jpa.JpaUtil.getSingleResult;
024import static org.mitre.util.jpa.JpaUtil.saveOrUpdate;
025
026import javax.persistence.EntityManager;
027import javax.persistence.PersistenceContext;
028import javax.persistence.TypedQuery;
029
030import org.mitre.openid.connect.model.PairwiseIdentifier;
031import org.mitre.openid.connect.repository.PairwiseIdentifierRepository;
032import org.springframework.stereotype.Repository;
033import org.springframework.transaction.annotation.Transactional;
034
035/**
036 * @author jricher
037 *
038 */
039@Repository
040public class JpaPairwiseIdentifierRepository implements PairwiseIdentifierRepository {
041
042        @PersistenceContext(unitName="defaultPersistenceUnit")
043        private EntityManager manager;
044
045        /* (non-Javadoc)
046         * @see org.mitre.openid.connect.repository.PairwiseIdentifierRepository#getBySectorIdentifier(java.lang.String, java.lang.String)
047         */
048        @Override
049        public PairwiseIdentifier getBySectorIdentifier(String sub, String sectorIdentifierUri) {
050                TypedQuery<PairwiseIdentifier> query = manager.createNamedQuery(PairwiseIdentifier.QUERY_BY_SECTOR_IDENTIFIER, PairwiseIdentifier.class);
051                query.setParameter(PairwiseIdentifier.PARAM_SUB, sub);
052                query.setParameter(PairwiseIdentifier.PARAM_SECTOR_IDENTIFIER, sectorIdentifierUri);
053
054                return getSingleResult(query.getResultList());
055        }
056
057        /* (non-Javadoc)
058         * @see org.mitre.openid.connect.repository.PairwiseIdentifierRepository#save(org.mitre.openid.connect.model.PairwiseIdentifier)
059         */
060        @Override
061        @Transactional(value="defaultTransactionManager")
062        public void save(PairwiseIdentifier pairwise) {
063                saveOrUpdate(pairwise.getId(), manager, pairwise);
064        }
065
066}