View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class is the lookup helper for {@link org.kuali.rice.kns.document.authorization.PessimisticLock} objects
45   *
46   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * Hides the applicable links when the PessimisticLock is not owned by the current user
60       *
61       * @see org.kuali.rice.kns.lookup.LookupableHelperService#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject, java.util.List, java.util.List pkNames)
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       * This overridden method checks whether the user is an admin user according to {@link PessimisticLockService#isPessimisticLockAdminUser(Person)} and if the user is not an admin user the user field is set to Read Only and the lookup field
77       *
78       * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getRows()
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                  // hide a field and forcibly set a field
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                 // Since the removed field is the first one in the list, use FieldUtils to re-wrap the remaining fields accordingly.
104                 localRows = FieldUtils.wrapFields(fieldList, numColumns);
105             }
106             return localRows;
107         }
108     }
109 
110     /**
111      * This method implementation is used to search for objects
112      *
113      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getSearchResults(java.util.Map)
114      */
115     @Override
116     public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
117         // remove hidden fields
118         LookupUtils.removeHiddenCriteriaFields( getBusinessObjectClass(), fieldValues );
119         // force criteria if not admin user
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         //set owner's principal id and remove owner principal name field 
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         //replace principal name with principal id in fieldValues
143         List searchResults;
144         searchResults = (List) getLookupService().findCollectionBySearchHelper(getBusinessObjectClass(), fieldValues, true);
145         // sort list if default sort column given
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