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