DATALDAP-181 - Polishing.

Reorder methods. Add overrides to LdapRepository returning List. Consistent override formatting. Add package-info for repository package. Revert import order changes.

Original pull request: #20.
This commit is contained in:
Mark Paluch
2020-11-25 14:27:48 +01:00
parent a7dd511b31
commit 0ea9561d55
4 changed files with 173 additions and 116 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.ldap.repository;
import java.util.List;
import java.util.Optional;
import javax.naming.Name;
@@ -23,13 +24,34 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.ldap.query.LdapQuery;
/**
* Ldap specific extensions to CrudRepository.
* Ldap specific extensions to {@link CrudRepository}.
*
* @author Mattias Hellborg Arthursson
* @author Mark Paluch
*/
public interface LdapRepository<T> extends CrudRepository<T, Name> {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable)
*/
@Override
<S extends T> List<S> saveAll(Iterable<S> entities);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll()
*/
@Override
List<T> findAll();
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAllById()
*/
@Override
List<T> findAllById(Iterable<Name> names);
/**
* Find one entry matching the specified query.
*

View File

@@ -0,0 +1,5 @@
/**
* LDAP specific repository implementation.
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.ldap.repository;

View File

@@ -96,7 +96,8 @@ public class LdapRepositoryFactory extends RepositoryFactorySupport {
information.getDomainType());
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key, org.springframework.data.repository.query.EvaluationContextProvider)
*/
@Override

View File

@@ -15,6 +15,15 @@
*/
package org.springframework.data.ldap.repository.support;
import static org.springframework.ldap.query.LdapQueryBuilder.*;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.naming.Name;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Persistable;
import org.springframework.data.ldap.repository.LdapRepository;
@@ -28,14 +37,6 @@ import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.util.Assert;
import javax.naming.Name;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static org.springframework.ldap.query.LdapQueryBuilder.*;
/**
* Base repository implementation for LDAP.
*
@@ -55,8 +56,8 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
* Creates a new {@link SimpleLdapRepository}.
*
* @param ldapOperations must not be {@literal null}.
* @param odm must not be {@literal null}.
* @param entityType must not be {@literal null}.
* @param odm must not be {@literal null}.
* @param entityType must not be {@literal null}.
*/
public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> entityType) {
@@ -69,31 +70,12 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
this.entityType = entityType;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#count()
*/
@Override
public long count() {
// -------------------------------------------------------------------------
// Methods from CrudRepository
// -------------------------------------------------------------------------
Filter filter = odm.filterFor(entityType, null);
CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler();
LdapQuery query = query().attributes(OBJECTCLASS_ATTRIBUTE).filter(filter);
ldapOperations.search(query, callback);
return callback.getNoOfRows();
}
private <S extends T> boolean isNew(S entity, @Nullable Name id) {
if (entity instanceof Persistable) {
Persistable<?> persistable = (Persistable<?>) entity;
return persistable.isNew();
} else {
return id == null;
}
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
*/
@Override
@@ -112,18 +94,20 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
return entity;
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable)
*/
@Override
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
public <S extends T> List<S> saveAll(Iterable<S> entities) {
return StreamSupport.stream(entities.spliterator(), false) //
.map(this::save) //
.collect(Collectors.toList());
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findById(java.io.Serializable)
*/
@Override
@@ -138,17 +122,118 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
}
}
/* (non-Javadoc)
* @see org.springframework.data.ldap.repository.LdapRepository#findAll(org.springframework.ldap.query.LdapQuery)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable)
*/
@Override
public List<T> findAll(LdapQuery ldapQuery) {
public boolean existsById(Name name) {
Assert.notNull(ldapQuery, "LdapQuery must not be null");
return ldapOperations.find(ldapQuery, entityType);
Assert.notNull(name, "Id must not be null");
return findById(name).isPresent();
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll()
*/
@Override
public List<T> findAll() {
return ldapOperations.findAll(entityType);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAllById(java.lang.Iterable)
*/
@Override
public List<T> findAllById(Iterable<Name> names) {
return StreamSupport.stream(names.spliterator(), false) //
.map(this::findById) //
.flatMap(Optionals::toStream) //
.collect(Collectors.toList());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#count()
*/
@Override
public long count() {
Filter filter = odm.filterFor(entityType, null);
CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler();
LdapQuery query = query().attributes(OBJECTCLASS_ATTRIBUTE).filter(filter);
ldapOperations.search(query, callback);
return callback.getNoOfRows();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteById(java.io.Serializable)
*/
@Override
public void deleteById(Name name) {
Assert.notNull(name, "Id must not be null");
ldapOperations.unbind(name);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
*/
@Override
public void delete(T entity) {
Assert.notNull(entity, "Entity must not be null");
ldapOperations.delete(entity);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAllById(java.lang.Iterable)
*/
@Override
public void deleteAllById(Iterable<? extends Name> names) {
Assert.notNull(names, "Names must not be null.");
names.forEach(this::deleteById);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll(java.lang.Iterable)
*/
@Override
public void deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "Entities must not be null.");
entities.forEach(this::delete);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll()
*/
@Override
public void deleteAll() {
deleteAll(findAll());
}
// -------------------------------------------------------------------------
// Methods from LdapRepository
// ------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.ldap.repository.LdapRepository#findOne(org.springframework.ldap.query.LdapQuery)
*/
@Override
@@ -163,83 +248,27 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
}
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable)
/*
* (non-Javadoc)
* @see org.springframework.data.ldap.repository.LdapRepository#findAll(org.springframework.ldap.query.LdapQuery)
*/
@Override
public boolean existsById(Name name) {
public List<T> findAll(LdapQuery ldapQuery) {
Assert.notNull(name, "Id must not be null");
return findById(name).isPresent();
Assert.notNull(ldapQuery, "LdapQuery must not be null");
return ldapOperations.find(ldapQuery, entityType);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll()
*/
@Override
public List<T> findAll() {
return ldapOperations.findAll(entityType);
private <S extends T> boolean isNew(S entity, @Nullable Name id) {
if (entity instanceof Persistable) {
Persistable<?> persistable = (Persistable<?>) entity;
return persistable.isNew();
} else {
return id == null;
}
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAllById(java.lang.Iterable)
*/
@Override
public List<T> findAllById(final Iterable<Name> names) {
return StreamSupport.stream(names.spliterator(), false) //
.map(this::findById) //
.flatMap(Optionals::toStream) //
.collect(Collectors.toList());
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteById(java.io.Serializable)
*/
@Override
public void deleteById(Name name) {
Assert.notNull(name, "Id must not be null");
ldapOperations.unbind(name);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
*/
@Override
public void delete(T entity) {
Assert.notNull(entity, "Entity must not be null");
ldapOperations.delete(entity);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll(java.lang.Iterable)
*/
@Override
public void deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "Entities must not be null.");
entities.forEach(this::delete);
}
@Override
public void deleteAllById(Iterable<? extends Name> names) {
Assert.notNull(names, "Names must not be null.");
names.forEach(this::deleteById);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll()
*/
@Override
public void deleteAll() {
deleteAll(findAll());
}
}