1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.lookup;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.criteria.Predicate;
21 import org.kuali.rice.core.api.criteria.PredicateUtils;
22 import org.kuali.rice.core.api.criteria.QueryByCriteria;
23 import org.kuali.rice.core.api.util.ConcreteKeyValue;
24 import org.kuali.rice.core.api.util.KeyValue;
25 import org.kuali.rice.core.web.format.BooleanFormatter;
26 import org.kuali.rice.core.web.format.CollectionFormatter;
27 import org.kuali.rice.core.web.format.DateFormatter;
28 import org.kuali.rice.core.web.format.Formatter;
29 import org.kuali.rice.kew.api.KewApiConstants;
30 import org.kuali.rice.kim.api.KimConstants;
31 import org.kuali.rice.kim.api.group.Group;
32 import org.kuali.rice.kim.api.group.GroupQueryResults;
33 import org.kuali.rice.kim.api.identity.Person;
34 import org.kuali.rice.kim.api.identity.principal.Principal;
35 import org.kuali.rice.kim.api.identity.principal.PrincipalQueryResults;
36 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
37 import org.kuali.rice.kim.api.type.KimAttributeField;
38 import org.kuali.rice.kim.api.type.KimType;
39 import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
40 import org.kuali.rice.kim.framework.type.KimTypeService;
41 import org.kuali.rice.kim.impl.KIMPropertyConstants;
42 import org.kuali.rice.kim.impl.group.GroupBo;
43 import org.kuali.rice.kim.impl.type.KimTypeLookupableHelperServiceImpl;
44 import org.kuali.rice.kim.util.KimCommonUtilsInternal;
45 import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
46 import org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceHelper;
47 import org.kuali.rice.kns.lookup.HtmlData;
48 import org.kuali.rice.kns.web.comparator.CellComparatorHelper;
49 import org.kuali.rice.kns.web.struts.form.LookupForm;
50 import org.kuali.rice.kns.web.ui.Column;
51 import org.kuali.rice.kns.web.ui.Field;
52 import org.kuali.rice.kns.web.ui.ResultRow;
53 import org.kuali.rice.kns.web.ui.Row;
54 import org.kuali.rice.krad.bo.BusinessObject;
55 import org.kuali.rice.krad.bo.PersistableBusinessObject;
56 import org.kuali.rice.krad.data.KradDataServiceLocator;
57 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
58 import org.kuali.rice.krad.keyvalues.IndicatorValuesFinder;
59 import org.kuali.rice.krad.keyvalues.KeyValuesFinder;
60 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
61 import org.kuali.rice.krad.util.GlobalVariables;
62 import org.kuali.rice.krad.util.KRADConstants;
63 import org.kuali.rice.krad.util.UrlFactory;
64
65 import java.sql.Date;
66 import java.sql.Timestamp;
67 import java.util.ArrayList;
68 import java.util.Calendar;
69 import java.util.Collection;
70 import java.util.Collections;
71 import java.util.Comparator;
72 import java.util.HashMap;
73 import java.util.Iterator;
74 import java.util.List;
75 import java.util.Map;
76 import java.util.Properties;
77
78 import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
79
80 public class GroupLookupableHelperServiceImpl extends KimLookupableHelperServiceImpl {
81
82
83
84
85 private static final String KIM_TYPE_ID_PROPERTY_NAME = "kimTypeId";
86 private List<Row> grpRows = new ArrayList<Row>();
87 private List<Row> attrRows = new ArrayList<Row>();
88 private String typeId = "";
89 private List<KimAttributeField> attrDefinitions;
90 private final Map<String, String> groupTypeValuesCache = new HashMap<String, String>();
91
92 @Override
93 public List<HtmlData> getCustomActionUrls(BusinessObject bo, List pkNames) {
94 GroupBo groupImpl = (GroupBo) bo;
95 List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
96 if(allowsNewOrCopyAction(KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_TYPE_NAME)){
97 anchorHtmlDataList.add(getEditGroupUrl(groupImpl));
98 }
99 return anchorHtmlDataList;
100 }
101
102 protected HtmlData getEditGroupUrl(GroupBo groupBo) {
103 String href = "";
104
105 Properties parameters = new Properties();
106 parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.DOC_HANDLER_METHOD);
107 parameters.put(KRADConstants.PARAMETER_COMMAND, KewApiConstants.INITIATE_COMMAND);
108 parameters.put(KRADConstants.DOCUMENT_TYPE_NAME, KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_TYPE_NAME);
109 parameters.put(KimConstants.PrimaryKeyConstants.GROUP_ID, groupBo.getId());
110 if (StringUtils.isNotBlank(getReturnLocation())) {
111 parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation());
112 }
113 href = UrlFactory.parameterizeUrl(KimCommonUtilsInternal.getKimBasePath()+KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_ACTION, parameters);
114
115 HtmlData.AnchorHtmlData anchorHtmlData = new HtmlData.AnchorHtmlData(href,
116 KRADConstants.DOC_HANDLER_METHOD, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL);
117 return anchorHtmlData;
118 }
119
120
121
122
123
124
125
126 @Override
127 public List<GroupBo> getSearchResults(java.util.Map<String,String> fieldValues) {
128 Map<String, String> criteriaMap = new HashMap<String, String>(fieldValues);
129 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
130 criteriaMap.remove(KRADConstants.DOC_FORM_KEY);
131 criteriaMap.remove(KRADConstants.BACK_LOCATION);
132 criteriaMap.remove(KRADConstants.DOC_NUM);
133 String refToRef = criteriaMap.get(KRADConstants.REFERENCES_TO_REFRESH);
134 if (StringUtils.isNotBlank(refToRef) && refToRef.equalsIgnoreCase("GroupBo")) {
135 criteriaMap.remove(KRADConstants.REFERENCES_TO_REFRESH);
136 }
137
138 if (!criteriaMap.isEmpty()) {
139 List<Predicate> predicates = new ArrayList<Predicate>();
140
141 if (StringUtils.isNotBlank(criteriaMap.get(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME))) {
142 QueryByCriteria.Builder principalCriteria = QueryByCriteria.Builder.create();
143 Predicate principalPred = like("principalName", criteriaMap.get(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME));
144 principalCriteria.setPredicates(principalPred);
145
146
147 PrincipalQueryResults principals = KimApiServiceLocator.getIdentityService()
148 .findPrincipals(principalCriteria.build());
149 List<String> principalIds = new ArrayList<String>();
150 for (Principal principal : principals.getResults()) {
151 principalIds.add(principal.getPrincipalId());
152 }
153 if (CollectionUtils.isNotEmpty(principalIds)) {
154 Timestamp currentTime = new Timestamp(Calendar.getInstance().getTimeInMillis());
155 predicates.add( and(
156 in("members.memberId", principalIds.toArray(
157 new String[principalIds.size()])),
158 equal("members.typeCode", KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode()),
159 and(
160 or(isNull("members.activeFromDateValue"), lessThanOrEqual("members.activeFromDateValue", currentTime)),
161 or(isNull("members.activeToDateValue"), greaterThan("members.activeToDateValue", currentTime))
162 )
163 ));
164 }
165
166 }
167 criteriaMap.remove(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME);
168
169 predicates.add(PredicateUtils.convertMapToPredicate(criteriaMap));
170 criteria.setPredicates(and(predicates.toArray(new Predicate[predicates.size()])));
171 }
172 GroupQueryResults groupResults = KimApiServiceLocator.getGroupService().findGroups(criteria.build());
173 List<Group> groups = groupResults.getResults();
174
175
176 Map<String, GroupBo> groupBos = new HashMap<String, GroupBo>(groups.size());
177 for (Group group : groups) {
178 if (groupBos.get(group.getId()) == null) {
179 groupBos.put(group.getId(), GroupBo.from(group));
180 }
181 }
182
183
184 return new ArrayList<GroupBo>(groupBos.values());
185 }
186
187 @Override
188 public boolean checkForAdditionalFields(Map<String, String> fieldValues) {
189 List<Row> attributeRows = setupAttributeRows(fieldValues);
190 if (attributeRows.isEmpty()) {
191 setAttrRows(attributeRows);
192 } else if (CollectionUtils.isEmpty(getAttrRows())) {
193 setAttrRows(attributeRows);
194 }
195 if (getAttrRows().size() > 0) {
196 return true;
197 }
198 return false;
199 }
200
201
202 @Override
203 public List<Row> getRows() {
204 if (getGrpRows().isEmpty()) {
205 List<Row> rows = super.getRows();
206 List<Row> returnRows = new ArrayList<Row>();
207 for (Row row : rows) {
208 for (int i = row.getFields().size() - 1; i >= 0; i--) {
209 Field field = row.getFields().get(i);
210 if (field.getPropertyName().equals(KIM_TYPE_ID_PROPERTY_NAME)) {
211 Field typeField = new Field();
212 typeField.setFieldLabel("Type");
213 typeField.setPropertyName(KIM_TYPE_ID_PROPERTY_NAME);
214 typeField.setFieldValidValues(getGroupTypeOptions());
215 typeField.setFieldType(Field.DROPDOWN);
216 row.getFields().set(i, typeField);
217 }
218 }
219 returnRows.add(row);
220 }
221
222 Field typeField = new Field();
223 typeField.setFieldLabel("Principal Name");
224 typeField.setPropertyName(KIMPropertyConstants.Person.PRINCIPAL_NAME);
225 typeField.setFieldType(Field.TEXT);
226 typeField.setMaxLength(40);
227 typeField.setSize(20);
228 typeField.setQuickFinderClassNameImpl("org.kuali.rice.kim.api.identity.Person");
229 typeField.setFieldConversions( "principalName:principalName" );
230 typeField.setLookupParameters( "principalName:principalName" );
231
232
233
234 List<Field> fields = (returnRows.isEmpty()) ? new ArrayList<Field>() : returnRows.get(returnRows.size() - 1).getFields();
235 if (!fields.isEmpty() && fields.get(fields.size() - 1).getFieldType().equals(Field.BLANK_SPACE)) {
236
237 int insertLoc = fields.size() - 1;
238 while (insertLoc >= 0 && fields.get(insertLoc).getFieldType().equals(Field.BLANK_SPACE)) {
239 insertLoc--;
240 }
241 fields.set(insertLoc + 1, typeField);
242 returnRows.get(returnRows.size() - 1).setFields(fields);
243 } else {
244
245 int fieldLen = fields.size();
246 fields = new ArrayList<Field>();
247 fields.add(typeField);
248 for (int i = 1; i < fieldLen; i++) {
249 Field blankSpace = new Field();
250 blankSpace.setFieldType(Field.BLANK_SPACE);
251 blankSpace.setPropertyName(Field.BLANK_SPACE);
252 fields.add(blankSpace);
253 }
254 returnRows.add(new Row(fields));
255 }
256
257 setGrpRows(returnRows);
258 }
259 if (getAttrRows().isEmpty()) {
260 setAttrDefinitions(Collections.<KimAttributeField>emptyList());
261 return getGrpRows();
262 }
263 List<Row> fullRows = new ArrayList<Row>();
264 fullRows.addAll(getGrpRows());
265 fullRows.addAll(getAttrRows());
266 return fullRows;
267 }
268
269
270 @Override
271 public List<Column> getColumns() {
272 List<Column> columns = super.getColumns();
273 for (Row row : attrRows) {
274 for (Field field : row.getFields()) {
275 Column newColumn = new Column();
276 newColumn.setColumnTitle(field.getFieldLabel());
277 newColumn.setMaxLength(getColumnMaxLength(field.getPropertyName()));
278 newColumn.setPropertyName(field.getPropertyName());
279 newColumn.setFormatter(field.getFormatter());
280 columns.add(newColumn);
281 }
282 }
283 return columns;
284 }
285
286 @Override
287 public Collection<GroupBo> performLookup(LookupForm lookupForm, Collection resultTable, boolean bounded) {
288 setBackLocation((String) lookupForm.getFieldsForLookup().get(KRADConstants.BACK_LOCATION));
289 setDocFormKey((String) lookupForm.getFieldsForLookup().get(KRADConstants.DOC_FORM_KEY));
290 List<GroupBo> displayList;
291
292
293 if (bounded) {
294 displayList = getSearchResults(lookupForm.getFieldsForLookup());
295 }
296 else {
297 displayList = (List<GroupBo>)getSearchResultsUnbounded(lookupForm.getFieldsForLookup());
298 }
299
300 HashMap<String,Class> propertyTypes = new HashMap<String, Class>();
301
302 boolean hasReturnableRow = false;
303
304 List returnKeys = getReturnKeys();
305 List pkNames = KRADServiceLocatorWeb.getLegacyDataAdapter().listPrimaryKeyFieldNames(getBusinessObjectClass());
306 Person user = GlobalVariables.getUserSession().getPerson();
307
308
309 for (Iterator iter = displayList.iterator(); iter.hasNext();) {
310 BusinessObject element = (BusinessObject) iter.next();
311 if(element instanceof PersistableBusinessObject){
312 lookupForm.setLookupObjectId(((PersistableBusinessObject)element).getObjectId());
313 }
314
315 BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user);
316
317 HtmlData returnUrl = getReturnUrl(element, lookupForm, returnKeys, businessObjectRestrictions);
318
319 String actionUrls = getActionUrls(element, pkNames, businessObjectRestrictions);
320
321 if("".equals(actionUrls)){
322 actionUrls = ACTION_URLS_EMPTY;
323 }
324
325 List<Column> columns = getColumns();
326 for (Object element2 : columns) {
327
328 Column col = (Column) element2;
329 Formatter formatter = col.getFormatter();
330
331
332 String propValue = KRADConstants.EMPTY_STRING;
333 Object prop = null;
334 if (col.getPropertyName().matches("\\w+\\.\\d+$")) {
335 String id = col.getPropertyName().substring(col.getPropertyName().lastIndexOf('.') + 1);
336 prop = ((GroupBo)element).getGroupAttributeValueById(id);
337 }
338 if (prop == null) {
339 prop = (String) KradDataServiceLocator.getDataObjectService().wrap(element).getPropertyValueNullSafe(col.getPropertyName());
340 } else {
341 }
342
343
344 Class propClass = propertyTypes.get(col.getPropertyName());
345 if ( propClass == null ) {
346 try {
347 propClass = KRADServiceLocatorWeb.getLegacyDataAdapter().getPropertyType(element,
348 col.getPropertyName());
349 propertyTypes.put( col.getPropertyName(), propClass );
350 } catch (Exception e) {
351 throw new RuntimeException("Cannot access PropertyType for property " + "'" + col.getPropertyName() + "' " + " on an instance of '" + element.getClass().getName() + "'.", e);
352 }
353 }
354
355
356 if (prop != null) {
357
358 if (prop instanceof Boolean) {
359 formatter = new BooleanFormatter();
360 }
361
362
363 if (prop instanceof Date) {
364 formatter = new DateFormatter();
365 }
366
367
368 if (prop instanceof Collection && formatter == null) {
369 formatter = new CollectionFormatter();
370 }
371
372 if (formatter != null) {
373 propValue = (String) formatter.format(prop);
374 }
375 else {
376 propValue = prop.toString();
377 if (col.getPropertyName().equals(KIM_TYPE_ID_PROPERTY_NAME)) {
378 propValue = groupTypeValuesCache.get(prop.toString());
379 }
380 }
381 }
382
383
384 col.setComparator(CellComparatorHelper.getAppropriateComparatorForPropertyClass(propClass));
385 col.setValueComparator(CellComparatorHelper.getAppropriateValueComparatorForPropertyClass(propClass));
386
387 propValue = maskValueIfNecessary(element.getClass(), col.getPropertyName(), propValue, businessObjectRestrictions);
388
389 col.setPropertyValue(propValue);
390
391 if (StringUtils.isNotBlank(propValue)) {
392 col.setColumnAnchor(getInquiryUrl(element, col.getPropertyName()));
393
394 }
395 }
396
397 ResultRow row = new ResultRow(columns, returnUrl.constructCompleteHtmlTag(), actionUrls);
398 row.setRowId(returnUrl.getName());
399 row.setReturnUrlHtmlData(returnUrl);
400
401
402 if (getBusinessObjectDictionaryService().isExportable(getBusinessObjectClass())) {
403 row.setBusinessObject(element);
404 }
405 if(element instanceof PersistableBusinessObject){
406 row.setObjectId((((PersistableBusinessObject)element).getObjectId()));
407 }
408
409
410 boolean rowReturnable = isResultReturnable(element);
411 row.setRowReturnable(rowReturnable);
412 if (rowReturnable) {
413 hasReturnableRow = true;
414 }
415 resultTable.add(row);
416 }
417
418 lookupForm.setHasReturnableRow(hasReturnableRow);
419
420 return displayList;
421 }
422
423
424 private List<KeyValue> getGroupTypeOptions() {
425 List<KeyValue> options = new ArrayList<KeyValue>();
426 options.add(new ConcreteKeyValue("", ""));
427
428 Collection<KimType> kimGroupTypes = KimApiServiceLocator.getKimTypeInfoService().findAllKimTypes();
429
430 for (KimType kimType : kimGroupTypes) {
431 if (KimTypeLookupableHelperServiceImpl.hasGroupTypeService(kimType) && groupTypeValuesCache.get(kimType.getId()) == null) {
432 String value = kimType.getNamespaceCode().trim() + KRADConstants.FIELD_CONVERSION_PAIR_SEPARATOR + kimType.getName().trim();
433 options.add(new ConcreteKeyValue(kimType.getId(), value));
434 }
435 }
436 Collections.sort(options, new Comparator<KeyValue>() {
437 @Override
438 public int compare(KeyValue k1, KeyValue k2) {
439 return k1.getValue().compareTo(k2.getValue());
440 }
441 });
442 return options;
443 }
444
445 private List<Row> setupAttributeRows(Map fieldValues) {
446 List<Row> returnRows = new ArrayList<Row>();
447 for (Row row : getGrpRows()) {
448 Field field = row.getFields().get(0);
449 if (field.getPropertyName().equals(KIM_TYPE_ID_PROPERTY_NAME) && StringUtils.isNotBlank(field.getPropertyValue())) {
450 if (!StringUtils.isBlank(getTypeId()) || !getTypeId().equals(field.getPropertyValue())) {
451 setTypeId(field.getPropertyValue());
452 setAttrRows(new ArrayList<Row>());
453 KimType kimType = getTypeInfoService().getKimType( field.getPropertyValue() );
454 KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(kimType);
455 List<KimAttributeField> definitions = kimTypeService.getAttributeDefinitions(kimType.getId());
456 setAttrDefinitions(definitions);
457 for (KimAttributeField d : definitions) {
458 final AttributeDefinition definition = DataDictionaryTypeServiceHelper
459 .toKimAttributeDefinition(d);
460 List<Field> fields = new ArrayList<Field>();
461 Field typeField = new Field();
462
463 String attrDefnId = d.getId();
464 typeField.setFieldLabel(definition.getLabel());
465 typeField.setPropertyName("attributes(" + definition.getName()+")");
466 typeField.setPropertyValue(fieldValues.get(typeField.getPropertyName()));
467 if (definition.getControl().isSelect()) {
468 typeField.setFieldValidValues(definition.getOptionsFinder().getKeyValues());
469 typeField.setFieldType(Field.DROPDOWN);
470 } else if (definition.getControl().isText()){
471 typeField.setMaxLength(definition.getMaxLength());
472 if (definition.getControl().getSize() != null) {
473 typeField.setSize(definition.getControl().getSize());
474 }
475 typeField.setFieldType(Field.TEXT);
476 } else if (definition.getControl().isRadio()) {
477 typeField.setFieldValidValues(definition.getOptionsFinder().getKeyValues());
478 typeField.setFieldType(Field.RADIO);
479 } else if (definition.getControl().isCheckbox()) {
480 KeyValuesFinder finder = new IndicatorValuesFinder();
481 typeField.setFieldValidValues(finder.getKeyValues());
482 typeField.setFieldType(Field.RADIO);
483
484 } else if (definition.getControl().isHidden()) {
485 typeField.setFieldType(Field.HIDDEN);
486 } else if (definition.getControl().isLookupReadonly()) {
487 typeField.setFieldType(Field.LOOKUP_READONLY);
488 } else if (definition.getControl().isTextarea()) {
489 typeField.setMaxLength(definition.getMaxLength());
490 if (definition.getControl().getSize() != null) {
491 typeField.setSize(definition.getControl().getSize());
492 }
493 typeField.setFieldType(Field.TEXT_AREA);
494 }
495 fields.add(typeField);
496 returnRows.add(new Row(fields));
497 }
498 } else {
499 return getAttrRows();
500 }
501 } else if (field.getPropertyName().equals(KIM_TYPE_ID_PROPERTY_NAME) && StringUtils.isBlank(field.getPropertyValue())) {
502 setTypeId("");
503 }
504 }
505 return returnRows;
506 }
507
508 public List<Row> getGrpRows() {
509 return this.grpRows;
510 }
511
512 public void setGrpRows(List<Row> grpRows) {
513 this.grpRows = grpRows;
514 }
515
516 public List<KimAttributeField> getAttrDefinitions() {
517 return this.attrDefinitions;
518 }
519
520 public void setAttrDefinitions(List<KimAttributeField> attrDefinitions) {
521 this.attrDefinitions = attrDefinitions;
522 }
523
524 public List<Row> getAttrRows() {
525 return this.attrRows;
526 }
527
528 public void setAttrRows(List<Row> attrRows) {
529 this.attrRows = attrRows;
530 }
531
532 public String getTypeId() {
533 return this.typeId;
534 }
535
536 public void setTypeId(String typeId) {
537 this.typeId = typeId;
538 }
539
540 @Override
541 public void performClear(LookupForm lookupForm) {
542 super.performClear(lookupForm);
543 this.attrRows = new ArrayList<Row>();
544 }
545
546 }