View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.reporting.dao;
17  
18  import java.util.List;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.NoResultException;
22  import javax.persistence.OptimisticLockException;
23  import javax.persistence.PersistenceContext;
24  import javax.persistence.Query;
25  
26  import org.kuali.mobility.file.entity.File;
27  
28  //import org.kuali.mobility.reporting.entity.File;
29  import org.kuali.mobility.reporting.entity.Submission;
30  import org.springframework.stereotype.Repository;
31  
32  /**
33   * @author Kuali Mobility Team (mobility.collab@kuali.org)
34   */
35  @Repository
36  public class ReportingDaoImpl implements ReportingDao {
37  
38  	@PersistenceContext
39  	private EntityManager entityManager;
40  
41  	@SuppressWarnings("unchecked")
42  	public List<Submission> findAllSubmissions() {
43  		try {
44  			Query query = entityManager.createQuery("select s from Submission s");
45  			return query.getResultList();
46  		} catch (NoResultException e) {
47  			return null;
48  		}
49  	}
50  
51  	@SuppressWarnings("unchecked")
52  	public List<Submission> findAllSubmissionsByParentId(Long id) {
53  		try {
54  			Query query = entityManager.createQuery("select s from Submission s where s.parentId = :id or s.id = :id order by s.revisionNumber desc");
55  			query.setParameter("id", id);
56  			return query.getResultList();
57  		} catch (NoResultException e) {
58  			return null;
59  		}
60  	}
61  
62  	public Submission findSubmissionById(Long id) {
63  		try {
64  			Query query = entityManager.createQuery("select s from Submission s where s.id = :id");
65  			query.setParameter("id", id);
66  			return (Submission) query.getSingleResult();
67  		} catch (Exception e) {
68  			return null;
69  		}
70  	}
71  
72  	public Long saveSubmission(Submission submission) {
73  		if (submission == null) {
74  			return null;
75  		}
76  		try {
77  			if (submission.getId() == null) {
78  				entityManager.persist(submission);
79  			} else {
80  				entityManager.merge(submission);
81  			}
82  		} catch (OptimisticLockException oe) {
83  			return null;
84  		}
85  		return submission.getId();
86  	}
87  
88  	public Long saveAttachment(File file) {
89  		if (file == null) {
90  			return null;
91  		}
92  		try {
93  			if (file.getId() == null) {
94  				entityManager.persist(file);
95  			} else {
96  				entityManager.merge(file);
97  			}
98  		} catch (OptimisticLockException oe) {
99  			return null;
100 		}
101 		return file.getId();
102 	}
103 
104 	public EntityManager getEntityManager() {
105 		return entityManager;
106 	}
107 
108 	public void setEntityManager(EntityManager entityManager) {
109 		this.entityManager = entityManager;
110 	}
111 
112 }