|  1 |     | 
   package org.kuali.rice.krms.impl.repository;  | 
  |  2 |     | 
     | 
  |  3 |     | 
   import java.util.ArrayList;  | 
  |  4 |     | 
   import java.util.Collections;  | 
  |  5 |     | 
   import java.util.HashMap;  | 
  |  6 |     | 
   import java.util.List;  | 
  |  7 |     | 
   import java.util.Map;  | 
  |  8 |     | 
     | 
  |  9 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  10 |     | 
   import org.kuali.rice.krad.service.BusinessObjectService;  | 
  |  11 |     | 
   import org.kuali.rice.krms.api.repository.function.FunctionDefinition;  | 
  |  12 |     | 
   import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService;  | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
     | 
  |  18 |     | 
     | 
  |  19 |     | 
     | 
  |  20 |    17 |    public class FunctionBoServiceImpl implements FunctionRepositoryService, FunctionBoService { | 
  |  21 |     | 
                     | 
  |  22 |     | 
       private BusinessObjectService businessObjectService;  | 
  |  23 |     | 
         | 
  |  24 |     | 
           @Override  | 
  |  25 |     | 
           public FunctionDefinition getFunction(String functionId) { | 
  |  26 |    1 |                    return getFunctionById(functionId);  | 
  |  27 |     | 
           }  | 
  |  28 |     | 
             | 
  |  29 |     | 
           @Override  | 
  |  30 |     | 
           public List<FunctionDefinition> getFunctions(List<String> functionIds) { | 
  |  31 |     | 
                     | 
  |  32 |    1 |                    List<FunctionDefinition> functionDefinitions = new ArrayList<FunctionDefinition>();  | 
  |  33 |    1 |                    for (String functionId : functionIds){ | 
  |  34 |    2 |                            functionDefinitions.add( getFunctionById(functionId) );  | 
  |  35 |     | 
                   }  | 
  |  36 |    1 |            return Collections.unmodifiableList(functionDefinitions);  | 
  |  37 |     | 
           }  | 
  |  38 |     | 
             | 
  |  39 |     | 
             | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
     | 
  |  43 |     | 
     | 
  |  44 |     | 
     | 
  |  45 |     | 
           @Override  | 
  |  46 |     | 
           public FunctionDefinition createFunction(FunctionDefinition function) { | 
  |  47 |    3 |                    if (function == null){ | 
  |  48 |    1 |                    throw new IllegalArgumentException("function is null"); | 
  |  49 |     | 
                   }  | 
  |  50 |     | 
                     | 
  |  51 |    2 |                    final String nameKey = function.getName();  | 
  |  52 |    2 |                    final String namespaceKey = function.getNamespace();  | 
  |  53 |    2 |                    final FunctionDefinition existing = getFunctionByNameAndNamespace(nameKey, namespaceKey);  | 
  |  54 |    2 |                    if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){ | 
  |  55 |    1 |                throw new IllegalStateException("the function to create already exists: " + function);                         | 
  |  56 |     | 
                   }  | 
  |  57 |     | 
                     | 
  |  58 |    1 |                    FunctionBo functionBo = FunctionBo.from(function);  | 
  |  59 |    1 |                    businessObjectService.save(functionBo);  | 
  |  60 |    1 |                    return FunctionBo.to(functionBo);  | 
  |  61 |     | 
           }  | 
  |  62 |     | 
     | 
  |  63 |     | 
             | 
  |  64 |     | 
     | 
  |  65 |     | 
     | 
  |  66 |     | 
     | 
  |  67 |     | 
     | 
  |  68 |     | 
           @Override  | 
  |  69 |     | 
           public void updateFunction(FunctionDefinition function) { | 
  |  70 |    3 |                    if (function == null){ | 
  |  71 |    1 |                            throw new IllegalArgumentException("function is null"); | 
  |  72 |     | 
                   }  | 
  |  73 |     | 
                     | 
  |  74 |    2 |                    final String functionIdKey = function.getId();  | 
  |  75 |    2 |                    final FunctionDefinition existing = getFunctionById(functionIdKey);  | 
  |  76 |    2 |                    if (existing == null) { | 
  |  77 |    1 |                            throw new IllegalStateException("the function does not exist: " + function); | 
  |  78 |     | 
                   }  | 
  |  79 |     | 
                   final FunctionDefinition toUpdate;  | 
  |  80 |    1 |                    if (!existing.getId().equals(function.getId())){ | 
  |  81 |    0 |                            final FunctionDefinition.Builder builder = FunctionDefinition.Builder.create(function);  | 
  |  82 |    0 |                            builder.setId(existing.getId());  | 
  |  83 |    0 |                            toUpdate = builder.build();  | 
  |  84 |    0 |                    } else { | 
  |  85 |    1 |                            toUpdate = function;  | 
  |  86 |     | 
                   }  | 
  |  87 |     | 
     | 
  |  88 |    1 |                    businessObjectService.save(FunctionBo.from(toUpdate));  | 
  |  89 |    1 |            }  | 
  |  90 |     | 
             | 
  |  91 |     | 
         | 
  |  92 |     | 
             | 
  |  93 |     | 
     | 
  |  94 |     | 
     | 
  |  95 |     | 
     | 
  |  96 |     | 
     | 
  |  97 |     | 
           @Override  | 
  |  98 |     | 
           public FunctionDefinition getFunctionById(String functionId) { | 
  |  99 |    10 |                    if (StringUtils.isBlank(functionId)){ | 
  |  100 |    1 |                throw new IllegalArgumentException("functionId is null or blank");                         | 
  |  101 |     | 
                   }  | 
  |  102 |    9 |                    FunctionBo functionBo = businessObjectService.findBySinglePrimaryKey(FunctionBo.class, functionId);  | 
  |  103 |    9 |                    return FunctionBo.to(functionBo);  | 
  |  104 |     | 
           }  | 
  |  105 |     | 
             | 
  |  106 |     | 
             | 
  |  107 |     | 
     | 
  |  108 |     | 
     | 
  |  109 |     | 
     | 
  |  110 |     | 
     | 
  |  111 |     | 
     | 
  |  112 |     | 
           public FunctionDefinition getFunctionByNameAndNamespace( String name, String namespace ){ | 
  |  113 |    8 |                    if (StringUtils.isBlank(name)){ | 
  |  114 |    2 |                            throw new IllegalArgumentException("name is null or blank"); | 
  |  115 |     | 
                   }  | 
  |  116 |    6 |                    if (StringUtils.isBlank(namespace)){ | 
  |  117 |    2 |                            throw new IllegalArgumentException("namespace is null or blank"); | 
  |  118 |     | 
                   }  | 
  |  119 |     | 
                                     | 
  |  120 |    4 |            final Map<String, Object> map = new HashMap<String, Object>();  | 
  |  121 |    4 |            map.put("name", name); | 
  |  122 |    4 |            map.put("namespace", namespace); | 
  |  123 |    4 |                    FunctionBo functionBo = businessObjectService.findByPrimaryKey(FunctionBo.class, Collections.unmodifiableMap(map));  | 
  |  124 |    4 |                    return FunctionBo.to(functionBo);  | 
  |  125 |     | 
           }  | 
  |  126 |     | 
             | 
  |  127 |     | 
       public void setBusinessObjectService(BusinessObjectService businessObjectService) { | 
  |  128 |    17 |            this.businessObjectService = businessObjectService;  | 
  |  129 |    17 |        }  | 
  |  130 |     | 
   }  |