Fix equals method for CF

This commit is contained in:
Andrii Landiak 2025-03-21 16:31:30 +02:00
parent cb8b7bbbeb
commit 8f1fff61df
6 changed files with 53 additions and 16 deletions

View File

@ -144,6 +144,9 @@ public class DefaultCalculatedFieldCache implements CalculatedFieldCache {
calculatedFieldFetchLock.lock();
try {
CalculatedField calculatedField = calculatedFieldService.findById(tenantId, calculatedFieldId);
if (calculatedField == null) {
return;
}
EntityId cfEntityId = calculatedField.getEntityId();
calculatedFields.put(calculatedFieldId, calculatedField);

View File

@ -31,7 +31,6 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.sync.ie.EntityExportData;
import org.thingsboard.server.common.data.sync.ie.EntityImportResult;
import org.thingsboard.server.common.data.util.ThrowingRunnable;
import org.thingsboard.server.dao.cf.CalculatedFieldService;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.queue.util.TbCoreComponent;
@ -62,7 +61,6 @@ public class DefaultEntitiesExportImportService implements EntitiesExportImportS
private final Map<EntityType, EntityImportService<?, ?, ?>> importServices = new HashMap<>();
private final RelationService relationService;
private final CalculatedFieldService calculatedFieldService;
private final RateLimitService rateLimitService;
private final TbLogEntityActionService logEntityActionService;

View File

@ -621,6 +621,7 @@ public class VersionControlTest extends AbstractControllerTest {
assertThat(importedField.getName()).isEqualTo(deviceCalculatedField.getName());
assertThat(importedField.getType()).isEqualTo(deviceCalculatedField.getType());
assertThat(importedField.getId()).isNotEqualTo(deviceCalculatedField.getId());
assertThat(importedField.getConfiguration().getArguments().get("T").getRefEntityId()).isEqualTo(importedAsset.getId());
});
List<CalculatedField> importedAssetCalculatedFields = findCalculatedFieldsByEntityId(importedAsset.getId());
@ -629,6 +630,7 @@ public class VersionControlTest extends AbstractControllerTest {
assertThat(importedField.getName()).isEqualTo(assetCalculatedField.getName());
assertThat(importedField.getType()).isEqualTo(assetCalculatedField.getType());
assertThat(importedField.getId()).isNotEqualTo(assetCalculatedField.getId());
assertThat(importedField.getConfiguration().getArguments().get("T").getRefEntityId()).isEqualTo(importedDevice.getId());
});
}

View File

@ -19,7 +19,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.thingsboard.server.common.data.BaseData;
@ -37,10 +36,10 @@ import org.thingsboard.server.common.data.validation.Length;
import org.thingsboard.server.common.data.validation.NoXss;
import java.io.Serial;
import java.util.Objects;
@Schema
@Data
@EqualsAndHashCode(callSuper = true)
public class CalculatedField extends BaseData<CalculatedFieldId> implements HasName, HasTenantId, HasVersion, HasDebugSettings {
@Serial
@ -112,6 +111,48 @@ public class CalculatedField extends BaseData<CalculatedFieldId> implements HasN
return super.getCreatedTime();
}
// Getter is ignored for serialization
@JsonIgnore
public boolean isDebugMode() {
return debugMode;
}
// Setter is annotated for deserialization
@JsonSetter
public void setDebugMode(boolean debugMode) {
this.debugMode = debugMode;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CalculatedField that)) return false;
if (!super.equals(o)) return false;
return Objects.equals(tenantId, that.tenantId) &&
Objects.equals(entityId, that.entityId) &&
Objects.equals(name, that.name) &&
Objects.equals(debugSettings, that.debugSettings) &&
Objects.equals(configuration, that.configuration) &&
type == that.type && debugMode == that.debugMode &&
configurationVersion == that.configurationVersion &&
Objects.equals(version, that.version);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + Objects.hashCode(tenantId);
result = 31 * result + Objects.hashCode(entityId);
result = 31 * result + Objects.hashCode(type);
result = 31 * result + Objects.hashCode(name);
result = 31 * result + Boolean.hashCode(debugMode);
result = 31 * result + Objects.hashCode(debugSettings);
result = 31 * result + Integer.hashCode(configurationVersion);
result = 31 * result + Objects.hashCode(configuration);
result = 31 * result + Objects.hashCode(version);
return result;
}
@Override
public String toString() {
return new StringBuilder()
@ -128,16 +169,4 @@ public class CalculatedField extends BaseData<CalculatedFieldId> implements HasN
.toString();
}
// Getter is ignored for serialization
@JsonIgnore
public boolean isDebugMode() {
return debugMode;
}
// Setter is annotated for deserialization
@JsonSetter
public void setDebugMode(boolean debugMode) {
this.debugMode = debugMode;
}
}

View File

@ -16,13 +16,16 @@
package org.thingsboard.server.common.data.cf.configuration;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.cf.CalculatedFieldType;
@Data
@EqualsAndHashCode(callSuper = true)
public class ScriptCalculatedFieldConfiguration extends BaseCalculatedFieldConfiguration implements CalculatedFieldConfiguration {
@Override
public CalculatedFieldType getType() {
return CalculatedFieldType.SCRIPT;
}
}

View File

@ -16,9 +16,11 @@
package org.thingsboard.server.common.data.cf.configuration;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.thingsboard.server.common.data.cf.CalculatedFieldType;
@Data
@EqualsAndHashCode(callSuper = true)
public class SimpleCalculatedFieldConfiguration extends BaseCalculatedFieldConfiguration implements CalculatedFieldConfiguration {
@Override