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