001/**************************************************************** 002 * Licensed to the Apache Software Foundation (ASF) under one * 003 * or more contributor license agreements. See the NOTICE file * 004 * distributed with this work for additional information * 005 * regarding copyright ownership. The ASF licenses this file * 006 * to you under the Apache License, Version 2.0 (the * 007 * "License"); you may not use this file except in compliance * 008 * with the License. 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, * 013 * software distributed under the License is distributed on an * 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 015 * KIND, either express or implied. See the License for the * 016 * specific language governing permissions and limitations * 017 * under the License. * 018 ****************************************************************/ 019package org.apache.james.mailbox.jpa.user; 020 021import java.util.List; 022 023import javax.persistence.EntityManagerFactory; 024import javax.persistence.NoResultException; 025import javax.persistence.PersistenceException; 026 027import org.apache.james.mailbox.exception.SubscriptionException; 028import org.apache.james.mailbox.jpa.JPATransactionalMapper; 029import org.apache.james.mailbox.store.user.SubscriptionMapper; 030import org.apache.james.mailbox.store.user.model.Subscription; 031 032/** 033 * JPA implementation of a {@link SubscriptionMapper}. This class is not thread-safe! 034 */ 035public class JPASubscriptionMapper extends JPATransactionalMapper implements SubscriptionMapper { 036 037 public JPASubscriptionMapper(final EntityManagerFactory entityManagerFactory) { 038 super(entityManagerFactory); 039 } 040 041 042 /** 043 * @see org.apache.james.mailbox.store.user.SubscriptionMapper#findMailboxSubscriptionForUser(java.lang.String, java.lang.String) 044 */ 045 public Subscription findMailboxSubscriptionForUser(final String user, final String mailbox) throws SubscriptionException { 046 try { 047 return (Subscription) getEntityManager().createNamedQuery("findFindMailboxSubscriptionForUser") 048 .setParameter("userParam", user).setParameter("mailboxParam", mailbox).getSingleResult(); 049 } catch (NoResultException e) { 050 return null; 051 } catch (PersistenceException e) { 052 throw new SubscriptionException(e); 053 } 054 } 055 056 /** 057 * @throws SubscriptionException 058 * @see org.apache.james.mailbox.store.user.SubscriptionMapper#save(Subscription) 059 */ 060 public void save(Subscription subscription) throws SubscriptionException { 061 try { 062 getEntityManager().persist(subscription); 063 } catch (PersistenceException e) { 064 throw new SubscriptionException(e); 065 } 066 } 067 068 /** 069 * @throws SubscriptionException 070 * @see org.apache.james.mailbox.store.user.SubscriptionMapper#findSubscriptionsForUser(java.lang.String) 071 */ 072 @SuppressWarnings("unchecked") 073 public List<Subscription> findSubscriptionsForUser(String user) throws SubscriptionException { 074 try { 075 return (List<Subscription>) getEntityManager().createNamedQuery("findSubscriptionsForUser").setParameter("userParam", user).getResultList(); 076 } catch (PersistenceException e) { 077 throw new SubscriptionException(e); 078 } 079 } 080 081 /** 082 * @throws SubscriptionException 083 * @see org.apache.james.mailbox.store.user.SubscriptionMapper#delete(Subscription) 084 */ 085 public void delete(Subscription subscription) throws SubscriptionException { 086 try { 087 getEntityManager().remove(subscription); 088 } catch (PersistenceException e) { 089 throw new SubscriptionException(e); 090 } 091 } 092}