1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.organization.dao.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.LinkedHashSet; |
20 | |
import java.util.List; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
import javax.persistence.EntityManager; |
24 | |
import javax.persistence.NoResultException; |
25 | |
import javax.persistence.PersistenceContext; |
26 | |
import javax.persistence.Query; |
27 | |
|
28 | |
import org.kuali.student.common.dao.impl.AbstractSearchableCrudDaoImpl; |
29 | |
import org.kuali.student.core.organization.dao.OrganizationDao; |
30 | |
import org.kuali.student.core.organization.dto.OrgTreeInfo; |
31 | |
import org.kuali.student.core.organization.entity.Org; |
32 | |
import org.kuali.student.core.organization.entity.OrgOrgRelation; |
33 | |
import org.kuali.student.core.organization.entity.OrgOrgRelationType; |
34 | |
import org.kuali.student.core.organization.entity.OrgPersonRelation; |
35 | |
import org.kuali.student.core.organization.entity.OrgPersonRelationType; |
36 | |
import org.kuali.student.core.organization.entity.OrgPositionRestriction; |
37 | |
|
38 | 25 | public class OrganizationDaoImpl extends AbstractSearchableCrudDaoImpl implements OrganizationDao { |
39 | |
|
40 | |
@PersistenceContext(unitName = "Organization") |
41 | |
@Override |
42 | |
public void setEm(EntityManager em) { |
43 | 1 | super.setEm(em); |
44 | 1 | } |
45 | |
|
46 | |
@Override |
47 | |
public List<OrgPersonRelation> getAllOrgPersonRelationsByPerson(String personId) { |
48 | 2 | Query query = em.createNamedQuery("OrgPersonRelation.getAllOrgPersonRelationsByPerson"); |
49 | 2 | query.setParameter("personId", personId); |
50 | |
@SuppressWarnings("unchecked") |
51 | 2 | List<OrgPersonRelation> resultList = query.getResultList(); |
52 | 2 | return resultList; |
53 | |
} |
54 | |
|
55 | |
@Override |
56 | |
public List<OrgPersonRelation> getAllOrgPersonRelationsByOrg(String orgId) { |
57 | 2 | Query query = em.createNamedQuery("OrgPersonRelation.getAllOrgPersonRelationsByOrg"); |
58 | 2 | query.setParameter("orgId", orgId); |
59 | |
@SuppressWarnings("unchecked") |
60 | 2 | List<OrgPersonRelation> resultList = query.getResultList(); |
61 | 2 | return resultList; |
62 | |
} |
63 | |
|
64 | |
@Override |
65 | |
public List<String> getPersonIdsForOrgByRelationType(String orgId, String orgPersonRelationTypeKey) { |
66 | 2 | Query query = em.createNamedQuery("OrgPersonRelationType.getPersonIdsForOrgByRelationType"); |
67 | 2 | query.setParameter("orgId", orgId); |
68 | 2 | query.setParameter("orgPersonRelationTypeKey", orgPersonRelationTypeKey); |
69 | |
@SuppressWarnings("unchecked") |
70 | 2 | List<String> resultList = query.getResultList(); |
71 | 2 | return resultList; |
72 | |
} |
73 | |
|
74 | |
@Override |
75 | |
public List<Org> getOrganizationsByIdList(List<String> orgIdList) { |
76 | 0 | Query query = em.createNamedQuery("Org.getOrganizationsByIdList"); |
77 | 0 | query.setParameter("orgIdList", orgIdList); |
78 | |
@SuppressWarnings("unchecked") |
79 | 0 | List<Org> orgs = query.getResultList(); |
80 | 0 | return orgs; |
81 | |
} |
82 | |
|
83 | |
@Override |
84 | |
public List<OrgOrgRelation> getOrgOrgRelationsByOrg(String orgId) { |
85 | 2 | Query query = em.createNamedQuery("OrgOrgRelation.OrgOrgRelation"); |
86 | 2 | query.setParameter("orgId", orgId); |
87 | |
@SuppressWarnings("unchecked") |
88 | 2 | List<OrgOrgRelation> orgOrgRelations = query.getResultList(); |
89 | 2 | return orgOrgRelations; |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public List<OrgPositionRestriction> getPositionRestrictionsByOrg( |
94 | |
String orgId) { |
95 | 2 | Query query = em.createNamedQuery("OrgPositionRestriction.findOrgPositionRestrictions"); |
96 | 2 | query.setParameter("orgId", orgId); |
97 | |
@SuppressWarnings("unchecked") |
98 | 2 | List<OrgPositionRestriction> orgPositionRestrictions = query.getResultList(); |
99 | 2 | return orgPositionRestrictions; |
100 | |
|
101 | |
} |
102 | |
|
103 | |
@Override |
104 | |
public List<String> getAllDescendants(String orgId, String orgHierarchy) { |
105 | 4 | Query query = em.createNamedQuery("OrgOrgRelation.getAllDescendants"); |
106 | 4 | query.setParameter("orgHierarchy", orgHierarchy); |
107 | 4 | return getAllLevels(query, "orgId", orgId); |
108 | |
} |
109 | |
|
110 | |
@Override |
111 | |
public List<String> getAllAncestors(String orgId, String orgHierarchy) { |
112 | 7 | Query query = em.createNamedQuery("OrgOrgRelation.getAncestors"); |
113 | 7 | query.setParameter("orgHierarchy", orgHierarchy); |
114 | 7 | return getAllLevels(query, "orgId", orgId); |
115 | |
} |
116 | |
|
117 | |
private List<String> getAllLevels(Query query, String paramName, String paramValue) { |
118 | |
|
119 | 61 | Set<String> valSet = new LinkedHashSet<String>(); |
120 | 61 | query.setParameter(paramName, paramValue); |
121 | |
@SuppressWarnings("unchecked") |
122 | 61 | List<String> nextLevelList = query.getResultList(); |
123 | 61 | valSet.addAll(nextLevelList); |
124 | 61 | for (String resultStr : nextLevelList) { |
125 | 50 | valSet.addAll(getAllLevels(query, paramName, resultStr)); |
126 | |
} |
127 | 61 | return new ArrayList<String>(valSet); |
128 | |
} |
129 | |
|
130 | |
@Override |
131 | |
public List<OrgOrgRelationType> getOrgOrgRelationTypesForOrgHierarchy(String orgHierarchyKey) { |
132 | 2 | Query query = em.createNamedQuery("OrgOrgRelationType.getOrgOrgRelationTypesForOrgHierarchy"); |
133 | 2 | query.setParameter("orgHierarchy", orgHierarchyKey); |
134 | |
@SuppressWarnings("unchecked") |
135 | 2 | List<OrgOrgRelationType> orgOrgRelationTypes = query.getResultList(); |
136 | 2 | return orgOrgRelationTypes; |
137 | |
|
138 | |
} |
139 | |
|
140 | |
@Override |
141 | |
public boolean validatePositionRestriction(String orgId, |
142 | |
String orgPersonRelationTypeKey) { |
143 | 1 | Query query = em.createNamedQuery("OrgPositionRestriction.validatePositionRestriction"); |
144 | 1 | query.setParameter("orgId", orgId); |
145 | 1 | query.setParameter("orgPersonRelationTypeKey", orgPersonRelationTypeKey); |
146 | 1 | Long valid = (Long)query.getSingleResult(); |
147 | 1 | return valid.intValue()>0; |
148 | |
} |
149 | |
|
150 | |
@Override |
151 | |
public List<OrgPersonRelationType> getOrgPersonRelationTypesForOrgType(String orgTypeKey) { |
152 | 2 | Query query = em.createNamedQuery("OrgPersonRelationType.getOrgPersonRelationTypesForOrgType"); |
153 | 2 | query.setParameter("orgTypeKey", orgTypeKey); |
154 | |
@SuppressWarnings("unchecked") |
155 | 2 | List<OrgPersonRelationType> orgRelationTypes = query.getResultList(); |
156 | 2 | return orgRelationTypes; |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public List<OrgOrgRelation> getOrgOrgRelationsByIdList(List<String> orgOrgRelationIdList) { |
161 | 3 | Query query = em.createNamedQuery("OrgOrgRelation.getOrgOrgRelationsByIdList"); |
162 | 3 | query.setParameter("idList", orgOrgRelationIdList); |
163 | |
@SuppressWarnings("unchecked") |
164 | 3 | List<OrgOrgRelation> orgRelationTypes = query.getResultList(); |
165 | 3 | return orgRelationTypes; |
166 | |
} |
167 | |
|
168 | |
@Override |
169 | |
public List<OrgPersonRelation> getOrgPersonRelationsByIdList(List<String> orgPersonRelationIdList) { |
170 | 3 | Query query = em.createNamedQuery("OrgPersonRelation.getOrgPersonRelationsByIdList"); |
171 | 3 | query.setParameter("idList", orgPersonRelationIdList); |
172 | |
@SuppressWarnings("unchecked") |
173 | 3 | List<OrgPersonRelation> orgRelations = query.getResultList(); |
174 | 3 | return orgRelations; |
175 | |
} |
176 | |
|
177 | |
@Override |
178 | |
public List<OrgPersonRelation> getOrgPersonRelationsByPerson(String personId, String orgId) { |
179 | 4 | Query query = em.createNamedQuery("OrgPersonRelation.getOrgPersonRelationsByPerson"); |
180 | 4 | query.setParameter("personId", personId); |
181 | 4 | query.setParameter("orgId", orgId); |
182 | |
@SuppressWarnings("unchecked") |
183 | 4 | List<OrgPersonRelation> orgRelations = query.getResultList(); |
184 | |
|
185 | 4 | return orgRelations; |
186 | |
} |
187 | |
|
188 | |
@Override |
189 | |
public List<OrgOrgRelationType> getOrgOrgRelationTypesForOrgType(String orgTypeKey) { |
190 | |
|
191 | |
|
192 | 2 | Query query = em.createNamedQuery("OrgOrgRelationType.getOrgOrgRelationTypesForOrgType"); |
193 | 2 | query.setParameter("orgTypeKey", orgTypeKey); |
194 | |
@SuppressWarnings("unchecked") |
195 | 2 | List<OrgOrgRelationType> orgOrgRelations = query.getResultList(); |
196 | |
|
197 | 2 | return orgOrgRelations; |
198 | |
} |
199 | |
|
200 | |
@Override |
201 | |
public List<OrgOrgRelation> getOrgOrgRelationsByRelatedOrg(String relatedOrgId) { |
202 | 2 | Query query = em.createNamedQuery("OrgOrgRelation.getOrgOrgRelationsByRelatedOrg"); |
203 | 2 | query.setParameter("relatedOrgId", relatedOrgId); |
204 | |
@SuppressWarnings("unchecked") |
205 | 2 | List<OrgOrgRelation> orgOrgRelations = query.getResultList(); |
206 | |
|
207 | 2 | return orgOrgRelations; |
208 | |
|
209 | |
} |
210 | |
|
211 | |
@Override |
212 | |
public List<OrgTreeInfo> getOrgTreeInfo(String orgId, |
213 | |
String orgHierarchyId) { |
214 | 161 | Query query = em.createNamedQuery("OrgOrgRelation.getOrgTreeInfo"); |
215 | 161 | query.setParameter("orgId",orgId); |
216 | 161 | query.setParameter("orgHierarchyId",orgHierarchyId); |
217 | |
@SuppressWarnings("unchecked") |
218 | 161 | List<OrgTreeInfo> orgTreeInfos = query.getResultList(); |
219 | |
|
220 | 161 | return orgTreeInfos; |
221 | |
} |
222 | |
|
223 | |
@Override |
224 | |
public boolean hasOrgPersonRelation(String orgId, String personId, String orgPersonRelationTypeKey) { |
225 | 4 | Query query = em.createNamedQuery("OrgPersonRelationType.hasOrgPersonRelation"); |
226 | 4 | query.setParameter("orgId", orgId); |
227 | 4 | query.setParameter("personId", personId); |
228 | 4 | query.setParameter("orgPersonRelationTypeKey", orgPersonRelationTypeKey); |
229 | 4 | Long valid = (Long)query.getSingleResult(); |
230 | 4 | return valid.intValue()>0; |
231 | |
} |
232 | |
|
233 | |
@Override |
234 | |
public boolean hasOrgOrgRelation(String orgId, String comparisonOrgId, |
235 | |
String orgOrgRelationTypeKey) { |
236 | |
|
237 | 2 | Query query = em.createNamedQuery("OrgOrgRelation.hasOrgOrgRelation"); |
238 | 2 | query.setParameter("orgId",orgId); |
239 | 2 | query.setParameter("comparisonOrgId",comparisonOrgId); |
240 | 2 | query.setParameter("orgOrgRelationTypeKey",orgOrgRelationTypeKey); |
241 | |
|
242 | 2 | Long relationCount = (Long)query.getSingleResult(); |
243 | |
|
244 | 2 | return relationCount>0; |
245 | |
} |
246 | |
@Override |
247 | |
public OrgPositionRestriction getPositionRestrictionByOrgAndPersonRelationTypeKey(String orgId, String orgPersonRelationTypeKey) { |
248 | 3 | Query query = em.createNamedQuery("OrgPositionRestriction.getPositionRestrictionByOrgAndPersonRelationTypeKey"); |
249 | 3 | query.setParameter("orgId",orgId); |
250 | 3 | query.setParameter("orgPersonRelationTypeKey",orgPersonRelationTypeKey); |
251 | |
|
252 | 3 | return (OrgPositionRestriction) query.getSingleResult(); |
253 | |
} |
254 | |
|
255 | |
@Override |
256 | |
public Long getOrgMemebershipCount(String orgId) { |
257 | 169 | Query query = em.createNamedQuery("OrgPersonRelation.getOrgMembershipCount"); |
258 | 169 | query.setParameter("orgId", orgId); |
259 | |
|
260 | 169 | return (Long)query.getSingleResult(); |
261 | |
} |
262 | |
|
263 | |
@Override |
264 | |
public Org getOrgByRelatedOrgAndType(String relatedOrgId, String relationTypeKey) { |
265 | 0 | Query query = em.createNamedQuery("Org.getOrgByRelatedOrgAndType"); |
266 | 0 | query.setParameter("relatedOrgId", relatedOrgId); |
267 | 0 | query.setParameter("relationTypeKey", relationTypeKey); |
268 | |
|
269 | |
try{ |
270 | 0 | return (Org) query.getSingleResult(); |
271 | 0 | }catch(NoResultException e){ |
272 | 0 | return null; |
273 | |
} |
274 | |
} |
275 | |
} |