55package org .lfenergy .compas .sct .commons ;
66
77import lombok .NonNull ;
8+ import lombok .RequiredArgsConstructor ;
89import org .lfenergy .compas .scl2007b4 .model .SCL ;
910import org .lfenergy .compas .scl2007b4 .model .TBay ;
1011import org .lfenergy .compas .scl2007b4 .model .TSubstation ;
1112import org .lfenergy .compas .scl2007b4 .model .TVoltageLevel ;
1213import org .lfenergy .compas .sct .commons .api .SubstationEditor ;
1314import 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 ;
1715
16+ @ RequiredArgsConstructor
1817public class SubstationService implements SubstationEditor {
1918
19+ private final VoltageLevelService voltageLevelService ;
20+
2021 @ Override
2122 public void addSubstation (@ NonNull SCL scd , @ NonNull SCL ssd ) throws ScdException {
2223 if (scd .getSubstation ().size () > 1 ) {
@@ -25,53 +26,48 @@ public void addSubstation(@NonNull SCL scd, @NonNull SCL ssd) throws ScdExceptio
2526 if (ssd .getSubstation ().size () != 1 ) {
2627 throw new ScdException (String .format ("SSD file must have exactly 1 Substation, but got %d" , ssd .getSubstation ().size ()));
2728 }
28- TSubstation ssdTSubstation = ssd .getSubstation ().get ( 0 );
29+ TSubstation ssdTSubstation = ssd .getSubstation ().getFirst ( );
2930 if (scd .getSubstation ().isEmpty ()) {
3031 scd .getSubstation ().add (ssdTSubstation );
3132 } 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 ())){
3535 for (TVoltageLevel tvl : ssdTSubstation .getVoltageLevel ()) {
36- updateVoltageLevel (scdSubstationAdapter , tvl );
36+ updateVoltageLevel (scd , tvl );
3737 }
38- } else
38+ } else {
3939 throw new ScdException ("SCD file must have only one Substation and the Substation name from SSD file is" +
4040 " different from the one in SCD file. The files are rejected." );
41+ }
4142 }
4243 }
4344
4445 /**
4546 * 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
4748 * @param vl VoltageLevel to create/update
4849 * @throws ScdException throws when unable to create new VoltageLevel section which is not already present in Substation
4950 */
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 ));
6055 }
6156
57+
6258 /**
6359 * 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
6561 * @param tBay Bay to add
6662 */
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 ));
7571 }
7672
7773}
0 commit comments