Coverage Report - org.kuali.rice.krms.impl.repository.FunctionBoServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
FunctionBoServiceImpl
0%
0/45
0%
0/22
3.571
 
 1  
 /**
 2  
  * Copyright 2005-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  
 package org.kuali.rice.krms.impl.repository;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.HashMap;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.kuali.rice.krad.service.BusinessObjectService;
 26  
 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
 27  
 import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService;
 28  
 
 29  
 /**
 30  
  * Default implementation of the {@link FunctionService}.
 31  
  * 
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 33  
  *
 34  
  */
 35  0
 public class FunctionBoServiceImpl implements FunctionRepositoryService, FunctionBoService {
 36  
                 
 37  
     private BusinessObjectService businessObjectService;
 38  
     
 39  
         @Override
 40  
         public FunctionDefinition getFunction(String functionId) {
 41  0
                 return getFunctionById(functionId);
 42  
         }
 43  
         
 44  
         @Override
 45  
         public List<FunctionDefinition> getFunctions(List<String> functionIds) {
 46  
                 
 47  0
                 List<FunctionDefinition> functionDefinitions = new ArrayList<FunctionDefinition>();
 48  0
                 for (String functionId : functionIds){
 49  0
                         functionDefinitions.add( getFunctionById(functionId) );
 50  
                 }
 51  0
         return Collections.unmodifiableList(functionDefinitions);
 52  
         }
 53  
         
 54  
         /**
 55  
          * This method will create a {@link FunctionDefintion} as described
 56  
          * by the function passed in.
 57  
          * 
 58  
          * @see org.kuali.rice.krms.impl.repository.FunctionBoService#createFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
 59  
          */
 60  
         @Override
 61  
         public FunctionDefinition createFunction(FunctionDefinition function) {
 62  0
                 if (function == null){
 63  0
                 throw new IllegalArgumentException("function is null");
 64  
                 }
 65  
                 
 66  0
                 final String nameKey = function.getName();
 67  0
                 final String namespaceKey = function.getNamespace();
 68  0
                 final FunctionDefinition existing = getFunctionByNameAndNamespace(nameKey, namespaceKey);
 69  0
                 if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
 70  0
             throw new IllegalStateException("the function to create already exists: " + function);                        
 71  
                 }
 72  
                 
 73  0
                 FunctionBo functionBo = FunctionBo.from(function);
 74  0
                 businessObjectService.save(functionBo);
 75  0
                 return FunctionBo.to(functionBo);
 76  
         }
 77  
 
 78  
         /**
 79  
          * This overridden method updates an existing Function in the repository
 80  
          * 
 81  
          * @see org.kuali.rice.krms.impl.repository.FunctionBoService#updateFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
 82  
          */
 83  
         @Override
 84  
         public void updateFunction(FunctionDefinition function) {
 85  0
                 if (function == null){
 86  0
                         throw new IllegalArgumentException("function is null");
 87  
                 }
 88  
                 
 89  0
                 final String functionIdKey = function.getId();
 90  0
                 final FunctionDefinition existing = getFunctionById(functionIdKey);
 91  0
                 if (existing == null) {
 92  0
                         throw new IllegalStateException("the function does not exist: " + function);
 93  
                 }
 94  
                 final FunctionDefinition toUpdate;
 95  0
                 if (!existing.getId().equals(function.getId())){
 96  0
                         final FunctionDefinition.Builder builder = FunctionDefinition.Builder.create(function);
 97  0
                         builder.setId(existing.getId());
 98  0
                         toUpdate = builder.build();
 99  0
                 } else {
 100  0
                         toUpdate = function;
 101  
                 }
 102  
 
 103  0
                 businessObjectService.save(FunctionBo.from(toUpdate));
 104  0
         }
 105  
         
 106  
     
 107  
         /**
 108  
          * This overridden method retrieves a function by the given function id.
 109  
          * 
 110  
          * @see org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionById(java.lang.String)
 111  
          */
 112  
         @Override
 113  
         public FunctionDefinition getFunctionById(String functionId) {
 114  0
                 if (StringUtils.isBlank(functionId)){
 115  0
             throw new IllegalArgumentException("functionId is null or blank");                        
 116  
                 }
 117  0
                 FunctionBo functionBo = businessObjectService.findBySinglePrimaryKey(FunctionBo.class, functionId);
 118  0
                 return FunctionBo.to(functionBo);
 119  
         }
 120  
         
 121  
         /**
 122  
          * 
 123  
          * This overridden method retrieves a function by the given name and namespace.
 124  
          * 
 125  
          * @see org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionByNameAndNamespace(java.lang.String, java.lang.String)
 126  
          */
 127  
         public FunctionDefinition getFunctionByNameAndNamespace( String name, String namespace ){
 128  0
                 if (StringUtils.isBlank(name)){
 129  0
                         throw new IllegalArgumentException("name is null or blank");
 130  
                 }
 131  0
                 if (StringUtils.isBlank(namespace)){
 132  0
                         throw new IllegalArgumentException("namespace is null or blank");
 133  
                 }
 134  
                                 
 135  0
         final Map<String, Object> map = new HashMap<String, Object>();
 136  0
         map.put("name", name);
 137  0
         map.put("namespace", namespace);
 138  0
                 FunctionBo functionBo = businessObjectService.findByPrimaryKey(FunctionBo.class, Collections.unmodifiableMap(map));
 139  0
                 return FunctionBo.to(functionBo);
 140  
         }
 141  
         
 142  
     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
 143  0
         this.businessObjectService = businessObjectService;
 144  0
     }
 145  
 }