package ch.difty.scipamato.core.persistence;

import java.util.List;
import java.util.Optional;

import org.springframework.transaction.annotation.Transactional;

import ch.difty.scipamato.common.entity.filter.ScipamatoFilter;
import ch.difty.scipamato.common.persistence.paging.PaginationContext;
import ch.difty.scipamato.core.entity.CoreEntity;
import ch.difty.scipamato.core.entity.IdScipamatoEntity;
import ch.difty.scipamato.core.entity.User;
import ch.difty.scipamato.core.persistence.user.UserRepository;

/**
 * Abstract base repository class providing the fundamental functionality of a
 * jooq read-only Service
 *
 * @author u.joss
 *
 * @param <ID>
 *            the type of the id in the {@link CoreEntity}
 * @param <T>
 *            the entity type, extending {@link CoreEntity}
 * @param <F>
 *            the filter, extending {@link ScipamatoFilter}
 * @param <REPO>
 *            the entity repository (extending {@link EntityRepository}
 */
@Transactional(readOnly = true)
public abstract class JooqReadOnlyService<ID extends Number, T extends IdScipamatoEntity<ID>, F extends ScipamatoFilter, REPO extends ReadOnlyRepository<T, ID, F>>
        implements ReadOnlyService<ID, T, F> {

    private static final long serialVersionUID = 1L;

    private final REPO           repo;
    private final UserRepository userRepo;

    protected JooqReadOnlyService(final REPO repo, final UserRepository userRepo) {
        this.repo = repo;
        this.userRepo = userRepo;
    }

    protected REPO getRepository() {
        return repo;
    }

    protected UserRepository getUserRepository() {
        return userRepo;
    }

    @Override
    public Optional<T> findById(ID id) {
        T entity = repo.findById(id);
        enrichAuditNamesOf(entity);
        return Optional.ofNullable(entity);
    }

    protected void enrichAuditNamesOf(T entity) {
        if (entity != null) {
            setCreatedMetaData(entity);
            setModifiedMeta(entity);
        }

    }

    private void setCreatedMetaData(T entity) {
        final Optional<User> user = getUserWithId(entity.getCreatedBy());
        entity.setCreatedByName(user.map(User::getDisplayValue)
            .orElse(null));
        entity.setCreatedByFullName(user.map(User::getFullName)
            .orElse(null));
    }

    private void setModifiedMeta(T entity) {
        entity.setLastModifiedByName(getUserWithId(entity.getLastModifiedBy()).map(User::getDisplayValue)
            .orElse(null));
    }

    private Optional<User> getUserWithId(final Integer id) {
        if (id != null && getUserRepository() != null) {
            return Optional.ofNullable(getUserRepository().findById(id));
        }
        return Optional.empty();

    }

    @Override
    public List<T> findPageByFilter(F filter, PaginationContext paginationContext) {
        final List<T> entities = repo.findPageByFilter(filter, paginationContext);
        entities.forEach(this::enrichAuditNamesOf);
        return entities;
    }

    @Override
    public int countByFilter(F filter) {
        return repo.countByFilter(filter);
    }

    @Override
    public List<ID> findPageOfIdsByFilter(F filter, PaginationContext paginationContext) {
        return repo.findPageOfIdsByFilter(filter, paginationContext);
    }

}

@@ -152,24 +152,67 @@
 sactional;%0A%0A
+import ch.difty.scipamato.common.AssertAs;%0A
 import ch.di
@@ -259,24 +259,24 @@
 matoFilter;%0A
-
 import ch.di
@@ -1487,46 +1487,102 @@
 o = 
-repo;%0A        this.userRepo = userRepo
+AssertAs.notNull(repo, %22repo%22);%0A        this.userRepo = AssertAs.notNull(userRepo, %22userRepo%22)
 ;%0A  
@@ -1780,16 +1780,22 @@
 indById(
+final 
 ID id) %7B
@@ -1803,16 +1803,22 @@
         
+final 
 T entity
@@ -1964,16 +1964,22 @@
 NamesOf(
+final 
 T entity
@@ -2095,25 +2095,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    p
@@ -2142,16 +2142,22 @@
 etaData(
+final 
 T entity
@@ -2458,16 +2458,22 @@
 iedMeta(
+final 
 T entity
@@ -2704,42 +2704,9 @@
 null
- && getUserRepository() != null) %7B
+)
 %0A   
@@ -2776,26 +2776,16 @@
 d(id));%0A
-        %7D%0A
         
@@ -2859,32 +2859,38 @@
 indPageByFilter(
+final 
 F filter, Pagina
@@ -2874,32 +2874,38 @@
 (final F filter,
+ final
  PaginationConte
@@ -3130,32 +3130,38 @@
 t countByFilter(
+final 
 F filter) %7B%0A    
@@ -3262,16 +3262,22 @@
 yFilter(
+final 
 F filter
@@ -3277,16 +3277,22 @@
  filter,
+ final
  Paginat

Refactor JooqReadOnlyService to complete test coverage