Browse Source

Add version to most entities

pull/11112/head
ViacheslavKlimov 2 years ago
parent
commit
67b8ded9f4
  1. 18
      application/src/main/data/upgrade/3.7.0/schema_update.sql
  2. 5
      common/data/src/main/java/org/thingsboard/server/common/data/Customer.java
  3. 8
      common/data/src/main/java/org/thingsboard/server/common/data/DashboardInfo.java
  4. 4
      common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfile.java
  5. 8
      common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java
  6. 9
      common/data/src/main/java/org/thingsboard/server/common/data/User.java
  7. 7
      common/data/src/main/java/org/thingsboard/server/common/data/asset/Asset.java
  8. 5
      common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetProfile.java
  9. 9
      common/data/src/main/java/org/thingsboard/server/common/data/edge/Edge.java
  10. 7
      common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleChain.java
  11. 12
      common/data/src/main/java/org/thingsboard/server/common/data/security/DeviceCredentials.java
  12. 2
      common/data/src/main/java/org/thingsboard/server/common/data/sync/ie/EntityExportData.java
  13. 11
      common/data/src/main/java/org/thingsboard/server/common/data/widget/BaseWidgetType.java
  14. 9
      common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetTypeDetails.java
  15. 7
      common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetsBundle.java
  16. 20
      dao/src/main/java/org/thingsboard/server/dao/model/BaseSqlEntity.java
  17. 29
      dao/src/main/java/org/thingsboard/server/dao/model/BaseVersionedSqlEntity.java
  18. 4
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractAlarmCommentEntity.java
  19. 15
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractAssetEntity.java
  20. 18
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractDeviceEntity.java
  21. 16
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractEdgeEntity.java
  22. 16
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractEntityViewEntity.java
  23. 20
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractWidgetTypeEntity.java
  24. 1
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetInfoEntity.java
  25. 16
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetProfileEntity.java
  26. 9
      dao/src/main/java/org/thingsboard/server/dao/model/sql/CustomerEntity.java
  27. 11
      dao/src/main/java/org/thingsboard/server/dao/model/sql/DashboardEntity.java
  28. 10
      dao/src/main/java/org/thingsboard/server/dao/model/sql/DashboardInfoEntity.java
  29. 21
      dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceCredentialsEntity.java
  30. 11
      dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceProfileEntity.java
  31. 11
      dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleChainEntity.java
  32. 10
      dao/src/main/java/org/thingsboard/server/dao/model/sql/UserEntity.java
  33. 17
      dao/src/main/java/org/thingsboard/server/dao/model/sql/WidgetsBundleEntity.java
  34. 12
      dao/src/main/java/org/thingsboard/server/dao/sql/JpaAbstractDao.java
  35. 16
      dao/src/main/resources/sql/schema-entities.sql

18
application/src/main/data/upgrade/3.7.0/schema_update.sql

@ -14,4 +14,20 @@
-- limitations under the License.
--
ALTER TABLE device ADD COLUMN IF NOT EXISTS version INT DEFAULT 0;
-- Optimistic locking update START
ALTER TABLE device ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE device_profile ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE device_credentials ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE asset ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE asset_profile ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE entity_view ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE tb_user ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE customer ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE edge ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE rule_chain ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE dashboard ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE widget_type ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
ALTER TABLE widgets_bundle ADD COLUMN IF NOT EXISTS version INT DEFAULT 1;
-- Optimistic locking update END

5
common/data/src/main/java/org/thingsboard/server/common/data/Customer.java

