diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetManagement.java
index 58be29bca..7194fab9e 100644
--- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetManagement.java
+++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetManagement.java
@@ -125,6 +125,20 @@ public interface TargetManagement {
+ SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
long countByInstalledDistributionSet(long distId);
+ /**
+ * Checks if there is already a {@link Target} that has the given distribution set Id assigned or installed.
+ *
+ * @param distId
+ * to search for
+ * @return true if a {@link Target} exists.
+ *
+ * @throws EntityNotFoundException
+ * if distribution set with given ID does not exist
+ */
+ @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET + SpringEvalExpressions.HAS_AUTH_OR
+ + SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
+ boolean existsByInstalledOrAssignedDistributionSet(long distId);
+
/**
* Count {@link TargetFilterQuery}s for given target filter query.
*
diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetManagement.java
index 8e1450480..e2549c32f 100644
--- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetManagement.java
+++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetManagement.java
@@ -646,6 +646,13 @@ public class JpaTargetManagement implements TargetManagement {
return targetRepository.countByInstalledDistributionSetId(distId);
}
+ @Override
+ public boolean existsByInstalledOrAssignedDistributionSet(final long distId) {
+ throwEntityNotFoundIfDsDoesNotExist(distId);
+
+ return targetRepository.existsByInstalledOrAssignedDistributionSet(distId);
+ }
+
@Override
public Page findByTargetFilterQueryAndNonDS(final Pageable pageRequest, final long distributionSetId,
final String targetFilterQuery) {
diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/TargetRepository.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/TargetRepository.java
index 59146744e..474920f76 100644
--- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/TargetRepository.java
+++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/TargetRepository.java
@@ -208,6 +208,16 @@ public interface TargetRepository extends BaseEntityRepository,
*/
Long countByInstalledDistributionSetId(Long distId);
+ /**
+ * Checks if there is already a {@link Target} that has the given distribution set Id assigned or installed.
+ *
+ * @param distId
+ * to check
+ * @return true if a {@link Target} exists.
+ */
+ @Query("SELECT CASE WHEN COUNT(t)>0 THEN 'true' ELSE 'false' END FROM JpaTarget t WHERE t.installedDistributionSet.id=:distId OR t.assignedDistributionSet.id=:distId")
+ boolean existsByInstalledOrAssignedDistributionSet(@Param("distId") Long distId);
+
/**
* Finds all {@link Target}s in the repository.
*
diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/TargetManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/TargetManagementTest.java
index 3bb78a538..992af5e1d 100644
--- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/TargetManagementTest.java
+++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/TargetManagementTest.java
@@ -39,9 +39,9 @@ import org.eclipse.hawkbit.repository.event.remote.entity.SoftwareModuleCreatedE
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent;
+import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.InvalidTargetAddressException;
-import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.TenantNotExistException;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetMetadata;
@@ -108,6 +108,8 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
"DistributionSet");
verifyThrownExceptionBy(() -> targetManagement.countByInstalledDistributionSet(NOT_EXIST_IDL),
"DistributionSet");
+ verifyThrownExceptionBy(() -> targetManagement.existsByInstalledOrAssignedDistributionSet(NOT_EXIST_IDL),
+ "DistributionSet");
verifyThrownExceptionBy(() -> targetManagement.countByTargetFilterQuery(NOT_EXIST_IDL), "TargetFilterQuery");
verifyThrownExceptionBy(() -> targetManagement.countByRsqlAndNonDS(NOT_EXIST_IDL, "name==*"),
@@ -465,10 +467,14 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
.isEqualTo(0);
assertThat(targetManagement.countByInstalledDistributionSet(set.getId())).as("Target count is wrong")
.isEqualTo(0);
+ assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(set.getId())).as("Target count is wrong")
+ .isFalse();
assertThat(targetManagement.countByAssignedDistributionSet(set2.getId())).as("Target count is wrong")
.isEqualTo(0);
assertThat(targetManagement.countByInstalledDistributionSet(set2.getId())).as("Target count is wrong")
.isEqualTo(0);
+ assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(set2.getId())).as("Target count is wrong")
+ .isFalse();
Target target = createTargetWithAttributes("4711");
@@ -488,10 +494,14 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
.isEqualTo(0);
assertThat(targetManagement.countByInstalledDistributionSet(set.getId())).as("Target count is wrong")
.isEqualTo(1);
+ assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(set.getId())).as("Target count is wrong")
+ .isTrue();
assertThat(targetManagement.countByAssignedDistributionSet(set2.getId())).as("Target count is wrong")
.isEqualTo(1);
assertThat(targetManagement.countByInstalledDistributionSet(set2.getId())).as("Target count is wrong")
.isEqualTo(0);
+ assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(set2.getId())).as("Target count is wrong")
+ .isTrue();
assertThat(target.getLastTargetQuery()).as("Target query is not work").isGreaterThanOrEqualTo(current);
assertThat(deploymentManagement.getAssignedDistributionSet("4711").get()).as("Assigned ds size is wrong")
.isEqualTo(set2);
diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/grid/support/assignment/SwModulesToDistributionSetAssignmentSupport.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/grid/support/assignment/SwModulesToDistributionSetAssignmentSupport.java
index b717c01e6..cb3d458ac 100644
--- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/grid/support/assignment/SwModulesToDistributionSetAssignmentSupport.java
+++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/grid/support/assignment/SwModulesToDistributionSetAssignmentSupport.java
@@ -114,8 +114,8 @@ public class SwModulesToDistributionSetAssignmentSupport
return false;
}
- if (targetManagement.countByFilters(null, null, null, ds.getId(), Boolean.FALSE, "") > 0) {
- /* Distribution is already assigned */
+ if (targetManagement.existsByInstalledOrAssignedDistributionSet(ds.getId())) {
+ /* Distribution is already assigned/installed */
notification.displayValidationError(i18n.getMessage("message.dist.inuse", ds.getNameVersion()));
return false;
}