1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sec.businessobject.lookup;
17
18 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
19
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.kuali.ole.sec.SecPropertyConstants;
29 import org.kuali.ole.sec.businessobject.ModelMember;
30 import org.kuali.ole.sys.context.SpringContext;
31 import org.kuali.rice.core.api.criteria.Predicate;
32 import org.kuali.rice.core.api.criteria.QueryByCriteria;
33 import org.kuali.rice.core.api.membership.MemberType;
34 import org.kuali.rice.kim.api.KimConstants;
35 import org.kuali.rice.kim.api.group.Group;
36 import org.kuali.rice.kim.api.group.GroupQueryResults;
37 import org.kuali.rice.kim.api.identity.Person;
38 import org.kuali.rice.kim.api.role.Role;
39 import org.kuali.rice.kim.api.role.RoleQueryResults;
40 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
41 import org.kuali.rice.kim.impl.KIMPropertyConstants;
42 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
43 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
44 import org.kuali.rice.kns.service.DataDictionaryService;
45 import org.kuali.rice.kns.util.FieldUtils;
46 import org.kuali.rice.kns.web.ui.Row;
47 import org.kuali.rice.krad.bo.BusinessObject;
48 import org.kuali.rice.krad.util.KRADConstants;
49 import org.kuali.rice.krad.util.KRADPropertyConstants;
50
51
52
53
54
55 public class ModelMemberLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
56
57 protected List<Row> rows;
58
59 public ModelMemberLookupableHelperServiceImpl() {
60 rows = null;
61 }
62
63
64
65
66 @Override
67 public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
68 List results = new ArrayList();
69
70 Map<String, String> searchValues = new HashMap<String, String>();
71
72 String memberTypeCode = fieldValues.get(SecPropertyConstants.MEMBER_TYPE_CODE);
73 if (MemberType.ROLE.getCode().equals(memberTypeCode)) {
74 List<String> roleSearchFields = getRoleLookupFields();
75 roleSearchFields.remove(SecPropertyConstants.MEMBER_TYPE_CODE);
76 for (String roleField : roleSearchFields) {
77 if (fieldValues.containsKey(roleField) && StringUtils.isNotBlank(fieldValues.get(roleField))) {
78 searchValues.put(roleField, fieldValues.get(roleField));
79 }
80 }
81
82 RoleQueryResults resultRoles = KimApiServiceLocator.getRoleService().findRoles(toQuery(searchValues));
83 for (Role kimRoleInfo : resultRoles.getResults()) {
84 ModelMember member = new ModelMember();
85 member.setMemberId(kimRoleInfo.getId());
86 member.setMemberTypeCode(MemberType.ROLE.getCode());
87 member.setMemberName(kimRoleInfo.getNamespaceCode() + "-" + kimRoleInfo.getName());
88 member.setActive(kimRoleInfo.isActive());
89
90 results.add(member);
91 }
92 }
93 else if ( MemberType.GROUP.getCode().equals(memberTypeCode)) {
94 List<String> groupSearchFields = getGroupLookupFields();
95 groupSearchFields.remove(SecPropertyConstants.MEMBER_TYPE_CODE);
96 for (String groupField : groupSearchFields) {
97 if (fieldValues.containsKey(groupField) && StringUtils.isNotBlank(fieldValues.get(groupField))) {
98 searchValues.put(groupField, fieldValues.get(groupField));
99 }
100 }
101
102 GroupQueryResults resultGroups = KimApiServiceLocator.getGroupService().findGroups(toQuery(searchValues));
103 for (Group group : resultGroups.getResults()) {
104 ModelMember member = new ModelMember();
105 member.setMemberId(group.getId());
106 member.setMemberTypeCode( MemberType.GROUP.getCode() );
107 member.setMemberName(group.getNamespaceCode() + "-" + group.getName());
108 member.setActive(group.isActive());
109
110 results.add(member);
111 }
112 }
113 else {
114 List<String> personSearchFields = getPersonLookupFields();
115 personSearchFields.remove(SecPropertyConstants.MEMBER_TYPE_CODE);
116 for (String personField : personSearchFields) {
117 if (fieldValues.containsKey(personField) && StringUtils.isNotBlank(fieldValues.get(personField))) {
118 searchValues.put(personField, fieldValues.get(personField));
119 }
120 }
121
122 List<? extends Person> resultPersons = KimApiServiceLocator.getPersonService().findPeople(searchValues);
123 for (Person person : resultPersons) {
124 ModelMember member = new ModelMember();
125 member.setMemberId(person.getPrincipalId());
126 member.setMemberTypeCode(MemberType.PRINCIPAL.getCode());
127 member.setMemberName(person.getName());
128 member.setActive(person.isActive());
129
130 results.add(member);
131 }
132 }
133
134 return results;
135 }
136
137
138
139
140 @Override
141 protected void setRows() {
142 List<String> lookupFieldAttributeList = new ArrayList<String>();
143 if (getParameters().containsKey(SecPropertyConstants.MEMBER_TYPE_CODE)) {
144 String memberTypeCode = ((String[]) getParameters().get(SecPropertyConstants.MEMBER_TYPE_CODE))[0];
145
146 if (MemberType.ROLE.getCode().equals(memberTypeCode)) {
147 lookupFieldAttributeList = getRoleLookupFields();
148 }
149 else if (MemberType.GROUP.getCode().equals(memberTypeCode)) {
150 lookupFieldAttributeList = getGroupLookupFields();
151 }
152 else {
153 lookupFieldAttributeList = getPersonLookupFields();
154 }
155 }
156 else {
157 lookupFieldAttributeList = getPersonLookupFields();
158 }
159
160
161 List fields = new ArrayList();
162 int numCols = 0;
163 try {
164 fields = FieldUtils.createAndPopulateFieldsForLookup(lookupFieldAttributeList, getReadOnlyFieldsList(), getBusinessObjectClass());
165
166 BusinessObjectEntry boe = (BusinessObjectEntry) SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(this.getBusinessObjectClass().getName());
167 numCols = boe.getLookupDefinition().getNumOfColumns();
168
169 }
170 catch (InstantiationException e) {
171 throw new RuntimeException("Unable to create instance of business object class", e);
172 }
173 catch (IllegalAccessException e) {
174 throw new RuntimeException("Unable to create instance of business object class", e);
175 }
176
177 if (numCols == 0) {
178 numCols = KRADConstants.DEFAULT_NUM_OF_COLUMNS;
179 }
180
181 rows = FieldUtils.wrapFields(fields, numCols);
182 }
183
184
185
186
187
188
189
190 @Override
191 public List<Row> getRows() {
192 return rows;
193 }
194
195
196
197
198
199
200 protected List<String> getRoleLookupFields() {
201 List<String> lookupFields = new ArrayList<String>();
202
203 lookupFields.add(SecPropertyConstants.ROLE_ID);
204 lookupFields.add(SecPropertyConstants.ROLE_NAME);
205 lookupFields.add(KimConstants.UniqueKeyConstants.NAMESPACE_CODE);
206 lookupFields.add(KRADPropertyConstants.ACTIVE);
207 lookupFields.add(SecPropertyConstants.MEMBER_TYPE_CODE);
208
209 return lookupFields;
210 }
211
212
213
214
215
216
217 protected List<String> getGroupLookupFields() {
218 List<String> lookupFields = new ArrayList<String>();
219
220 lookupFields.add(SecPropertyConstants.GROUP_ID);
221 lookupFields.add(SecPropertyConstants.GROUP_NAME);
222 lookupFields.add(KRADPropertyConstants.ACTIVE);
223 lookupFields.add(SecPropertyConstants.MEMBER_TYPE_CODE);
224
225 return lookupFields;
226 }
227
228
229
230
231
232
233 protected List<String> getPersonLookupFields() {
234 List<String> lookupFields = new ArrayList<String>();
235
236 lookupFields.add(SecPropertyConstants.PRINCIPAL_NAME);
237 lookupFields.add(SecPropertyConstants.PRINCIPAL_ID);
238 lookupFields.add(SecPropertyConstants.ENTITY_ID);
239 lookupFields.add(SecPropertyConstants.FIRST_NAME);
240 lookupFields.add(SecPropertyConstants.MIDDLE_NAME);
241 lookupFields.add(SecPropertyConstants.LAST_NAME);
242 lookupFields.add(SecPropertyConstants.EMAIL_ADDRESS);
243 lookupFields.add(SecPropertyConstants.EMPLOYEE_ID);
244 lookupFields.add(KRADPropertyConstants.ACTIVE);
245 lookupFields.add(SecPropertyConstants.MEMBER_TYPE_CODE);
246
247 return lookupFields;
248 }
249
250 private QueryByCriteria toQuery(Map<String,?> fieldValues) {
251 Set<Predicate> preds = new HashSet<Predicate>();
252 for (String key : fieldValues.keySet()) {
253 preds.add(equal(key, fieldValues.get(key)));
254 }
255 Predicate[] predicates = new Predicate[0];
256 predicates = preds.toArray(predicates);
257 return QueryByCriteria.Builder.fromPredicates(predicates);
258 }
259
260 }