refactoring

This commit is contained in:
dashevchenko 2025-03-12 19:24:28 +02:00
parent 29e9a3d122
commit e1dfa3f803
7 changed files with 25 additions and 22 deletions

View File

@ -121,7 +121,7 @@ public abstract class BaseEntityImportService<I extends EntityId, E extends Expo
CompareResult compareResult = compare(ctx, exportData, prepared, existingEntity);
if (compareResult.isNeedUpdate()) {
if (compareResult.isUpdateNeeded()) {
E savedEntity = saveOrUpdate(ctx, prepared, exportData, idProvider, compareResult);
boolean created = existingEntity == null;
importResult.setCreated(created);
@ -142,8 +142,12 @@ public abstract class BaseEntityImportService<I extends EntityId, E extends Expo
@Data
@AllArgsConstructor
protected static class CompareResult {
private boolean needUpdate;
private boolean updateNeeded;
private boolean externalIdChangedOnly;
public CompareResult(boolean updateNeeded) {
this.updateNeeded = updateNeeded;
}
}
protected boolean updateRelatedEntitiesIfUnmodified(EntitiesImportCtx ctx, E prepared, D exportData, IdProvider idProvider) {
@ -162,21 +166,17 @@ public abstract class BaseEntityImportService<I extends EntityId, E extends Expo
var existingCopy = deepCopy(existing);
cleanupForComparison(newCopy);
cleanupForComparison(existingCopy);
var needUpdate = !newCopy.equals(existingCopy);
var entityChanged = !newCopy.equals(existingCopy);
boolean externalIdChangedOnly = false;
if (needUpdate) {
if (entityChanged) {
log.debug("[{}] Found update.", prepared.getId());
log.debug("[{}] From: {}", prepared.getId(), newCopy);
log.debug("[{}] To: {}", prepared.getId(), existingCopy);
externalIdChangedOnly = isExternalIdChangedOnly(newCopy, existingCopy);
cleanupExternalId(newCopy);
cleanupExternalId(existingCopy);
externalIdChangedOnly = newCopy.equals(existingCopy);
}
return new CompareResult(existing == null || needUpdate, externalIdChangedOnly);
}
private boolean isExternalIdChangedOnly(E newCopy, E existingCopy) {
newCopy.setExternalId(null);
existingCopy.setExternalId(null);
return newCopy.equals(existingCopy);
return new CompareResult(existing == null || entityChanged, externalIdChangedOnly);
}
protected abstract E deepCopy(E e);
@ -189,6 +189,10 @@ public abstract class BaseEntityImportService<I extends EntityId, E extends Expo
}
}
protected void cleanupExternalId(E e) {
e.setExternalId(null);
}
protected abstract E saveOrUpdate(EntitiesImportCtx ctx, E entity, D exportData, IdProvider idProvider, CompareResult compareResult);

View File

@ -118,7 +118,7 @@ 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.setNeedUpdate(result.isNeedUpdate() || !prepared.getConfiguration().equals(existing.getConfiguration()));
result.setUpdateNeeded(result.isUpdateNeeded() || !prepared.getConfiguration().equals(existing.getConfiguration()));
return result;
}

View File

@ -117,11 +117,11 @@ 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.isNeedUpdate()) {
if (!result.isUpdateNeeded()) {
RuleChainMetaData newMD = exportData.getMetaData();
RuleChainMetaData existingMD = ruleChainService.loadRuleChainMetaData(ctx.getTenantId(), prepared.getId());
existingMD.setRuleChainId(null);
result.setNeedUpdate(!newMD.equals(existingMD));
result.setUpdateNeeded(!newMD.equals(existingMD));
}
return result;
}

View File

@ -50,7 +50,7 @@ public class WidgetTypeImportService extends BaseEntityImportService<WidgetTypeI
@Override
protected CompareResult compare(EntitiesImportCtx ctx, WidgetTypeExportData exportData, WidgetTypeDetails prepared, WidgetTypeDetails existing) {
return new CompareResult(true, false);
return new CompareResult(true);
}
@Override

View File

@ -76,7 +76,7 @@ public class WidgetsBundleImportService extends BaseEntityImportService<WidgetsB
@Override
protected CompareResult compare(EntitiesImportCtx ctx, WidgetsBundleExportData exportData, WidgetsBundle prepared, WidgetsBundle existing) {
return new CompareResult(true, false);
return new CompareResult(true);
}
@Override

File diff suppressed because one or more lines are too long