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.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 | |
|
46 | |
|
47 | |
|
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 | |
|
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 | |
|
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 | |
|
97 | 0 | reportDuplicateDetailTypes(allDetailTypes); |
98 | |
|
99 | |
|
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 | |
|
109 | 0 | fieldValues.remove(COMPONENT_NAME); |
110 | |
|
111 | 0 | results = super.getSearchResultsUnbounded(fieldValues); |
112 | |
|
113 | 0 | attachDataDictionaryDetailTypes(results, ddDetailTypes); |
114 | |
|
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 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
private void reportDuplicateDetailTypes( |
134 | |
List<ComponentBo> allDetailTypes) { |
135 | |
|
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 | |
|
155 | |
|
156 | |
|
157 | |
|
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 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
private void attachDataDictionaryDetailTypes( |
181 | |
List<? extends BusinessObject> parameters, |
182 | |
List<Component> ddDetailTypes) { |
183 | |
|
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 | |
|