Accomplish AbstractCrudService

pull/98/head
johnniang 2019-02-19 21:59:53 +08:00
parent b286f2f69d
commit 6577d062dc
2 changed files with 200 additions and 29 deletions

View File

@ -1,5 +1,21 @@
package cc.ryanc.halo.service.base;
import cc.ryanc.halo.exception.NotFoundException;
import cc.ryanc.halo.logging.Logger;
import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* Abstract service implementation.
*
@ -8,4 +24,155 @@ package cc.ryanc.halo.service.base;
*/
public abstract class AbstractCrudService<DOMAIN, ID> implements CrudService<DOMAIN, ID> {
private final Logger log = Logger.getLogger(getClass());
private final String domainName;
private final BaseRepository<DOMAIN, ID> repository;
protected AbstractCrudService(BaseRepository<DOMAIN, ID> repository) {
this.repository = repository;
// Get domain name
Class<DOMAIN> domainClass = (Class<DOMAIN>) fetchType(0);
domainName = domainClass.getSimpleName();
}
/**
* Gets actual generic type.
*
* @param index generic type index
* @return real generic type will be returned
*/
private Type fetchType(int index) {
Assert.isTrue(index >= 0 && index <= 1, "type index must be between 0 to 1");
return ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[index];
}
@Override
public List<DOMAIN> listAll() {
return repository.findAll();
}
@Override
public List<DOMAIN> listAll(Sort sort) {
Assert.notNull(sort, "Sort info must not be null");
return repository.findAll(sort);
}
@Override
public Page<DOMAIN> listAll(Pageable pageable) {
Assert.notNull(pageable, "Pageable info must not be null");
return repository.findAll(pageable);
}
@Override
public List<DOMAIN> listAllByIds(Collection<ID> ids) {
return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : repository.findAllById(ids);
}
@Override
public List<DOMAIN> listAllByIds(Collection<ID> ids, Sort sort) {
Assert.notNull(sort, "Sort info must not be null");
return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : repository.findAllByIdIn(ids, sort);
}
@Override
public Optional<DOMAIN> fetchById(ID id) {
Assert.notNull(id, domainName + " id must not be null");
return repository.findById(id);
}
@Override
public DOMAIN getById(ID id) {
return fetchById(id).orElseThrow(() -> new NotFoundException(domainName + " was not found"));
}
@Override
public DOMAIN getNullableById(ID id) {
return fetchById(id).orElse(null);
}
@Override
public boolean existsById(ID id) {
Assert.notNull(id, domainName + " id must not be null");
return repository.existsById(id);
}
@Override
public void mustExistById(ID id) {
if (!existsById(id)) {
throw new NotFoundException(domainName + " was not exist");
}
}
@Override
public DOMAIN create(DOMAIN domain) {
Assert.notNull(domain, domainName + " data must not be null");
return repository.save(domain);
}
@Override
public List<DOMAIN> createInBatch(Collection<DOMAIN> domains) {
return CollectionUtils.isEmpty(domains) ? Collections.emptyList() : repository.saveAll(domains);
}
@Override
public DOMAIN update(DOMAIN domain) {
Assert.notNull(domain, domainName + " data must not be null");
return repository.saveAndFlush(domain);
}
@Override
public List<DOMAIN> updateInBatch(Collection<DOMAIN> domains) {
return CollectionUtils.isEmpty(domains) ? Collections.emptyList() : repository.saveAll(domains);
}
@Override
public DOMAIN removeById(ID id) {
// Get non null domain by id
DOMAIN domain = getById(id);
// Remove it
remove(domain);
// return the deleted domain
return domain;
}
@Override
public void remove(DOMAIN domain) {
Assert.notNull(domain, domainName + " data must not be null");
repository.delete(domain);
}
@Override
public void removeInBatch(Collection<ID> ids) {
if (CollectionUtils.isEmpty(ids)) {
log.warn(domainName + " id collection is empty");
return;
}
repository.deleteByIdIn(ids);
}
@Override
public void removeAll(Collection<DOMAIN> domains) {
if (CollectionUtils.isEmpty(domains)) {
log.warn(domainName + " collection is empty");
return;
}
repository.deleteInBatch(domains);
}
}

View File

@ -3,7 +3,10 @@ package cc.ryanc.halo.service.base;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -19,58 +22,59 @@ public interface CrudService<DOMAIN, ID> {
// **************** Select
@NonNull
List<DOMAIN> listAll();
List<DOMAIN> listAll(Sort sort);
@NonNull
List<DOMAIN> listAll(@NonNull Sort sort);
Page<DOMAIN> listAll(Pageable pageable);
@NonNull
Page<DOMAIN> listAll(@NonNull Pageable pageable);
// <P> List<P> listAll(Class<P> projectionType);
//
// <P> List<P> listAll(Class<P> projectionType, Sort sort);
//
// <P> Page<P> listAll(Class<P> projectionType, Pageable pageable);
@NonNull
List<DOMAIN> listAllByIds(@NonNull Collection<ID> ids);
List<DOMAIN> listAllByIds(Iterable<ID> ids);
@NonNull
List<DOMAIN> listAllByIds(@NonNull Collection<ID> ids, @NonNull Sort sort);
List<DOMAIN> listAllByIds(Iterable<ID> ids, Sort sort);
@NonNull
Optional<DOMAIN> fetchById(@NonNull ID id);
Map<ID, DOMAIN> listAllByIdsAsMap(Iterable<ID> ids);
@NonNull
DOMAIN getById(@NonNull ID id);
Map<ID, DOMAIN> listAllByIdsAsMap(Iterable<ID> ids, Sort sort);
@Nullable
DOMAIN getNullableById(@NonNull ID id);
Optional<DOMAIN> fetchById(ID id);
boolean existsById(@NonNull ID id);
DOMAIN getById(ID id);
DOMAIN getNullableById(ID id);
boolean existsById(ID id);
void mustExistById(ID id);
void mustExistById(@NonNull ID id);
// **************** Create
DOMAIN create(DOMAIN domain);
@NonNull
DOMAIN create(@NonNull DOMAIN domain);
List<DOMAIN> createInBatch(Iterable<DOMAIN> domains);
@NonNull
List<DOMAIN> createInBatch(@NonNull Collection<DOMAIN> domains);
// **************** Update
@NonNull
DOMAIN update(@NonNull DOMAIN domain);
DOMAIN update(DOMAIN domain);
List<DOMAIN> updateInBatch(Iterable<DOMAIN> domains);
@NonNull
List<DOMAIN> updateInBatch(@NonNull Collection<DOMAIN> domains);
// **************** Delete
@NonNull
DOMAIN removeById(@NonNull ID id);
void removeById(ID id);
void remove(@NonNull DOMAIN domain);
void remove(DOMAIN domain);
void removeInBatch(@NonNull Collection<ID> ids);
void removeInBatch(Iterable<ID> ids);
void removeAll(Iterable<DOMAIN> domains);
void removeAll(@NonNull Collection<DOMAIN> domains);
}