1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.server.applicationstate.dao.impl;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.PersistenceContext;
24 import javax.persistence.Query;
25
26 import org.kuali.student.common.dao.impl.AbstractSearchableCrudDaoImpl;
27 import org.kuali.student.common.exceptions.AlreadyExistsException;
28 import org.kuali.student.common.exceptions.DoesNotExistException;
29 import org.kuali.student.common.ui.server.applicationstate.dao.ApplicationStateDao;
30 import org.kuali.student.common.ui.server.applicationstate.entity.ApplicationState;
31
32
33
34
35
36 public class ApplicationStateDaoImpl extends AbstractSearchableCrudDaoImpl implements ApplicationStateDao {
37
38 private final static String DEFAULT_USER_ID = "APPLICATION";
39
40 @PersistenceContext(unitName = "ApplicationState")
41 @Override
42 public void setEm(EntityManager em) {
43 super.setEm(em);
44 }
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public ApplicationState getApplicationState(String applicationId, String referenceKey, String referenceType, String userId) throws DoesNotExistException {
59 Query query = em.createNamedQuery("ApplicationState.getApplicationStateByAppRefUserId");
60 query.setParameter("applicationId", applicationId);
61 query.setParameter("referenceKey", referenceKey);
62 query.setParameter("referenceType", referenceType);
63 query.setParameter("userId", userId);
64
65 try {
66 ApplicationState appState = (ApplicationState) query.getSingleResult();
67 return appState;
68 } catch(javax.persistence.NoResultException e) {
69 throw new DoesNotExistException("Application state does not exist");
70 }
71 }
72
73
74
75
76
77
78
79
80
81
82
83 public ApplicationState getApplicationState(String applicationId, String referenceKey, String referenceType) throws DoesNotExistException {
84 return getApplicationState(applicationId, referenceKey, referenceType, DEFAULT_USER_ID);
85 }
86
87
88
89
90
91
92
93
94 public ApplicationState createApplicationState(ApplicationState appState) throws AlreadyExistsException {
95 if(appState.getUserId() == null) {
96 appState.setUserId(DEFAULT_USER_ID);
97 }
98
99 try {
100 getApplicationState(appState.getApplicationId(), appState.getReferenceKey(), appState.getReferenceType(), appState.getUserId());
101 } catch(DoesNotExistException e) {
102 return super.create(appState);
103 }
104 throw new AlreadyExistsException("Application state already exists");
105 }
106
107
108
109
110
111
112
113
114 public List<String> createApplicationState(Collection<ApplicationState> appStates) throws AlreadyExistsException {
115 List<String> newAppStateList = new ArrayList<String>();
116 for(ApplicationState appState : appStates) {
117 ApplicationState newAppState = createApplicationState(appState);
118 newAppStateList.add(newAppState.getId());
119 }
120 return newAppStateList;
121 }
122
123 }