1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.lookup;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.kuali.rice.kns.bo.BusinessObject;
26 import org.kuali.rice.kim.bo.Person;
27 import org.kuali.rice.kim.bo.entity.KimPrincipal;
28 import org.kuali.rice.kim.service.KIMServiceLocator;
29 import org.kuali.rice.kim.util.KIMPropertyConstants;
30 import org.kuali.rice.kns.document.authorization.PessimisticLock;
31 import org.kuali.rice.kns.service.KNSServiceLocator;
32 import org.kuali.rice.kns.service.PessimisticLockService;
33 import org.kuali.rice.kns.util.BeanPropertyComparator;
34 import org.kuali.rice.kns.util.FieldUtils;
35 import org.kuali.rice.kns.util.GlobalVariables;
36 import org.kuali.rice.kns.util.KNSConstants;
37 import org.kuali.rice.kns.util.KNSPropertyConstants;
38 import org.kuali.rice.kns.util.ObjectUtils;
39 import org.kuali.rice.kns.util.RiceKeyConstants;
40 import org.kuali.rice.kns.web.ui.Field;
41 import org.kuali.rice.kns.web.ui.Row;
42
43
44
45
46
47
48
49 public class PessimisticLockLookupableHelperServiceImpl extends AbstractLookupableHelperServiceImpl {
50 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PessimisticLockLookupableHelperServiceImpl.class);
51
52 private static final long serialVersionUID = -5839142187907211804L;
53 private static final String OWNER_PRINCIPAL_ID_PROPERTY_NAME = "ownedByPrincipalIdentifier";
54 private static final String OWNER_PRINCIPAL_NAME_PROPERTY_NAME = "ownedByUser.principalName";
55
56 private List<Row> localRows;
57
58
59
60
61
62
63 @Override
64 public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
65 PessimisticLock lock = (PessimisticLock)businessObject;
66 if ( (lock.isOwnedByUser(GlobalVariables.getUserSession().getPerson())) || (KNSServiceLocator.getPessimisticLockService().isPessimisticLockAdminUser(GlobalVariables.getUserSession().getPerson())) ) {
67 List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
68 anchorHtmlDataList.add(getUrlData(businessObject, KNSConstants.DELETE_METHOD, pkNames));
69 return anchorHtmlDataList;
70 } else {
71 return super.getEmptyActionUrls();
72 }
73 }
74
75
76
77
78
79
80 @Override
81 public List<Row> getRows() {
82 Person currentUser = GlobalVariables.getUserSession().getPerson();
83 if (KNSServiceLocator.getPessimisticLockService().isPessimisticLockAdminUser(currentUser)) {
84 return super.getRows();
85 } else {
86 if ( (ObjectUtils.isNull(localRows)) || localRows.isEmpty() ) {
87 List<Field> fieldList = new ArrayList<Field>();
88 int numColumns = -1;
89
90 for (Iterator<Row> iterator = super.getRows().iterator(); iterator.hasNext();) {
91 Row row = (Row) iterator.next();
92 if (numColumns == -1) {
93 numColumns = row.getFields().size();
94 }
95 for (Iterator<Field> iterator2 = row.getFields().iterator(); iterator2.hasNext();) {
96 Field field = (Field) iterator2.next();
97 if (!(KNSPropertyConstants.OWNED_BY_USER + "." + KIMPropertyConstants.Person.PRINCIPAL_NAME).equals(field.getPropertyName()) &&
98 !Field.BLANK_SPACE.equals(field.getFieldType())) {
99 fieldList.add(field);
100 }
101 }
102 }
103
104 localRows = FieldUtils.wrapFields(fieldList, numColumns);
105 }
106 return localRows;
107 }
108 }
109
110
111
112
113
114
115 @Override
116 public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
117
118 LookupUtils.removeHiddenCriteriaFields( getBusinessObjectClass(), fieldValues );
119
120 Person currentUser = GlobalVariables.getUserSession().getPerson();
121 if (!KNSServiceLocator.getPessimisticLockService().isPessimisticLockAdminUser(currentUser)) {
122 fieldValues.put(KNSPropertyConstants.OWNED_BY_PRINCIPAL_ID,GlobalVariables.getUserSession().getPerson().getPrincipalId());
123 }
124
125
126 String principalName = fieldValues.get(OWNER_PRINCIPAL_NAME_PROPERTY_NAME);
127 if (!StringUtils.isEmpty(principalName)) {
128 KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
129 if (principal != null) {
130 fieldValues.put(OWNER_PRINCIPAL_ID_PROPERTY_NAME, principal.getPrincipalId());
131 }
132 fieldValues.remove(OWNER_PRINCIPAL_NAME_PROPERTY_NAME);
133 }
134
135 setBackLocation(fieldValues.get(KNSConstants.BACK_LOCATION));
136 setDocFormKey(fieldValues.get(KNSConstants.DOC_FORM_KEY));
137 setReferencesToRefresh(fieldValues.get(KNSConstants.REFERENCES_TO_REFRESH));
138 if (LOG.isInfoEnabled()) {
139 LOG.info("Search Criteria: " + fieldValues);
140 }
141
142
143 List searchResults;
144 searchResults = (List) getLookupService().findCollectionBySearchHelper(getBusinessObjectClass(), fieldValues, true);
145
146 List defaultSortColumns = getDefaultSortColumns();
147 if (defaultSortColumns.size() > 0) {
148 Collections.sort(searchResults, new BeanPropertyComparator(getDefaultSortColumns(), true));
149 }
150 return searchResults;
151 }
152
153 @Override
154 public void validateSearchParameters(Map fieldValues) {
155 super.validateSearchParameters(fieldValues);
156 if (StringUtils.isNotEmpty((String)fieldValues.get(OWNER_PRINCIPAL_NAME_PROPERTY_NAME))) {
157 Person person = KIMServiceLocator.getPersonService().getPersonByPrincipalName((String)fieldValues.get(OWNER_PRINCIPAL_NAME_PROPERTY_NAME));
158 if (person == null) {
159 String attributeLabel = getDataDictionaryService().getAttributeLabel(getBusinessObjectClass(), OWNER_PRINCIPAL_NAME_PROPERTY_NAME);
160 GlobalVariables.getMessageMap().putError(OWNER_PRINCIPAL_NAME_PROPERTY_NAME, RiceKeyConstants.ERROR_EXISTENCE, attributeLabel);
161 }
162 }
163 }
164
165 }
166