Add SpecificationBuilder to avoid duplicate code

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-02-08 08:47:12 +01:00
parent be45209598
commit 4574c78908
3 changed files with 135 additions and 115 deletions

View File

@@ -16,6 +16,7 @@ import javax.validation.constraints.NotNull;
import org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions; import org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException; import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.model.TargetFilterQuery; import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
import org.eclipse.hawkbit.repository.specifications.SpecificationsBuilder;
import org.eclipse.hawkbit.repository.specifications.TargetFilterQuerySpecification; import org.eclipse.hawkbit.repository.specifications.TargetFilterQuerySpecification;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@@ -33,7 +34,7 @@ import com.google.common.base.Strings;
/** /**
* Business service facade for managing {@link TargetFilterQuery}s. * Business service facade for managing {@link TargetFilterQuery}s.
* *
* *
* *
*/ */
@@ -47,7 +48,7 @@ public class TargetFilterQueryManagement {
/** /**
* creating new {@link TargetFilterQuery}. * creating new {@link TargetFilterQuery}.
* *
* @param customTargetFilter * @param customTargetFilter
* @return the created {@link TargetFilterQuery} * @return the created {@link TargetFilterQuery}
*/ */
@@ -57,16 +58,15 @@ public class TargetFilterQueryManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET)
public TargetFilterQuery createTargetFilterQuery(@NotNull final TargetFilterQuery customTargetFilter) { public TargetFilterQuery createTargetFilterQuery(@NotNull final TargetFilterQuery customTargetFilter) {
if (targetFilterQueryRepository.findByName(customTargetFilter.getName()) != null) { if (this.targetFilterQueryRepository.findByName(customTargetFilter.getName()) != null) {
throw new EntityAlreadyExistsException(customTargetFilter.getName()); throw new EntityAlreadyExistsException(customTargetFilter.getName());
} }
final TargetFilterQuery filter = targetFilterQueryRepository.save(customTargetFilter); return this.targetFilterQueryRepository.save(customTargetFilter);
return filter;
} }
/** /**
* Delete target filter query. * Delete target filter query.
* *
* @param targetFilterQueryId * @param targetFilterQueryId
* IDs of target filter query to be deleted * IDs of target filter query to be deleted
*/ */
@@ -74,26 +74,26 @@ public class TargetFilterQueryManagement {
@Transactional @Transactional
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_DELETE_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_DELETE_TARGET)
public void deleteTargetFilterQuery(@NotNull final Long targetFilterQueryId) { public void deleteTargetFilterQuery(@NotNull final Long targetFilterQueryId) {
targetFilterQueryRepository.delete(targetFilterQueryId); this.targetFilterQueryRepository.delete(targetFilterQueryId);
} }
/** /**
* *
* Retrieves all target filter query{@link TargetFilterQuery}. * Retrieves all target filter query{@link TargetFilterQuery}.
* *
* @param pageable * @param pageable
* pagination parameter * pagination parameter
* @return the found {@link TargetFilterQuery}s * @return the found {@link TargetFilterQuery}s
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Page<TargetFilterQuery> findAllTargetFilterQuery(@NotNull final Pageable pageable) { public Page<TargetFilterQuery> findAllTargetFilterQuery(@NotNull final Pageable pageable) {
return targetFilterQueryRepository.findAll(pageable); return this.targetFilterQueryRepository.findAll(pageable);
} }
/** /**
* Retrieves all target filter query which {@link TargetFilterQuery}. * Retrieves all target filter query which {@link TargetFilterQuery}.
* *
* *
* @param pageable * @param pageable
* pagination parameter * pagination parameter
* @param name * @param name
@@ -102,7 +102,7 @@ public class TargetFilterQueryManagement {
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Page<TargetFilterQuery> findTargetFilterQueryByFilters(@NotNull final Pageable pageable, final String name) { public Page<TargetFilterQuery> findTargetFilterQueryByFilters(@NotNull final Pageable pageable, final String name) {
final List<Specification<TargetFilterQuery>> specList = new ArrayList<Specification<TargetFilterQuery>>(); final List<Specification<TargetFilterQuery>> specList = new ArrayList<>();
if (!Strings.isNullOrEmpty(name)) { if (!Strings.isNullOrEmpty(name)) {
specList.add(TargetFilterQuerySpecification.likeName(name)); specList.add(TargetFilterQuerySpecification.likeName(name));
} }
@@ -110,7 +110,7 @@ public class TargetFilterQueryManagement {
} }
/** /**
* *
* @param pageable * @param pageable
* pagination parameter * pagination parameter
* @param specList * @param specList
@@ -119,51 +119,43 @@ public class TargetFilterQueryManagement {
*/ */
private Page<TargetFilterQuery> findTargetFilterQueryByCriteriaAPI(@NotNull final Pageable pageable, private Page<TargetFilterQuery> findTargetFilterQueryByCriteriaAPI(@NotNull final Pageable pageable,
final List<Specification<TargetFilterQuery>> specList) { final List<Specification<TargetFilterQuery>> specList) {
Specifications<TargetFilterQuery> specs = null; if (specList == null || specList.isEmpty()) {
if (!specList.isEmpty()) { return this.targetFilterQueryRepository.findAll(pageable);
specs = Specifications.where(specList.get(0));
}
if (specList.size() > 1) {
for (final Specification<TargetFilterQuery> s : specList.subList(1, specList.size())) {
specs = specs.and(s);
}
}
if (specs == null) {
return targetFilterQueryRepository.findAll(pageable);
} else {
return targetFilterQueryRepository.findAll(specs, pageable);
} }
final Specifications<TargetFilterQuery> specs = SpecificationsBuilder.combineWithAnd(specList);
return this.targetFilterQueryRepository.findAll(specs, pageable);
} }
/** /**
* Find target filter query by name. * Find target filter query by name.
* *
* @param targetFilterQueryName * @param targetFilterQueryName
* Target filter query name * Target filter query name
* @return the found {@link TargetFilterQuery} * @return the found {@link TargetFilterQuery}
* *
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public TargetFilterQuery findTargetFilterQueryByName(@NotNull final String targetFilterQueryName) { public TargetFilterQuery findTargetFilterQueryByName(@NotNull final String targetFilterQueryName) {
return targetFilterQueryRepository.findByName(targetFilterQueryName); return this.targetFilterQueryRepository.findByName(targetFilterQueryName);
} }
/** /**
* Find target filter query by id. * Find target filter query by id.
* *
* @param targetFilterQueryId * @param targetFilterQueryId
* Target filter query id * Target filter query id
* @return the found {@link TargetFilterQuery} * @return the found {@link TargetFilterQuery}
* *
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public TargetFilterQuery findTargetFilterQueryById(@NotNull final Long targetFilterQueryId) { public TargetFilterQuery findTargetFilterQueryById(@NotNull final Long targetFilterQueryId) {
return targetFilterQueryRepository.findOne(targetFilterQueryId); return this.targetFilterQueryRepository.findOne(targetFilterQueryId);
} }
/** /**
* updates the {@link TargetFilterQuery}. * updates the {@link TargetFilterQuery}.
* *
* @param targetFilterQuery * @param targetFilterQuery
* to be updated * to be updated
* @return the updated {@link TargetFilterQuery} * @return the updated {@link TargetFilterQuery}
@@ -174,7 +166,7 @@ public class TargetFilterQueryManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
public TargetFilterQuery updateTargetFilterQuery(@NotNull final TargetFilterQuery targetFilterQuery) { public TargetFilterQuery updateTargetFilterQuery(@NotNull final TargetFilterQuery targetFilterQuery) {
Assert.notNull(targetFilterQuery.getId()); Assert.notNull(targetFilterQuery.getId());
return targetFilterQueryRepository.save(targetFilterQuery); return this.targetFilterQueryRepository.save(targetFilterQuery);
} }
} }

View File

@@ -47,6 +47,7 @@ import org.eclipse.hawkbit.repository.model.TargetTagAssigmentResult;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus; import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
import org.eclipse.hawkbit.repository.model.Target_; import org.eclipse.hawkbit.repository.model.Target_;
import org.eclipse.hawkbit.repository.rsql.RSQLUtility; import org.eclipse.hawkbit.repository.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.specifications.SpecificationsBuilder;
import org.eclipse.hawkbit.repository.specifications.TargetSpecifications; import org.eclipse.hawkbit.repository.specifications.TargetSpecifications;
import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -59,7 +60,6 @@ import org.springframework.data.domain.SliceImpl;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -114,7 +114,7 @@ public class TargetManagement {
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Target findTargetByControllerID(@NotEmpty final String controllerId) { public Target findTargetByControllerID(@NotEmpty final String controllerId) {
return targetRepository.findByControllerId(controllerId); return this.targetRepository.findByControllerId(controllerId);
} }
/** /**
@@ -131,7 +131,7 @@ public class TargetManagement {
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Target findTargetByControllerIDWithDetails(@NotEmpty final String controllerId) { public Target findTargetByControllerIDWithDetails(@NotEmpty final String controllerId) {
final Target result = targetRepository.findByControllerId(controllerId); final Target result = this.targetRepository.findByControllerId(controllerId);
// load lazy relations // load lazy relations
if (result != null) { if (result != null) {
result.getTargetInfo().getControllerAttributes().size(); result.getTargetInfo().getControllerAttributes().size();
@@ -158,7 +158,8 @@ public class TargetManagement {
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public List<Target> findTargetsByControllerID(@NotEmpty final Collection<String> controllerIDs) { public List<Target> findTargetsByControllerID(@NotEmpty final Collection<String> controllerIDs) {
return targetRepository.findAll(TargetSpecifications.byControllerIdWithStatusAndAssignedInJoin(controllerIDs)); return this.targetRepository
.findAll(TargetSpecifications.byControllerIdWithStatusAndAssignedInJoin(controllerIDs));
} }
/** /**
@@ -168,7 +169,7 @@ public class TargetManagement {
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Long countTargetsAll() { public Long countTargetsAll() {
return targetRepository.count(); return this.targetRepository.count();
} }
/** /**
@@ -190,7 +191,7 @@ public class TargetManagement {
} }
return cb.conjunction(); return cb.conjunction();
}; };
return criteriaNoCountDao.findAll(spec, pageable, Target.class); return this.criteriaNoCountDao.findAll(spec, pageable, Target.class);
} }
/** /**
@@ -233,7 +234,7 @@ public class TargetManagement {
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Page<Target> findTargetsAll(@NotNull final Specification<Target> spec, @NotNull final Pageable pageable) { public Page<Target> findTargetsAll(@NotNull final Specification<Target> spec, @NotNull final Pageable pageable) {
return targetRepository.findAll(spec, pageable); return this.targetRepository.findAll(spec, pageable);
} }
/** /**
@@ -251,7 +252,8 @@ public class TargetManagement {
public List<Target> findTargetsByControllerIDsWithTags(@NotNull final List<String> controllerIDs) { public List<Target> findTargetsByControllerIDsWithTags(@NotNull final List<String> controllerIDs) {
final List<List<String>> partition = Lists.partition(controllerIDs, Constants.MAX_ENTRIES_IN_STATEMENT); final List<List<String>> partition = Lists.partition(controllerIDs, Constants.MAX_ENTRIES_IN_STATEMENT);
return partition.stream() return partition.stream()
.map(ids -> targetRepository.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(ids))) .map(ids -> this.targetRepository
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(ids)))
.flatMap(t -> t.stream()).collect(Collectors.toList()); .flatMap(t -> t.stream()).collect(Collectors.toList());
} }
@@ -270,7 +272,7 @@ public class TargetManagement {
public Target updateTarget(@NotNull final Target target) { public Target updateTarget(@NotNull final Target target) {
Assert.notNull(target.getId()); Assert.notNull(target.getId());
target.setNew(false); target.setNew(false);
return targetRepository.save(target); return this.targetRepository.save(target);
} }
/** /**
@@ -287,7 +289,7 @@ public class TargetManagement {
+ SpringEvalExpressions.IS_CONTROLLER) + SpringEvalExpressions.IS_CONTROLLER)
public List<Target> updateTargets(@NotNull final List<Target> targets) { public List<Target> updateTargets(@NotNull final List<Target> targets) {
targets.forEach(target -> target.setNew(false)); targets.forEach(target -> target.setNew(false));
return targetRepository.save(targets); return this.targetRepository.save(targets);
} }
/** /**
@@ -305,11 +307,11 @@ public class TargetManagement {
// tenant! Delete statement are not automatically enhanced with the // tenant! Delete statement are not automatically enhanced with the
// @FilterDef of the // @FilterDef of the
// hibernate session. // hibernate session.
final List<Long> targetsForCurrentTenant = targetRepository.findAll(Lists.newArrayList(targetIDs)).stream() final List<Long> targetsForCurrentTenant = this.targetRepository.findAll(Lists.newArrayList(targetIDs)).stream()
.map(Target::getId).collect(Collectors.toList()); .map(Target::getId).collect(Collectors.toList());
if (!targetsForCurrentTenant.isEmpty()) { if (!targetsForCurrentTenant.isEmpty()) {
targetInfoRepository.deleteByTargetIdIn(targetsForCurrentTenant); this.targetInfoRepository.deleteByTargetIdIn(targetsForCurrentTenant);
targetRepository.deleteByIdIn(targetsForCurrentTenant); this.targetRepository.deleteByIdIn(targetsForCurrentTenant);
} }
} }
@@ -329,7 +331,7 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_READ_TARGET)
public Page<Target> findTargetByAssignedDistributionSet(@NotNull final Long distributionSetID, public Page<Target> findTargetByAssignedDistributionSet(@NotNull final Long distributionSetID,
@NotNull final Pageable pageReq) { @NotNull final Pageable pageReq) {
return targetRepository.findByAssignedDistributionSetId(pageReq, distributionSetID); return this.targetRepository.findByAssignedDistributionSetId(pageReq, distributionSetID);
} }
/** /**
@@ -350,7 +352,7 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_READ_TARGET)
public Page<Target> findTargetByAssignedDistributionSet(@NotNull final Long distributionSetID, public Page<Target> findTargetByAssignedDistributionSet(@NotNull final Long distributionSetID,
final Specification<Target> spec, @NotNull final Pageable pageReq) { final Specification<Target> spec, @NotNull final Pageable pageReq) {
return targetRepository.findAll((Specification<Target>) (root, query, cb) -> cb.and( return this.targetRepository.findAll((Specification<Target>) (root, query, cb) -> cb.and(
TargetSpecifications.hasAssignedDistributionSet(distributionSetID).toPredicate(root, query, cb), TargetSpecifications.hasAssignedDistributionSet(distributionSetID).toPredicate(root, query, cb),
spec.toPredicate(root, query, cb)), pageReq); spec.toPredicate(root, query, cb)), pageReq);
} }
@@ -370,7 +372,7 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_READ_TARGET)
public Page<Target> findTargetByInstalledDistributionSet(@NotNull final Long distributionSetID, public Page<Target> findTargetByInstalledDistributionSet(@NotNull final Long distributionSetID,
@NotNull final Pageable pageReq) { @NotNull final Pageable pageReq) {
return targetRepository.findByTargetInfoInstalledDistributionSetId(pageReq, distributionSetID); return this.targetRepository.findByTargetInfoInstalledDistributionSetId(pageReq, distributionSetID);
} }
/** /**
@@ -389,7 +391,7 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_READ_TARGET)
public Page<Target> findTargetByInstalledDistributionSet(final Long distributionSetId, public Page<Target> findTargetByInstalledDistributionSet(final Long distributionSetId,
final Specification<Target> spec, final Pageable pageable) { final Specification<Target> spec, final Pageable pageable) {
return targetRepository.findAll((Specification<Target>) (root, query, cb) -> cb.and( return this.targetRepository.findAll((Specification<Target>) (root, query, cb) -> cb.and(
TargetSpecifications.hasInstalledDistributionSet(distributionSetId).toPredicate(root, query, cb), TargetSpecifications.hasInstalledDistributionSet(distributionSetId).toPredicate(root, query, cb),
spec.toPredicate(root, query, cb)), pageable); spec.toPredicate(root, query, cb)), pageable);
} }
@@ -409,7 +411,7 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Page<Target> findTargetByUpdateStatus(@NotNull final Pageable pageable, public Page<Target> findTargetByUpdateStatus(@NotNull final Pageable pageable,
@NotNull final TargetUpdateStatus status) { @NotNull final TargetUpdateStatus status) {
return targetRepository.findByTargetInfoUpdateStatus(pageable, status); return this.targetRepository.findByTargetInfoUpdateStatus(pageable, status);
} }
/** /**
@@ -508,39 +510,18 @@ public class TargetManagement {
* @return the page with the found {@link Target} * @return the page with the found {@link Target}
*/ */
private Slice<Target> findByCriteriaAPI(final Pageable pageable, final List<Specification<Target>> specList) { private Slice<Target> findByCriteriaAPI(final Pageable pageable, final List<Specification<Target>> specList) {
final Specifications<Target> specs = creatingTargetSpecifications(specList); if (specList == null || specList.isEmpty()) {
return this.criteriaNoCountDao.findAll(pageable, Target.class);
if (specs == null) {
return criteriaNoCountDao.findAll(pageable, Target.class);
} else {
return criteriaNoCountDao.findAll(specs, pageable, Target.class);
} }
return this.criteriaNoCountDao.findAll(SpecificationsBuilder.combineWithAnd(specList), pageable, Target.class);
} }
private Long countByCriteriaAPI(final List<Specification<Target>> specList) { private Long countByCriteriaAPI(final List<Specification<Target>> specList) {
final Specifications<Target> specs = creatingTargetSpecifications(specList); if (specList == null || specList.isEmpty()) {
return this.targetRepository.count();
if (specs == null) {
return targetRepository.count();
} else {
return targetRepository.count(specs);
} }
} return this.targetRepository.count(SpecificationsBuilder.combineWithAnd(specList));
private static Specifications<Target> creatingTargetSpecifications(final List<Specification<Target>> specList) {
Specifications<Target> specs = null;
if (!specList.isEmpty()) {
specs = Specifications.where(specList.get(0));
}
if (specList.size() > 1) {
specList.remove(0);
for (final Specification<Target> s : specList) {
specs = specs.and(s);
}
}
return specs;
} }
/** /**
@@ -581,9 +562,10 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
public TargetTagAssigmentResult toggleTagAssignment(@NotEmpty final Collection<String> targetIds, public TargetTagAssigmentResult toggleTagAssignment(@NotEmpty final Collection<String> targetIds,
@NotNull final String tagName) { @NotNull final String tagName) {
final TargetTag tag = targetTagRepository.findByNameEquals(tagName); final TargetTag tag = this.targetTagRepository.findByNameEquals(tagName);
final List<Target> alreadyAssignedTargets = targetRepository.findByTagNameAndControllerIdIn(tagName, targetIds); final List<Target> alreadyAssignedTargets = this.targetRepository.findByTagNameAndControllerIdIn(tagName,
final List<Target> allTargets = targetRepository targetIds);
final List<Target> allTargets = this.targetRepository
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(targetIds)); .findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(targetIds));
// all are already assigned -> unassign // all are already assigned -> unassign
@@ -592,7 +574,7 @@ public class TargetManagement {
final TargetTagAssigmentResult result = new TargetTagAssigmentResult(0, 0, alreadyAssignedTargets.size(), final TargetTagAssigmentResult result = new TargetTagAssigmentResult(0, 0, alreadyAssignedTargets.size(),
Collections.emptyList(), alreadyAssignedTargets, tag); Collections.emptyList(), alreadyAssignedTargets, tag);
afterCommit.afterCommit(() -> eventBus.post(new TargetTagAssigmentResultEvent(result))); this.afterCommit.afterCommit(() -> this.eventBus.post(new TargetTagAssigmentResultEvent(result)));
return result; return result;
} }
@@ -600,12 +582,12 @@ public class TargetManagement {
// some or none are assigned -> assign // some or none are assigned -> assign
allTargets.forEach(target -> target.getTags().add(tag)); allTargets.forEach(target -> target.getTags().add(tag));
final TargetTagAssigmentResult result = new TargetTagAssigmentResult(alreadyAssignedTargets.size(), final TargetTagAssigmentResult result = new TargetTagAssigmentResult(alreadyAssignedTargets.size(),
allTargets.size(), 0, targetRepository.save(allTargets), Collections.emptyList(), tag); allTargets.size(), 0, this.targetRepository.save(allTargets), Collections.emptyList(), tag);
afterCommit.afterCommit(() -> eventBus.post(new TargetTagAssigmentResultEvent(result))); this.afterCommit.afterCommit(() -> this.eventBus.post(new TargetTagAssigmentResultEvent(result)));
// no reason to persist the tag // no reason to persist the tag
entityManager.detach(tag); this.entityManager.detach(tag);
return result; return result;
} }
@@ -623,16 +605,16 @@ public class TargetManagement {
@NotNull @NotNull
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
public List<Target> assignTag(@NotEmpty final Collection<String> targetIds, @NotNull final TargetTag tag) { public List<Target> assignTag(@NotEmpty final Collection<String> targetIds, @NotNull final TargetTag tag) {
final List<Target> allTargets = targetRepository final List<Target> allTargets = this.targetRepository
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(targetIds)); .findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(targetIds));
allTargets.forEach(target -> target.getTags().add(tag)); allTargets.forEach(target -> target.getTags().add(tag));
final List<Target> save = targetRepository.save(allTargets); final List<Target> save = this.targetRepository.save(allTargets);
afterCommit.afterCommit(() -> { this.afterCommit.afterCommit(() -> {
final TargetTagAssigmentResult assigmentResult = new TargetTagAssigmentResult(0, save.size(), 0, save, final TargetTagAssigmentResult assigmentResult = new TargetTagAssigmentResult(0, save.size(), 0, save,
Collections.emptyList(), tag); Collections.emptyList(), tag);
eventBus.post(new TargetTagAssigmentResultEvent(assigmentResult)); this.eventBus.post(new TargetTagAssigmentResultEvent(assigmentResult));
}); });
return save; return save;
@@ -641,11 +623,11 @@ public class TargetManagement {
private List<Target> unAssignTag(@NotEmpty final Collection<Target> targets, @NotNull final TargetTag tag) { private List<Target> unAssignTag(@NotEmpty final Collection<Target> targets, @NotNull final TargetTag tag) {
targets.forEach(target -> target.getTags().remove(tag)); targets.forEach(target -> target.getTags().remove(tag));
final List<Target> save = targetRepository.save(targets); final List<Target> save = this.targetRepository.save(targets);
afterCommit.afterCommit(() -> { this.afterCommit.afterCommit(() -> {
final TargetTagAssigmentResult assigmentResult = new TargetTagAssigmentResult(0, 0, save.size(), final TargetTagAssigmentResult assigmentResult = new TargetTagAssigmentResult(0, 0, save.size(),
Collections.emptyList(), save, tag); Collections.emptyList(), save, tag);
eventBus.post(new TargetTagAssigmentResultEvent(assigmentResult)); this.eventBus.post(new TargetTagAssigmentResultEvent(assigmentResult));
}); });
return save; return save;
} }
@@ -678,7 +660,7 @@ public class TargetManagement {
@Transactional @Transactional
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
public Target unAssignTag(@NotNull final String controllerID, @NotNull final TargetTag targetTag) { public Target unAssignTag(@NotNull final String controllerID, @NotNull final TargetTag targetTag) {
final List<Target> allTargets = targetRepository final List<Target> allTargets = this.targetRepository
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(Arrays.asList(controllerID))); .findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(Arrays.asList(controllerID)));
final List<Target> unAssignTag = unAssignTag(allTargets, targetTag); final List<Target> unAssignTag = unAssignTag(allTargets, targetTag);
return unAssignTag.isEmpty() ? null : unAssignTag.get(0); return unAssignTag.isEmpty() ? null : unAssignTag.get(0);
@@ -729,7 +711,7 @@ public class TargetManagement {
@NotNull final Long orderByDistributionId, final Long filterByDistributionId, @NotNull final Long orderByDistributionId, final Long filterByDistributionId,
final Collection<TargetUpdateStatus> filterByStatus, final String filterBySearchText, final Collection<TargetUpdateStatus> filterByStatus, final String filterBySearchText,
final Boolean selectTargetWithNoTag, final String... filterByTagNames) { final Boolean selectTargetWithNoTag, final String... filterByTagNames) {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); final CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
final CriteriaQuery<Target> query = cb.createQuery(Target.class); final CriteriaQuery<Target> query = cb.createQuery(Target.class);
final Root<Target> targetRoot = query.from(Target.class); final Root<Target> targetRoot = query.from(Target.class);
@@ -771,7 +753,7 @@ public class TargetManagement {
// multiselect order) of the array and // multiselect order) of the array and
// the 2nd contains the selectCase int value. // the 2nd contains the selectCase int value.
final int pageSize = pageable.getPageSize(); final int pageSize = pageable.getPageSize();
final List<Target> resultList = entityManager.createQuery(query).setFirstResult(pageable.getOffset()) final List<Target> resultList = this.entityManager.createQuery(query).setFirstResult(pageable.getOffset())
.setMaxResults(pageSize + 1).getResultList(); .setMaxResults(pageSize + 1).getResultList();
final boolean hasNext = resultList.size() > pageSize; final boolean hasNext = resultList.size() > pageSize;
return new SliceImpl<>(resultList, pageable, hasNext); return new SliceImpl<>(resultList, pageable, hasNext);
@@ -800,7 +782,7 @@ public class TargetManagement {
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Long countTargetByAssignedDistributionSet(final Long distId) { public Long countTargetByAssignedDistributionSet(final Long distId) {
return targetRepository.countByAssignedDistributionSetId(distId); return this.targetRepository.countByAssignedDistributionSetId(distId);
} }
/** /**
@@ -813,7 +795,7 @@ public class TargetManagement {
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Long countTargetByInstalledDistributionSet(final Long distId) { public Long countTargetByInstalledDistributionSet(final Long distId) {
return targetRepository.countByTargetInfoInstalledDistributionSetId(distId); return this.targetRepository.countByTargetInfoInstalledDistributionSetId(distId);
} }
/** /**
@@ -824,10 +806,10 @@ public class TargetManagement {
*/ */
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public List<TargetIdName> findAllTargetIds() { public List<TargetIdName> findAllTargetIds() {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); final CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
final CriteriaQuery<TargetIdName> query = cb.createQuery(TargetIdName.class); final CriteriaQuery<TargetIdName> query = cb.createQuery(TargetIdName.class);
final Root<Target> targetRoot = query.from(Target.class); final Root<Target> targetRoot = query.from(Target.class);
return entityManager.createQuery(query.multiselect(targetRoot.get(Target_.id), return this.entityManager.createQuery(query.multiselect(targetRoot.get(Target_.id),
targetRoot.get(Target_.controllerId), targetRoot.get(Target_.name))).getResultList(); targetRoot.get(Target_.controllerId), targetRoot.get(Target_.name))).getResultList();
} }
@@ -860,7 +842,7 @@ public class TargetManagement {
public List<TargetIdName> findAllTargetIdsByFilters(final PageRequest pageRequest, public List<TargetIdName> findAllTargetIdsByFilters(final PageRequest pageRequest,
final Long filterByDistributionId, final Collection<TargetUpdateStatus> filterByStatus, final Long filterByDistributionId, final Collection<TargetUpdateStatus> filterByStatus,
final String filterBySearchText, final Boolean selectTargetWithNoTag, final String... filterByTagNames) { final String filterBySearchText, final Boolean selectTargetWithNoTag, final String... filterByTagNames) {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); final CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
final CriteriaQuery<Object[]> query = cb.createQuery(Object[].class); final CriteriaQuery<Object[]> query = cb.createQuery(Object[].class);
final Root<Target> targetRoot = query.from(Target.class); final Root<Target> targetRoot = query.from(Target.class);
List<Object[]> resultList; List<Object[]> resultList;
@@ -897,7 +879,7 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public List<TargetIdName> findAllTargetIdsByTargetFilterQuery(final PageRequest pageRequest, public List<TargetIdName> findAllTargetIdsByTargetFilterQuery(final PageRequest pageRequest,
@NotNull final TargetFilterQuery targetFilterQuery) { @NotNull final TargetFilterQuery targetFilterQuery) {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); final CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
final CriteriaQuery<Object[]> query = cb.createQuery(Object[].class); final CriteriaQuery<Object[]> query = cb.createQuery(Object[].class);
final Root<Target> targetRoot = query.from(Target.class); final Root<Target> targetRoot = query.from(Target.class);
final CriteriaQuery<Object[]> multiselect = query.multiselect(targetRoot.get(Target_.id), final CriteriaQuery<Object[]> multiselect = query.multiselect(targetRoot.get(Target_.id),
@@ -923,7 +905,7 @@ public class TargetManagement {
@PreDestroy @PreDestroy
void destroy() { void destroy() {
eventBus.unregister(this); this.eventBus.unregister(this);
} }
/** /**
@@ -952,12 +934,12 @@ public class TargetManagement {
public Target createTarget(@NotNull final Target target, @NotNull final TargetUpdateStatus status, public Target createTarget(@NotNull final Target target, @NotNull final TargetUpdateStatus status,
final Long lastTargetQuery, final URI address) { final Long lastTargetQuery, final URI address) {
if (targetRepository.findByControllerId(target.getControllerId()) != null) { if (this.targetRepository.findByControllerId(target.getControllerId()) != null) {
throw new EntityAlreadyExistsException(target.getControllerId()); throw new EntityAlreadyExistsException(target.getControllerId());
} }
target.setNew(true); target.setNew(true);
final Target savedTarget = targetRepository.save(target); final Target savedTarget = this.targetRepository.save(target);
final TargetInfo targetInfo = savedTarget.getTargetInfo(); final TargetInfo targetInfo = savedTarget.getTargetInfo();
targetInfo.setUpdateStatus(status); targetInfo.setUpdateStatus(status);
if (lastTargetQuery != null) { if (lastTargetQuery != null) {
@@ -967,7 +949,7 @@ public class TargetManagement {
targetInfo.setAddress(address.toString()); targetInfo.setAddress(address.toString());
} }
targetInfo.setNew(true); targetInfo.setNew(true);
final Target targetToReturn = targetInfoRepository.save(targetInfo).getTarget(); final Target targetToReturn = this.targetInfoRepository.save(targetInfo).getTarget();
targetInfo.setNew(false); targetInfo.setNew(false);
return targetToReturn; return targetToReturn;
@@ -1010,7 +992,7 @@ public class TargetManagement {
@NotNull @NotNull
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET)
public List<Target> createTargets(@NotNull final List<Target> targets) { public List<Target> createTargets(@NotNull final List<Target> targets) {
if (!targets.isEmpty() && targetRepository.countByControllerIdIn( if (!targets.isEmpty() && this.targetRepository.countByControllerIdIn(
targets.stream().map(target -> target.getControllerId()).collect(Collectors.toList())) > 0) { targets.stream().map(target -> target.getControllerId()).collect(Collectors.toList())) > 0) {
throw new EntityAlreadyExistsException(); throw new EntityAlreadyExistsException();
} }
@@ -1043,7 +1025,7 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET)
public List<Target> createTargets(@NotNull final Collection<Target> targets, public List<Target> createTargets(@NotNull final Collection<Target> targets,
@NotNull final TargetUpdateStatus status, final long lastTargetQuery, final URI address) { @NotNull final TargetUpdateStatus status, final long lastTargetQuery, final URI address) {
if (targetRepository.countByControllerIdIn( if (this.targetRepository.countByControllerIdIn(
targets.stream().map(target -> target.getControllerId()).collect(Collectors.toList())) > 0) { targets.stream().map(target -> target.getControllerId()).collect(Collectors.toList())) > 0) {
throw new EntityAlreadyExistsException(); throw new EntityAlreadyExistsException();
} }
@@ -1065,8 +1047,8 @@ public class TargetManagement {
@NotNull @NotNull
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public List<Target> findTargetsByTag(@NotNull final String tagName) { public List<Target> findTargetsByTag(@NotNull final String tagName) {
final TargetTag tag = targetTagRepository.findByNameEquals(tagName); final TargetTag tag = this.targetTagRepository.findByNameEquals(tagName);
return targetRepository.findByTag(tag); return this.targetRepository.findByTag(tag);
} }
/** /**
@@ -1079,7 +1061,7 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Long countTargetByTargetFilterQuery(@NotNull final TargetFilterQuery targetFilterQuery) { public Long countTargetByTargetFilterQuery(@NotNull final TargetFilterQuery targetFilterQuery) {
final Specification<Target> specs = RSQLUtility.parse(targetFilterQuery.getQuery(), TargetFields.class); final Specification<Target> specs = RSQLUtility.parse(targetFilterQuery.getQuery(), TargetFields.class);
return targetRepository.count(specs); return this.targetRepository.count(specs);
} }
/** /**
@@ -1092,7 +1074,7 @@ public class TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
public Long countTargetByTargetFilterQuery(@NotNull final String targetFilterQuery) { public Long countTargetByTargetFilterQuery(@NotNull final String targetFilterQuery) {
final Specification<Target> specs = RSQLUtility.parse(targetFilterQuery, TargetFields.class); final Specification<Target> specs = RSQLUtility.parse(targetFilterQuery, TargetFields.class);
return targetRepository.count(specs); return this.targetRepository.count(specs);
} }
private List<Object[]> getTargetIdNameResultSet(final PageRequest pageRequest, final CriteriaBuilder cb, private List<Object[]> getTargetIdNameResultSet(final PageRequest pageRequest, final CriteriaBuilder cb,
@@ -1109,10 +1091,10 @@ public class TargetManagement {
} }
} }
multiselect.orderBy(orders); multiselect.orderBy(orders);
resultList = entityManager.createQuery(multiselect).setFirstResult(pageRequest.getOffset()) resultList = this.entityManager.createQuery(multiselect).setFirstResult(pageRequest.getOffset())
.setMaxResults(pageRequest.getPageSize()).getResultList(); .setMaxResults(pageRequest.getPageSize()).getResultList();
} else { } else {
resultList = entityManager.createQuery(multiselect).getResultList(); resultList = this.entityManager.createQuery(multiselect).getResultList();
} }
return resultList; return resultList;
} }

View File

@@ -0,0 +1,46 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.specifications;
import java.util.List;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
/**
* Helper class to easily combine {@link Specification} instances.
*
*/
public final class SpecificationsBuilder {
private SpecificationsBuilder() {
}
/**
* Combine all given specification with and. The first specification is the
* where clause.
*
* @param specList
* all specification wich will combine
* @return <null> if the given specification list is empty
*/
public static <T> Specifications<T> combineWithAnd(final List<Specification<T>> specList) {
if (specList.isEmpty()) {
return null;
}
Specifications<T> specs = Specifications.where(specList.get(0));
specList.remove(0);
for (final Specification<T> specification : specList) {
specs = specs.and(specification);
}
return specs;
}
}