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