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