1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.impl.parameter; |
18 | |
|
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.core.api.criteria.CriteriaLookupService; |
21 | |
import org.kuali.rice.core.api.criteria.GenericQueryResults; |
22 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
23 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
24 | |
import org.kuali.rice.core.api.exception.RiceIllegalStateException; |
25 | |
import org.kuali.rice.core.api.parameter.Parameter; |
26 | |
import org.kuali.rice.core.api.parameter.ParameterKey; |
27 | |
import org.kuali.rice.core.api.parameter.ParameterQueryResults; |
28 | |
import org.kuali.rice.core.api.parameter.ParameterRepositoryService; |
29 | |
import org.kuali.rice.core.api.util.Truth; |
30 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
31 | |
import org.kuali.rice.krad.util.KRADConstants; |
32 | |
|
33 | |
import java.util.ArrayList; |
34 | |
import java.util.Collection; |
35 | |
import java.util.Collections; |
36 | |
import java.util.HashMap; |
37 | |
import java.util.List; |
38 | |
import java.util.Map; |
39 | |
|
40 | 32 | public final class ParameterRepositoryServiceImpl implements ParameterRepositoryService { |
41 | |
private static final String SUB_PARAM_SEPARATOR = "="; |
42 | |
|
43 | |
private BusinessObjectService businessObjectService; |
44 | |
private CriteriaLookupService criteriaLookupService; |
45 | |
|
46 | |
@Override |
47 | |
public Parameter createParameter(Parameter parameter) { |
48 | 3 | if (parameter == null) { |
49 | 1 | throw new RiceIllegalArgumentException("parameter is null"); |
50 | |
} |
51 | |
|
52 | 2 | final ParameterKey key = ParameterKey.create(parameter.getApplicationId(), parameter.getNamespaceCode(), parameter.getComponentCode(), parameter.getName()); |
53 | 2 | final Parameter existing = getParameter(key); |
54 | 2 | if (existing != null && existing.getApplicationId().equals(parameter.getApplicationId())) { |
55 | 1 | throw new RiceIllegalStateException("the parameter to create already exists: " + parameter); |
56 | |
} |
57 | |
|
58 | 1 | return ParameterBo.to(businessObjectService.save(ParameterBo.from(parameter))); |
59 | |
} |
60 | |
|
61 | |
@Override |
62 | |
public Parameter updateParameter(Parameter parameter) { |
63 | 3 | if (parameter == null) { |
64 | 1 | throw new RiceIllegalArgumentException("parameter is null"); |
65 | |
} |
66 | |
|
67 | 2 | final ParameterKey key = ParameterKey.create(parameter.getApplicationId(), parameter.getNamespaceCode(), parameter.getComponentCode(), parameter.getName()); |
68 | 2 | final Parameter existing = getParameter(key); |
69 | 2 | if (existing == null) { |
70 | 1 | throw new RiceIllegalStateException("the parameter does not exist: " + parameter); |
71 | |
} |
72 | |
|
73 | |
final Parameter toUpdate; |
74 | 1 | if (!existing.getApplicationId().equals(parameter.getApplicationId())) { |
75 | 0 | final Parameter.Builder builder = Parameter.Builder.create(parameter); |
76 | 0 | builder.setApplicationId(existing.getApplicationId()); |
77 | 0 | toUpdate = builder.build(); |
78 | 0 | } else { |
79 | 1 | toUpdate = parameter; |
80 | |
} |
81 | |
|
82 | 1 | return ParameterBo.to(businessObjectService.save(ParameterBo.from(toUpdate))); |
83 | |
} |
84 | |
|
85 | |
@Override |
86 | |
public Parameter getParameter(ParameterKey key) { |
87 | 24 | if (key == null) { |
88 | 1 | throw new RiceIllegalArgumentException("key is null"); |
89 | |
} |
90 | |
|
91 | 23 | final Map<String, Object> map = new HashMap<String, Object>(); |
92 | 23 | map.put("name", key.getName()); |
93 | 23 | map.put("applicationId", key.getApplicationId()); |
94 | 23 | map.put("namespaceCode", key.getNamespaceCode()); |
95 | 23 | map.put("componentCode", key.getComponentCode()); |
96 | 23 | ParameterBo bo = businessObjectService.findByPrimaryKey(ParameterBo.class, Collections.unmodifiableMap(map)); |
97 | |
|
98 | 23 | if (bo == null & !KRADConstants.DEFAULT_PARAMETER_APPLICATION_ID.equals(key.getApplicationId())) { |
99 | 8 | map.put("applicationId", KRADConstants.DEFAULT_PARAMETER_APPLICATION_ID); |
100 | 8 | bo = businessObjectService.findByPrimaryKey(ParameterBo.class, Collections.unmodifiableMap(map)); |
101 | |
} |
102 | |
|
103 | 23 | return ParameterBo.to(bo); |
104 | |
} |
105 | |
|
106 | |
@Override |
107 | |
public String getParameterValueAsString(ParameterKey key) { |
108 | 11 | final Parameter p = getParameter(key); |
109 | 11 | return p != null ? p.getValue() : null; |
110 | |
} |
111 | |
|
112 | |
@Override |
113 | |
public Boolean getParameterValueAsBoolean(ParameterKey key) { |
114 | 6 | final Parameter p = getParameter(key); |
115 | 6 | final String value = p != null ? p.getValue() : null; |
116 | 6 | return Truth.strToBooleanIgnoreCase(value); |
117 | |
} |
118 | |
|
119 | |
@Override |
120 | |
public Collection<String> getParameterValuesAsString(ParameterKey key) { |
121 | 9 | return splitOn(getParameterValueAsString(key), ";"); |
122 | |
} |
123 | |
|
124 | |
@Override |
125 | |
public String getSubParameterValueAsString(ParameterKey key, String subParameterName) { |
126 | 12 | if (StringUtils.isBlank(subParameterName)) { |
127 | 6 | throw new RiceIllegalArgumentException("subParameterName is blank"); |
128 | |
} |
129 | |
|
130 | 6 | Collection<String> values = getParameterValuesAsString(key); |
131 | 6 | return getSubParameter(values, subParameterName); |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
public Collection<String> getSubParameterValuesAsString(ParameterKey key, String subParameterName) { |
136 | 6 | return splitOn(getSubParameterValueAsString(key, subParameterName), ","); |
137 | |
} |
138 | |
|
139 | |
private String getSubParameter(Collection<String> values, String subParameterName) { |
140 | 6 | for (String value : values) { |
141 | 4 | if (subParameterName.equals(StringUtils.substringBefore(value, SUB_PARAM_SEPARATOR))) { |
142 | 4 | return StringUtils.trimToNull(StringUtils.substringAfter(value, SUB_PARAM_SEPARATOR)); |
143 | |
} |
144 | |
} |
145 | 2 | return null; |
146 | |
} |
147 | |
|
148 | |
private Collection<String> splitOn(String strValues, String delim) { |
149 | 12 | if (StringUtils.isEmpty(delim)) { |
150 | 0 | throw new RiceIllegalArgumentException("delim is empty"); |
151 | |
} |
152 | |
|
153 | 12 | if (strValues == null || StringUtils.isBlank(strValues)) { |
154 | 4 | return Collections.emptyList(); |
155 | |
} |
156 | |
|
157 | 8 | final Collection<String> values = new ArrayList<String>(); |
158 | 30 | for (String value : strValues.split(delim)) { |
159 | 22 | values.add(value.trim()); |
160 | |
} |
161 | |
|
162 | 8 | return Collections.unmodifiableCollection(values); |
163 | |
} |
164 | |
|
165 | |
@Override |
166 | |
public ParameterQueryResults findParameters(QueryByCriteria queryByCriteria) { |
167 | 0 | if (queryByCriteria == null) { |
168 | 0 | throw new IllegalArgumentException("queryByCriteria is null"); |
169 | |
} |
170 | |
|
171 | 0 | GenericQueryResults<ParameterBo> results = criteriaLookupService.lookup(ParameterBo.class, queryByCriteria); |
172 | |
|
173 | 0 | ParameterQueryResults.Builder builder = ParameterQueryResults.Builder.create(); |
174 | 0 | builder.setMoreResultsAvailable(results.isMoreResultsAvailable()); |
175 | 0 | builder.setTotalRowCount(results.getTotalRowCount()); |
176 | |
|
177 | 0 | final List<Parameter> ims = new ArrayList<Parameter>(); |
178 | 0 | for (ParameterBo bo : results.getResults()) { |
179 | 0 | ims.add(ParameterBo.to(bo)); |
180 | |
} |
181 | |
|
182 | 0 | builder.setResults(ims); |
183 | 0 | return builder.build(); |
184 | |
} |
185 | |
|
186 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
187 | 26 | this.businessObjectService = businessObjectService; |
188 | 26 | } |
189 | |
|
190 | |
public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) { |
191 | 0 | this.criteriaLookupService = criteriaLookupService; |
192 | 0 | } |
193 | |
} |