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