Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ParameterEvaluatorServiceImpl |
|
| 2.0;2 |
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 | ||
17 | package org.kuali.rice.core.impl.parameter; | |
18 | ||
19 | ||
20 | import org.apache.commons.lang.StringUtils; | |
21 | import org.kuali.rice.core.api.parameter.EvaluationOperator; | |
22 | import org.kuali.rice.core.api.parameter.Parameter; | |
23 | import org.kuali.rice.core.api.parameter.ParameterEvaluator; | |
24 | import org.kuali.rice.core.api.parameter.ParameterEvaluatorService; | |
25 | import org.kuali.rice.core.framework.parameter.ParameterService; | |
26 | ||
27 | import java.util.Arrays; | |
28 | import java.util.Collections; | |
29 | import java.util.List; | |
30 | ||
31 | 0 | public class ParameterEvaluatorServiceImpl implements ParameterEvaluatorService { |
32 | ||
33 | private ParameterService parameterService; | |
34 | ||
35 | public void setParameterService(ParameterService parameterService) { | |
36 | 0 | this.parameterService = parameterService; |
37 | 0 | } |
38 | ||
39 | /** | |
40 | * This method will return an instance of the parameterEvaluator bean defined in Spring, initialized with the Parameter | |
41 | * corresponding to the specified componentClass and parameterName and the values of the Parameter. | |
42 | * | |
43 | * @param componentClass | |
44 | * @param parameterName | |
45 | * @return ParameterEvaluator instance initialized with the Parameter corresponding to the specified componentClass and | |
46 | * parameterName and the values of the Parameter | |
47 | */ | |
48 | @Override | |
49 | public ParameterEvaluator getParameterEvaluator(Class componentClass, String parameterName) { | |
50 | 0 | return getParameterEvaluator(parameterService.getParameter(componentClass, parameterName)); |
51 | } | |
52 | ||
53 | /** | |
54 | * This method will return an instance of the parameterEvaluator bean defined in Spring, initialized with the Parameter | |
55 | * corresponding to the specified componentClass and parameterName and the values of the Parameter. | |
56 | * | |
57 | * @param namespaceCode | |
58 | * @param detailTypeCode | |
59 | * @param parameterName | |
60 | * @return ParameterEvaluator instance initialized with the Parameter corresponding to the specified componentClass and | |
61 | * parameterName and the values of the Parameter | |
62 | */ | |
63 | @Override | |
64 | public ParameterEvaluator getParameterEvaluator(String namespaceCode, String detailTypeCode, String parameterName) { | |
65 | 0 | return getParameterEvaluator(parameterService.getParameter(namespaceCode, detailTypeCode, parameterName)); |
66 | } | |
67 | ||
68 | /** | |
69 | * This method will return an instance of the parameterEvaluator bean defined in Spring, initialized with the Parameter | |
70 | * corresponding to the specified componentClass and parameterName, the values of the Parameter, the knowledge of whether the | |
71 | * values are allowed or denied, and the constrainedValue. | |
72 | * | |
73 | * @param componentClass | |
74 | * @param parameterName | |
75 | * @return ParameterEvaluator instance initialized with the Parameter corresponding to the specified componentClass and | |
76 | * parameterName, the values of the Parameter, the knowledge of whether the values are allowed or denied, and the | |
77 | * constrainedValue | |
78 | */ | |
79 | @Override | |
80 | public ParameterEvaluator getParameterEvaluator(Class componentClass, String parameterName, String constrainedValue) { | |
81 | 0 | return getParameterEvaluator(parameterService.getParameter(componentClass, parameterName), constrainedValue); |
82 | } | |
83 | ||
84 | /** | |
85 | * This method will return an instance of the parameterEvaluator bean defined in Spring, initialized with the Parameter | |
86 | * corresponding to the specified componentClass and parameterName and the values of the Parameter. | |
87 | * | |
88 | * @param namespaceCode | |
89 | * @param detailTypeCode | |
90 | * @param parameterName | |
91 | * @return ParameterEvaluator instance initialized with the Parameter corresponding to the specified componentClass and | |
92 | * parameterName and the values of the Parameter | |
93 | */ | |
94 | @Override | |
95 | public ParameterEvaluator getParameterEvaluator(String namespaceCode, String detailTypeCode, String parameterName, String constrainedValue) { | |
96 | 0 | return getParameterEvaluator(parameterService.getParameter(namespaceCode, detailTypeCode, parameterName), constrainedValue); |
97 | } | |
98 | ||
99 | /** | |
100 | * This method will return an instance of the parameterEvaluator bean defined in Spring, initialized with the Parameter | |
101 | * corresponding to the specified componentClass and parameterName, the values of the Parameter that correspond to the specified | |
102 | * constrainingValue, the knowledge of whether the values are allowed or denied, and the constrainedValue. | |
103 | * | |
104 | * @param componentClass | |
105 | * @param parameterName | |
106 | * @return ParameterEvaluator instance initialized with the Parameter corresponding to the specified componentClass and | |
107 | * parameterName, the values of the Parameter that correspond to the specified constrainingValue, the knowledge of | |
108 | * whether the values are allowed or denied, and the constrainedValue | |
109 | */ | |
110 | @Override | |
111 | public ParameterEvaluator getParameterEvaluator(Class componentClass, String parameterName, String constrainingValue, | |
112 | String constrainedValue) { | |
113 | 0 | return getParameterEvaluator(parameterService.getParameter(componentClass, parameterName), constrainingValue, constrainedValue); |
114 | } | |
115 | ||
116 | /** | |
117 | * This method will return an instance of the parameterEvaluator bean defined in Spring, initialized with the Parameter | |
118 | * corresponding to the specified componentClass and allowParameterName or to the specified componentClass and denyParameterName | |
119 | * (depending on which restricts based on the constraining value) or an instance of AlwaysSucceedParameterEvaluatorImpl if | |
120 | * neither restricts, the values of the Parameter that correspond to the specified constrainingValue, the knowledge of whether | |
121 | * the values are allowed or denied, and the constrainedValue. | |
122 | * | |
123 | * @param componentClass | |
124 | * @param allowParameterName | |
125 | * @param denyParameterName | |
126 | * @param constrainingValue | |
127 | * @param constrainedValue | |
128 | * @return AlwaysSucceedParameterEvaluatorImpl or ParameterEvaluator instance initialized with the Parameter that corresponds to | |
129 | * the constrainingValue restriction, the values of the Parameter that correspond to the specified constrainingValue, | |
130 | * the knowledge of whether the values are allowed or denied, and the constrainedValue | |
131 | */ | |
132 | @Override | |
133 | public ParameterEvaluator getParameterEvaluator(Class componentClass, String allowParameterName, String denyParameterName, | |
134 | String constrainingValue, String constrainedValue) { | |
135 | 0 | Parameter allowParameter = parameterService.getParameter(componentClass, allowParameterName); |
136 | 0 | Parameter denyParameter = parameterService.getParameter(componentClass, denyParameterName); |
137 | 0 | if (!getParameterValues(allowParameter, constrainingValue).isEmpty() && !getParameterValues(denyParameter, constrainingValue).isEmpty()) { |
138 | 0 | throw new IllegalArgumentException("The getParameterEvaluator(Class componentClass, String allowParameterName, String denyParameterName, String constrainingValue, String constrainedValue) method of ParameterServiceImpl does not facilitate evaluation of combination allow and deny parameters that both have values for the constraining value: " + allowParameterName + " / " + denyParameterName + " / " + constrainingValue); |
139 | } | |
140 | 0 | if (getParameterValues(allowParameter, constrainingValue).isEmpty() && getParameterValues(denyParameter, constrainingValue).isEmpty()) { |
141 | 0 | return AlwaysSucceedParameterEvaluatorImpl.getInstance(); |
142 | } | |
143 | 0 | return getParameterEvaluator(getParameterValues(denyParameter, constrainingValue).isEmpty() ? allowParameter : denyParameter, constrainingValue, constrainedValue); |
144 | } | |
145 | ||
146 | protected List<String> getParameterValues(Parameter parameter, String constrainingValue) { | |
147 | 0 | List<String> constraintValuePairs = getParameterValues(parameter); |
148 | 0 | for (String pair : constraintValuePairs) { |
149 | 0 | if (StringUtils.equals(constrainingValue, StringUtils.substringBefore(pair, "="))) { |
150 | 0 | return Arrays.asList(StringUtils.substringAfter(pair, "=").split(",")); |
151 | } | |
152 | } | |
153 | 0 | return Collections.emptyList(); |
154 | } | |
155 | ||
156 | private List<String> getParameterValues(Parameter parameter) { | |
157 | 0 | if (parameter == null || StringUtils.isBlank(parameter.getValue())) { |
158 | 0 | return Collections.emptyList(); |
159 | } | |
160 | 0 | return Arrays.asList(parameter.getValue().split(";")); |
161 | } | |
162 | ||
163 | protected ParameterEvaluatorImpl getParameterEvaluator(Parameter parameter) { | |
164 | 0 | ParameterEvaluatorImpl parameterEvaluator = new ParameterEvaluatorImpl(); |
165 | 0 | parameterEvaluator.setParameter(parameter); |
166 | 0 | parameterEvaluator.setConstraintIsAllow(constraintIsAllow(parameter)); |
167 | 0 | parameterEvaluator.setValues(getParameterValues(parameter)); |
168 | 0 | return parameterEvaluator; |
169 | } | |
170 | ||
171 | protected ParameterEvaluatorImpl getParameterEvaluator(Parameter parameter, String constrainedValue) { | |
172 | 0 | ParameterEvaluatorImpl parameterEvaluator = getParameterEvaluator(parameter); |
173 | 0 | parameterEvaluator.setConstrainedValue(constrainedValue); |
174 | 0 | return parameterEvaluator; |
175 | } | |
176 | ||
177 | protected ParameterEvaluatorImpl getParameterEvaluator(Parameter parameter, String constrainingValue, | |
178 | String constrainedValue) { | |
179 | 0 | ParameterEvaluatorImpl parameterEvaluator = getParameterEvaluator(parameter, constrainedValue); |
180 | 0 | parameterEvaluator.setValues(getParameterValues(parameter, constrainingValue)); |
181 | 0 | return parameterEvaluator; |
182 | } | |
183 | ||
184 | private boolean constraintIsAllow(Parameter parameter) { | |
185 | 0 | return EvaluationOperator.ALLOW.equals(parameter.getEvaluationOperator()); |
186 | } | |
187 | } |