All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.rocketbase.commons.service.AppUserJpaServiceImpl Maven / Gradle / Ivy
package io.rocketbase.commons.service;
import io.rocketbase.commons.dto.appuser.QueryAppUser;
import io.rocketbase.commons.model.AppUserJpaEntity;
import io.rocketbase.commons.repository.AppUserJpaRepository;
import io.rocketbase.commons.util.Nulls;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.time.Instant;
import java.util.*;
@RequiredArgsConstructor
public class AppUserJpaServiceImpl implements AppUserPersistenceService, PredicateHelper {
private final AppUserJpaRepository repository;
@Override
public Optional findByUsername(String username) {
return repository.findByUsername(username);
}
@Override
public Optional findByEmail(String email) {
return repository.findByEmail(email);
}
@Override
@Transactional
public Page findAll(QueryAppUser query, Pageable pageable) {
if (query == null || query.isEmpty()) {
return repository.findAll(pageable);
}
Specification specification = (Specification) (root, criteriaQuery, cb) -> {
Predicate result = cb.equal(root.get("enabled"), Nulls.notNull(query.getEnabled(), true));
List textSearch = new ArrayList<>();
addToListIfNotEmpty(textSearch, Nulls.notEmpty(query.getUsername(), query.getFreetext()), "username", root, cb);
addToListIfNotEmpty(textSearch, Nulls.notEmpty(query.getFirstName(), query.getFreetext()), "firstName", root, cb);
addToListIfNotEmpty(textSearch, Nulls.notEmpty(query.getLastName(), query.getFreetext()), "lastName", root, cb);
addToListIfNotEmpty(textSearch, Nulls.notEmpty(query.getEmail(), query.getFreetext()), "email", root, cb);
if (!textSearch.isEmpty()) {
if (StringUtils.isEmpty(query.getFreetext())) {
result = cb.and(result, cb.and(textSearch.toArray(new Predicate[]{})));
} else {
result = cb.and(result, cb.or(textSearch.toArray(new Predicate[]{})));
}
}
List furtherFilters = new ArrayList<>();
if (!StringUtils.isEmpty(query.getHasRole())) {
criteriaQuery.distinct(true);
Predicate roles = cb.upper(root.join("roles")).in(query.getHasRole().toUpperCase());
furtherFilters.add(roles);
}
if (query.getKeyValues() != null && !query.getKeyValues().isEmpty()) {
criteriaQuery.distinct(true);
MapJoin mapJoin = root.joinMap("keyValueMap");
for (Map.Entry keyEntry : query.getKeyValues().entrySet()) {
furtherFilters.add(cb.and(cb.equal(mapJoin.key(), keyEntry.getKey()), cb.equal(mapJoin.value(), keyEntry.getValue())));
}
}
if (!furtherFilters.isEmpty()) {
result = cb.and(result, cb.and(furtherFilters.toArray(new Predicate[]{})));
}
return result;
};
Page result = repository.findAll(specification, pageable);
// in order to initialize lazy map
result.stream()
.forEach(v -> initLazyObjects(v));
return result;
}
@Override
@Transactional
public AppUserJpaEntity save(AppUserJpaEntity entity) {
return initLazyObjects(repository.save(entity));
}
@Override
public Optional findById(String id) {
return repository.findById(id);
}
@Override
public long count() {
return repository.count();
}
@Override
@Transactional
public void delete(AppUserJpaEntity entity) {
repository.delete(entity);
}
@Override
@Transactional
public void deleteAll() {
repository.deleteAll();
}
@Override
public AppUserJpaEntity initNewInstance() {
return AppUserJpaEntity.builder()
.id(UUID.randomUUID().toString())
.created(Instant.now())
.roles(new ArrayList<>())
.build();
}
protected AppUserJpaEntity initLazyObjects(AppUserJpaEntity entity) {
if (entity != null) {
if (entity.getKeyValueMap() != null) {
// in order to initialize lazy map
entity.getKeyValueMap().size();
}
if (entity.getRoles() != null) {
// in order to initialize lazy map
entity.getRoles().size();
}
}
return entity;
}
}