@ -30,7 +30,7 @@ import org.thingsboard.server.common.data.validation.Length;
import org.thingsboard.server.common.data.validation.NoXss;
@EqualsAndHashCode(callSuper = true)
public class Customer extends ContactBased<CustomerId> implements HasTenantId, ExportableEntity<CustomerId>, HasTitle {
public class Customer extends ContactBased<CustomerId> implements HasTenantId, ExportableEntity<CustomerId>, HasTitle, HasVersion {
private static final long serialVersionUID = -1599722990298929275L;
@ -43,6 +43,8 @@ public class Customer extends ContactBased<CustomerId> implements HasTenantId, E
@Getter @Setter
private CustomerId externalId;
@Getter @Setter
private Integer version;
public Customer() {
super();
@ -57,6 +59,7 @@ public class Customer extends ContactBased<CustomerId> implements HasTenantId, E
this.tenantId = customer.getTenantId();
this.title = customer.getTitle();
this.externalId = customer.getExternalId();
this.version = customer.getVersion();
}
public TenantId getTenantId() {

8
common/data/src/main/java/org/thingsboard/server/common/data/DashboardInfo.java

@ -17,6 +17,8 @@ package org.thingsboard.server.common.data;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.TenantId;
@ -29,7 +31,7 @@ import java.util.Objects;
import java.util.Set;
@Schema
public class DashboardInfo extends BaseData<DashboardId> implements HasName, HasTenantId, HasTitle, HasImage {
public class DashboardInfo extends BaseData<DashboardId> implements HasName, HasTenantId, HasTitle, HasImage, HasVersion {
private static final long serialVersionUID = -9080404114760433799L;
@ -43,6 +45,9 @@ public class DashboardInfo extends BaseData<DashboardId> implements HasName, Has
private boolean mobileHide;
private Integer mobileOrder;
@Getter @Setter
private Integer version;
public DashboardInfo() {
super();
}
@ -59,6 +64,7 @@ public class DashboardInfo extends BaseData<DashboardId> implements HasName, Has
this.assignedCustomers = dashboardInfo.getAssignedCustomers();
this.mobileHide = dashboardInfo.isMobileHide();
this.mobileOrder = dashboardInfo.getMobileOrder();
this.version = dashboardInfo.getVersion();
}
@Schema(description = "JSON object with the dashboard Id. " +

4
common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfile.java

@ -42,7 +42,7 @@ import java.io.IOException;
@ToString(exclude = {"image", "profileDataBytes"})
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class DeviceProfile extends BaseData<DeviceProfileId> implements HasName, HasTenantId, HasOtaPackage, HasRuleEngineProfile, ExportableEntity<DeviceProfileId>, HasImage, HasDefaultOption {
public class DeviceProfile extends BaseData<DeviceProfileId> implements HasName, HasTenantId, HasOtaPackage, HasRuleEngineProfile, ExportableEntity<DeviceProfileId>, HasImage, HasDefaultOption, HasVersion {
private static final long serialVersionUID = 6998485460273302018L;
@ -97,6 +97,7 @@ public class DeviceProfile extends BaseData<DeviceProfileId> implements HasName,
private RuleChainId defaultEdgeRuleChainId;
private DeviceProfileId externalId;
private Integer version;
public DeviceProfile() {
super();
@ -122,6 +123,7 @@ public class DeviceProfile extends BaseData<DeviceProfileId> implements HasName,
this.softwareId = deviceProfile.getSoftwareId();
this.defaultEdgeRuleChainId = deviceProfile.getDefaultEdgeRuleChainId();
this.externalId = deviceProfile.getExternalId();
this.version = deviceProfile.getVersion();
}
@Schema(description = "JSON object with the device profile Id. " +

8
common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java

@ -36,7 +36,7 @@ import org.thingsboard.server.common.data.validation.NoXss;
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class EntityView extends BaseDataWithAdditionalInfo<EntityViewId>
implements HasName, HasTenantId, HasCustomerId, ExportableEntity<EntityViewId> {
implements HasName, HasTenantId, HasCustomerId, HasVersion, ExportableEntity<EntityViewId> {
private static final long serialVersionUID = 5582010124562018986L;
@ -60,6 +60,7 @@ public class EntityView extends BaseDataWithAdditionalInfo<EntityViewId>
private long endTimeMs;
private EntityViewId externalId;
private Integer version;
public EntityView() {
super();
@ -80,6 +81,7 @@ public class EntityView extends BaseDataWithAdditionalInfo<EntityViewId>
this.startTimeMs = entityView.getStartTimeMs();
this.endTimeMs = entityView.getEndTimeMs();
this.externalId = entityView.getExternalId();
this.version = entityView.getVersion();
}
@Schema(description = "JSON object with Customer Id. Use 'assignEntityViewToCustomer' to change the Customer Id.", accessMode = Schema.AccessMode.READ_ONLY)
@ -102,7 +104,7 @@ public class EntityView extends BaseDataWithAdditionalInfo<EntityViewId>
@Schema(description = "JSON object with the Entity View Id. " +
"Specify this field to update the Entity View. " +
"Referencing non-existing Entity View Id will cause error. " +
"Omit this field to create new Entity View." )
"Omit this field to create new Entity View.")
@Override
public EntityViewId getId() {
return super.getId();
@ -114,7 +116,7 @@ public class EntityView extends BaseDataWithAdditionalInfo<EntityViewId>
return super.getCreatedTime();
}
@Schema(description = "Additional parameters of the device",implementation = com.fasterxml.jackson.databind.JsonNode.class)
@Schema(description = "Additional parameters of the device", implementation = com.fasterxml.jackson.databind.JsonNode.class)
@Override
public JsonNode getAdditionalInfo() {
return super.getAdditionalInfo();

9
common/data/src/main/java/org/thingsboard/server/common/data/User.java

@ -20,6 +20,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId;
@ -33,7 +35,7 @@ import static org.apache.commons.lang3.StringUtils.isNotEmpty;
@Schema
@EqualsAndHashCode(callSuper = true)
public class User extends BaseDataWithAdditionalInfo<UserId> implements HasName, HasTenantId, HasCustomerId, NotificationRecipient {
public class User extends BaseDataWithAdditionalInfo<UserId> implements HasName, HasTenantId, HasCustomerId, NotificationRecipient, HasVersion {
private static final long serialVersionUID = 8250339805336035966L;
@ -50,6 +52,9 @@ public class User extends BaseDataWithAdditionalInfo<UserId> implements HasName,
@NoXss
private String phone;
@Getter @Setter
private Integer version;
public User() {
super();
}
@ -67,6 +72,7 @@ public class User extends BaseDataWithAdditionalInfo<UserId> implements HasName,
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
this.phone = user.getPhone();
this.version = user.getVersion();
}
@ -222,4 +228,5 @@ public class User extends BaseDataWithAdditionalInfo<UserId> implements HasName,
public boolean isCustomerUser() {
return !isSystemAdmin() && !isTenantAdmin();
}
}

7
common/data/src/main/java/org/thingsboard/server/common/data/asset/Asset.java

@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.ExportableEntity;
import org.thingsboard.server.common.data.HasCustomerId;
import org.thingsboard.server.common.data.HasLabel;
import org.thingsboard.server.common.data.HasTenantId;
import org.thingsboard.server.common.data.HasVersion;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.AssetProfileId;
import org.thingsboard.server.common.data.id.CustomerId;
@ -36,7 +37,7 @@ import java.util.Optional;
@Schema
@EqualsAndHashCode(callSuper = true)
public class Asset extends BaseDataWithAdditionalInfo<AssetId> implements HasLabel, HasTenantId, HasCustomerId, ExportableEntity<AssetId> {
public class Asset extends BaseDataWithAdditionalInfo<AssetId> implements HasLabel, HasTenantId, HasCustomerId, HasVersion, ExportableEntity<AssetId> {
private static final long serialVersionUID = 2807343040519543363L;
@ -56,6 +57,8 @@ public class Asset extends BaseDataWithAdditionalInfo<AssetId> implements HasLab
@Getter @Setter
private AssetId externalId;
@Getter @Setter
private Integer version;
public Asset() {
super();
@ -74,6 +77,7 @@ public class Asset extends BaseDataWithAdditionalInfo<AssetId> implements HasLab
this.label = asset.getLabel();
this.assetProfileId = asset.getAssetProfileId();
this.externalId = asset.getExternalId();
this.version = asset.getVersion();
}
public void update(Asset asset) {
@ -85,6 +89,7 @@ public class Asset extends BaseDataWithAdditionalInfo<AssetId> implements HasLab
this.assetProfileId = asset.getAssetProfileId();
Optional.ofNullable(asset.getAdditionalInfo()).ifPresent(this::setAdditionalInfo);
this.externalId = asset.getExternalId();
this.version = asset.getVersion();
}
@Schema(description = "JSON object with the asset Id. " +

5
common/data/src/main/java/org/thingsboard/server/common/data/asset/AssetProfile.java

@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.HasImage;
import org.thingsboard.server.common.data.HasName;
import org.thingsboard.server.common.data.HasRuleEngineProfile;
import org.thingsboard.server.common.data.HasTenantId;
import org.thingsboard.server.common.data.HasVersion;
import org.thingsboard.server.common.data.id.AssetProfileId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.RuleChainId;
@ -39,7 +40,7 @@ import org.thingsboard.server.common.data.validation.NoXss;
@ToString(exclude = {"image"})
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class AssetProfile extends BaseData<AssetProfileId> implements HasName, HasTenantId, HasRuleEngineProfile, ExportableEntity<AssetProfileId>, HasImage, HasDefaultOption {
public class AssetProfile extends BaseData<AssetProfileId> implements HasName, HasTenantId, HasRuleEngineProfile, ExportableEntity<AssetProfileId>, HasImage, HasDefaultOption, HasVersion {
private static final long serialVersionUID = 6998485460273302018L;
@ -74,6 +75,7 @@ public class AssetProfile extends BaseData<AssetProfileId> implements HasName, H
private RuleChainId defaultEdgeRuleChainId;
private AssetProfileId externalId;
private Integer version;
public AssetProfile() {
super();
@ -95,6 +97,7 @@ public class AssetProfile extends BaseData<AssetProfileId> implements HasName, H
this.defaultQueueName = assetProfile.getDefaultQueueName();
this.defaultEdgeRuleChainId = assetProfile.getDefaultEdgeRuleChainId();
this.externalId = assetProfile.getExternalId();
this.version = assetProfile.getVersion();
}
@Schema(description = "JSON object with the asset profile Id. " +

9
common/data/src/main/java/org/thingsboard/server/common/data/edge/Edge.java

@ -17,12 +17,14 @@ package org.thingsboard.server.common.data.edge;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.thingsboard.server.common.data.BaseDataWithAdditionalInfo;
import org.thingsboard.server.common.data.HasCustomerId;
import org.thingsboard.server.common.data.HasLabel;
import org.thingsboard.server.common.data.HasTenantId;
import org.thingsboard.server.common.data.HasVersion;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EdgeId;
import org.thingsboard.server.common.data.id.RuleChainId;
@ -34,7 +36,7 @@ import org.thingsboard.server.common.data.validation.NoXss;
@EqualsAndHashCode(callSuper = true)
@ToString
@Setter
public class Edge extends BaseDataWithAdditionalInfo<EdgeId> implements HasLabel, HasTenantId, HasCustomerId {
public class Edge extends BaseDataWithAdditionalInfo<EdgeId> implements HasLabel, HasTenantId, HasCustomerId, HasVersion {
private static final long serialVersionUID = 4934987555236873728L;
@ -57,6 +59,9 @@ public class Edge extends BaseDataWithAdditionalInfo<EdgeId> implements HasLabel
@Length(fieldName = "secret")
private String secret;
@Getter
private Integer version;
public Edge() {
super();
}
@ -75,6 +80,7 @@ public class Edge extends BaseDataWithAdditionalInfo<EdgeId> implements HasLabel
this.name = edge.getName();
this.routingKey = edge.getRoutingKey();
this.secret = edge.getSecret();
this.version = edge.getVersion();
}
public void update(Edge edge) {
@ -86,6 +92,7 @@ public class Edge extends BaseDataWithAdditionalInfo<EdgeId> implements HasLabel
this.name = edge.getName();
this.routingKey = edge.getRoutingKey();
this.secret = edge.getSecret();
this.version = edge.getVersion();
}
@Schema(description = "JSON object with the Edge Id. " +

7
common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleChain.java

@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.ExportableEntity;
import org.thingsboard.server.common.data.HasDefaultOption;
import org.thingsboard.server.common.data.HasName;
import org.thingsboard.server.common.data.HasTenantId;
import org.thingsboard.server.common.data.HasVersion;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.RuleNodeId;
import org.thingsboard.server.common.data.id.TenantId;
@ -36,7 +37,7 @@ import org.thingsboard.server.common.data.validation.NoXss;
@Data
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class RuleChain extends BaseDataWithAdditionalInfo<RuleChainId> implements HasName, HasTenantId, ExportableEntity<RuleChainId>, HasDefaultOption {
public class RuleChain extends BaseDataWithAdditionalInfo<RuleChainId> implements HasName, HasTenantId, ExportableEntity<RuleChainId>, HasDefaultOption, HasVersion {
private static final long serialVersionUID = -5656679015121935465L;
@ -58,6 +59,7 @@ public class RuleChain extends BaseDataWithAdditionalInfo<RuleChainId> implement
private transient JsonNode configuration;
private RuleChainId externalId;
private Integer version;
@JsonIgnore
private byte[] configurationBytes;
@ -79,6 +81,7 @@ public class RuleChain extends BaseDataWithAdditionalInfo<RuleChainId> implement
this.root = ruleChain.isRoot();
this.setConfiguration(ruleChain.getConfiguration());
this.setExternalId(ruleChain.getExternalId());
this.version = ruleChain.getVersion();
}
@Override
@ -89,7 +92,7 @@ public class RuleChain extends BaseDataWithAdditionalInfo<RuleChainId> implement
@Schema(description = "JSON object with the Rule Chain Id. " +
"Specify this field to update the Rule Chain. " +
"Referencing non-existing Rule Chain Id will cause error. " +
"Omit this field to create new rule chain." )
"Omit this field to create new rule chain.")
@Override
public RuleChainId getId() {
return super.getId();

12
common/data/src/main/java/org/thingsboard/server/common/data/security/DeviceCredentials.java

@ -17,20 +17,26 @@ package org.thingsboard.server.common.data.security;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.thingsboard.server.common.data.BaseData;
import org.thingsboard.server.common.data.HasVersion;
import org.thingsboard.server.common.data.id.DeviceCredentialsId;
import org.thingsboard.server.common.data.id.DeviceId;
@Schema
@EqualsAndHashCode(callSuper = true)
public class DeviceCredentials extends BaseData<DeviceCredentialsId> implements DeviceCredentialsFilter {
public class DeviceCredentials extends BaseData<DeviceCredentialsId> implements DeviceCredentialsFilter, HasVersion {
private static final long serialVersionUID = -7869261127032877765L;
private DeviceId deviceId;
private DeviceCredentialsType credentialsType;
private String credentialsId;
private String credentialsValue;
@Getter @Setter
private Integer version;
public DeviceCredentials() {
super();
}
@ -45,6 +51,7 @@ public class DeviceCredentials extends BaseData<DeviceCredentialsId> implements
this.credentialsType = deviceCredentials.getCredentialsType();
this.credentialsId = deviceCredentials.getCredentialsId();
this.credentialsValue = deviceCredentials.getCredentialsValue();
this.version = deviceCredentials.getVersion();
}
@Schema(requiredMode = Schema.RequiredMode.REQUIRED, accessMode = Schema.AccessMode.READ_ONLY, description = "The Id is automatically generated during device creation. " +
@ -111,4 +118,5 @@ public class DeviceCredentials extends BaseData<DeviceCredentialsId> implements
+ credentialsId + ", credentialsValue=" + credentialsValue + ", createdTime=" + createdTime + ", id="
+ id + "]";
}
}

2
common/data/src/main/java/org/thingsboard/server/common/data/sync/ie/EntityExportData.java

@ -56,7 +56,7 @@ public class EntityExportData<E extends ExportableEntity<? extends EntityId>> {
.comparing(AttributeExportData::getKey).thenComparing(AttributeExportData::getLastUpdateTs);
@JsonProperty(index = 2)
@JsonTbEntity
@JsonTbEntity // FIXME: version is serialized. also check single entity export/import from UI!
private E entity;
@JsonProperty(index = 1)
private EntityType entityType;

11
common/data/src/main/java/org/thingsboard/server/common/data/widget/BaseWidgetType.java

@ -17,16 +17,19 @@ package org.thingsboard.server.common.data.widget;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.BaseData;
import org.thingsboard.server.common.data.HasName;
import org.thingsboard.server.common.data.HasTenantId;
import org.thingsboard.server.common.data.HasVersion;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.WidgetTypeId;
import org.thingsboard.server.common.data.validation.Length;
import org.thingsboard.server.common.data.validation.NoXss;
@Data
public class BaseWidgetType extends BaseData<WidgetTypeId> implements HasName, HasTenantId {
@EqualsAndHashCode(callSuper = true)
public class BaseWidgetType extends BaseData<WidgetTypeId> implements HasName, HasTenantId, HasVersion {
private static final long serialVersionUID = 8388684344603660756L;
@ -44,6 +47,8 @@ public class BaseWidgetType extends BaseData<WidgetTypeId> implements HasName, H
@Schema(description = "Whether widget type is deprecated.", example = "true")
private boolean deprecated;
private Integer version;
public BaseWidgetType() {
super();
}
@ -58,12 +63,13 @@ public class BaseWidgetType extends BaseData<WidgetTypeId> implements HasName, H
this.fqn = widgetType.getFqn();
this.name = widgetType.getName();
this.deprecated = widgetType.isDeprecated();
this.version = widgetType.getVersion();
}
@Schema(description = "JSON object with the Widget Type Id. " +
"Specify this field to update the Widget Type. " +
"Referencing non-existing Widget Type Id will cause error. " +
"Omit this field to create new Widget Type." )
"Omit this field to create new Widget Type.")
@Override
public WidgetTypeId getId() {
return super.getId();
@ -74,4 +80,5 @@ public class BaseWidgetType extends BaseData<WidgetTypeId> implements HasName, H
public long getCreatedTime() {
return super.getCreatedTime();
}
}

9
common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetTypeDetails.java

@ -18,8 +18,7 @@ package org.thingsboard.server.common.data.widget;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.ExportableEntity;
import org.thingsboard.server.common.data.HasImage;
import org.thingsboard.server.common.data.HasName;
@ -29,7 +28,8 @@ import org.thingsboard.server.common.data.validation.Length;
import org.thingsboard.server.common.data.validation.NoXss;
@Data
@JsonPropertyOrder({ "fqn", "name", "deprecated", "image", "description", "descriptor", "externalId" })
@EqualsAndHashCode(callSuper = true)
@JsonPropertyOrder({"fqn", "name", "deprecated", "image", "description", "descriptor", "externalId"})
public class WidgetTypeDetails extends WidgetType implements HasName, HasTenantId, HasImage, ExportableEntity<WidgetTypeId> {
@Schema(description = "Relative or external image URL. Replaced with image data URL (Base64) in case of relative URL and 'inlineImages' option enabled.")
@ -42,8 +42,6 @@ public class WidgetTypeDetails extends WidgetType implements HasName, HasTenantI
@Schema(description = "Tags of the widget type")
private String[] tags;
@Getter
@Setter
private WidgetTypeId externalId;
public WidgetTypeDetails() {
@ -65,4 +63,5 @@ public class WidgetTypeDetails extends WidgetType implements HasName, HasTenantI
this.tags = widgetTypeDetails.getTags();
this.externalId = widgetTypeDetails.getExternalId();
}
}

7
common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetsBundle.java

@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.HasImage;
import org.thingsboard.server.common.data.HasName;
import org.thingsboard.server.common.data.HasTenantId;
import org.thingsboard.server.common.data.HasTitle;
import org.thingsboard.server.common.data.HasVersion;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.WidgetsBundleId;
import org.thingsboard.server.common.data.validation.Length;
@ -33,7 +34,7 @@ import org.thingsboard.server.common.data.validation.NoXss;
@Schema
@EqualsAndHashCode(callSuper = true)
public class WidgetsBundle extends BaseData<WidgetsBundleId> implements HasName, HasTenantId, ExportableEntity<WidgetsBundleId>, HasTitle, HasImage {
public class WidgetsBundle extends BaseData<WidgetsBundleId> implements HasName, HasTenantId, ExportableEntity<WidgetsBundleId>, HasTitle, HasImage, HasVersion {
private static final long serialVersionUID = -7627368878362410489L;
@ -76,6 +77,9 @@ public class WidgetsBundle extends BaseData<WidgetsBundleId> implements HasName,
@Getter
@Setter
private WidgetsBundleId externalId;
@Getter
@Setter
private Integer version;
public WidgetsBundle() {
super();
@ -94,6 +98,7 @@ public class WidgetsBundle extends BaseData<WidgetsBundleId> implements HasName,
this.description = widgetsBundle.getDescription();
this.order = widgetsBundle.getOrder();
this.externalId = widgetsBundle.getExternalId();
this.version = widgetsBundle.getVersion();
}
@Schema(description = "JSON object with the Widget Bundle Id. " +

20
dao/src/main/java/org/thingsboard/server/dao/model/BaseSqlEntity.java

@ -16,17 +16,18 @@
package org.thingsboard.server.dao.model;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.persistence.Column;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.BaseData;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UUIDBased;
import org.thingsboard.server.dao.DaoUtil;
import jakarta.persistence.Column;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -48,6 +49,19 @@ public abstract class BaseSqlEntity<D> implements BaseEntity<D> {
@Column(name = ModelConstants.CREATED_TIME_PROPERTY, updatable = false)
protected long createdTime;
public BaseSqlEntity() {
}
public BaseSqlEntity(BaseData<?> domain) {
this.id = domain.getUuidId();
this.createdTime = domain.getCreatedTime();
}
public BaseSqlEntity(BaseSqlEntity<?> entity) {
this.id = entity.id;
this.createdTime = entity.createdTime;
}
@Override
public UUID getUuid() {
return id;

29
dao/src/main/java/org/thingsboard/server/dao/model/BaseVersionedSqlEntity.java

@ -20,15 +20,42 @@ import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.Version;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.thingsboard.server.common.data.BaseData;
import org.thingsboard.server.common.data.HasVersion;
@Data
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
public abstract class BaseVersionedSqlEntity<D> extends BaseSqlEntity<D> implements HasVersion {
public abstract class BaseVersionedSqlEntity<D extends BaseData & HasVersion> extends BaseSqlEntity<D> implements HasVersion {
@Getter @Setter
@Version
@Column(name = ModelConstants.VERSION_PROPERTY)
protected Integer version;
public BaseVersionedSqlEntity() {
super();
}
public BaseVersionedSqlEntity(D domain) {
super(domain);
this.version = domain.getVersion();
}
public BaseVersionedSqlEntity(BaseVersionedSqlEntity<?> entity) {
super(entity);
this.version = entity.version;
}
@Override
public String toString() {
return "BaseVersionedSqlEntity{" +
"id=" + id +
", createdTime=" + createdTime +
", version=" + version +
'}';
}
}

4
dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractAlarmCommentEntity.java

@ -26,7 +26,6 @@ import org.thingsboard.server.common.data.alarm.AlarmCommentType;
import org.thingsboard.server.common.data.id.AlarmCommentId;
import org.thingsboard.server.common.data.id.AlarmId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.dao.model.BaseEntity;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonConverter;
@ -40,7 +39,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.ALARM_COMMENT_TYPE
@Data
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
public abstract class AbstractAlarmCommentEntity<T extends AlarmComment> extends BaseSqlEntity<T> implements BaseEntity<T> {
public abstract class AbstractAlarmCommentEntity<T extends AlarmComment> extends BaseSqlEntity<T> {
@Column(name = ALARM_COMMENT_ALARM_ID, columnDefinition = "uuid")
private UUID alarmId;
@ -94,4 +93,5 @@ public abstract class AbstractAlarmCommentEntity<T extends AlarmComment> extends
alarmComment.setComment(comment);
return alarmComment;
}
}

15
dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractAssetEntity.java

@ -26,7 +26,7 @@ import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.AssetProfileId;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonConverter;
@ -42,7 +42,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.EXTERNAL_ID_PROPER
@Data
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
public abstract class AbstractAssetEntity<T extends Asset> extends BaseSqlEntity<T> {
public abstract class AbstractAssetEntity<T extends Asset> extends BaseVersionedSqlEntity<T> {
@Column(name = ASSET_TENANT_ID_PROPERTY)
private UUID tenantId;
@ -73,11 +73,8 @@ public abstract class AbstractAssetEntity<T extends Asset> extends BaseSqlEntity
super();
}
public AbstractAssetEntity(Asset asset) {
if (asset.getId() != null) {
this.setUuid(asset.getId().getId());
}
this.setCreatedTime(asset.getCreatedTime());
public AbstractAssetEntity(T asset) {
super(asset);
if (asset.getTenantId() != null) {
this.tenantId = asset.getTenantId().getId();
}
@ -97,8 +94,7 @@ public abstract class AbstractAssetEntity<T extends Asset> extends BaseSqlEntity
}
public AbstractAssetEntity(AssetEntity assetEntity) {
this.setId(assetEntity.getId());
this.setCreatedTime(assetEntity.getCreatedTime());
super(assetEntity);
this.tenantId = assetEntity.getTenantId();
this.customerId = assetEntity.getCustomerId();
this.assetProfileId = assetEntity.getAssetProfileId();
@ -112,6 +108,7 @@ public abstract class AbstractAssetEntity<T extends Asset> extends BaseSqlEntity
protected Asset toAsset() {
Asset asset = new Asset(new AssetId(id));
asset.setCreatedTime(createdTime);
asset.setVersion(version);
if (tenantId != null) {
asset.setTenantId(TenantId.fromUUID(tenantId));
}

18
dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractDeviceEntity.java

@ -41,7 +41,7 @@ import java.util.UUID;
@Data
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
public abstract class AbstractDeviceEntity<T extends Device> extends BaseVersionedSqlEntity<T> {
public abstract class AbstractDeviceEntity<T extends Device> extends BaseVersionedSqlEntity<T> {
@Column(name = ModelConstants.DEVICE_TENANT_ID_PROPERTY, columnDefinition = "uuid")
private UUID tenantId;
@ -83,11 +83,8 @@ public abstract class AbstractDeviceEntity<T extends Device> extends BaseVersion
super();
}
public AbstractDeviceEntity(Device device) {
if (device.getId() != null) {
this.setUuid(device.getUuidId());
}
this.setCreatedTime(device.getCreatedTime());
public AbstractDeviceEntity(T device) {
super(device);
if (device.getTenantId() != null) {
this.tenantId = device.getTenantId().getId();
}
@ -111,12 +108,10 @@ public abstract class AbstractDeviceEntity<T extends Device> extends BaseVersion
if (device.getExternalId() != null) {
this.externalId = device.getExternalId().getId();
}
this.version = device.getVersion();
}
public AbstractDeviceEntity(DeviceEntity deviceEntity) {
this.setId(deviceEntity.getId());
this.setCreatedTime(deviceEntity.getCreatedTime());
public AbstractDeviceEntity(AbstractDeviceEntity<T> deviceEntity) {
super(deviceEntity);
this.tenantId = deviceEntity.getTenantId();
this.customerId = deviceEntity.getCustomerId();
this.deviceProfileId = deviceEntity.getDeviceProfileId();
@ -128,12 +123,12 @@ public abstract class AbstractDeviceEntity<T extends Device> extends BaseVersion
this.firmwareId = deviceEntity.getFirmwareId();
this.softwareId = deviceEntity.getSoftwareId();
this.externalId = deviceEntity.getExternalId();
this.version = deviceEntity.getVersion();
}
protected Device toDevice() {
Device device = new Device(new DeviceId(getUuid()));
device.setCreatedTime(createdTime);
device.setVersion(version);
if (tenantId != null) {
device.setTenantId(TenantId.fromUUID(tenantId));
}
@ -157,7 +152,6 @@ public abstract class AbstractDeviceEntity<T extends Device> extends BaseVersion
if (externalId != null) {
device.setExternalId(new DeviceId(externalId));
}
device.setVersion(version);
return device;
}

16
dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractEdgeEntity.java

@ -26,7 +26,7 @@ import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EdgeId;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonConverter;
@ -44,7 +44,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.EDGE_TYPE_PROPERTY
@Data
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
public abstract class AbstractEdgeEntity<T extends Edge> extends BaseSqlEntity<T> {
public abstract class AbstractEdgeEntity<T extends Edge> extends BaseVersionedSqlEntity<T> {
@Column(name = EDGE_TENANT_ID_PROPERTY, columnDefinition = "uuid")
private UUID tenantId;
@ -78,11 +78,8 @@ public abstract class AbstractEdgeEntity<T extends Edge> extends BaseSqlEntity<T
super();
}
public AbstractEdgeEntity(Edge edge) {
if (edge.getId() != null) {
this.setUuid(edge.getId().getId());
}
this.setCreatedTime(edge.getCreatedTime());
public AbstractEdgeEntity(T edge) {
super(edge);
if (edge.getTenantId() != null) {
this.tenantId = edge.getTenantId().getId();
}
@ -101,8 +98,7 @@ public abstract class AbstractEdgeEntity<T extends Edge> extends BaseSqlEntity<T
}
public AbstractEdgeEntity(EdgeEntity edgeEntity) {
this.setId(edgeEntity.getId());
this.setCreatedTime(edgeEntity.getCreatedTime());
super(edgeEntity);
this.tenantId = edgeEntity.getTenantId();
this.customerId = edgeEntity.getCustomerId();
this.rootRuleChainId = edgeEntity.getRootRuleChainId();
@ -117,6 +113,7 @@ public abstract class AbstractEdgeEntity<T extends Edge> extends BaseSqlEntity<T
protected Edge toEdge() {
Edge edge = new Edge(new EdgeId(getUuid()));
edge.setCreatedTime(createdTime);
edge.setVersion(version);
if (tenantId != null) {
edge.setTenantId(TenantId.fromUUID(tenantId));
}
@ -134,4 +131,5 @@ public abstract class AbstractEdgeEntity<T extends Edge> extends BaseSqlEntity<T
edge.setAdditionalInfo(additionalInfo);
return edge;
}
}

16
dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractEntityViewEntity.java

@ -32,7 +32,7 @@ import org.thingsboard.server.common.data.id.EntityIdFactory;
import org.thingsboard.server.common.data.id.EntityViewId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.objects.TelemetryEntityView;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonConverter;
@ -48,7 +48,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_PROPER
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
@Slf4j
public abstract class AbstractEntityViewEntity<T extends EntityView> extends BaseSqlEntity<T> {
public abstract class AbstractEntityViewEntity<T extends EntityView> extends BaseVersionedSqlEntity<T> {
@Column(name = ModelConstants.ENTITY_VIEW_ENTITY_ID_PROPERTY)
private UUID entityId;
@ -89,11 +89,8 @@ public abstract class AbstractEntityViewEntity<T extends EntityView> extends Bas
super();
}
public AbstractEntityViewEntity(EntityView entityView) {
if (entityView.getId() != null) {
this.setUuid(entityView.getId().getId());
}
this.setCreatedTime(entityView.getCreatedTime());
public AbstractEntityViewEntity(T entityView) {
super(entityView);
if (entityView.getEntityId() != null) {
this.entityId = entityView.getEntityId().getId();
this.entityType = entityView.getEntityId().getEntityType();
@ -120,8 +117,7 @@ public abstract class AbstractEntityViewEntity<T extends EntityView> extends Bas
}
public AbstractEntityViewEntity(EntityViewEntity entityViewEntity) {
this.setId(entityViewEntity.getId());
this.setCreatedTime(entityViewEntity.getCreatedTime());
super(entityViewEntity);
this.entityId = entityViewEntity.getEntityId();
this.entityType = entityViewEntity.getEntityType();
this.tenantId = entityViewEntity.getTenantId();
@ -138,6 +134,7 @@ public abstract class AbstractEntityViewEntity<T extends EntityView> extends Bas
protected EntityView toEntityView() {
EntityView entityView = new EntityView(new EntityViewId(getUuid()));
entityView.setCreatedTime(createdTime);
entityView.setVersion(version);
if (entityId != null) {
entityView.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType.name(), entityId));
@ -163,4 +160,5 @@ public abstract class AbstractEntityViewEntity<T extends EntityView> extends Bas
}
return entityView;
}
}

20
dao/src/main/java/org/thingsboard/server/dao/model/sql/AbstractWidgetTypeEntity.java

@ -15,23 +15,22 @@
*/
package org.thingsboard.server.dao.model.sql;
import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.WidgetTypeId;
import org.thingsboard.server.common.data.widget.BaseWidgetType;
import org.thingsboard.server.dao.model.BaseEntity;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import java.util.UUID;
@Data
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
public abstract class AbstractWidgetTypeEntity<T extends BaseWidgetType> extends BaseSqlEntity<T> {
public abstract class AbstractWidgetTypeEntity<T extends BaseWidgetType> extends BaseVersionedSqlEntity<T> {
@Column(name = ModelConstants.WIDGET_TYPE_TENANT_ID_PROPERTY)
private UUID tenantId;
@ -49,11 +48,8 @@ public abstract class AbstractWidgetTypeEntity<T extends BaseWidgetType> extends
super();
}
public AbstractWidgetTypeEntity(BaseWidgetType widgetType) {
if (widgetType.getId() != null) {
this.setUuid(widgetType.getId().getId());
}
this.setCreatedTime(widgetType.getCreatedTime());
public AbstractWidgetTypeEntity(T widgetType) {
super(widgetType);
if (widgetType.getTenantId() != null) {
this.tenantId = widgetType.getTenantId().getId();
}
@ -63,8 +59,7 @@ public abstract class AbstractWidgetTypeEntity<T extends BaseWidgetType> extends
}
public AbstractWidgetTypeEntity(AbstractWidgetTypeEntity widgetTypeEntity) {
this.setId(widgetTypeEntity.getId());
this.setCreatedTime(widgetTypeEntity.getCreatedTime());
super(widgetTypeEntity);
this.tenantId = widgetTypeEntity.getTenantId();
this.fqn = widgetTypeEntity.getFqn();
this.name = widgetTypeEntity.getName();
@ -74,6 +69,7 @@ public abstract class AbstractWidgetTypeEntity<T extends BaseWidgetType> extends
protected BaseWidgetType toBaseWidgetType() {
BaseWidgetType widgetType = new BaseWidgetType(new WidgetTypeId(getUuid()));
widgetType.setCreatedTime(createdTime);
widgetType.setVersion(version);
if (tenantId != null) {
widgetType.setTenantId(TenantId.fromUUID(tenantId));
}

1
dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetInfoEntity.java

@ -18,6 +18,7 @@ package org.thingsboard.server.dao.model.sql;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.asset.AssetInfo;
import java.util.HashMap;

16
dao/src/main/java/org/thingsboard/server/dao/model/sql/AssetProfileEntity.java

@ -15,6 +15,9 @@
*/
package org.thingsboard.server.dao.model.sql;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.asset.AssetProfile;
@ -22,19 +25,16 @@ import org.thingsboard.server.common.data.id.AssetProfileId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import java.util.UUID;
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.ASSET_PROFILE_TABLE_NAME)
public final class AssetProfileEntity extends BaseSqlEntity<AssetProfile> {
public final class AssetProfileEntity extends BaseVersionedSqlEntity<AssetProfile> {
@Column(name = ModelConstants.ASSET_PROFILE_TENANT_ID_PROPERTY)
private UUID tenantId;
@ -71,13 +71,10 @@ public final class AssetProfileEntity extends BaseSqlEntity<AssetProfile> {
}
public AssetProfileEntity(AssetProfile assetProfile) {
if (assetProfile.getId() != null) {
this.setUuid(assetProfile.getId().getId());
}
super(assetProfile);
if (assetProfile.getTenantId() != null) {
this.tenantId = assetProfile.getTenantId().getId();
}
this.setCreatedTime(assetProfile.getCreatedTime());
this.name = assetProfile.getName();
this.image = assetProfile.getImage();
this.description = assetProfile.getDescription();
@ -101,6 +98,7 @@ public final class AssetProfileEntity extends BaseSqlEntity<AssetProfile> {
public AssetProfile toData() {
AssetProfile assetProfile = new AssetProfile(new AssetProfileId(this.getUuid()));
assetProfile.setCreatedTime(createdTime);
assetProfile.setVersion(version);
if (tenantId != null) {
assetProfile.setTenantId(TenantId.fromUUID(tenantId));
}

9
dao/src/main/java/org/thingsboard/server/dao/model/sql/CustomerEntity.java

@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonConverter;
@ -35,7 +36,7 @@ import java.util.UUID;
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.CUSTOMER_TABLE_NAME)
public final class CustomerEntity extends BaseSqlEntity<Customer> {
public final class CustomerEntity extends BaseVersionedSqlEntity<Customer> {
@Column(name = ModelConstants.CUSTOMER_TENANT_ID_PROPERTY)
private UUID tenantId;
@ -82,10 +83,7 @@ public final class CustomerEntity extends BaseSqlEntity<Customer> {
}
public CustomerEntity(Customer customer) {
if (customer.getId() != null) {
this.setUuid(customer.getId().getId());
}
this.setCreatedTime(customer.getCreatedTime());
super(customer);
this.tenantId = customer.getTenantId().getId();
this.title = customer.getTitle();
this.country = customer.getCountry();
@ -107,6 +105,7 @@ public final class CustomerEntity extends BaseSqlEntity<Customer> {
public Customer toData() {
Customer customer = new Customer(new CustomerId(this.getUuid()));
customer.setCreatedTime(createdTime);
customer.setVersion(version);
customer.setTenantId(TenantId.fromUUID(tenantId));
customer.setTitle(title);
customer.setCountry(country);

11
dao/src/main/java/org/thingsboard/server/dao/model/sql/DashboardEntity.java

@ -30,7 +30,7 @@ import org.thingsboard.server.common.data.ShortCustomerInfo;
import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonConverter;
@ -42,7 +42,7 @@ import java.util.UUID;
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.DASHBOARD_TABLE_NAME)
public final class DashboardEntity extends BaseSqlEntity<Dashboard> {
public final class DashboardEntity extends BaseVersionedSqlEntity<Dashboard> {
private static final JavaType assignedCustomersType =
JacksonUtil.constructCollectionType(HashSet.class, ShortCustomerInfo.class);
@ -77,10 +77,7 @@ public final class DashboardEntity extends BaseSqlEntity<Dashboard> {
}
public DashboardEntity(Dashboard dashboard) {
if (dashboard.getId() != null) {
this.setUuid(dashboard.getId().getId());
}
this.setCreatedTime(dashboard.getCreatedTime());
super(dashboard);
if (dashboard.getTenantId() != null) {
this.tenantId = dashboard.getTenantId().getId();
}
@ -105,6 +102,7 @@ public final class DashboardEntity extends BaseSqlEntity<Dashboard> {
public Dashboard toData() {
Dashboard dashboard = new Dashboard(new DashboardId(this.getUuid()));
dashboard.setCreatedTime(this.getCreatedTime());
dashboard.setVersion(version);
if (tenantId != null) {
dashboard.setTenantId(TenantId.fromUUID(tenantId));
}
@ -125,4 +123,5 @@ public final class DashboardEntity extends BaseSqlEntity<Dashboard> {
}
return dashboard;
}
}

10
dao/src/main/java/org/thingsboard/server/dao/model/sql/DashboardInfoEntity.java

@ -28,7 +28,7 @@ import org.thingsboard.server.common.data.ShortCustomerInfo;
import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import java.util.HashSet;
@ -39,7 +39,7 @@ import java.util.UUID;
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.DASHBOARD_TABLE_NAME)
public class DashboardInfoEntity extends BaseSqlEntity<DashboardInfo> {
public class DashboardInfoEntity extends BaseVersionedSqlEntity<DashboardInfo> {
private static final JavaType assignedCustomersType =
JacksonUtil.constructCollectionType(HashSet.class, ShortCustomerInfo.class);
@ -67,10 +67,7 @@ public class DashboardInfoEntity extends BaseSqlEntity<DashboardInfo> {
}
public DashboardInfoEntity(DashboardInfo dashboardInfo) {
if (dashboardInfo.getId() != null) {
this.setUuid(dashboardInfo.getId().getId());
}
this.setCreatedTime(dashboardInfo.getCreatedTime());
super(dashboardInfo);
if (dashboardInfo.getTenantId() != null) {
this.tenantId = dashboardInfo.getTenantId().getId();
}
@ -91,6 +88,7 @@ public class DashboardInfoEntity extends BaseSqlEntity<DashboardInfo> {
public DashboardInfo toData() {
DashboardInfo dashboardInfo = new DashboardInfo(new DashboardId(this.getUuid()));
dashboardInfo.setCreatedTime(createdTime);
dashboardInfo.setVersion(version);
if (tenantId != null) {
dashboardInfo.setTenantId(TenantId.fromUUID(tenantId));
}

21
dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceCredentialsEntity.java

@ -15,28 +15,27 @@
*/
package org.thingsboard.server.dao.model.sql;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.id.DeviceCredentialsId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.security.DeviceCredentials;
import org.thingsboard.server.common.data.security.DeviceCredentialsType;
import org.thingsboard.server.dao.model.BaseEntity;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import java.util.UUID;
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.DEVICE_CREDENTIALS_TABLE_NAME)
public final class DeviceCredentialsEntity extends BaseSqlEntity<DeviceCredentials> implements BaseEntity<DeviceCredentials> {
public final class DeviceCredentialsEntity extends BaseVersionedSqlEntity<DeviceCredentials> {
@Column(name = ModelConstants.DEVICE_CREDENTIALS_DEVICE_ID_PROPERTY)
private UUID deviceId;
@ -56,10 +55,7 @@ public final class DeviceCredentialsEntity extends BaseSqlEntity<DeviceCredentia
}
public DeviceCredentialsEntity(DeviceCredentials deviceCredentials) {
if (deviceCredentials.getId() != null) {
this.setUuid(deviceCredentials.getId().getId());
}
this.setCreatedTime(deviceCredentials.getCreatedTime());
super(deviceCredentials);
if (deviceCredentials.getDeviceId() != null) {
this.deviceId = deviceCredentials.getDeviceId().getId();
}
@ -72,6 +68,7 @@ public final class DeviceCredentialsEntity extends BaseSqlEntity<DeviceCredentia
public DeviceCredentials toData() {
DeviceCredentials deviceCredentials = new DeviceCredentials(new DeviceCredentialsId(this.getUuid()));
deviceCredentials.setCreatedTime(createdTime);
deviceCredentials.setVersion(version);
if (deviceId != null) {
deviceCredentials.setDeviceId(new DeviceId(deviceId));
}

11
dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceProfileEntity.java

@ -38,7 +38,7 @@ import org.thingsboard.server.common.data.id.DeviceProfileId;
import org.thingsboard.server.common.data.id.OtaPackageId;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonConverter;
@ -48,7 +48,7 @@ import java.util.UUID;
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.DEVICE_PROFILE_TABLE_NAME)
public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> {
public final class DeviceProfileEntity extends BaseVersionedSqlEntity<DeviceProfile> {
@Column(name = ModelConstants.DEVICE_PROFILE_TENANT_ID_PROPERTY)
private UUID tenantId;
@ -111,13 +111,10 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> {
}
public DeviceProfileEntity(DeviceProfile deviceProfile) {
if (deviceProfile.getId() != null) {
this.setUuid(deviceProfile.getId().getId());
}
super(deviceProfile);
if (deviceProfile.getTenantId() != null) {
this.tenantId = deviceProfile.getTenantId().getId();
}
this.setCreatedTime(deviceProfile.getCreatedTime());
this.name = deviceProfile.getName();
this.type = deviceProfile.getType();
this.image = deviceProfile.getImage();
@ -152,6 +149,7 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> {
public DeviceProfile toData() {
DeviceProfile deviceProfile = new DeviceProfile(new DeviceProfileId(this.getUuid()));
deviceProfile.setCreatedTime(createdTime);
deviceProfile.setVersion(version);
if (tenantId != null) {
deviceProfile.setTenantId(TenantId.fromUUID(tenantId));
}
@ -187,4 +185,5 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> {
return deviceProfile;
}
}

11
dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleChainEntity.java

@ -30,7 +30,7 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.common.data.rule.RuleChainType;
import org.thingsboard.server.dao.DaoUtil;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonConverter;
@ -40,7 +40,7 @@ import java.util.UUID;
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.RULE_CHAIN_TABLE_NAME)
public class RuleChainEntity extends BaseSqlEntity<RuleChain> {
public class RuleChainEntity extends BaseVersionedSqlEntity<RuleChain> {
@Column(name = ModelConstants.RULE_CHAIN_TENANT_ID_PROPERTY)
private UUID tenantId;
@ -76,10 +76,7 @@ public class RuleChainEntity extends BaseSqlEntity<RuleChain> {
}
public RuleChainEntity(RuleChain ruleChain) {
if (ruleChain.getId() != null) {
this.setUuid(ruleChain.getUuidId());
}
this.setCreatedTime(ruleChain.getCreatedTime());
super(ruleChain);
this.tenantId = DaoUtil.getId(ruleChain.getTenantId());
this.name = ruleChain.getName();
this.type = ruleChain.getType();
@ -99,6 +96,7 @@ public class RuleChainEntity extends BaseSqlEntity<RuleChain> {
public RuleChain toData() {
RuleChain ruleChain = new RuleChain(new RuleChainId(this.getUuid()));
ruleChain.setCreatedTime(createdTime);
ruleChain.setVersion(version);
ruleChain.setTenantId(TenantId.fromUUID(tenantId));
ruleChain.setName(name);
ruleChain.setType(type);
@ -114,4 +112,5 @@ public class RuleChainEntity extends BaseSqlEntity<RuleChain> {
}
return ruleChain;
}
}

10
dao/src/main/java/org/thingsboard/server/dao/model/sql/UserEntity.java

@ -29,7 +29,7 @@ import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.security.Authority;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonConverter;
@ -42,7 +42,7 @@ import java.util.UUID;
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.USER_PG_HIBERNATE_TABLE_NAME)
public class UserEntity extends BaseSqlEntity<User> {
public class UserEntity extends BaseVersionedSqlEntity<User> {
@Column(name = ModelConstants.USER_TENANT_ID_PROPERTY)
private UUID tenantId;
@ -74,10 +74,7 @@ public class UserEntity extends BaseSqlEntity<User> {
}
public UserEntity(User user) {
if (user.getId() != null) {
this.setUuid(user.getId().getId());
}
this.setCreatedTime(user.getCreatedTime());
super(user);
this.authority = user.getAuthority();
if (user.getTenantId() != null) {
this.tenantId = user.getTenantId().getId();
@ -96,6 +93,7 @@ public class UserEntity extends BaseSqlEntity<User> {
public User toData() {
User user = new User(new UserId(this.getUuid()));
user.setCreatedTime(createdTime);
user.setVersion(version);
user.setAuthority(authority);
if (tenantId != null) {
user.setTenantId(TenantId.fromUUID(tenantId));

17
dao/src/main/java/org/thingsboard/server/dao/model/sql/WidgetsBundleEntity.java

@ -16,24 +16,24 @@
package org.thingsboard.server.dao.model.sql;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.WidgetsBundleId;
import org.thingsboard.server.common.data.widget.WidgetsBundle;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.BaseVersionedSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import java.util.UUID;
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.WIDGETS_BUNDLE_TABLE_NAME)
public final class WidgetsBundleEntity extends BaseSqlEntity<WidgetsBundle> {
public final class WidgetsBundleEntity extends BaseVersionedSqlEntity<WidgetsBundle> {
@Column(name = ModelConstants.WIDGETS_BUNDLE_TENANT_ID_PROPERTY)
private UUID tenantId;
@ -61,10 +61,7 @@ public final class WidgetsBundleEntity extends BaseSqlEntity<WidgetsBundle> {
}
public WidgetsBundleEntity(WidgetsBundle widgetsBundle) {
if (widgetsBundle.getId() != null) {
this.setUuid(widgetsBundle.getId().getId());
}
this.setCreatedTime(widgetsBundle.getCreatedTime());
super(widgetsBundle);
if (widgetsBundle.getTenantId() != null) {
this.tenantId = widgetsBundle.getTenantId().getId();
}
@ -82,6 +79,7 @@ public final class WidgetsBundleEntity extends BaseSqlEntity<WidgetsBundle> {
public WidgetsBundle toData() {
WidgetsBundle widgetsBundle = new WidgetsBundle(new WidgetsBundleId(id));
widgetsBundle.setCreatedTime(createdTime);
widgetsBundle.setVersion(version);
if (tenantId != null) {
widgetsBundle.setTenantId(TenantId.fromUUID(tenantId));
}
@ -95,4 +93,5 @@ public final class WidgetsBundleEntity extends BaseSqlEntity<WidgetsBundle> {
}
return widgetsBundle;
}
}

12
dao/src/main/java/org/thingsboard/server/dao/sql/JpaAbstractDao.java

@ -85,12 +85,18 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
protected E doSave(E entity, boolean isNew) {
if (isNew) {
if (entity instanceof HasVersion versionedEntity) {
versionedEntity.setVersion(1);
}
entityManager.persist(entity);
} else {
if (entity instanceof HasVersion versionedEntity) {
if (versionedEntity.getVersion() == null) {
HasVersion existingEntity = entityManager.find(versionedEntity.getClass(), entity.getUuid());
versionedEntity.setVersion(existingEntity.getVersion()); // manually resetting the version to latest to allow force overwrite of the entity
// fixme tmp
throw new IllegalArgumentException("TEST - unexpected null version for " + versionedEntity);
// HasVersion existingEntity = entityManager.find(versionedEntity.getClass(), entity.getUuid());
// versionedEntity.setVersion(existingEntity.getVersion()); // manually resetting the version to latest to allow force overwrite of the entity
}
entity = entityManager.merge(entity);
entityManager.flush();
@ -137,6 +143,8 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
@Override
@Transactional
public boolean removeById(TenantId tenantId, UUID id) {
// jdbcTemplate.queryForObject("DELETE FROM " + getEntityType().getTableName() + " WHERE id = ? RETURNING version", Integer.class, id);
// TODO: increment version...
getRepository().deleteById(id);
log.debug("Remove request: {}", id);
return !getRepository().existsById(id);

16
dao/src/main/resources/sql/schema-entities.sql

@ -145,6 +145,7 @@ CREATE TABLE IF NOT EXISTS customer (
zip varchar(255),
external_id uuid,
is_public boolean,
version INT DEFAULT 1,
CONSTRAINT customer_title_unq_key UNIQUE (tenant_id, title),
CONSTRAINT customer_external_id_unq_key UNIQUE (tenant_id, external_id)
);
@ -160,6 +161,7 @@ CREATE TABLE IF NOT EXISTS dashboard (
mobile_order int,
image varchar(1000000),
external_id uuid,
version INT DEFAULT 1,
CONSTRAINT dashboard_external_id_unq_key UNIQUE (tenant_id, external_id)
);
@ -175,6 +177,7 @@ CREATE TABLE IF NOT EXISTS rule_chain (
debug_mode boolean,
tenant_id uuid,
external_id uuid,
version INT DEFAULT 1,
CONSTRAINT rule_chain_external_id_unq_key UNIQUE (tenant_id, external_id)
);
@ -252,6 +255,7 @@ CREATE TABLE IF NOT EXISTS asset_profile (
default_queue_name varchar(255),
default_edge_rule_chain_id uuid,
external_id uuid,
version INT DEFAULT 1,
CONSTRAINT asset_profile_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT asset_profile_external_id_unq_key UNIQUE (tenant_id, external_id),
CONSTRAINT fk_default_rule_chain_asset_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id),
@ -270,6 +274,7 @@ CREATE TABLE IF NOT EXISTS asset (
tenant_id uuid,
type varchar(255),
external_id uuid,
version INT DEFAULT 1,
CONSTRAINT asset_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT asset_external_id_unq_key UNIQUE (tenant_id, external_id),
CONSTRAINT fk_asset_profile FOREIGN KEY (asset_profile_id) REFERENCES asset_profile(id)
@ -295,6 +300,7 @@ CREATE TABLE IF NOT EXISTS device_profile (
provision_device_key varchar,
default_edge_rule_chain_id uuid,
external_id uuid,
version INT DEFAULT 1,
CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
CONSTRAINT device_profile_external_id_unq_key UNIQUE (tenant_id, external_id),
@ -340,7 +346,7 @@ CREATE TABLE IF NOT EXISTS device (
firmware_id uuid,
software_id uuid,
external_id uuid,
version INT DEFAULT 0,
version INT DEFAULT 1,
CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT device_external_id_unq_key UNIQUE (tenant_id, external_id),
CONSTRAINT fk_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id),
@ -355,6 +361,7 @@ CREATE TABLE IF NOT EXISTS device_credentials (
credentials_type varchar(255),
credentials_value varchar,
device_id uuid,
version INT DEFAULT 1,
CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id),
CONSTRAINT device_credentials_device_id_unq_key UNIQUE (device_id)
);
@ -439,7 +446,8 @@ CREATE TABLE IF NOT EXISTS tb_user (
first_name varchar(255),
last_name varchar(255),
phone varchar(255),
tenant_id uuid
tenant_id uuid,
version INT DEFAULT 1
);
CREATE TABLE IF NOT EXISTS tenant_profile (
@ -495,6 +503,7 @@ CREATE TABLE IF NOT EXISTS widget_type (
description varchar(1024),
tags text[],
external_id uuid,
version INT DEFAULT 1,
CONSTRAINT uq_widget_type_fqn UNIQUE (tenant_id, fqn),
CONSTRAINT widget_type_external_id_unq_key UNIQUE (tenant_id, external_id)
);
@ -509,6 +518,7 @@ CREATE TABLE IF NOT EXISTS widgets_bundle (
description varchar(1024),
widgets_bundle_order int,
external_id uuid,
version INT DEFAULT 1,
CONSTRAINT uq_widgets_bundle_alias UNIQUE (tenant_id, alias),
CONSTRAINT widgets_bundle_external_id_unq_key UNIQUE (tenant_id, external_id)
);
@ -536,6 +546,7 @@ CREATE TABLE IF NOT EXISTS entity_view (
end_ts bigint,
additional_info varchar,
external_id uuid,
version INT DEFAULT 1,
CONSTRAINT entity_view_external_id_unq_key UNIQUE (tenant_id, external_id)
);
@ -741,6 +752,7 @@ CREATE TABLE IF NOT EXISTS edge (
routing_key varchar(255),
secret varchar(255),
tenant_id uuid,
version INT DEFAULT 1,
CONSTRAINT edge_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT edge_routing_key_unq_key UNIQUE (routing_key)
);

Loading…
Cancel
Save