View Javadoc
1   /**
2    * Copyright 2005-2014 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.coreservice.impl.parameter;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.text.StrSubstitutor;
20  import org.kuali.rice.core.api.config.property.ConfigStrLookup;
21  import org.kuali.rice.coreservice.api.parameter.Parameter;
22  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
23  import org.kuali.rice.coreservice.api.parameter.ParameterRepositoryService;
24  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
25  import org.kuali.rice.krad.service.KualiModuleService;
26  import org.kuali.rice.krad.util.KRADConstants;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  
31  /**
32   * Default implementation of {@link ParameterService}.
33   */
34  public class ParameterServiceImpl implements ParameterService {
35  
36      private static final StrSubstitutor CONFIG_SUBSTITUTOR = new StrSubstitutor(new ConfigStrLookup());
37  
38      private KualiModuleService kualiModuleService;
39      private ParameterRepositoryService parameterRepositoryService;
40      private String applicationId = KRADConstants.DEFAULT_PARAMETER_APPLICATION_ID;
41  
42      /**
43       * {@inheritDoc}
44       */
45      @Override
46      public Parameter createParameter(Parameter parameter) {
47          return parameterRepositoryService.createParameter(parameter);
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public Parameter updateParameter(Parameter parameter) {
55          return parameterRepositoryService.updateParameter(parameter);
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public Parameter getParameter(String namespaceCode, String componentCode, String parameterName) {
63          return exec(new Fun<Parameter>() {
64              @Override public Parameter f(ParameterKey key) {
65                  return parameterRepositoryService.getParameter(key);
66              }
67          }, namespaceCode, componentCode, parameterName);
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public Parameter getParameter(Class<?> componentClass, String parameterName) {
75          return exec(new Fun<Parameter>() {
76              @Override public Parameter f(ParameterKey key) {
77                  return parameterRepositoryService.getParameter(key);
78              }
79          }, componentClass, parameterName);
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public Boolean parameterExists(String namespaceCode, String componentCode, String parameterName) {
87          return exec(new Fun<Boolean>() {
88              @Override
89              public Boolean f(ParameterKey key) {
90                  return Boolean.valueOf(parameterRepositoryService.getParameter(key) != null);
91              }
92          }, namespaceCode, componentCode, parameterName);
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public Boolean parameterExists(Class<?> componentClass, String parameterName) {
100         return exec(new Fun<Boolean>() {
101             @Override
102             public Boolean f(ParameterKey key) {
103                 return Boolean.valueOf(parameterRepositoryService.getParameter(key) != null);
104             }
105         }, componentClass, parameterName);
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public Boolean getParameterValueAsBoolean(String namespaceCode, String componentCode, String parameterName) {
113         return exec(new Fun<Boolean>() {
114             @Override
115             public Boolean f(ParameterKey key) {
116                 return parameterRepositoryService.getParameterValueAsBoolean(key);
117             }
118         }, namespaceCode, componentCode, parameterName);
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public Boolean getParameterValueAsBoolean(String namespaceCode, String componentCode, String parameterName, Boolean defaultValue) {
126         final Boolean value = getParameterValueAsBoolean(namespaceCode, componentCode, parameterName);
127         return (value != null) ? value : defaultValue;
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public Boolean getParameterValueAsBoolean(Class<?> componentClass, String parameterName) {
135         return exec(new Fun<Boolean>() {
136             @Override
137             public Boolean f(ParameterKey key) {
138                 return parameterRepositoryService.getParameterValueAsBoolean(key);
139             }
140         }, componentClass, parameterName);
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public Boolean getParameterValueAsBoolean(Class<?> componentClass, String parameterName, Boolean defaultValue) {
148         final Boolean value = getParameterValueAsBoolean(componentClass, parameterName);
149         return (value != null) ? value : defaultValue;
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     public String getParameterValueAsString(String namespaceCode, String componentCode, String parameterName) {
157         return exec(new Fun<String>() {
158             @Override
159             public String f(ParameterKey key) {
160                 return parameterRepositoryService.getParameterValueAsString(key);
161             }
162         }, namespaceCode, componentCode, parameterName);
163     }
164 
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     public String getParameterValueAsString(String namespaceCode, String componentCode, String parameterName, String defaultValue) {
170         final String value = getParameterValueAsString(namespaceCode, componentCode, parameterName);
171         return (value != null) ? value : defaultValue;
172     }
173 
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public String getParameterValueAsString(Class<?> componentClass, String parameterName) {
179         return exec(new Fun<String>() {
180             @Override public String f(ParameterKey key) {
181                 return parameterRepositoryService.getParameterValueAsString(key);
182             }
183         }, componentClass, parameterName);
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public String getParameterValueAsString(Class<?> componentClass, String parameterName, String defaultValue) {
191         final String value = getParameterValueAsString(componentClass, parameterName);
192         return (value != null) ? value : defaultValue;
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public String getParameterValueAsFilteredString(String namespaceCode, String componentCode, String parameterName) {
200         final String value = getParameterValueAsString(namespaceCode, componentCode, parameterName);
201         return CONFIG_SUBSTITUTOR.replace(value);
202     }
203 
204     /**
205      * {@inheritDoc}
206      */
207     @Override
208     public String getParameterValueAsFilteredString(String namespaceCode, String componentCode, String parameterName, String defaultValue) {
209         final String value = getParameterValueAsFilteredString(namespaceCode, componentCode, parameterName);
210         return (value != null) ? value : defaultValue;
211     }
212 
213     /**
214      * {@inheritDoc}
215      */
216     @Override
217     public String getParameterValueAsFilteredString(Class<?> componentClass, String parameterName) {
218         final String value = getParameterValueAsString(componentClass, parameterName);
219         return CONFIG_SUBSTITUTOR.replace(value);
220     }
221 
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public String getParameterValueAsFilteredString(Class<?> componentClass, String parameterName, String defaultValue) {
227         final String value = getParameterValueAsString(componentClass, parameterName);
228         return (value != null) ? value : defaultValue;
229     }
230 
231     /**
232      * {@inheritDoc}
233      */
234     @Override
235     public Collection<String> getParameterValuesAsString(String namespaceCode, String componentCode, String parameterName) {
236         return exec(new Fun<Collection<String>>() {
237             @Override public Collection<String> f(ParameterKey key) {
238                 return parameterRepositoryService.getParameterValuesAsString(key);
239             }
240         }, namespaceCode, componentCode, parameterName);
241     }
242 
243     /**
244      * {@inheritDoc}
245      */
246     @Override
247     public Collection<String> getParameterValuesAsString(Class<?> componentClass, String parameterName) {
248         return exec(new Fun<Collection<String>>() {
249             @Override public Collection<String> f(ParameterKey key) {
250                 return parameterRepositoryService.getParameterValuesAsString(key);
251             }
252         }, componentClass, parameterName);
253     }
254 
255     /**
256      * {@inheritDoc}
257      */
258     @Override
259     public Collection<String> getParameterValuesAsFilteredString(String namespaceCode, String componentCode, String parameterName) {
260         Collection<String> unfilteredValues = getParameterValuesAsString(namespaceCode, componentCode, parameterName);
261         Collection<String> filteredValues = new ArrayList<String>();
262 
263         for (String unfilteredValue : unfilteredValues) {
264             filteredValues.add(CONFIG_SUBSTITUTOR.replace(unfilteredValue));
265         }
266 
267         return filteredValues;
268     }
269 
270     /**
271      * {@inheritDoc}
272      */
273     @Override
274     public Collection<String> getParameterValuesAsFilteredString(Class<?> componentClass, String parameterName) {
275         Collection<String> unfilteredValues = getParameterValuesAsString(componentClass, parameterName);
276         Collection<String> filteredValues = new ArrayList<String>();
277 
278         for (String unfilteredValue : unfilteredValues) {
279             filteredValues.add(CONFIG_SUBSTITUTOR.replace(unfilteredValue));
280         }
281 
282         return filteredValues;
283     }
284 
285     /**
286      * {@inheritDoc}
287      */
288     @Override
289     public String getSubParameterValueAsString(String namespaceCode, String componentCode, String parameterName, final String subParameterName) {
290         return exec(new Fun<String>() {
291             @Override public String f(ParameterKey key) {
292                 return parameterRepositoryService.getSubParameterValueAsString(key, subParameterName);
293             }
294         }, namespaceCode, componentCode, parameterName);
295     }
296 
297     /**
298      * {@inheritDoc}
299      */
300     @Override
301     public String getSubParameterValueAsString(Class<?> componentClass, String parameterName, final String subParameterName) {
302         return exec(new Fun<String>() {
303             @Override public String f(ParameterKey key) {
304                 return parameterRepositoryService.getSubParameterValueAsString(key, subParameterName);
305             }
306         }, componentClass, parameterName);
307     }
308 
309     /**
310      * {@inheritDoc}
311      */
312     @Override
313     public String getSubParameterValueAsFilteredString(String namespaceCode, String componentCode, String parameterName, String subParameterName) {
314         final String value = getSubParameterValueAsString(namespaceCode, componentCode, parameterName, subParameterName);
315         return CONFIG_SUBSTITUTOR.replace(value);
316     }
317 
318     /**
319      * {@inheritDoc}
320      */
321     @Override
322     public String getSubParameterValueAsFilteredString(Class<?> componentClass, String parameterName, String subParameterName) {
323         final String value = getSubParameterValueAsString(componentClass, parameterName, subParameterName);
324         return CONFIG_SUBSTITUTOR.replace(value);
325     }
326 
327     /**
328      * {@inheritDoc}
329      */
330     @Override
331     public Collection<String> getSubParameterValuesAsString(String namespaceCode, String componentCode, String parameterName, final String subParameterName) {
332         return exec(new Fun<Collection<String>>() {
333             @Override public Collection<String> f(ParameterKey key) {
334                 return parameterRepositoryService.getSubParameterValuesAsString(key, subParameterName);
335             }
336         }, namespaceCode, componentCode, parameterName);
337     }
338 
339     /**
340      * {@inheritDoc}
341      */
342     @Override
343     public Collection<String> getSubParameterValuesAsString(Class<?> componentClass, String parameterName, final String subParameterName) {
344         return exec(new Fun<Collection<String>>() {
345             @Override public Collection<String> f(ParameterKey key) {
346                 return parameterRepositoryService.getSubParameterValuesAsString(key, subParameterName);
347             }
348         }, componentClass, parameterName);
349     }
350 
351     /**
352      * {@inheritDoc}
353      */
354     @Override
355     public Collection<String> getSubParameterValuesAsFilteredString(String namespaceCode, String componentCode, String parameterName, String subParameterName) {
356         Collection<String> unfilteredValues = getSubParameterValuesAsFilteredString(namespaceCode, componentCode, parameterName, subParameterName);
357         Collection<String> filteredValues = new ArrayList<String>();
358 
359         for (String unfilteredValue : unfilteredValues) {
360             filteredValues.add(CONFIG_SUBSTITUTOR.replace(unfilteredValue));
361         }
362 
363         return filteredValues;
364     }
365 
366     /**
367      * {@inheritDoc}
368      */
369     @Override
370     public Collection<String> getSubParameterValuesAsFilteredString(Class<?> componentClass, String parameterName, String subParameterName) {
371         Collection<String> unfilteredValues = getSubParameterValuesAsFilteredString(componentClass, parameterName, subParameterName);
372         Collection<String> filteredValues = new ArrayList<String>();
373 
374         for (String unfilteredValue : unfilteredValues) {
375             filteredValues.add(CONFIG_SUBSTITUTOR.replace(unfilteredValue));
376         }
377 
378         return filteredValues;
379     }
380 
381     public void setKualiModuleService(KualiModuleService kualiModuleService) {
382         this.kualiModuleService = kualiModuleService;
383     }
384 
385     public void setParameterRepositoryService(ParameterRepositoryService parameterRepositoryService) {
386         this.parameterRepositoryService = parameterRepositoryService;
387     }
388 
389     public void setApplicationId(String applicationId) {
390         this.applicationId = applicationId;
391     }
392 
393     //utilities that act as a poor-man's closure & higher order functions - these help consolidate validation & construction of parameter keys
394     private <R> R exec(Fun<R> fun, String namespaceCode, String componentCode, String parameterName) {
395         if (StringUtils.isBlank(applicationId)) {
396             throw new IllegalStateException("applicationId is blank - this service is not configured correctly");
397         }
398 
399         return fun.f(ParameterKey.create(applicationId, namespaceCode, componentCode, parameterName));
400     }
401 
402     private <R> R exec(Fun<R> fun, Class<?> componentClass, String parameterName) {
403         return exec(fun, kualiModuleService.getNamespaceCode(componentClass), kualiModuleService.getComponentCode(componentClass), parameterName);
404     }
405 
406     private interface Fun<R> {
407         R f(ParameterKey key);
408     }
409 }