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