[#1778] Prevent deletion of Software Module of locked DS (#1793)

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-08-01 08:57:50 +03:00
committed by GitHub
parent 6106d3c16c
commit ae09e2fbef
4 changed files with 69 additions and 44 deletions

View File

@@ -35,6 +35,7 @@ import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.eclipse.hawkbit.repository.event.remote.SoftwareModuleDeletedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.SoftwareModuleCreatedEvent;
@@ -75,20 +76,22 @@ public class JpaSoftwareModule extends AbstractJpaNamedVersionedEntity implement
private static final String DELETED_PROPERTY = "deleted";
@Setter
@ManyToOne
@JoinColumn(name = "module_type", nullable = false, updatable = false, foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_module_type"))
@NotNull
private JpaSoftwareModuleType type;
@CascadeOnDelete
@OneToMany(fetch = FetchType.LAZY, mappedBy = "softwareModule", cascade = {
CascadeType.PERSIST }, targetEntity = JpaArtifact.class, orphanRemoval = true)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "softwareModule", cascade = { CascadeType.PERSIST }, targetEntity = JpaArtifact.class, orphanRemoval = true)
private List<JpaArtifact> artifacts;
@Column(name = "vendor", nullable = true, length = SoftwareModule.VENDOR_MAX_SIZE)
@Setter
@Column(name = "vendor", length = SoftwareModule.VENDOR_MAX_SIZE)
@Size(max = SoftwareModule.VENDOR_MAX_SIZE)
private String vendor;
@Setter
@Column(name = "encrypted")
private boolean encrypted;
@@ -127,17 +130,9 @@ public class JpaSoftwareModule extends AbstractJpaNamedVersionedEntity implement
this.encrypted = encrypted;
}
public void setType(final JpaSoftwareModuleType type) {
this.type = type;
}
@Override
public List<Artifact> getArtifacts() {
if (artifacts == null) {
return Collections.emptyList();
}
return Collections.unmodifiableList(artifacts);
return artifacts == null ? Collections.emptyList() : Collections.unmodifiableList(artifacts);
}
public void addArtifact(final Artifact artifact) {
@@ -145,14 +140,15 @@ public class JpaSoftwareModule extends AbstractJpaNamedVersionedEntity implement
throw new LockedException(JpaSoftwareModule.class, getId(), "ADD_ARTIFACT");
}
if (artifacts == null) {
artifacts = new ArrayList<>(4);
artifacts.add((JpaArtifact) artifact);
return;
}
if (!artifacts.contains(artifact)) {
artifacts.add((JpaArtifact) artifact);
if (artifact instanceof JpaArtifact jpaArtifact) {
if (artifacts == null) {
artifacts = new ArrayList<>(4);
artifacts.add(jpaArtifact);
} else if (!artifacts.contains(jpaArtifact)) {
artifacts.add(jpaArtifact);
}
} else {
throw new UnsupportedOperationException("Only JpaArtifact is supported");
}
}
@@ -169,10 +165,6 @@ public class JpaSoftwareModule extends AbstractJpaNamedVersionedEntity implement
}
}
public void setVendor(final String vendor) {
this.vendor = vendor;
}
public void lock() {
locked = true;
}
@@ -181,28 +173,29 @@ public class JpaSoftwareModule extends AbstractJpaNamedVersionedEntity implement
locked = false;
}
/**
* Marks or un-marks this software module as deleted.
*
* @param deleted
* {@code true} if the software module should be marked as deleted
* otherwise {@code false}
*/
public void setDeleted(final boolean deleted) {
if (assignedTo != null) {
final List<DistributionSet> lockedDS = assignedTo.stream()
.filter(DistributionSet::isLocked)
.filter(ds -> !ds.isDeleted())
.toList();
if (!lockedDS.isEmpty()) {
final StringBuilder sb = new StringBuilder("Part of ");
if (lockedDS.size() == 1) {
sb.append("a locked distribution set: ");
} else {
sb.append(lockedDS.size()).append(" locked distribution sets: ");
}
for (final DistributionSet ds : lockedDS) {
sb.append(ds.getName()).append(":").append(ds.getVersion()).append(" (").append(ds.getId()).append("), ");
}
sb.delete(sb.length() - 2, sb.length());
throw new LockedException(JpaSoftwareModule.class, getId(), "DELETE", sb.toString());
};
}
this.deleted = deleted;
}
/**
* Marks this software module as encrypted.
*
* @param encrypted
* {@code true} if the software module should be marked as encrypted
* otherwise {@code false}
*/
public void setEncrypted(final boolean encrypted) {
this.encrypted = encrypted;
}
@Override
public void fireCreateEvent(final DescriptorEvent descriptorEvent) {
EventPublisherHolder.getInstance().getEventPublisher().publishEvent(