1
2
3
4
5
6
7
8
9
10
11
12
13
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
29 import org.kuali.mobility.reporting.entity.Submission;
30 import org.springframework.stereotype.Repository;
31
32
33
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 }