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