class SecurityRoleFunctionServiceImpl implements SecurityRoleFunctionService {

    @Override
    public SecurityRoleFunction createSecurityRoleFunction(SecurityRoleFunctionCreateRequest securityRoleFunctionCreateRequest)
    {
        // Validate and trim the security role to function mapping create request.
        securityRoleFunctionHelper.validateAndTrimSecurityRoleFunctionCreateRequest(securityRoleFunctionCreateRequest);

        // Ensure a security role to function mapping with the specified parameters doesn't already exist.
        SecurityRoleFunctionEntity securityRoleFunctionEntity =
            securityRoleFunctionDao.getSecurityRoleFunctionByKey(securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey());
        if (securityRoleFunctionEntity != null)
        {
            throw new AlreadyExistsException(String.format(
                "Unable to create security role to function mapping for \"%s\" security role name and \"%s\" security function name because it already exists.",
                securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey().getSecurityRoleName(),
                securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey().getSecurityFunctionName()));
        }

        // Retrieve and ensure that a security role with the specified name exists.
        SecurityRoleEntity securityRoleEntity =
            securityRoleDaoHelper.getSecurityRoleEntity(securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey().getSecurityRoleName());

        // Retrieve and ensure that a security function with the specified name exists.
        SecurityFunctionEntity securityFunctionEntity =
            securityFunctionDaoHelper.getSecurityFunctionEntity(securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey().getSecurityFunctionName());

        // Creates and persist a security role to function mapping entity.
        securityRoleFunctionEntity = createSecurityRoleFunctionEntity(securityRoleEntity, securityFunctionEntity);

        // Persist the new entity.
        securityRoleFunctionDao.saveAndRefresh(securityRoleFunctionEntity);

        // Create a security role to function mapping object from the entity and return it.
        return createSecurityRoleFunctionFromEntity(securityRoleFunctionEntity);
    }

}