class FunctionalityService {

    public FunctionalityDTO create(Long projectId, NewFunctionalityDTO newDto) throws BadRequestException {
        ObjectUtil.trimStringValues(newDto.getFunctionality());

        // Compute new position and verify all technical rules related to the position of the node in the tree
        TreePosition treePosition = computeDestinationTreePosition(projectId, newDto.getReferenceId(), newDto.getRelativePosition(), null);

        // Define some properties (before toEntity for no NullPointerException)
        FunctionalityDTO dtoToCreate = newDto.getFunctionality();
        dtoToCreate.setId(null);
        dtoToCreate.setParentId(treePosition.getParentId());
        dtoToCreate.setOrder(treePosition.getOrder());

        // The created entity must be valid against business rules
        validateBusinessRules(projectId, dtoToCreate);

        final Functionality entity = mapper.toEntity(dtoToCreate);
        entity.setProjectId(projectId);
        entity.setCreationDateTime(new Date());
        entity.setUpdateDateTime(entity.getCreationDateTime());

        // New entities are not covered
        final boolean isFolder = entity.getType() == FunctionalityType.FOLDER;
        entity.setCoveredScenarios(isFolder ? null : 0);
        entity.setCoveredCountryScenarios(null);
        entity.setIgnoredScenarios(isFolder ? null : 0);
        entity.setIgnoredCountryScenarios(null);

        return mapper.toDto(repository.save(entity));
    }

}