Skip to content

Commit

Permalink
Move Target tag deprecations to end of classes (#1882)
Browse files Browse the repository at this point in the history
Signed-off-by: Marinov Avgustin <[email protected]>
  • Loading branch information
avgustinmm authored Oct 16, 2024
1 parent 745663f commit 8d9f076
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -632,22 +632,6 @@ Slice<Target> findByFilterOrderByLinkedDistributionSet(@NotNull Pageable pageabl
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
Page<Target> findByRsqlAndTag(@NotNull Pageable pageable, @NotNull String rsqlParam, long tagId);

/**
* Toggles {@link TargetTag} assignment to given {@link Target}s by means that
* if some (or all) of the targets in the list have the {@link Tag} not yet
* assigned, they will be. Only if all of them have the tag already assigned
* they will be removed instead.
*
* @deprecated since 0.6.0 - not very usable with very unclear logic
* @param controllerIds to toggle for
* @param tagName to toggle
* @return TagAssigmentResult with all metadata of the assignment outcome.
* @throws EntityNotFoundException if tag with given name does not exist
*/
@Deprecated
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
TargetTagAssignmentResult toggleTagAssignment(@NotEmpty Collection<String> controllerIds, @NotEmpty String tagName);

/**
* Initiates {@link TargetType} assignment to given {@link Target}s. If some
* targets in the list have the {@link TargetType} not yet assigned, they will
Expand Down Expand Up @@ -705,19 +689,6 @@ Slice<Target> findByFilterOrderByLinkedDistributionSet(@NotNull Pageable pageabl
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
List<Target> unassignTag(@NotEmpty List<String> controllerIds, long targetTagId);

/**
* Un-assign a {@link TargetTag} assignment to given {@link Target}.
*
* @deprecated since 0.6.0 - use {@link #unassignTag(List, long)} instead
* @param controllerId to un-assign for
* @param targetTagId to un-assign
* @return the unassigned target or <null> if no target is unassigned
* @throws EntityNotFoundException if TAG with given ID does not exist
*/
@Deprecated(forRemoval = true)
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
Target unassignTag(@NotEmpty String controllerId, long targetTagId);

/**
* Un-assign a {@link TargetType} assignment to given {@link Target}.
*
Expand Down Expand Up @@ -991,4 +962,33 @@ Page<TargetMetadata> findMetaDataByControllerIdAndRsql(@NotNull Pageable pageabl
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_REPOSITORY)
TargetMetadata updateMetadata(@NotEmpty String controllerId, @NotNull MetaData metadata);
}

/**
* Toggles {@link TargetTag} assignment to given {@link Target}s by means that
* if some (or all) of the targets in the list have the {@link Tag} not yet
* assigned, they will be. Only if all of them have the tag already assigned
* they will be removed instead.
*
* @deprecated since 0.6.0 - not very usable with very unclear logic
* @param controllerIds to toggle for
* @param tagName to toggle
* @return TagAssigmentResult with all metadata of the assignment outcome.
* @throws EntityNotFoundException if tag with given name does not exist
*/
@Deprecated
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
TargetTagAssignmentResult toggleTagAssignment(@NotEmpty Collection<String> controllerIds, @NotEmpty String tagName);

/**
* Un-assign a {@link TargetTag} assignment to given {@link Target}.
*
* @deprecated since 0.6.0 - use {@link #unassignTag(List, long)} instead
* @param controllerId to un-assign for
* @param targetTagId to un-assign
* @return the unassigned target or <null> if no target is unassigned
* @throws EntityNotFoundException if TAG with given ID does not exist
*/
@Deprecated(forRemoval = true)
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
Target unassignTag(@NotEmpty String controllerId, long targetTagId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@

/**
* Management service for {@link TargetTag}s.
*
*/
public interface TargetTagManagement {

/**
* count {@link TargetTag}s.
* Count {@link TargetTag}s.
*
* @return size of {@link TargetTag}s
*/
Expand All @@ -49,54 +48,38 @@ public interface TargetTagManagement {
/**
* Creates a new {@link TargetTag}.
*
* @param create
* to be created
*
* @param create to be created
* @return the new created {@link TargetTag}
*
* @throws EntityAlreadyExistsException
* if given object already exists
* @throws ConstraintViolationException
* if fields are not filled as specified. Check
* {@link TagCreate} for field constraints.
* @throws EntityAlreadyExistsException if given object already exists
* @throws ConstraintViolationException if fields are not filled as specified. Check {@link TagCreate} for field constraints.
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET)
TargetTag create(@NotNull @Valid TagCreate create);

/**
* created multiple {@link TargetTag}s.
* Created multiple {@link TargetTag}s.
*
* @param creates
* to be created
* @param creates to be created
* @return the new created {@link TargetTag}s
*
* @throws EntityAlreadyExistsException
* if given object has already an ID.
* @throws ConstraintViolationException
* if fields are not filled as specified. Check
* {@link TagCreate} for field constraints.
* @throws EntityAlreadyExistsException if given object has already an ID.
* @throws ConstraintViolationException if fields are not filled as specified. Check {@link TagCreate} for field constraints.
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET)
List<TargetTag> create(@NotNull @Valid Collection<TagCreate> creates);

/**
* Deletes {@link TargetTag} with given name.
*
* @param targetTagName
* tag name of the {@link TargetTag} to be deleted
*
* @throws EntityNotFoundException
* if tag with given name does not exist
* @param targetTagName tag name of the {@link TargetTag} to be deleted
* @throws EntityNotFoundException if tag with given name does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_DELETE_TARGET)
void delete(@NotEmpty String targetTagName);

/**
* returns all {@link TargetTag}s.
*
* @param pageable
* page parameter
*
* @param pageable page parameter
* @return all {@link TargetTag}s
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
Expand All @@ -105,27 +88,20 @@ public interface TargetTagManagement {
/**
* Retrieves all target tags based on the given specification.
*
* @param pageable
* pagination parameter
* @param rsqlParam
* rsql query string
*
* @param pageable pagination parameter
* @param rsqlParam rsql query string
* @return the found {@link Target}s, never {@code null}
*
* @throws RSQLParameterUnsupportedFieldException
* if a field in the RSQL string is used but not provided by the
* @throws RSQLParameterUnsupportedFieldException if a field in the RSQL string is used but not provided by the
* given {@code fieldNameProvider}
* @throws RSQLParameterSyntaxException
* if the RSQL syntax is wrong
* @throws RSQLParameterSyntaxException if the RSQL syntax is wrong
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
Page<TargetTag> findByRsql(@NotNull Pageable pageable, @NotNull String rsqlParam);

/**
* Find {@link TargetTag} based on given Name.
*
* @param name
* to look for.
* @param name to look for.
* @return {@link TargetTag}
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
Expand All @@ -134,8 +110,7 @@ public interface TargetTagManagement {
/**
* Finds {@link TargetTag} by given id.
*
* @param id
* to search for
* @param id to search for
* @return the found {@link TargetTag}
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
Expand All @@ -144,28 +119,21 @@ public interface TargetTagManagement {
/**
* Finds {@link TargetTag} by given ids.
*
* @param ids
* the ids to for
* @param ids the ids to for
* @return the found {@link TargetTag}s
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET)
List<TargetTag> get(@NotEmpty Collection<Long> ids);

/**
* updates the {@link TargetTag}.
* Updates the {@link TargetTag}.
*
* @param update
* the {@link TargetTag} with updated values
* @param update the {@link TargetTag} with updated values
* @return the updated {@link TargetTag}
*
* @throws EntityNotFoundException
* in case the {@link TargetTag} does not exists and cannot be
* updated
* @throws ConstraintViolationException
* if fields are not filled as specified. Check
* {@link TagUpdate} for field constraints.
* @throws EntityNotFoundException in case the {@link TargetTag} does not exist and cannot be updated
* @throws ConstraintViolationException if fields are not filled as specified. Check {@link TagUpdate} for field constraints.
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
TargetTag update(@NotNull @Valid TagUpdate update);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -478,43 +478,6 @@ private static boolean hasNoTypeFilterActive(final FilterParams filterParams) {
return Boolean.TRUE.equals(filterParams.getSelectTargetWithNoTargetType());
}

@Override
@Transactional
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public TargetTagAssignmentResult toggleTagAssignment(final Collection<String> controllerIds, final String tagName) {
final TargetTag tag = targetTagRepository
.findByNameEquals(tagName)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagName));
final List<JpaTarget> allTargets = targetRepository
.findAll(TargetSpecifications.byControllerIdWithTagsInJoin(controllerIds));
if (allTargets.size() < controllerIds.size()) {
throw new EntityNotFoundException(Target.class, controllerIds,
allTargets.stream().map(Target::getControllerId).toList());
}

final List<JpaTarget> alreadyAssignedTargets = targetRepository.findAll(
TargetSpecifications.hasTagName(tagName).and(TargetSpecifications.hasControllerIdIn(controllerIds)));

// all are already assigned -> unassign
if (alreadyAssignedTargets.size() == allTargets.size()) {

alreadyAssignedTargets.forEach(target -> target.removeTag(tag));
return new TargetTagAssignmentResult(0, Collections.emptyList(),
Collections.unmodifiableList(alreadyAssignedTargets), tag);
}

allTargets.removeAll(alreadyAssignedTargets);
// some or none are assigned -> assign
allTargets.forEach(target -> target.addTag(tag));
final TargetTagAssignmentResult result = new TargetTagAssignmentResult(alreadyAssignedTargets.size(),
targetRepository.saveAll(allTargets), Collections.emptyList(), tag);

// no reason to persist the tag
entityManager.detach(tag);
return result;
}

@Override
@Transactional
@Retryable(include = {
Expand Down Expand Up @@ -619,25 +582,6 @@ public List<Target> unassignTag(final List<String> controllerIds, final long tar
return result;
}

@Override
@Transactional
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public Target unassignTag(final String controllerId, final long targetTagId) {
final JpaTarget target = getByControllerIdAndThrowIfNotFound(controllerId);

final TargetTag tag = targetTagRepository.findById(targetTagId)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));

target.removeTag(tag);

final Target result = targetRepository.save(target);

// No reason to save the tag
entityManager.detach(tag);
return result;
}

@Override
@Transactional
@Retryable(include = {
Expand Down Expand Up @@ -974,4 +918,60 @@ public Page<Target> findByControllerAttributesRequested(final Pageable pageReq)
return JpaManagementHelper.findAllWithCountBySpec(targetRepository, pageReq,
List.of(TargetSpecifications.hasRequestControllerAttributesTrue()));
}

@Override
@Transactional
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public TargetTagAssignmentResult toggleTagAssignment(final Collection<String> controllerIds, final String tagName) {
final TargetTag tag = targetTagRepository
.findByNameEquals(tagName)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagName));
final List<JpaTarget> allTargets = targetRepository
.findAll(TargetSpecifications.byControllerIdWithTagsInJoin(controllerIds));
if (allTargets.size() < controllerIds.size()) {
throw new EntityNotFoundException(Target.class, controllerIds,
allTargets.stream().map(Target::getControllerId).toList());
}

final List<JpaTarget> alreadyAssignedTargets = targetRepository.findAll(
TargetSpecifications.hasTagName(tagName).and(TargetSpecifications.hasControllerIdIn(controllerIds)));

// all are already assigned -> unassign
if (alreadyAssignedTargets.size() == allTargets.size()) {

alreadyAssignedTargets.forEach(target -> target.removeTag(tag));
return new TargetTagAssignmentResult(0, Collections.emptyList(),
Collections.unmodifiableList(alreadyAssignedTargets), tag);
}

allTargets.removeAll(alreadyAssignedTargets);
// some or none are assigned -> assign
allTargets.forEach(target -> target.addTag(tag));
final TargetTagAssignmentResult result = new TargetTagAssignmentResult(alreadyAssignedTargets.size(),
targetRepository.saveAll(allTargets), Collections.emptyList(), tag);

// no reason to persist the tag
entityManager.detach(tag);
return result;
}

@Override
@Transactional
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public Target unassignTag(final String controllerId, final long targetTagId) {
final JpaTarget target = getByControllerIdAndThrowIfNotFound(controllerId);

final TargetTag tag = targetTagRepository.findById(targetTagId)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));

target.removeTag(tag);

final Target result = targetRepository.save(target);

// No reason to save the tag
entityManager.detach(tag);
return result;
}
}
Loading

0 comments on commit 8d9f076

Please sign in to comment.