View Javadoc

1   /**
2    * Copyright (c) 2009-2010, Elias Gomes. All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5    * following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8    * disclaimer.
9    * 
10   * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided with the distribution.
12   * 
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
14   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
18   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20   */
21  package net.sourceforge.mystique.infrastructure.persistence;
22  
23  import java.util.List;
24  
25  import javax.persistence.Query;
26  
27  import net.sourceforge.mystique.domain.entity.Attachment;
28  import net.sourceforge.mystique.domain.entity.Ticket;
29  import net.sourceforge.mystique.domain.repository.AttachmentRepository;
30  import net.sourceforge.mystique.domain.repository.RepositoryException;
31  
32  import org.springframework.dao.DataIntegrityViolationException;
33  import org.springframework.stereotype.Repository;
34  import org.springframework.transaction.annotation.Transactional;
35  
36  /**
37   * @author Elias Gomes [eliasgomes@users.sourceforge.net]
38   */
39  @Repository
40  public class AttachmentJpaRepository extends JpaRepository<Attachment, Long> implements AttachmentRepository {
41  
42  	@Override
43  	@Transactional(readOnly = true)
44  	public Attachment findById(Long id) throws RepositoryException {
45  
46  		return entityManager.find(Attachment.class, id);
47  
48  	}
49  
50  	@Override
51  	@Transactional(readOnly = true)
52  	@SuppressWarnings("unchecked")
53  	public List<Attachment> findByTicket(Ticket ticket, int offset, int limit) throws RepositoryException {
54  
55  		Query query = entityManager.createNamedQuery("Attachment.findByTicket");
56  		query.setParameter("ticket", ticket);
57  
58  		query.setFirstResult(offset);
59  		query.setMaxResults(limit);
60  
61  		return query.getResultList();
62  
63  	}
64  
65  	@Override
66  	@Transactional
67  	public void store(Attachment attachment) throws RepositoryException {
68  
69  		if (attachment.getId() != null) {
70  			throw new DataIntegrityViolationException("exception.updateNotSupported");
71  		}
72  
73  		entityManager.persist(attachment);
74  		entityManager.flush();
75  
76  	}
77  
78  }