1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.ComponentBo;
22 import org.kuali.rice.coreservice.impl.component.DerivedComponentBo;
23 import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
24 import org.kuali.rice.kim.api.KimConstants;
25 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
27 import org.kuali.rice.krad.bo.BusinessObject;
28 import org.kuali.rice.krad.util.GlobalVariables;
29 import org.kuali.rice.krad.util.KRADConstants;
30
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.regex.Pattern;
37
38
39
40
41
42
43
44 public class ParameterLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
45
46 private static final long serialVersionUID = 4381873774407301041L;
47
48 private static final Log LOG = LogFactory.getLog(ParameterLookupableHelperServiceImpl.class);
49 private static final String COMPONENT_NAME = "component.name";
50 private static final String COMPONENT_CODE = "componentCode";
51
52 @Override
53 protected boolean allowsMaintenanceEditAction(BusinessObject businessObject) {
54
55 ParameterBo parm = (ParameterBo)businessObject;
56
57 Map<String, String> permissionDetails = new HashMap<String, String>();
58 permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, parm.getNamespaceCode());
59 permissionDetails.put(KimConstants.AttributeConstants.COMPONENT_NAME, parm.getComponentCode());
60 permissionDetails.put(KimConstants.AttributeConstants.PARAMETER_NAME, parm.getName());
61 return KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(
62 GlobalVariables.getUserSession().getPerson().getPrincipalId(), KRADConstants.KNS_NAMESPACE,
63 KimConstants.PermissionTemplateNames.MAINTAIN_SYSTEM_PARAMETER, permissionDetails,
64 Collections.<String, String>emptyMap());
65 }
66
67 @Override
68 public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
69
70 if (fieldValues.containsKey(COMPONENT_NAME) && fieldValues.containsKey(COMPONENT_CODE)) {
71
72 fieldValues.remove(COMPONENT_CODE);
73 fieldValues.put(COMPONENT_CODE,"");
74 }
75
76 String componentNameFieldValue = fieldValues.get(COMPONENT_NAME);
77 fieldValues.remove(COMPONENT_NAME);
78
79 List<ParameterBo> results = (List<ParameterBo>)super.getSearchResultsUnbounded(fieldValues);
80 normalizeParameterComponents(results);
81
82 if (!StringUtils.isBlank(componentNameFieldValue)) {
83 componentNameFieldValue = componentNameFieldValue.trim();
84 componentNameFieldValue = componentNameFieldValue.replace("*", ".*");
85 componentNameFieldValue = ".*" + componentNameFieldValue + ".*";
86
87 Pattern pattern = Pattern.compile(componentNameFieldValue, Pattern.CASE_INSENSITIVE);
88
89 Iterator<ParameterBo> resultsIter = results.iterator();
90
91 while (resultsIter.hasNext()) {
92 ParameterBo result = resultsIter.next();
93 ComponentBo component = result.getComponent();
94 if (component != null && !pattern.matcher(component.getName()).matches()) {
95 resultsIter.remove();
96 }
97 }
98 }
99
100 return results;
101 }
102
103 private void normalizeParameterComponents(List<ParameterBo> parameters) {
104
105 for (ParameterBo parameterBo : parameters) {
106 if (parameterBo.getComponent() == null) {
107 parameterBo.setComponent(DerivedComponentBo.toComponentBo(parameterBo.getDerivedComponent()));
108 }
109 }
110 }
111
112 }
113