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