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