| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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.util.AttributeSet; |
| 28 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
| 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 | |
|
| 47 | |
|
| 48 | |
|
| 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 = KimApiServiceLocator.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 | |
|
| 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 | |
|
| 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 | |
|
| 98 | 0 | reportDuplicateDetailTypes(allDetailTypes); |
| 99 | |
|
| 100 | |
|
| 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 | |
|
| 110 | 0 | fieldValues.remove(COMPONENT_NAME); |
| 111 | |
|
| 112 | 0 | results = super.getSearchResultsUnbounded(fieldValues); |
| 113 | |
|
| 114 | 0 | attachDataDictionaryDetailTypes(results, ddDetailTypes); |
| 115 | |
|
| 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 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
private void reportDuplicateDetailTypes( |
| 135 | |
List<ComponentBo> allDetailTypes) { |
| 136 | |
|
| 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 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 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 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
private void attachDataDictionaryDetailTypes( |
| 180 | |
List<? extends BusinessObject> parameters, |
| 181 | |
List<Component> ddDetailTypes) { |
| 182 | |
|
| 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 | |
|