View Javadoc
1   /**
2    * Copyright 2005-2014 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.coreservice.web.parameter;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.kuali.rice.coreservice.impl.component.DerivedComponentBo;
22  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
23  import org.kuali.rice.kim.api.KimConstants;
24  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
26  import org.kuali.rice.krad.bo.BusinessObject;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.KRADConstants;
29  
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.regex.Pattern;
36  
37  /**
38   * This is a description of what this class does - kellerj don't forget to fill this in.
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   *
42   */
43  public class ParameterLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
44  
45      private static final long serialVersionUID = 4381873774407301041L;
46  
47      private static final Log LOG = LogFactory.getLog(ParameterLookupableHelperServiceImpl.class);
48      private static final String COMPONENT_NAME = "component.name";
49      private static final String COMPONENT_CODE = "componentCode";
50  
51      @Override
52      protected boolean allowsMaintenanceEditAction(BusinessObject businessObject) {
53  
54          ParameterBo parm = (ParameterBo)businessObject;
55  
56          Map<String, String> permissionDetails = new HashMap<String, String>();
57          permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, parm.getNamespaceCode());
58          permissionDetails.put(KimConstants.AttributeConstants.COMPONENT_NAME, parm.getComponentCode());
59          permissionDetails.put(KimConstants.AttributeConstants.PARAMETER_NAME, parm.getName());
60          return KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(
61                  GlobalVariables.getUserSession().getPerson().getPrincipalId(), KRADConstants.KNS_NAMESPACE,
62                  KimConstants.PermissionTemplateNames.MAINTAIN_SYSTEM_PARAMETER, permissionDetails,
63                  Collections.<String, String>emptyMap());
64      }
65  
66      @Override
67      public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
68  
69          if (fieldValues.containsKey(COMPONENT_NAME) && fieldValues.containsKey(COMPONENT_CODE)) {
70              //remove hidden derived component code if component name exists.
71              fieldValues.remove(COMPONENT_CODE);
72              fieldValues.put(COMPONENT_CODE,"");
73          }
74  
75          String componentNameFieldValue = fieldValues.get(COMPONENT_NAME);
76          fieldValues.remove(COMPONENT_NAME);
77  
78          List<ParameterBo> results = (List<ParameterBo>)super.getSearchResultsUnbounded(fieldValues);
79          normalizeParameterComponents(results);
80  
81          if (!StringUtils.isBlank(componentNameFieldValue)) {
82              componentNameFieldValue = componentNameFieldValue.trim();
83              componentNameFieldValue = componentNameFieldValue.replace("*", ".*");
84              componentNameFieldValue = ".*" + componentNameFieldValue + ".*";
85  
86              Pattern pattern = Pattern.compile(componentNameFieldValue, Pattern.CASE_INSENSITIVE);
87  
88              Iterator<ParameterBo> resultsIter = results.iterator();
89              while (resultsIter.hasNext()) {
90                  ParameterBo result = resultsIter.next();
91                  if ((result.getComponent() == null ) ||
92                      (!pattern.matcher(result.getComponent().getName()).matches())) {
93                          resultsIter.remove();
94                  }
95              }
96          }
97  
98          return results;
99      }
100 
101     private void normalizeParameterComponents(List<ParameterBo> parameters) {
102         // attach the derived components where needed
103         for (ParameterBo parameterBo : parameters) {
104             if (parameterBo.getComponent() == null) {
105                 parameterBo.setComponent(DerivedComponentBo.toComponentBo(parameterBo.getDerivedComponent()));
106             }
107         }
108     }
109 
110 }
111