SystemManagement getTenantMetadata - fetch details, added method getTenantMetadataWithoutDetails (#2194)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -623,7 +623,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
return distributionSetRepository.countAutoAssignmentsForDistributionSet(id);
|
||||
}
|
||||
|
||||
// check if shall implicitly lock a distribution set
|
||||
// check if it shall implicitly lock a distribution set
|
||||
boolean isImplicitLockApplicable(final DistributionSet distributionSet) {
|
||||
final JpaDistributionSet jpaDistributionSet = (JpaDistributionSet) distributionSet;
|
||||
if (jpaDistributionSet.isLocked()) {
|
||||
@@ -631,7 +631,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!tenantConfigHelper.getConfigValue(IMPLICIT_LOCK_ENABLED, Boolean.class)) {
|
||||
if (Boolean.FALSE.equals(tenantConfigHelper.getConfigValue(IMPLICIT_LOCK_ENABLED, Boolean.class))) {
|
||||
// implicit lock disabled
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -173,8 +173,7 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst
|
||||
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
// Exception squid:S2229 - calling findTenants without transaction is
|
||||
// intended in this case
|
||||
// Exception squid:S2229 - calling findTenants without transaction is intended in this case
|
||||
@SuppressWarnings("squid:S2229")
|
||||
public void forEachTenant(final Consumer<String> consumer) {
|
||||
Page<String> tenants;
|
||||
@@ -207,7 +206,6 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst
|
||||
|
||||
@Override
|
||||
public SystemUsageReport getSystemUsageStatistics() {
|
||||
|
||||
final Number count = (Number) entityManager.createNativeQuery(countSoftwareModulesQuery).getSingleResult();
|
||||
|
||||
long sumOfArtifacts = 0;
|
||||
@@ -232,22 +230,12 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst
|
||||
|
||||
@Override
|
||||
public TenantMetaData getTenantMetadata() {
|
||||
final String tenant = tenantAware.getCurrentTenant();
|
||||
if (tenant == null) {
|
||||
throw new IllegalStateException("Tenant not set");
|
||||
}
|
||||
return getTenantMetadata0(true);
|
||||
}
|
||||
|
||||
final TenantMetaData metaData = tenantMetaDataRepository.findByTenantIgnoreCase(tenant);
|
||||
if (metaData == null) {
|
||||
if (repositoryProperties.isImplicitTenantCreateAllowed()) {
|
||||
log.info("Tenant {} doesn't exist create metadata", tenant, new Exception("Thread dump"));
|
||||
return createTenantMetadata0(tenant);
|
||||
} else {
|
||||
throw new EntityNotFoundException(TenantMetaData.class, tenant);
|
||||
}
|
||||
} else {
|
||||
return metaData;
|
||||
}
|
||||
@Override
|
||||
public TenantMetaData getTenantMetadataWithoutDetails() {
|
||||
return getTenantMetadata0(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -265,11 +253,9 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst
|
||||
@Retryable(retryFor = { ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX,
|
||||
backoff = @Backoff(delay = Constants.TX_RT_DELAY))
|
||||
public TenantMetaData updateTenantMetadata(final long defaultDsType) {
|
||||
final JpaTenantMetaData data = (JpaTenantMetaData) getTenantMetadata();
|
||||
|
||||
final JpaTenantMetaData data = (JpaTenantMetaData) getTenantMetadataWithoutDetails();
|
||||
data.setDefaultDsType(distributionSetTypeRepository.findById(defaultDsType)
|
||||
.orElseThrow(() -> new EntityNotFoundException(DistributionSetType.class, defaultDsType)));
|
||||
|
||||
return tenantMetaDataRepository.save(data);
|
||||
}
|
||||
|
||||
@@ -283,6 +269,28 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst
|
||||
return Database.POSTGRESQL == properties.getDatabase();
|
||||
}
|
||||
|
||||
private TenantMetaData getTenantMetadata0(final boolean withDetails) {
|
||||
final String tenant = tenantAware.getCurrentTenant();
|
||||
if (tenant == null) {
|
||||
throw new IllegalStateException("Tenant not set");
|
||||
}
|
||||
|
||||
final TenantMetaData metaData =
|
||||
withDetails ?
|
||||
tenantMetaDataRepository.findWitDetailsByTenantIgnoreCase(tenant) :
|
||||
tenantMetaDataRepository.findByTenantIgnoreCase(tenant);
|
||||
if (metaData == null) {
|
||||
if (repositoryProperties.isImplicitTenantCreateAllowed()) {
|
||||
log.info("Tenant {} doesn't exist create metadata", tenant, new Exception("Thread dump"));
|
||||
return createTenantMetadata0(tenant);
|
||||
} else {
|
||||
throw new EntityNotFoundException(TenantMetaData.class, tenant);
|
||||
}
|
||||
} else {
|
||||
return metaData;
|
||||
}
|
||||
}
|
||||
|
||||
private void usageStatsPerTenant(final SystemUsageReportWithTenants report) {
|
||||
forEachTenant(tenant -> report.addTenantData(systemStatsManagement.getStatsOfTenant()));
|
||||
}
|
||||
|
||||
@@ -35,7 +35,6 @@ import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
* {@link SoftwareModuleType} elements.
|
||||
*/
|
||||
@NoArgsConstructor // Default constructor for JPA
|
||||
@Getter
|
||||
@Entity
|
||||
@Table(name = "sp_ds_type_element")
|
||||
public class DistributionSetTypeElement implements Serializable {
|
||||
@@ -47,16 +46,22 @@ public class DistributionSetTypeElement implements Serializable {
|
||||
private DistributionSetTypeElementCompositeKey key;
|
||||
|
||||
@MapsId("dsType")
|
||||
@ManyToOne(optional = false, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "distribution_set_type", nullable = false, updatable = false, foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_type_element_element"))
|
||||
@ManyToOne(optional = false)
|
||||
@JoinColumn(
|
||||
name = "distribution_set_type", nullable = false, updatable = false,
|
||||
foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_type_element_element"))
|
||||
private JpaDistributionSetType dsType;
|
||||
|
||||
@Getter
|
||||
@MapsId("smType")
|
||||
@ManyToOne(optional = false, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "software_module_type", nullable = false, updatable = false, foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_type_element_smtype"))
|
||||
@ManyToOne(optional = false)
|
||||
@JoinColumn(
|
||||
name = "software_module_type", nullable = false, updatable = false,
|
||||
foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_type_element_smtype"))
|
||||
private JpaSoftwareModuleType smType;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@Column(name = "mandatory")
|
||||
private boolean mandatory;
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ public class JpaDistributionSetType extends AbstractJpaTypeEntity implements Dis
|
||||
|
||||
@OneToMany(
|
||||
mappedBy = "dsType", targetEntity = DistributionSetTypeElement.class,
|
||||
fetch = FetchType.EAGER,
|
||||
cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, orphanRemoval = true)
|
||||
private Set<DistributionSetTypeElement> elements = new HashSet<>();
|
||||
|
||||
@@ -118,10 +119,6 @@ public class JpaDistributionSetType extends AbstractJpaTypeEntity implements Dis
|
||||
return this;
|
||||
}
|
||||
|
||||
public Set<DistributionSetTypeElement> getElements() {
|
||||
return Collections.unmodifiableSet(elements);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DistributionSetType [key=" + getKey() + ", isDeleted()=" + isDeleted() + ", getId()=" + getId() + "]";
|
||||
|
||||
@@ -19,6 +19,8 @@ import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.ForeignKey;
|
||||
import jakarta.persistence.Index;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.NamedAttributeNode;
|
||||
import jakarta.persistence.NamedEntityGraph;
|
||||
import jakarta.persistence.OneToOne;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.persistence.UniqueConstraint;
|
||||
@@ -45,6 +47,7 @@ import org.eclipse.hawkbit.repository.model.TenantMetaData;
|
||||
@Table(name = "sp_tenant", indexes = {
|
||||
@Index(name = "sp_idx_tenant_prim", columnList = "tenant,id") }, uniqueConstraints = {
|
||||
@UniqueConstraint(columnNames = { "tenant" }, name = "uk_tenantmd_tenant") })
|
||||
@NamedEntityGraph(name = "TenantMetaData.withDetails", attributeNodes = { @NamedAttributeNode("defaultDsType") })
|
||||
@Entity
|
||||
// exception squid:S2160 - BaseEntity equals/hashcode is handling correctly for sub entities
|
||||
@SuppressWarnings("squid:S2160")
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaTenantMetaData;
|
||||
import org.eclipse.hawkbit.repository.model.TenantMetaData;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
@@ -26,21 +27,16 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@Transactional(readOnly = true)
|
||||
public interface TenantMetaDataRepository extends PagingAndSortingRepository<JpaTenantMetaData, Long>, CrudRepository<JpaTenantMetaData, Long> {
|
||||
|
||||
/**
|
||||
* Search {@link TenantMetaData} by tenant name.
|
||||
*
|
||||
* @param tenant to search for
|
||||
* @return found {@link TenantMetaData} or <code>null</code>
|
||||
*/
|
||||
// find without details
|
||||
TenantMetaData findByTenantIgnoreCase(String tenant);
|
||||
|
||||
@Transactional
|
||||
// find with details
|
||||
@EntityGraph(value = "TenantMetaData.withDetails", type = EntityGraph.EntityGraphType.LOAD)
|
||||
TenantMetaData findWitDetailsByTenantIgnoreCase(String tenant);
|
||||
|
||||
@Query("SELECT t.tenant FROM JpaTenantMetaData t")
|
||||
Page<String> findTenants(final Pageable pageable);
|
||||
|
||||
/**
|
||||
* @param tenant
|
||||
*/
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query("DELETE FROM JpaTenantMetaData t WHERE UPPER(t.tenant) = UPPER(:tenant)")
|
||||
|
||||
Reference in New Issue
Block a user