compare method refactoring
This commit is contained in:
parent
e1dfa3f803
commit
ccda83a9a5
@ -141,7 +141,7 @@ public abstract class BaseEntityImportService<I extends EntityId, E extends Expo
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
protected static class CompareResult {
|
||||
static class CompareResult {
|
||||
private boolean updateNeeded;
|
||||
private boolean externalIdChangedOnly;
|
||||
|
||||
@ -162,13 +162,17 @@ public abstract class BaseEntityImportService<I extends EntityId, E extends Expo
|
||||
protected abstract E prepare(EntitiesImportCtx ctx, E entity, E oldEntity, D exportData, IdProvider idProvider);
|
||||
|
||||
protected CompareResult compare(EntitiesImportCtx ctx, D exportData, E prepared, E existing) {
|
||||
if (existing == null) {
|
||||
log.debug("[{}] Found new entity.", prepared.getId());
|
||||
return new CompareResult(true);
|
||||
}
|
||||
var newCopy = deepCopy(prepared);
|
||||
var existingCopy = deepCopy(existing);
|
||||
cleanupForComparison(newCopy);
|
||||
cleanupForComparison(existingCopy);
|
||||
var entityChanged = !newCopy.equals(existingCopy);
|
||||
var updateNeeded = isUpdateNeeded(ctx, exportData, newCopy, existingCopy);
|
||||
boolean externalIdChangedOnly = false;
|
||||
if (entityChanged) {
|
||||
if (updateNeeded) {
|
||||
log.debug("[{}] Found update.", prepared.getId());
|
||||
log.debug("[{}] From: {}", prepared.getId(), newCopy);
|
||||
log.debug("[{}] To: {}", prepared.getId(), existingCopy);
|
||||
@ -176,7 +180,11 @@ public abstract class BaseEntityImportService<I extends EntityId, E extends Expo
|
||||
cleanupExternalId(existingCopy);
|
||||
externalIdChangedOnly = newCopy.equals(existingCopy);
|
||||
}
|
||||
return new CompareResult(existing == null || entityChanged, externalIdChangedOnly);
|
||||
return new CompareResult(updateNeeded, externalIdChangedOnly);
|
||||
}
|
||||
|
||||
protected boolean isUpdateNeeded(EntitiesImportCtx ctx, D exportData, E prepared, E existing) {
|
||||
return !prepared.equals(existing);
|
||||
}
|
||||
|
||||
protected abstract E deepCopy(E e);
|
||||
|
||||
@ -116,10 +116,8 @@ public class DashboardImportService extends BaseEntityImportService<DashboardId,
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompareResult compare(EntitiesImportCtx ctx, EntityExportData<Dashboard> exportData, Dashboard prepared, Dashboard existing) {
|
||||
CompareResult result = super.compare(ctx, exportData, prepared, existing);
|
||||
result.setUpdateNeeded(result.isUpdateNeeded() || !prepared.getConfiguration().equals(existing.getConfiguration()));
|
||||
return result;
|
||||
protected boolean isUpdateNeeded(EntitiesImportCtx ctx, EntityExportData<Dashboard> exportData, Dashboard prepared, Dashboard existing) {
|
||||
return super.isUpdateNeeded(ctx, exportData, prepared, existing) || !prepared.getConfiguration().equals(existing.getConfiguration());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -18,6 +18,7 @@ package org.thingsboard.server.service.sync.ie.importing.impl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thingsboard.server.common.data.Dashboard;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
import org.thingsboard.server.common.data.User;
|
||||
import org.thingsboard.server.common.data.audit.ActionType;
|
||||
@ -27,6 +28,7 @@ import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.rule.RuleChain;
|
||||
import org.thingsboard.server.common.data.rule.RuleChainMetaData;
|
||||
import org.thingsboard.server.common.data.rule.RuleNode;
|
||||
import org.thingsboard.server.common.data.sync.ie.EntityExportData;
|
||||
import org.thingsboard.server.common.data.sync.ie.RuleChainExportData;
|
||||
import org.thingsboard.server.dao.rule.RuleChainService;
|
||||
import org.thingsboard.server.dao.rule.RuleNodeDao;
|
||||
@ -115,15 +117,15 @@ public class RuleChainImportService extends BaseEntityImportService<RuleChainId,
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompareResult compare(EntitiesImportCtx ctx, RuleChainExportData exportData, RuleChain prepared, RuleChain existing) {
|
||||
CompareResult result = super.compare(ctx, exportData, prepared, existing);
|
||||
if (!result.isUpdateNeeded()) {
|
||||
protected boolean isUpdateNeeded(EntitiesImportCtx ctx, RuleChainExportData exportData, RuleChain prepared, RuleChain existing) {
|
||||
boolean updateNeeded = super.isUpdateNeeded(ctx, exportData, prepared, existing);
|
||||
if (!updateNeeded) {
|
||||
RuleChainMetaData newMD = exportData.getMetaData();
|
||||
RuleChainMetaData existingMD = ruleChainService.loadRuleChainMetaData(ctx.getTenantId(), prepared.getId());
|
||||
existingMD.setRuleChainId(null);
|
||||
result.setUpdateNeeded(!newMD.equals(existingMD));
|
||||
updateNeeded = !newMD.equals(existingMD);
|
||||
}
|
||||
return result;
|
||||
return updateNeeded;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -54,6 +54,7 @@ public class LwM2mObjectModelUtils {
|
||||
if (resource.getId() == null) {
|
||||
resource.setTitle(name + " id=" + objectModel.id + " v" + objectModel.version);
|
||||
}
|
||||
resource.setSearchText(resourceKey + LWM2M_SEPARATOR_SEARCH_TEXT + name);
|
||||
} else {
|
||||
throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
|
||||
}
|
||||
|
||||
@ -115,9 +115,9 @@ import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.eclipse.californium.elements.config.Configuration.DEFAULT_FILE_NAME;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.thingsboard.server.controller.TbResourceControllerTest.TEST_DATA;
|
||||
import static org.thingsboard.server.controller.TbResourceControllerTest.JS_TEST_FILE_NAME;
|
||||
|
||||
@DaoSqlTest
|
||||
public class VersionControlTest extends AbstractControllerTest {
|
||||
@ -1155,7 +1155,7 @@ public class VersionControlTest extends AbstractControllerTest {
|
||||
TbResource resource = new TbResource();
|
||||
resource.setResourceType(ResourceType.JKS);
|
||||
resource.setTitle(name);
|
||||
resource.setFileName(DEFAULT_FILE_NAME);
|
||||
resource.setFileName(JS_TEST_FILE_NAME);
|
||||
resource.setEncodedData(TEST_DATA);
|
||||
|
||||
return saveTbResource(resource);
|
||||
|
||||
@ -168,7 +168,7 @@ public class TbResourceInfo extends BaseData<TbResourceId> implements HasName, H
|
||||
if (!Objects.equals(resourceKey, that.resourceKey)) return false;
|
||||
if (!Objects.equals(publicResourceKey, that.publicResourceKey))
|
||||
return false;
|
||||
if (!Objects.equals(searchText, that.searchText)) return false;
|
||||
if (!Objects.equals(getSearchText(), that.getSearchText())) return false;
|
||||
if (!Objects.equals(etag, that.etag)) return false;
|
||||
if (!Objects.equals(fileName, that.fileName)) return false;
|
||||
if (!Objects.equals(descriptor, that.descriptor)) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user