/**
  * This overridden method retrieves a function by the given function id.
  *
  * @see org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionById(java.lang.String)
  */
 @Override
 public FunctionDefinition getFunctionById(String functionId) {
   if (StringUtils.isBlank(functionId)) {
     throw new IllegalArgumentException("functionId is null or blank");
   }
   FunctionBo functionBo =
       businessObjectService.findBySinglePrimaryKey(FunctionBo.class, functionId);
   return FunctionBo.to(functionBo);
 }
  /**
   * This method will create a {@link FunctionDefintion} as described by the function passed in.
   *
   * @see
   *     org.kuali.rice.krms.impl.repository.FunctionBoService#createFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
   */
  @Override
  public FunctionDefinition createFunction(FunctionDefinition function) {
    if (function == null) {
      throw new IllegalArgumentException("function is null");
    }

    final String nameKey = function.getName();
    final String namespaceKey = function.getNamespace();
    final FunctionDefinition existing = getFunctionByNameAndNamespace(nameKey, namespaceKey);
    if (existing != null
        && existing.getName().equals(nameKey)
        && existing.getNamespace().equals(namespaceKey)) {
      throw new IllegalStateException("the function to create already exists: " + function);
    }

    FunctionBo functionBo = FunctionBo.from(function);
    businessObjectService.save(functionBo);
    return FunctionBo.to(functionBo);
  }
  /**
   * This overridden method retrieves a function by the given name and namespace.
   *
   * @see
   *     org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionByNameAndNamespace(java.lang.String,
   *     java.lang.String)
   */
  public FunctionDefinition getFunctionByNameAndNamespace(String name, String namespace) {
    if (StringUtils.isBlank(name)) {
      throw new IllegalArgumentException("name is null or blank");
    }
    if (StringUtils.isBlank(namespace)) {
      throw new IllegalArgumentException("namespace is null or blank");
    }

    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", name);
    map.put("namespace", namespace);
    FunctionBo functionBo =
        businessObjectService.findByPrimaryKey(FunctionBo.class, Collections.unmodifiableMap(map));
    return FunctionBo.to(functionBo);
  }
  /**
   * This overridden method updates an existing Function in the repository
   *
   * @see
   *     org.kuali.rice.krms.impl.repository.FunctionBoService#updateFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
   */
  @Override
  public void updateFunction(FunctionDefinition function) {
    if (function == null) {
      throw new IllegalArgumentException("function is null");
    }

    final String functionIdKey = function.getId();
    final FunctionDefinition existing = getFunctionById(functionIdKey);
    if (existing == null) {
      throw new IllegalStateException("the function does not exist: " + function);
    }
    final FunctionDefinition toUpdate;
    if (!existing.getId().equals(function.getId())) {
      final FunctionDefinition.Builder builder = FunctionDefinition.Builder.create(function);
      builder.setId(existing.getId());
      toUpdate = builder.build();
    } else {
      toUpdate = function;
    }

    businessObjectService.save(FunctionBo.from(toUpdate));
  }