Coverage Report - org.kuali.rice.core.web.parameter.ParameterLookupableHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ParameterLookupableHelperServiceImpl
0%
0/60
0%
0/30
3.286
ParameterLookupableHelperServiceImpl$1
0%
0/2
N/A
3.286
 
 1  
 /*
 2  
  * Copyright 2006-2011 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.core.web.parameter;
 17  
 
 18  
 import org.apache.commons.collections.CollectionUtils;
 19  
 import org.apache.commons.collections.Predicate;
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 import org.kuali.rice.core.api.component.Component;
 24  
 import org.kuali.rice.core.framework.parameter.ParameterService;
 25  
 import org.kuali.rice.core.impl.component.ComponentBo;
 26  
 import org.kuali.rice.core.impl.parameter.ParameterBo;
 27  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 28  
 import org.kuali.rice.kim.util.KimConstants;
 29  
 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
 30  
 import org.kuali.rice.krad.bo.BusinessObject;
 31  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 32  
 import org.kuali.rice.krad.util.GlobalVariables;
 33  
 import org.kuali.rice.krad.util.KRADConstants;
 34  
 
 35  
 import java.util.ArrayList;
 36  
 import java.util.HashMap;
 37  
 import java.util.HashSet;
 38  
 import java.util.List;
 39  
 import java.util.Map;
 40  
 import java.util.Set;
 41  
 import java.util.regex.Pattern;
 42  
 import java.util.regex.PatternSyntaxException;
 43  
 
 44  
 /**
 45  
  * This is a description of what this class does - kellerj don't forget to fill this in.
 46  
  *
 47  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 48  
  *
 49  
  */
 50  0
 public class ParameterLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
 51  0
     private static final Log LOG = LogFactory.getLog(ParameterLookupableHelperServiceImpl.class);
 52  
     private static final String COMPONENT_NAME = "component.name";
 53  
     private static final String NAMESPACE_CODE = "namespaceCode";
 54  
 
 55  
     private ParameterService parameterService;
 56  
 
 57  
     @Override
 58  
     protected boolean allowsMaintenanceEditAction(BusinessObject businessObject) {
 59  
             
 60  0
         boolean allowsEdit = false;
 61  0
         ParameterBo parm = (ParameterBo)businessObject;
 62  
         
 63  0
         Map<String, String> permissionDetails = new HashMap<String, String>();
 64  0
         permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, parm.getNamespaceCode());
 65  0
         permissionDetails.put(KimConstants.AttributeConstants.COMPONENT_NAME, parm.getComponentCode());
 66  0
         permissionDetails.put(KimConstants.AttributeConstants.PARAMETER_NAME, parm.getName());
 67  0
         allowsEdit = KimApiServiceLocator.getPermissionService().isAuthorizedByTemplateName(
 68  
                         GlobalVariables.getUserSession().getPerson().getPrincipalId(),
 69  
                                 KRADConstants.KRAD_NAMESPACE,
 70  
                                 KimConstants.PermissionTemplateNames.MAINTAIN_SYSTEM_PARAMETER,
 71  
                                 permissionDetails, null);
 72  
         
 73  0
         return allowsEdit;
 74  
     }
 75  
     
 76  
     @Override
 77  
     public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
 78  
         List<? extends BusinessObject> results;
 79  
         
 80  
         // get the DD detail types
 81  0
         List<Component> ddDetailTypes = KRADServiceLocatorWeb.getRiceApplicationConfigurationMediationService().getNonDatabaseComponents();
 82  0
         if (fieldValues.containsKey(COMPONENT_NAME) && !StringUtils.isBlank(fieldValues.get(COMPONENT_NAME))) {
 83  0
                 final Set<ComponentBo> matchingDetailTypes = new HashSet<ComponentBo>();
 84  
             // perform a basic database lookup for detail types codes
 85  0
             String namespaceCode = fieldValues.get(NAMESPACE_CODE);
 86  0
             String parameterDetailTypeName = fieldValues.get(COMPONENT_NAME);
 87  
 
 88  0
             List<ComponentBo> dbDetailTypes =
 89  
                     (List<ComponentBo>)getBusinessObjectService().findAll(ComponentBo.class);
 90  0
             List<ComponentBo> allDetailTypes = new ArrayList<ComponentBo>(ddDetailTypes.size() + dbDetailTypes.size());
 91  0
             allDetailTypes.addAll(dbDetailTypes);
 92  0
             for (Component fromDD : ddDetailTypes) {
 93  0
                 allDetailTypes.add(ComponentBo.from(fromDD));
 94  
             }
 95  
             
 96  
             // add some error logging if there are duplicates
 97  0
             reportDuplicateDetailTypes(allDetailTypes);
 98  
             
 99  
             // filter all detail types by their name
 100  0
             Pattern nameRegex = getParameterDetailTypeNameRegex(parameterDetailTypeName);
 101  0
             for (ComponentBo detailType : allDetailTypes) {
 102  0
                 if (StringUtils.isBlank(namespaceCode) || detailType.getNamespaceCode().equals(namespaceCode)) {
 103  0
                     if (nameRegex == null || (detailType.getCode() != null && nameRegex.matcher(detailType.getCode().toUpperCase()).matches())) {
 104  0
                             matchingDetailTypes.add(detailType);
 105  
                     }
 106  
                 }
 107  
             }
 108  
             // we're filtering in memory, so remove this criteria
 109  0
             fieldValues.remove(COMPONENT_NAME);
 110  
             
 111  0
             results = super.getSearchResultsUnbounded(fieldValues);
 112  
             // attach the DD detail types to your results before we filter (else filtering won't work correctly)
 113  0
             attachDataDictionaryDetailTypes(results, ddDetailTypes);
 114  
             // filter down to just results with matching parameter component (ParameterDetailType)
 115  0
             CollectionUtils.filter(results, new Predicate() {
 116  
                     public boolean evaluate(Object object) {
 117  0
                             return matchingDetailTypes.contains(((ParameterBo)object).getComponentCode());
 118  
                     }
 119  
             });
 120  0
         }
 121  
         else {
 122  0
             results = super.getSearchResultsUnbounded(fieldValues);
 123  0
             attachDataDictionaryDetailTypes(results, ddDetailTypes);
 124  
         }
 125  0
         return results;
 126  
     }
 127  
 
 128  
         /**
 129  
          * This method ...
 130  
          * 
 131  
          * @param allDetailTypes
 132  
          */
 133  
         private void reportDuplicateDetailTypes(
 134  
                         List<ComponentBo> allDetailTypes) {
 135  
                 // check for duplicates between DD and DB 
 136  0
                 Set<ComponentBo> dupCheck = new HashSet<ComponentBo>();
 137  0
                 for (ComponentBo detailType : allDetailTypes) {
 138  0
                         if (dupCheck.contains(detailType)) {
 139  0
                                 ComponentBo duplicate = null;
 140  0
                                 for (ComponentBo d : dupCheck) {
 141  0
                     if (d.equals(detailType)) {
 142  0
                                             duplicate = d;
 143  0
                                             break;
 144  
                     }
 145  
                                 }
 146  0
                                 LOG.error(ComponentBo.class.getSimpleName() + "found with duplicate keys: " + detailType + " and " + duplicate);
 147  0
                         } else {
 148  0
                                 dupCheck.add(detailType);
 149  
                         }
 150  
                 }
 151  0
         }
 152  
 
 153  
         /**
 154  
          * This method ...
 155  
          * 
 156  
          * @param parameterDetailTypeName
 157  
          * @return
 158  
          */
 159  
         private Pattern getParameterDetailTypeNameRegex(
 160  
                         String parameterDetailTypeName) {
 161  0
                 Pattern nameRegex = null;
 162  0
                 if (StringUtils.isNotBlank(parameterDetailTypeName)) {
 163  0
                     String patternStr = parameterDetailTypeName.replace("*", ".*").toUpperCase();
 164  
                     try {
 165  0
                         nameRegex = Pattern.compile(patternStr);
 166  
                     }
 167  0
                     catch (PatternSyntaxException ex) {
 168  0
                         LOG.error("Unable to parse parameterDetailTypeName pattern, ignoring.", ex);
 169  0
                     }
 170  
                 }
 171  0
                 return nameRegex;
 172  
         }
 173  
 
 174  
         /**
 175  
          * This method ...
 176  
          * 
 177  
          * @param parameters
 178  
          * @param ddDetailTypes
 179  
          */
 180  
         private void attachDataDictionaryDetailTypes(
 181  
                         List<? extends BusinessObject> parameters,
 182  
                         List<Component> ddDetailTypes) {
 183  
                 // attach the non-database parameterDetailTypes
 184  0
         Map<String, ComponentBo> ddDetailTypeMap = new HashMap<String, ComponentBo>(ddDetailTypes.size());
 185  0
         for (Component detailType : ddDetailTypes) {
 186  0
             ddDetailTypeMap.put(detailType.getCode(), ComponentBo.from(detailType));
 187  
         }
 188  0
         }
 189  
 
 190  
     public void setParameterService(ParameterService parameterService) {
 191  0
         this.parameterService = parameterService;
 192  0
     }
 193  
     
 194  
 }
 195