5
5
package org .lfenergy .compas .sct .commons ;
6
6
7
7
import lombok .NonNull ;
8
+ import lombok .RequiredArgsConstructor ;
8
9
import org .lfenergy .compas .scl2007b4 .model .SCL ;
9
10
import org .lfenergy .compas .scl2007b4 .model .TBay ;
10
11
import org .lfenergy .compas .scl2007b4 .model .TSubstation ;
11
12
import org .lfenergy .compas .scl2007b4 .model .TVoltageLevel ;
12
13
import org .lfenergy .compas .sct .commons .api .SubstationEditor ;
13
14
import org .lfenergy .compas .sct .commons .exception .ScdException ;
14
- import org .lfenergy .compas .sct .commons .scl .SclRootAdapter ;
15
- import org .lfenergy .compas .sct .commons .scl .sstation .SubstationAdapter ;
16
- import org .lfenergy .compas .sct .commons .scl .sstation .VoltageLevelAdapter ;
17
15
16
+ @ RequiredArgsConstructor
18
17
public class SubstationService implements SubstationEditor {
19
18
19
+ private final VoltageLevelService voltageLevelService ;
20
+
20
21
@ Override
21
22
public void addSubstation (@ NonNull SCL scd , @ NonNull SCL ssd ) throws ScdException {
22
23
if (scd .getSubstation ().size () > 1 ) {
@@ -25,53 +26,48 @@ public void addSubstation(@NonNull SCL scd, @NonNull SCL ssd) throws ScdExceptio
25
26
if (ssd .getSubstation ().size () != 1 ) {
26
27
throw new ScdException (String .format ("SSD file must have exactly 1 Substation, but got %d" , ssd .getSubstation ().size ()));
27
28
}
28
- TSubstation ssdTSubstation = ssd .getSubstation ().get ( 0 );
29
+ TSubstation ssdTSubstation = ssd .getSubstation ().getFirst ( );
29
30
if (scd .getSubstation ().isEmpty ()) {
30
31
scd .getSubstation ().add (ssdTSubstation );
31
32
} else {
32
- TSubstation scdTSubstation = scd .getSubstation ().get (0 );
33
- if (scdTSubstation .getName ().equalsIgnoreCase (ssdTSubstation .getName ())) {
34
- SubstationAdapter scdSubstationAdapter = new SclRootAdapter (scd ).getSubstationAdapter (scdTSubstation .getName ());
33
+ TSubstation scdTSubstation = scd .getSubstation ().getFirst ();
34
+ if (scdTSubstation .getName ().equalsIgnoreCase (ssdTSubstation .getName ())){
35
35
for (TVoltageLevel tvl : ssdTSubstation .getVoltageLevel ()) {
36
- updateVoltageLevel (scdSubstationAdapter , tvl );
36
+ updateVoltageLevel (scd , tvl );
37
37
}
38
- } else
38
+ } else {
39
39
throw new ScdException ("SCD file must have only one Substation and the Substation name from SSD file is" +
40
40
" different from the one in SCD file. The files are rejected." );
41
+ }
41
42
}
42
43
}
43
44
44
45
/**
45
46
* Creates new VoltageLevel section or updates VoltageLevel contents
46
- * @param scdSubstationAdapter Substation in which VoltageLevel should be created/updated
47
+ * @param scd SCL contain Substation in which VoltageLevel should be created/updated
47
48
* @param vl VoltageLevel to create/update
48
49
* @throws ScdException throws when unable to create new VoltageLevel section which is not already present in Substation
49
50
*/
50
- private void updateVoltageLevel (@ NonNull SubstationAdapter scdSubstationAdapter , TVoltageLevel vl ) throws ScdException {
51
- if (scdSubstationAdapter .getVoltageLevelAdapter (vl .getName ()).isPresent ()) {
52
- VoltageLevelAdapter scdVoltageLevelAdapter = scdSubstationAdapter .getVoltageLevelAdapter (vl .getName ())
53
- .orElseThrow (() -> new ScdException ("Unable to create VoltageLevelAdapter" ));
54
- for (TBay tbay : vl .getBay ()) {
55
- updateBay (scdVoltageLevelAdapter , tbay );
56
- }
57
- } else {
58
- scdSubstationAdapter .getCurrentElem ().getVoltageLevel ().add (vl );
59
- }
51
+ private void updateVoltageLevel (@ NonNull SCL scd , TVoltageLevel vl ) throws ScdException {
52
+ voltageLevelService .findVoltageLevel (scd , tVoltageLevel -> tVoltageLevel .getName ().equals (vl .getName ()))
53
+ .ifPresentOrElse (tVoltageLevel -> vl .getBay ().forEach (tBay -> updateBay (tVoltageLevel , tBay )),
54
+ ()-> scd .getSubstation ().getFirst ().getVoltageLevel ().add (vl ));
60
55
}
61
56
57
+
62
58
/**
63
59
* Adds new Bay in VoltageLevel or if already exist removes and replaces it
64
- * @param scdVoltageLevelAdapter VoltageLevel in which Bay should be created/updated
60
+ * @param tVoltageLevel VoltageLevel in which Bay should be created/updated
65
61
* @param tBay Bay to add
66
62
*/
67
- private void updateBay (@ NonNull VoltageLevelAdapter scdVoltageLevelAdapter , TBay tBay ) {
68
- if ( scdVoltageLevelAdapter . getBayAdapter ( tBay . getName ()). isPresent ()) {
69
- scdVoltageLevelAdapter . getCurrentElem ().getBay ( )
70
- .removeIf ( t -> t . getName (). equalsIgnoreCase ( tBay . getName ()));
71
- scdVoltageLevelAdapter . getCurrentElem (). getBay (). add ( tBay );
72
- } else {
73
- scdVoltageLevelAdapter . getCurrentElem () .getBay ().add (tBay );
74
- }
63
+ private void updateBay (@ NonNull TVoltageLevel tVoltageLevel , TBay tBay ) {
64
+ tVoltageLevel . getBay ()
65
+ . stream ().filter ( tBay1 -> tBay1 . getName (). equals ( tBay . getName ()) )
66
+ .findFirst ()
67
+ . ifPresentOrElse ( tBay1 -> {
68
+ tVoltageLevel . getBay (). removeIf ( t -> t . getName (). equalsIgnoreCase ( tBay . getName ()));
69
+ tVoltageLevel .getBay ().add (tBay );
70
+ }, ()-> tVoltageLevel . getBay (). add ( tBay ));
75
71
}
76
72
77
73
}
0 commit comments