From 31f1b88029d9b0b9e1faef15afded354373d5a58 Mon Sep 17 00:00:00 2001 From: Rehili Ghazwa Date: Wed, 2 Oct 2024 15:22:50 +0200 Subject: [PATCH] refacto to enhance complexity function --- .../modifications/ModificationUtils.java | 89 ++++++++++++------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/modifications/ModificationUtils.java b/src/main/java/org/gridsuite/modification/server/modifications/ModificationUtils.java index d65c2e4e0..d805676eb 100644 --- a/src/main/java/org/gridsuite/modification/server/modifications/ModificationUtils.java +++ b/src/main/java/org/gridsuite/modification/server/modifications/ModificationUtils.java @@ -732,38 +732,54 @@ private void modifyFeederAdder(ConnectablePositionAdder.FeederAdder feeder, AttributeModification equipmentId = getEquipmentId(modificationInfos); AttributeModification voltageLevelId = getVoltageLevelId(modificationInfos, feederNumber); AttributeModification busOrBusbarSectionId = getBusOrBusbarSectionId(modificationInfos, feederNumber); - int position = getPosition(connectionPosition == null ? null : connectionPosition.getValue(), - busOrBusbarSectionId == null ? null : busOrBusbarSectionId.getValue(), - network, - getVoltageLevel(network, voltageLevelId == null ? null : voltageLevelId.getValue())); - ReportNode connectionNameReport = applyElementaryModificationsAndReturnReport(feeder::withName, - () -> null, - connectionName == null && (connectionDirection != null || connectionPosition != null) ? equipmentId : connectionName, - getConnectionNameField(feederNumber)); - if (connectionNameReport != null) { - reports.add(connectionNameReport); - } - ReportNode connectionDirectionReport = applyElementaryModificationsAndReturnReport(feeder::withDirection, - () -> null, - connectionDirection == null && (connectionName != null || connectionPosition != null) ? - new AttributeModification<>(ConnectablePosition.Direction.UNDEFINED, OperationType.SET) : connectionDirection, - getConnectionDirectionField(feederNumber)); - if (connectionDirectionReport != null) { - reports.add(connectionDirectionReport); - } - ReportNode connectionPositionReport = applyElementaryModificationsAndReturnReport(feeder::withOrder, - () -> null, - connectionPosition == null && (connectionName != null || connectionDirection != null) ? - new AttributeModification<>(position, OperationType.SET) : connectionPosition, - getConnectionPositionField(feederNumber)); - if (connectionPositionReport != null) { - reports.add(connectionPositionReport); - } + int position = calculatePosition(connectionPosition, busOrBusbarSectionId, network, voltageLevelId); + + ReportNode connectionNameReport = processModification( + feeder::withName, connectionName, equipmentId, reports, + getConnectionNameField(feederNumber), connectionDirection, connectionPosition + ); + + ReportNode connectionDirectionReport = processModification( + feeder::withDirection, connectionDirection, + new AttributeModification<>(ConnectablePosition.Direction.UNDEFINED, OperationType.SET), + reports, getConnectionDirectionField(feederNumber), + connectionName, connectionPosition + ); + + ReportNode connectionPositionReport = processModification( + feeder::withOrder, connectionPosition, + new AttributeModification<>(position, OperationType.SET), + reports, getConnectionPositionField(feederNumber), + connectionName, connectionDirection + ); + if (connectionNameReport != null || connectionDirectionReport != null || connectionPositionReport != null) { feeder.add(); } } + private ReportNode processModification(Consumer setter, + AttributeModification modification, + AttributeModification defaultValue, + List reports, + String fieldName, + AttributeModification... dependentAttributes) { + + AttributeModification finalModification = (modification == null && anyNonNull(dependentAttributes)) + ? defaultValue + : modification; + + ReportNode report = applyElementaryModificationsAndReturnReport(setter, () -> null, finalModification, fieldName); + if (report != null) { + reports.add(report); + } + return report; + } + + private boolean anyNonNull(AttributeModification... attributes) { + return Arrays.stream(attributes).anyMatch(Objects::nonNull); + } + private T getConnectionDetail(BasicEquipmentModificationInfos modificationInfos, int feederNumber, Function branchFunc1, Function branchFunc2, @@ -798,12 +814,7 @@ private AttributeModification getBusOrBusbarSectionId(BasicEquipmentModi } private AttributeModification getEquipmentId(BasicEquipmentModificationInfos modificationInfos) { - if (modificationInfos instanceof BranchModificationInfos branchInfo) { - return AttributeModification.toAttributeModification(branchInfo.getEquipmentId(), OperationType.SET); - } else if (modificationInfos instanceof InjectionModificationInfos injectionInfo) { - return AttributeModification.toAttributeModification(injectionInfo.getEquipmentId(), OperationType.SET); - } - return null; + return AttributeModification.toAttributeModification(modificationInfos.getEquipmentId(), OperationType.SET); } private AttributeModification getConnectionName(BasicEquipmentModificationInfos modificationInfos, int feederNumber) { @@ -836,6 +847,18 @@ private AttributeModification getConnectionPosition(BasicEquipmentModif InjectionModificationInfos::getConnectionPosition); } + private int calculatePosition(AttributeModification connectionPosition, + AttributeModification busOrBusbarSectionId, + Network network, + AttributeModification voltageLevelId) { + return getPosition( + connectionPosition == null ? null : connectionPosition.getValue(), + busOrBusbarSectionId == null ? null : busOrBusbarSectionId.getValue(), + network, + getVoltageLevel(network, voltageLevelId == null ? null : voltageLevelId.getValue()) + ); + } + private void modifyConnection(AttributeModification terminalConnected, Identifiable equipment, Terminal terminal, List reports) { if (terminalConnected == null || equipment == null) { return;