/**
 * Copyright (c) 2017-2018, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.powsybl.cgmes.conversion.elements;

import com.powsybl.cgmes.conversion.Context;
import com.powsybl.cgmes.model.PowerFlow;
import com.powsybl.iidm.network.EnergySource;
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.GeneratorAdder;
import com.powsybl.triplestore.api.PropertyBag;

/**
 * @author Luma Zamarreño <zamarrenolm at aia.es>
 */
public class EquivalentInjectionConversion extends AbstractReactiveLimitsOwnerConversion {

    public EquivalentInjectionConversion(PropertyBag sm, Context context) {
        super("EquivalentInjection", sm, context);
    }

    @Override
    public void convert() {
        double minP = p.asDouble("minP", -Double.MAX_VALUE);
        double maxP = p.asDouble("maxP", Double.MAX_VALUE);
        EnergySource energySource = EnergySource.OTHER;
        PowerFlow f = powerFlow();

        double targetP = 0;
        double targetQ = 0;
        if (f.defined()) {
            targetP = -f.p();
            targetQ = -f.q();
        }
        boolean regulationCapability = p.asBoolean("regulationCapability", false);
        boolean regulationStatus = p.asBoolean("regulationStatus", regulationCapability);
        regulationStatus = regulationStatus && terminalConnected();
        double targetV = Double.NaN;
        if (terminalConnected() && regulationStatus) {
            targetV = p.asDouble("regulationTarget");
            if (targetV == 0) {
                fixed("regulationTarget", "Target voltage value can not be zero", targetV,
                        voltageLevel().getNominalV());
                targetV = voltageLevel().getNominalV();
            }
        }

        GeneratorAdder adder = voltageLevel().newGenerator()
                .setMinP(minP)
                .setMaxP(maxP)
                .setVoltageRegulatorOn(regulationStatus)
                // .setRegulatingTerminal(regulatingTerminal)
                .setTargetP(targetP)
                .setTargetQ(targetQ)
                .setTargetV(targetV)
                // .setRatedS(ratedS)
                .setEnergySource(energySource);
        identify(adder);
        connect(adder);
        Generator g = adder.add();
        convertedTerminals(g.getTerminal());
        convertReactiveLimits(g);
    }
}

@@ -735,16 +735,89 @@
 sion %7B%0A%0A
+    private static final String REGULATION_TARGET = %22regulationTarget%22;%0A%0A
     publ
@@ -1342,32 +1342,33 @@
 .q();%0A        %7D%0A
+%0A
         boolean 
@@ -1500,16 +1500,26 @@
 Status%22,
+ false) &&
  regulat
@@ -1531,18 +1531,280 @@
 pability
-)
 ;
+%0A        if (!p.containsKey(%22regulationStatus%22) %7C%7C !p.containsKey(REGULATION_TARGET)) %7B%0A            context.missing(String.format(%22Missing regulationStatus or regulationTarget for EquivalentInjection %25s. Voltage regulation is considered as off.%22, id));%0A        %7D%0A
 %0A       
@@ -1989,34 +1989,33 @@
 sDouble(
-%22regulationTarget%22
+REGULATION_TARGET
 );%0A     
@@ -2063,34 +2063,33 @@
   fixed(
-%22regulationTarget%22
+REGULATION_TARGET
 , %22Targe

Correct handling of missing attributes in EI during CGMES import (#908)