diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/TargetType.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/TargetType.java index ab6152e759..0cde5aceb0 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/TargetType.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/TargetType.java @@ -16,11 +16,17 @@ * {@link Target} * */ -public interface TargetType extends NamedEntity { +public interface TargetType extends Type { + /** - * Maximum length of color in Management UI. + * Target type doesn't support soft-delete so all target type instandces re not deleted. + * + * @return false */ - int COLOUR_MAX_SIZE = 16; + @Override + default boolean isDeleted() { + return false; + } /** * @return immutable set of optional {@link DistributionSetType}s @@ -32,18 +38,6 @@ public interface TargetType extends NamedEntity { */ Set getTargets(); - /** - * Checks if the given {@link DistributionSetType} is in - * {@link #getCompatibleDistributionSetTypes()}. - * - * @param distributionSetType - * search for - * @return true if found - */ - default boolean containsCompatibleDistributionSetType(final DistributionSetType distributionSetType) { - return containsCompatibleDistributionSetType(distributionSetType.getId()); - } - /** * Checks if the given {@link DistributionSetType} is in * {@link #getCompatibleDistributionSetTypes()}. @@ -64,9 +58,4 @@ default boolean containsCompatibleDistributionSetType(final Long distributionSet * @return the resulting target type */ TargetType removeDistributionSetType(final Long dsTypeId); - - /** - * @return get color code to be used in management UI views. - */ - String getColour(); } diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/AbstractJpaNamedEntity.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/AbstractJpaNamedEntity.java index 48bd866c90..de9d354e8b 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/AbstractJpaNamedEntity.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/AbstractJpaNamedEntity.java @@ -17,6 +17,8 @@ import org.eclipse.hawkbit.repository.model.NamedEntity; import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity; +import java.io.Serial; + /** * {@link TenantAwareBaseEntity} extension for all entities that are named in * addition to their technical ID. @@ -26,15 +28,17 @@ // sub entities @SuppressWarnings("squid:S2160") public abstract class AbstractJpaNamedEntity extends AbstractJpaTenantAwareBaseEntity implements NamedEntity { + + @Serial private static final long serialVersionUID = 1L; - @Column(name = "name", nullable = false, length = NamedEntity.NAME_MAX_SIZE) - @Size(min = 1, max = NamedEntity.NAME_MAX_SIZE) + @Column(name = "name", nullable = false, length = NAME_MAX_SIZE) + @Size(min = 1, max = NAME_MAX_SIZE) @NotNull private String name; - @Column(name = "description", length = NamedEntity.DESCRIPTION_MAX_SIZE) - @Size(max = NamedEntity.DESCRIPTION_MAX_SIZE) + @Column(name = "description", length = DESCRIPTION_MAX_SIZE) + @Size(max = DESCRIPTION_MAX_SIZE) private String description; /** diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/AbstractJpaTypeEntity.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/AbstractJpaTypeEntity.java new file mode 100644 index 0000000000..d9d4ac0968 --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/AbstractJpaTypeEntity.java @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.eclipse.hawkbit.repository.jpa.model; + +import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity; +import org.eclipse.hawkbit.repository.model.Type; + +import javax.persistence.Column; +import javax.persistence.MappedSuperclass; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import java.io.Serial; + +/** + * {@link TenantAwareBaseEntity} extension for all entities that are named in + * addition to their technical ID. + */ +@MappedSuperclass +// exception squid:S2160 - BaseEntity equals/hashcode is handling correctly for sub entities +@SuppressWarnings("squid:S2160") +public abstract class AbstractJpaTypeEntity extends AbstractJpaNamedEntity implements Type { + + @Serial + private static final long serialVersionUID = 1L; + + @Column(name = "type_key", nullable = false, updatable = false, length = KEY_MAX_SIZE) + @Size(min = 1, max = KEY_MAX_SIZE) + @NotNull + private String key; + + @Column(name = "colour", length = COLOUR_MAX_SIZE) + @Size(max = COLOUR_MAX_SIZE) + private String colour; + + /** + * Default constructor. + */ + protected AbstractJpaTypeEntity() { + // Default constructor needed for JPA entities + } + + /** + * Parameterized constructor. + * + * @param key + * of the {@link Type} + * @param colour + * of the {@link Type} + */ + AbstractJpaTypeEntity(final String name, final String description, final String key, final String colour) { + super(name, description); + this.key = key; + this.colour = colour; + } + + @Override + public String getKey() { + return key; + } + + @Override + public String getColour() { + return colour; + } + + public void setKey(final String key) { + this.key = key; + } + + public void setColour(final String colour) { + this.colour = colour; + } +} diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaDistributionSetType.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaDistributionSetType.java index 1492b7a1e7..624d152fbc 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaDistributionSetType.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaDistributionSetType.java @@ -31,8 +31,7 @@ import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Size; +import java.io.Serial; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -54,7 +53,9 @@ // exception squid:S2160 - BaseEntity equals/hashcode is handling correctly for // sub entities @SuppressWarnings("squid:S2160") -public class JpaDistributionSetType extends AbstractJpaNamedEntity implements DistributionSetType, EventAwareEntity { +public class JpaDistributionSetType extends AbstractJpaTypeEntity implements DistributionSetType, EventAwareEntity { + + @Serial private static final long serialVersionUID = 1L; @CascadeOnDelete @@ -62,15 +63,6 @@ public class JpaDistributionSetType extends AbstractJpaNamedEntity implements Di CascadeType.PERSIST }, fetch = FetchType.EAGER, orphanRemoval = true) private Set elements; - @Column(name = "type_key", nullable = false, updatable = false, length = DistributionSetType.KEY_MAX_SIZE) - @Size(min = 1, max = DistributionSetType.KEY_MAX_SIZE) - @NotNull - private String key; - - @Column(name = "colour", nullable = true, length = DistributionSetType.COLOUR_MAX_SIZE) - @Size(max = DistributionSetType.COLOUR_MAX_SIZE) - private String colour; - @Column(name = "deleted") private boolean deleted; @@ -109,9 +101,7 @@ public JpaDistributionSetType(final String key, final String name, final String * of the type. It will be null by default */ public JpaDistributionSetType(final String key, final String name, final String description, final String colour) { - super(name, description); - this.key = key; - this.colour = colour; + super(name, description, key, colour); } @Override @@ -205,15 +195,6 @@ public JpaDistributionSetType removeModuleType(final Long smTypeId) { return this; } - @Override - public String getKey() { - return key; - } - - public void setKey(final String key) { - this.key = key; - } - @Override public boolean checkComplete(final DistributionSet distributionSet) { final List smTypes = distributionSet.getModules().stream().map(SoftwareModule::getType) @@ -224,15 +205,6 @@ public boolean checkComplete(final DistributionSet distributionSet) { return smTypes.containsAll(getMandatoryModuleTypes()); } - @Override - public String getColour() { - return colour; - } - - public void setColour(final String colour) { - this.colour = colour; - } - public Set getElements() { if (elements == null) { return Collections.emptySet(); @@ -243,7 +215,7 @@ public Set getElements() { @Override public String toString() { - return "DistributionSetType [key=" + key + ", isDeleted()=" + isDeleted() + ", getId()=" + getId() + "]"; + return "DistributionSetType [key=" + getKey() + ", isDeleted()=" + isDeleted() + ", getId()=" + getId() + "]"; } @Override diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaSoftwareModuleType.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaSoftwareModuleType.java index 1816bfe63f..d04d3f0378 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaSoftwareModuleType.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaSoftwareModuleType.java @@ -15,8 +15,6 @@ import javax.persistence.Table; import javax.persistence.UniqueConstraint; import javax.validation.constraints.Min; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Size; import org.eclipse.hawkbit.repository.event.remote.SoftwareModuleTypeDeletedEvent; import org.eclipse.hawkbit.repository.event.remote.entity.SoftwareModuleTypeCreatedEvent; @@ -25,6 +23,8 @@ import org.eclipse.hawkbit.repository.model.helper.EventPublisherHolder; import org.eclipse.persistence.descriptors.DescriptorEvent; +import java.io.Serial; + /** * Type of a software modules. * @@ -38,22 +38,15 @@ // exception squid:S2160 - BaseEntity equals/hashcode is handling correctly for // sub entities @SuppressWarnings("squid:S2160") -public class JpaSoftwareModuleType extends AbstractJpaNamedEntity implements SoftwareModuleType, EventAwareEntity { - private static final long serialVersionUID = 1L; +public class JpaSoftwareModuleType extends AbstractJpaTypeEntity implements SoftwareModuleType, EventAwareEntity { - @Column(name = "type_key", nullable = false, length = SoftwareModuleType.KEY_MAX_SIZE) - @Size(min = 1, max = SoftwareModuleType.KEY_MAX_SIZE) - @NotNull - private String key; + @Serial + private static final long serialVersionUID = 1L; @Column(name = "max_ds_assignments", nullable = false) @Min(1) private int maxAssignments; - @Column(name = "colour", nullable = true, length = SoftwareModuleType.COLOUR_MAX_SIZE) - @Size(max = SoftwareModuleType.COLOUR_MAX_SIZE) - private String colour; - @Column(name = "deleted") private boolean deleted; @@ -90,11 +83,8 @@ public JpaSoftwareModuleType(final String key, final String name, final String d */ public JpaSoftwareModuleType(final String key, final String name, final String description, final int maxAssignments, final String colour) { - this.key = key; + super(name, description, key, colour); this.maxAssignments = maxAssignments; - setDescription(description); - setName(name); - this.colour = colour; } /** @@ -108,11 +98,6 @@ public void setMaxAssignments(final int maxAssignments) { this.maxAssignments = maxAssignments; } - @Override - public String getKey() { - return key; - } - @Override public int getMaxAssignments() { return maxAssignments; @@ -127,22 +112,9 @@ public void setDeleted(final boolean deleted) { this.deleted = deleted; } - @Override - public String getColour() { - return colour; - } - - public void setColour(final String colour) { - this.colour = colour; - } - @Override public String toString() { - return "SoftwareModuleType [key=" + key + ", getName()=" + getName() + ", getId()=" + getId() + "]"; - } - - public void setKey(final String key) { - this.key = key; + return "SoftwareModuleType [key=" + getKey() + ", getName()=" + getName() + ", getId()=" + getId() + "]"; } @Override diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaTargetType.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaTargetType.java index 495c27d6a5..65942fd85e 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaTargetType.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaTargetType.java @@ -19,7 +19,6 @@ import org.eclipse.persistence.annotations.CascadeOnDelete; import org.eclipse.persistence.descriptors.DescriptorEvent; -import javax.persistence.Column; import javax.persistence.ConstraintMode; import javax.persistence.Entity; import javax.persistence.FetchType; @@ -31,7 +30,7 @@ import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; -import javax.validation.constraints.Size; +import java.io.Serial; import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -45,14 +44,11 @@ @Table(name = "sp_target_type", indexes = { @Index(name = "sp_idx_target_type_prim", columnList = "tenant,id") }, uniqueConstraints = { @UniqueConstraint(columnNames = { "name", "tenant" }, name = "uk_target_type_name")}) -public class JpaTargetType extends AbstractJpaNamedEntity implements TargetType, EventAwareEntity{ +public class JpaTargetType extends AbstractJpaTypeEntity implements TargetType, EventAwareEntity { + @Serial private static final long serialVersionUID = 1L; - @Column(name = "colour", nullable = true, length = TargetType.COLOUR_MAX_SIZE) - @Size(max = TargetType.COLOUR_MAX_SIZE) - private String colour; - @CascadeOnDelete @ManyToMany(targetEntity = JpaDistributionSetType.class) @JoinTable(name = "sp_target_type_ds_type_relation", joinColumns = { @@ -74,15 +70,30 @@ public JpaTargetType() { * Constructor * * @param name - * Type name + * of the type + * @param description + * of the type + * @param colour + * of the type + */ + public JpaTargetType(final String name, final String description, final String colour) { + this(name, name, description, colour); + } + + /** + * Constructor. + * + * @param key + * of the type + * @param name + * of the type * @param description - * Description + * of the type * @param colour - * Colour + * of the type. It will be null by default */ - public JpaTargetType(String name, String description, String colour) { - super(name,description); - this.colour = colour; + public JpaTargetType(final String key, final String name, final String description, final String colour) { + super(name, description, key, colour); } /** @@ -90,13 +101,12 @@ public JpaTargetType(String name, String description, String colour) { * Distribution set type * @return Target type */ - public JpaTargetType addCompatibleDistributionSetType(final DistributionSetType dsSetType) { + public void addCompatibleDistributionSetType(final DistributionSetType dsSetType) { if (distributionSetTypes == null) { distributionSetTypes = new HashSet<>(); } distributionSetTypes.add(dsSetType); - return this; } /** @@ -129,28 +139,24 @@ public Set getTargets() { } @Override - public String getColour() { - return colour; - } - - public void setColour(final String colour) { - this.colour = colour; + public String toString() { + return "TargetType [key=" + getKey() + ", isDeleted()=" + isDeleted() + ", getId()=" + getId() + "]"; } @Override - public void fireCreateEvent(DescriptorEvent descriptorEvent) { + public void fireCreateEvent(final DescriptorEvent descriptorEvent) { EventPublisherHolder.getInstance().getEventPublisher().publishEvent( new TargetTypeCreatedEvent(this, EventPublisherHolder.getInstance().getApplicationId())); } @Override - public void fireUpdateEvent(DescriptorEvent descriptorEvent) { + public void fireUpdateEvent(final DescriptorEvent descriptorEvent) { EventPublisherHolder.getInstance().getEventPublisher().publishEvent( new TargetTypeUpdatedEvent(this, EventPublisherHolder.getInstance().getApplicationId())); } @Override - public void fireDeleteEvent(DescriptorEvent descriptorEvent) { + public void fireDeleteEvent(final DescriptorEvent descriptorEvent) { EventPublisherHolder.getInstance().getEventPublisher().publishEvent(new TargetTypeDeletedEvent( getTenant(), getId(), getClass(), EventPublisherHolder.getInstance().getApplicationId())); } diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/DB2/V1_12_27__target_type_inherit_type___DB2.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/DB2/V1_12_27__target_type_inherit_type___DB2.sql new file mode 100644 index 0000000000..86e3350b88 --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/DB2/V1_12_27__target_type_inherit_type___DB2.sql @@ -0,0 +1,3 @@ +ALTER TABLE sp_target_type ADD COLUMN type_key VARCHAR (64) NOT NULL DEFAULT ('_'); +UPDATE sp_target_type SET type_key = name; +ALTER TABLE sp_target_type ADD CONSTRAINT uk_target_type_key UNIQUE (type_key, tenant); \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_12_27__target_type_inherit_type___H2.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_12_27__target_type_inherit_type___H2.sql new file mode 100644 index 0000000000..86e3350b88 --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_12_27__target_type_inherit_type___H2.sql @@ -0,0 +1,3 @@ +ALTER TABLE sp_target_type ADD COLUMN type_key VARCHAR (64) NOT NULL DEFAULT ('_'); +UPDATE sp_target_type SET type_key = name; +ALTER TABLE sp_target_type ADD CONSTRAINT uk_target_type_key UNIQUE (type_key, tenant); \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_12_27__target_type_inherit_type___MYSQL.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_12_27__target_type_inherit_type___MYSQL.sql new file mode 100644 index 0000000000..86e3350b88 --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_12_27__target_type_inherit_type___MYSQL.sql @@ -0,0 +1,3 @@ +ALTER TABLE sp_target_type ADD COLUMN type_key VARCHAR (64) NOT NULL DEFAULT ('_'); +UPDATE sp_target_type SET type_key = name; +ALTER TABLE sp_target_type ADD CONSTRAINT uk_target_type_key UNIQUE (type_key, tenant); \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/POSTGRESQL/V1_12_27__target_type_inherit_type___POSTGRESQL.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/POSTGRESQL/V1_12_27__target_type_inherit_type___POSTGRESQL.sql new file mode 100644 index 0000000000..86e3350b88 --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/POSTGRESQL/V1_12_27__target_type_inherit_type___POSTGRESQL.sql @@ -0,0 +1,3 @@ +ALTER TABLE sp_target_type ADD COLUMN type_key VARCHAR (64) NOT NULL DEFAULT ('_'); +UPDATE sp_target_type SET type_key = name; +ALTER TABLE sp_target_type ADD CONSTRAINT uk_target_type_key UNIQUE (type_key, tenant); \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/SQL_SERVER/V1_12_27__target_type_inherit_type___SQL_SERVER.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/SQL_SERVER/V1_12_27__target_type_inherit_type___SQL_SERVER.sql new file mode 100644 index 0000000000..f64339420a --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/SQL_SERVER/V1_12_27__target_type_inherit_type___SQL_SERVER.sql @@ -0,0 +1,3 @@ +ALTER TABLE sp_target_type ADD type_key VARCHAR (64) NOT NULL DEFAULT ('_'); +UPDATE sp_target_type SET type_key = name; +ALTER TABLE sp_target_type ADD CONSTRAINT uk_target_type_key UNIQUE (type_key, tenant); \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/SoftwareModuleTypeManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/SoftwareModuleTypeManagementTest.java index a838a0daea..b6298351ba 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/SoftwareModuleTypeManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/SoftwareModuleTypeManagementTest.java @@ -74,14 +74,14 @@ public void updateNothingResultsInUnchangedRepositoryForType() { @Test @Description("Calling update for changed fields results in change in the repository.") - public void updateSoftareModuleTypeFieldsToNewValue() { + public void updateSoftwareModuleTypeFieldsToNewValue() { final SoftwareModuleType created = softwareModuleTypeManagement .create(entityFactory.softwareModuleType().create().key("test-key").name("test-name")); final SoftwareModuleType updated = softwareModuleTypeManagement.update( entityFactory.softwareModuleType().update(created.getId()).description("changed").colour("changed")); - assertThat(updated.getOptLockRevision()).as("Expected version number of updated entitity is") + assertThat(updated.getOptLockRevision()).as("Expected version number of updated entities is") .isEqualTo(created.getOptLockRevision() + 1); assertThat(updated.getDescription()).as("Updated description is").isEqualTo("changed"); assertThat(updated.getColour()).as("Updated vendor is").isEqualTo("changed");