1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.impl.group; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
21 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; |
22 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria; |
23 | |
import org.kuali.rice.kim.api.identity.principal.Principal; |
24 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
25 | |
import org.kuali.rice.kim.api.type.KimType; |
26 | |
import org.kuali.rice.kim.api.type.KimTypeInfoService; |
27 | |
import org.kuali.rice.kim.api.type.KimTypeService; |
28 | |
import org.kuali.rice.kim.bo.types.dto.AttributeDefinitionMap; |
29 | |
import org.kuali.rice.kim.service.KIMServiceLocatorWeb; |
30 | |
import org.kuali.rice.kim.util.KIMPropertyConstants; |
31 | |
import org.kuali.rice.kim.util.KimConstants; |
32 | |
import org.kuali.rice.krad.datadictionary.AttributeDefinition; |
33 | |
import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; |
34 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
35 | |
import org.kuali.rice.krad.util.KRADConstants; |
36 | |
|
37 | |
import javax.persistence.EntityManager; |
38 | |
import javax.persistence.PersistenceContext; |
39 | |
import java.sql.Timestamp; |
40 | |
import java.util.List; |
41 | |
import java.util.Map; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public class GroupDaoJpa implements GroupDao { |
50 | |
|
51 | 0 | private static final Logger LOG = Logger.getLogger(GroupDaoJpa.class); |
52 | |
@PersistenceContext(unitName="kim-unit") |
53 | |
private EntityManager entityManager; |
54 | |
private KimTypeInfoService kimTypeInfoService; |
55 | |
|
56 | |
public List<GroupBo> getGroups(Map<String,String> fieldValues) { |
57 | |
|
58 | 0 | Criteria crit = new Criteria(GroupBo.class.getName()); |
59 | 0 | BusinessObjectEntry boEntry = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry("org.kuali.rice.kim.impl.group.GroupBo"); |
60 | 0 | List lookupNames = boEntry.getLookupDefinition().getLookupFieldNames(); |
61 | 0 | String kimTypeId = null; |
62 | 0 | for (Map.Entry<String,String> entry : fieldValues.entrySet()) { |
63 | 0 | if (entry.getKey().equals("kimTypeId")) { |
64 | 0 | kimTypeId=entry.getValue(); |
65 | 0 | break; |
66 | |
} |
67 | |
} |
68 | 0 | AttributeDefinitionMap definitions = null; |
69 | 0 | for (Map.Entry<String, String> entry : fieldValues.entrySet()) { |
70 | 0 | if (StringUtils.isNotBlank(entry.getValue())) { |
71 | 0 | if (entry.getKey().contains(".")) { |
72 | 0 | Criteria subCrit = new Criteria(GroupAttributeBo.class.getName()); |
73 | 0 | String value = entry.getValue().replace('*', '%'); |
74 | |
|
75 | |
|
76 | |
|
77 | 0 | String[] values = StringUtils.split(value, KRADConstants.OR_LOGICAL_OPERATOR); |
78 | 0 | boolean valuesCriterionAdded = false; |
79 | 0 | if (values.length > 0) { |
80 | 0 | if (definitions == null) { |
81 | 0 | KimType kimTypeInfo = getKimTypeInfoService().getKimType(kimTypeId); |
82 | 0 | KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(kimTypeInfo); |
83 | 0 | definitions = kimTypeService.getAttributeDefinitions(kimTypeId); |
84 | |
} |
85 | 0 | AttributeDefinition definition = definitions.getByAttributeName(entry.getKey().substring(0, entry.getKey().indexOf('.'))); |
86 | |
|
87 | 0 | Criteria valuesCrit = new Criteria(GroupAttributeBo.class.getName()); |
88 | 0 | for (int i = 0; i < values.length; i++) { |
89 | 0 | String subValue = values[i]; |
90 | 0 | if (StringUtils.isNotBlank(subValue)) { |
91 | 0 | Criteria valueCrit = new Criteria(GroupAttributeBo.class.getName()); |
92 | |
|
93 | 0 | if (!Boolean.FALSE.equals(definition.getForceUppercase())) { |
94 | 0 | valueCrit.like("UPPER(__JPA_ALIAS[[0]]__.value)", subValue.toUpperCase()); |
95 | |
} |
96 | |
else { |
97 | 0 | valueCrit.like("value", subValue); |
98 | |
} |
99 | 0 | valuesCriterionAdded = true; |
100 | 0 | valuesCrit.or(valueCrit); |
101 | |
} |
102 | |
} |
103 | 0 | subCrit.and(valuesCrit); |
104 | |
|
105 | 0 | subCrit.eq("id", entry.getKey().substring(entry.getKey().indexOf(".") + 1, entry.getKey().length())); |
106 | 0 | subCrit.eq("kimTypeId", kimTypeId); |
107 | |
|
108 | 0 | subCrit.eq(KIMPropertyConstants.Group.GROUP_ID, crit.getAlias() + "." + KIMPropertyConstants.Group.GROUP_ID); |
109 | |
|
110 | |
|
111 | |
|
112 | 0 | if (valuesCriterionAdded) { |
113 | 0 | crit.exists(subCrit); |
114 | |
} |
115 | |
} |
116 | |
|
117 | 0 | } else { |
118 | 0 | if (lookupNames.contains(entry.getKey())) { |
119 | 0 | String value = entry.getValue().replace('*', '%'); |
120 | 0 | String[] values = StringUtils.split(value, KRADConstants.OR_LOGICAL_OPERATOR); |
121 | 0 | Criteria valuesCrit = new Criteria(GroupBo.class.getName()); |
122 | 0 | for (int i = 0; i < values.length; i++) { |
123 | 0 | String subValue = values[i]; |
124 | 0 | if (StringUtils.isNotBlank(subValue)) { |
125 | 0 | Criteria valueCrit = new Criteria(GroupBo.class.getName()); |
126 | |
|
127 | 0 | if (KimConstants.UniqueKeyConstants.GROUP_NAME.equals(entry.getKey())) { |
128 | 0 | valueCrit.like("UPPER(__JPA_ALIAS[[0]]__.name)", subValue.toUpperCase()); |
129 | |
} |
130 | |
else { |
131 | 0 | valueCrit.like(entry.getKey(), subValue); |
132 | |
} |
133 | 0 | valuesCrit.or(valueCrit); |
134 | |
} |
135 | |
} |
136 | 0 | crit.and(valuesCrit); |
137 | |
|
138 | 0 | } else { |
139 | 0 | if (entry.getKey().equals(KIMPropertyConstants.Person.PRINCIPAL_NAME)) { |
140 | |
|
141 | |
|
142 | 0 | Criteria memberSubCrit = new Criteria(GroupMemberBo.class.getName()); |
143 | 0 | memberSubCrit.eq(KIMPropertyConstants.GroupMember.GROUP_ID, crit.getAlias() +"."+ KIMPropertyConstants.Group.GROUP_ID); |
144 | |
|
145 | 0 | String principalName = entry.getValue(); |
146 | |
|
147 | 0 | LOG.debug("Searching on Principal Name: " + entry.getValue()); |
148 | 0 | Principal principalInfo = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName); |
149 | |
|
150 | 0 | if (principalInfo != null) |
151 | |
{ |
152 | 0 | LOG.debug("Retrieved Principal: " + principalInfo.getPrincipalName()); |
153 | 0 | String principalId = principalInfo.getPrincipalId(); |
154 | 0 | LOG.debug("Plugging in Principal ID: " + principalId + "as Member ID"); |
155 | 0 | memberSubCrit.like(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId); |
156 | |
|
157 | |
|
158 | 0 | Timestamp now = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp(); |
159 | 0 | Criteria afterActiveFromSubCrit = new Criteria(GroupMemberBo.class.getName()); |
160 | 0 | afterActiveFromSubCrit.lte(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE, now); |
161 | 0 | Criteria nullActiveFromSubCrit = new Criteria(GroupMemberBo.class.getName()); |
162 | 0 | nullActiveFromSubCrit.isNull(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE); |
163 | |
|
164 | |
|
165 | 0 | Criteria ActiveMemberSubCrit1 = new Criteria(GroupMemberBo.class.getName()); |
166 | 0 | ActiveMemberSubCrit1.or(afterActiveFromSubCrit); |
167 | 0 | ActiveMemberSubCrit1.or(nullActiveFromSubCrit); |
168 | |
|
169 | |
|
170 | 0 | Criteria afterActiveToSubCrit = new Criteria(GroupMemberBo.class.getName()); |
171 | 0 | afterActiveToSubCrit.gte(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE, now); |
172 | 0 | Criteria nullActiveToSubCrit = new Criteria(GroupMemberBo.class.getName()); |
173 | 0 | nullActiveToSubCrit.isNull(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE); |
174 | |
|
175 | 0 | Criteria ActiveMemberSubCrit2 = new Criteria(GroupMemberBo.class.getName()); |
176 | 0 | ActiveMemberSubCrit2.or(afterActiveToSubCrit); |
177 | 0 | ActiveMemberSubCrit2.or(nullActiveToSubCrit); |
178 | |
|
179 | 0 | memberSubCrit.and(ActiveMemberSubCrit1); |
180 | 0 | memberSubCrit.and(ActiveMemberSubCrit2); |
181 | 0 | } |
182 | |
|
183 | |
else |
184 | |
{ |
185 | 0 | LOG.debug("No Principal ID, plugging in blank string as Member ID"); |
186 | 0 | memberSubCrit.like(KIMPropertyConstants.GroupMember.MEMBER_ID, ""); |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | 0 | crit.exists(memberSubCrit); |
200 | 0 | } |
201 | |
} |
202 | |
} |
203 | |
} |
204 | |
} |
205 | 0 | List<GroupBo> groups = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
206 | |
|
207 | |
|
208 | 0 | return groups; |
209 | |
} |
210 | |
|
211 | |
public EntityManager getEntityManager() { |
212 | 0 | return this.entityManager; |
213 | |
} |
214 | |
|
215 | |
public void setEntityManager(EntityManager entityManager) { |
216 | 0 | this.entityManager = entityManager; |
217 | 0 | } |
218 | |
|
219 | |
protected KimTypeInfoService getKimTypeInfoService() { |
220 | 0 | if (kimTypeInfoService == null) { |
221 | 0 | kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService(); |
222 | |
} |
223 | 0 | return kimTypeInfoService; |
224 | |
} |
225 | |
} |