package com.jmb.springfactory.service.rol;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jmb.springfactory.dao.GenericMongoService;
import com.jmb.springfactory.dao.rol.RolMongoService;
import com.jmb.springfactory.exceptions.NotFoundException;
import com.jmb.springfactory.model.bo.BusinessObjectBase;
import com.jmb.springfactory.model.dto.RolDto;
import com.jmb.springfactory.model.entity.Rol;
import com.jmb.springfactory.service.GenericServiceImpl;

@Service
public class RolServiceImpl extends GenericServiceImpl<Rol, RolDto, BusinessObjectBase, String>
  implements RolService {
  
  @Autowired
  private RolMongoService rolMongoService;

  @Override
  public GenericMongoService<Rol, String> genericDao() {
    return rolMongoService;
  }

  @Override
  public Class<? extends Rol> getClazz() {
    return Rol.class;
  }

  @Override
  public Class<? extends RolDto> getDtoClazz() {
    return RolDto.class;
  }

  @Override
  public Class<? extends BusinessObjectBase> getBoClazz() {
    return BusinessObjectBase.class;
  }

  @Override
  public List<RolDto> findByNameContain(String name) throws NotFoundException {
    final List<Rol> foundRoles = rolMongoService.findByNameContain(name)
        .collect(Collectors.toList());
    if (foundRoles.isEmpty()) {
      throw new NotFoundException();
    }
    return this.convertListEntityToListDto(foundRoles);
  }


}

@@ -1255,16 +1255,86 @@
 eption %7B
+%0A%0A    serviceLog.debug(%22Searching roles with name equals to %22 + name);
 %0A    fin
@@ -1402,17 +1402,8 @@
 ame)
-%0A        
 .col
@@ -1433,82 +1433,185 @@
 ));%0A
+%0A
     
-if (foundRoles.isEmpty()) %7B%0A      throw new NotFoundException(
+throwNotFoundExceptionIfEmptyList(foundRoles);%0A    serviceLog.debug(%22Have been found %22 + foundRoles.size() + %22 roles%22
 );%0A
+%0A
     
-%7D
+serviceLog.debug(%22Transforming Roles to Dtos%22);
 %0A   
@@ -1668,11 +1668,178 @@
 );%0A  %7D%0A%0A
+  private void throwNotFoundExceptionIfEmptyList(List%3CRol%3E roles) throws NotFoundException %7B%0A    if (roles.isEmpty()) %7B%0A      throw new NotFoundException();%0A    %7D%0A  %7D%0A
 %0A%7D%0A

Refactor method to search entity