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.NoResultException;
26  import javax.persistence.Query;
27  
28  import net.sourceforge.mystique.domain.entity.Project;
29  import net.sourceforge.mystique.domain.entity.Version;
30  import net.sourceforge.mystique.domain.repository.VersionRepository;
31  import net.sourceforge.mystique.domain.repository.RepositoryException;
32  
33  import org.springframework.dao.DataIntegrityViolationException;
34  import org.springframework.stereotype.Repository;
35  import org.springframework.transaction.annotation.Transactional;
36  
37  /**
38   * @author Elias Gomes [eliasgomes@users.sourceforge.net]
39   */
40  @Repository
41  public class VersionJpaRepository extends JpaRepository<Version, Long> implements
42  		VersionRepository {
43  
44  	@Override
45  	@Transactional(readOnly = true)
46  	public Version findById(Long id) throws RepositoryException {
47  
48  		return entityManager.find(Version.class, id);
49  
50  	}
51  
52  	@Override
53  	@Transactional(readOnly = true)
54  	@SuppressWarnings("unchecked")
55  	public List<Version> findByProject(Project project, int offset, int limit) throws RepositoryException {
56  
57  		Query query = entityManager.createNamedQuery("Version.findByProject");
58  		query.setParameter("project", project);
59  
60  		query.setFirstResult(offset);
61  		query.setMaxResults(limit);
62  
63  		return query.getResultList();
64  
65  	}
66  
67  	@Override
68  	@Transactional(readOnly = true)
69  	public Version findByProjectAndNumber(Project project, String number) throws RepositoryException {
70  
71  		Query query = entityManager.createNamedQuery("Version.findByProjectAndNumber");
72  		query.setParameter("project", project);
73  		query.setParameter("number", number);
74  
75  		return (Version) query.getSingleResult();
76  
77  	}
78  
79  	@Override
80  	@Transactional
81  	public void store(Version version) throws RepositoryException {
82  
83  		try {
84  
85  			Version storedVersion = findByProjectAndNumber(version.getProject(), version.getNumber());
86  
87  			if (storedVersion != null && !storedVersion.getId().equals(version.getId())) {
88  				throw new DataIntegrityViolationException("exception.versionAlreadyRegistred");
89  			}
90  
91  		} catch (NoResultException e) {
92  
93  			entityManager.persist(version);
94  
95  			if (version.getId() != null) {
96  				entityManager.merge(version);
97  			}
98  
99  			entityManager.flush();
100 
101 		}
102 
103 	}
104 
105 }