View Javadoc

1   /**
2    * Copyright 2005-2013 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.kuali.rice.coreservice.api.parameter.Parameter;
20  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
21  import org.kuali.rice.coreservice.api.parameter.ParameterRepositoryService;
22  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
23  import org.kuali.rice.krad.service.KualiModuleService;
24  import org.kuali.rice.krad.util.KRADConstants;
25  
26  import java.util.Collection;
27  
28  public class ParameterServiceImpl implements ParameterService {
29      private KualiModuleService kualiModuleService;
30      private ParameterRepositoryService parameterRepositoryService;
31      private String applicationId = KRADConstants.DEFAULT_PARAMETER_APPLICATION_ID;
32  
33      @Override
34      public Parameter createParameter(Parameter parameter) {
35          return parameterRepositoryService.createParameter(parameter);
36      }
37  
38      @Override
39      public Parameter updateParameter(Parameter parameter) {
40          return parameterRepositoryService.updateParameter(parameter);
41      }
42  
43      @Override
44      public Parameter getParameter(String namespaceCode, String componentCode, String parameterName) {
45          return exec(new Fun<Parameter>() {
46              @Override public Parameter f(ParameterKey key) {
47                  return parameterRepositoryService.getParameter(key);
48              }
49          }, namespaceCode, componentCode, parameterName);
50      }
51  
52      @Override
53      public Parameter getParameter(Class<?> componentClass, String parameterName) {
54          return exec(new Fun<Parameter>() {
55              @Override public Parameter f(ParameterKey key) {
56                  return parameterRepositoryService.getParameter(key);
57              }
58          }, componentClass, parameterName);
59      }
60  
61      @Override
62      public Boolean parameterExists(String namespaceCode, String componentCode, String parameterName) {
63          return exec(new Fun<Boolean>() {
64              @Override
65              public Boolean f(ParameterKey key) {
66                  return Boolean.valueOf(parameterRepositoryService.getParameter(key) != null);
67              }
68          }, namespaceCode, componentCode, parameterName);
69      }
70  
71      @Override
72      public Boolean parameterExists(Class<?> componentClass, String parameterName) {
73          return exec(new Fun<Boolean>() {
74              @Override
75              public Boolean f(ParameterKey key) {
76                  return Boolean.valueOf(parameterRepositoryService.getParameter(key) != null);
77              }
78          }, componentClass, parameterName);
79      }
80  
81      @Override
82      public Boolean getParameterValueAsBoolean(String namespaceCode, String componentCode, String parameterName) {
83          return exec(new Fun<Boolean>() {
84              @Override
85              public Boolean f(ParameterKey key) {
86                  return parameterRepositoryService.getParameterValueAsBoolean(key);
87              }
88          }, namespaceCode, componentCode, parameterName);
89      }
90  
91      @Override
92      public Boolean getParameterValueAsBoolean(String namespaceCode, String componentCode, String parameterName, Boolean defaultValue) {
93          final Boolean value = getParameterValueAsBoolean(namespaceCode, componentCode, parameterName);
94          return (value != null) ? value : defaultValue;
95      }
96  
97      @Override
98      public Boolean getParameterValueAsBoolean(Class<?> componentClass, String parameterName) {
99          return exec(new Fun<Boolean>() {
100             @Override
101             public Boolean f(ParameterKey key) {
102                 return parameterRepositoryService.getParameterValueAsBoolean(key);
103             }
104         }, componentClass, parameterName);
105     }
106 
107     @Override
108     public Boolean getParameterValueAsBoolean(Class<?> componentClass, String parameterName, Boolean defaultValue) {
109         final Boolean value = getParameterValueAsBoolean(componentClass, parameterName);
110         return (value != null) ? value : defaultValue;
111     }
112 
113     @Override
114     public String getParameterValueAsString(String namespaceCode, String componentCode, String parameterName) {
115         return exec(new Fun<String>() {
116             @Override
117             public String f(ParameterKey key) {
118                 return parameterRepositoryService.getParameterValueAsString(key);
119             }
120         }, namespaceCode, componentCode, parameterName);
121     }
122 
123     @Override
124     public String getParameterValueAsString(String namespaceCode, String componentCode, String parameterName, String defaultValue) {
125         final String value = getParameterValueAsString(namespaceCode, componentCode, parameterName);
126         return (value != null) ? value : defaultValue;
127     }
128 
129     @Override
130     public String getParameterValueAsString(Class<?> componentClass, String parameterName) {
131         return exec(new Fun<String>() {
132             @Override public String f(ParameterKey key) {
133                 return parameterRepositoryService.getParameterValueAsString(key);
134             }
135         }, componentClass, parameterName);
136     }
137 
138     @Override
139     public String getParameterValueAsString(Class<?> componentClass, String parameterName, String defaultValue) {
140         final String value = getParameterValueAsString(componentClass, parameterName);
141         return (value != null) ? value : defaultValue;
142     }
143 
144     @Override
145     public Collection<String> getParameterValuesAsString(String namespaceCode, String componentCode, String parameterName) {
146         return exec(new Fun<Collection<String>>() {
147             @Override public Collection<String> f(ParameterKey key) {
148                 return parameterRepositoryService.getParameterValuesAsString(key);
149             }
150         }, namespaceCode, componentCode, parameterName);
151     }
152 
153     @Override
154     public Collection<String> getParameterValuesAsString(Class<?> componentClass, String parameterName) {
155         return exec(new Fun<Collection<String>>() {
156             @Override public Collection<String> f(ParameterKey key) {
157                 return parameterRepositoryService.getParameterValuesAsString(key);
158             }
159         }, componentClass, parameterName);
160     }
161 
162     @Override
163     public Collection<String> getSubParameterValuesAsString(String namespaceCode, String componentCode, String parameterName, final String constrainingValue) {
164         return exec(new Fun<Collection<String>>() {
165             @Override public Collection<String> f(ParameterKey key) {
166                 return parameterRepositoryService.getSubParameterValuesAsString(key, constrainingValue);
167             }
168         }, namespaceCode, componentCode, parameterName);
169     }
170 
171     @Override
172     public Collection<String> getSubParameterValuesAsString(Class<?> componentClass, String parameterName, final String constrainingValue) {
173         return exec(new Fun<Collection<String>>() {
174             @Override public Collection<String> f(ParameterKey key) {
175                 return parameterRepositoryService.getSubParameterValuesAsString(key, constrainingValue);
176             }
177         }, componentClass, parameterName);
178     }
179 
180     @Override
181     public String getSubParameterValueAsString(String namespaceCode, String componentCode, String parameterName, final String constrainingValue) {
182         return exec(new Fun<String>() {
183             @Override public String f(ParameterKey key) {
184                return parameterRepositoryService.getSubParameterValueAsString(key, constrainingValue);
185             }
186         }, namespaceCode, componentCode, parameterName);
187     }
188 
189     @Override
190     public String getSubParameterValueAsString(Class<?> componentClass, String parameterName, final String constrainingValue) {
191         return exec(new Fun<String>() {
192             @Override public String f(ParameterKey key) {
193                return parameterRepositoryService.getSubParameterValueAsString(key, constrainingValue);
194             }
195         }, componentClass, parameterName);
196     }
197 
198     public void setKualiModuleService(KualiModuleService kualiModuleService) {
199         this.kualiModuleService = kualiModuleService;
200     }
201 
202     public void setParameterRepositoryService(ParameterRepositoryService parameterRepositoryService) {
203         this.parameterRepositoryService = parameterRepositoryService;
204     }
205 
206     public void setApplicationId(String applicationId) {
207         this.applicationId = applicationId;
208     }
209 
210     //utilities that act as a poor-man's closure & higher order functions - these help consolidate validation & construction of parameter keys
211     private <R> R exec(Fun<R> fun, String namespaceCode, String componentCode, String parameterName) {
212         if (StringUtils.isBlank(applicationId)) {
213             throw new IllegalStateException("applicationId is blank - this service is not configured correctly");
214         }
215 
216         return fun.f(ParameterKey.create(applicationId, namespaceCode, componentCode, parameterName));
217     }
218 
219     private <R> R exec(Fun<R> fun, Class<?> componentClass, String parameterName) {
220         return exec(fun, kualiModuleService.getNamespaceCode(componentClass), kualiModuleService.getComponentCode(componentClass), parameterName);
221     }
222 
223     private interface Fun<R> {
224         R f(ParameterKey key);
225     }
226 }