protected PrivilegeEntity toEntity(Map<String, Object> properties, Long resourceId)
      throws AmbariException {
    PrivilegeEntity entity         = new PrivilegeEntity();
    String          permissionName = (String) properties.get(PERMISSION_NAME);
    ResourceEntity  resourceEntity = resourceDAO.findById(resourceId);
    PermissionEntity permission = getPermission(permissionName, resourceEntity);
    if (permission == null) {
      throw new AmbariException("Can't find a permission named " + permissionName +
          " for the resource.");
    }
    entity.setPermission(permission);
    entity.setResource(resourceEntity);

    String principalName = (String) properties.get(PRINCIPAL_NAME);
    String principalType = (String) properties.get(PRINCIPAL_TYPE);
    if (StringUtils.equalsIgnoreCase(PrincipalTypeEntity.GROUP_PRINCIPAL_TYPE_NAME, principalType)) {
      GroupEntity groupEntity = groupDAO.findGroupByName(principalName);
      if (groupEntity != null) {
        entity.setPrincipal(principalDAO.findById(groupEntity.getPrincipal().getId()));
      }
    } else if (StringUtils.equalsIgnoreCase(PrincipalTypeEntity.ROLE_PRINCIPAL_TYPE_NAME, principalType)) {
      PermissionEntity permissionEntity = permissionDAO.findByName(principalName);
      if (permissionEntity != null) {
        entity.setPrincipal(principalDAO.findById(permissionEntity.getPrincipal().getId()));
      }
    } else if (StringUtils.equalsIgnoreCase(PrincipalTypeEntity.USER_PRINCIPAL_TYPE_NAME, principalType)) {
      UserEntity userEntity = userDAO.findUserByName(principalName);
      if (userEntity != null) {
        entity.setPrincipal(principalDAO.findById(userEntity.getPrincipal().getId()));
      }
    } else {
      throw new AmbariException("Unknown principal type " + principalType);
    }
    if (entity.getPrincipal() == null) {
      throw new AmbariException("Could not find " + principalType + " named " + principalName);
    }
    return entity;
  }
Convert the given map of properties to a privilege entity for the resource
identified by the given id.

@param properties  the property map
@param resourceId  the resource id

@return the new privilege entity
ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PrivilegeResourceProvider.java