Search This Blog

Thursday, April 20, 2017

AX2012 Actiavte/Deactivate Ports using X++

create enum 1. AIFPortActivorDeactive  with Acivate,Decativate as enumvalues.
                    2. AIF portType with inbound and Outbound as enumvalues.

class krishhAIFPortsUtil extends RunBaseBatch
{
    Map serviceStatusMap;
    Map portWsdlUrlMap;
    AIFPortActivorDeactive portActiveorDeactive;
    AIFPortType aifPortType;


    DialogField     dlgActiveorDeactive;
    DialogField     dlgaifPortType;

    #define.CurrentVersion(1)
    #define.Version1(1)
    #localmacro.CurrentList
        portActiveorDeactive,
        aifPortType
    #endmacro
}

private void deployInboundPort(AifInboundPort port)
{
        startLengthyOperation();
        AifPortManager::deployPort(port.Name);
        this.updateServiceHostStatus();
        endLengthyOperation();
}

private void deployOutboundPort(AifOutboundPort port)
{
    startLengthyOperation();
    AifPortManager::deployPort(port.Name);
    endLengthyOperation();
}


public Object dialog()
{
    DialogRunbase       dialog = super();
    #resAppl
;

    dlgActiveorDeactive=dialog.addField(enumStr(AIFPortActivorDeactive), "Activate/Deactivate");
    dlgaifPortType=dialog.addField(enumStr(AIFPortType), "PortType");
    return dialog;
}

public boolean getFromDialog()
{
;
    portActiveorDeactive=dlgActiveorDeactive.value();
    aifPortType=dlgaifPortType.value();
    return super();
}

str getPortRuntimeError(AifInboundPort port)
{
    str serviceTypeName, status;
    boolean isPortStarted = false;
#Aif

    if (port.Deployed)
    {
        serviceTypeName = strFmt('%1.%2', #DotNetServiceNamespace, port.Name);

        if (!serviceStatusMap)
        {
            // If the map is null, then the IL appDomain does not exist and services are not available
            status = strFmt("@SYS345108");
        }
        else if (serviceStatusMap.exists(serviceTypeName))
        {
            status = serviceStatusMap.lookup(serviceTypeName);
            isPortStarted = strStartsWith(status, #WcfCommunicationState_Opened);
        }
    }

    return isPortStarted?"":status;
}

// BP deviation documented
str getPortWsdlUrl(AifInboundPort port)
{
    str wsdlUrl = '';
    AifPortName portName;
    AifWcfAdapter wcfAdapter;

    if (port.Deployed)
    {
        portName = port.Name;

        if (portWsdlUrlMap.exists(portName))
        {
            wsdlUrl = portWsdlUrlMap.lookup(portName);
        }
        else
        {
            wcfAdapter = AifWcfAdapter::getWcfAdapterForPort(port);

            if (wcfAdapter != null)
            {
                wsdlUrl = wcfAdapter.getWsdlUrl(port.ChannelId, port.Name);
            }

            portWsdlUrlMap.insert(portName, wsdlUrl);
        }
    }

    return wsdlUrl;
}

// BP deviation documented
boolean isPortRunning(AifInboundPort port)
{
    str serviceTypeName, status;
    boolean isPortStarted = false;
#Aif

    if (port.Deployed)
    {
        serviceTypeName = strFmt('%1.%2', #DotNetServiceNamespace, port.Name);

        // If the map is null, then the IL appDomain does not exist and services are not available
        if (serviceStatusMap && serviceStatusMap.exists(serviceTypeName))
        {
            status = serviceStatusMap.lookup(serviceTypeName);
            isPortStarted = strStartsWith(status, #WcfCommunicationState_Opened);
        }
    }

    return isPortStarted;
}

public container pack()
{
    return [#CurrentVersion,#CurrentList];
}

public boolean unpack(container packedClass)
{
    Version version = RunBase::getVersion(packedClass);
;
    switch (version)
    {
        case #CurrentVersion:
            [version,#CurrentList] = packedClass;
            break;
        default:
            return false;
    }

    return true;
}

public AIFPortType parmAifPortType(AIFPortType _aifPortType = aifPortType)
{
    aifPortType = _aifPortType;

    return aifPortType;
}

public AIFPortActivorDeactive parmPortActiveorDeactive(AIFPortActivorDeactive _portActiveorDeactive = portActiveorDeactive)
{
    portActiveorDeactive = _portActiveorDeactive;

    return portActiveorDeactive;
}

public void portinit()
{
    AifSetup::initialize();
    // Register out of the box adapters
    AifSetup::registerOutOfTheBoxAdapters();

    portWsdlUrlMap = new Map(Types::String, Types::String);
}

/// <summary>
///    Contains the code that does the actual job of the class.
/// </summary>
public void run()
{
    #OCCRetryCount
    AifInboundPort AifInboundPort;

    AifOutboundPort AifOutboundPort;

    try
    {
        this.updateServiceHostStatus();
        switch (aifPortType)
        {
            case AIFPortType::Inbound:
                if(AIFPortActivorDeactive::Activate==portActiveorDeactive)
                {
                    while select AifInboundPort //where AifInboundPort.PortCategory==AifPortCategory::Enhanced
                    {

                          if(this.isPortRunning(AifInboundPort))
                          {
                              this.portinit();
                              this.deployInboundPort(AifInboundPort);
                              if(this.getPortRuntimeError(AifInboundPort)!="")
                              {
                                  info(strFmt("@krishh1896",AifInboundPort.Name,this.getPortRuntimeError(AifInboundPort)));
                              }
                          }
                    }
                }
                else
                {
                    while select AifInboundPort where AifInboundPort.PortCategory==AifPortCategory::Enhanced
                    {
                          this.portinit();
                          this.undeployInboundPort(AifInboundPort);
                    }
                }
             break;
            case AIFPortType::OutBound:
                if(IFPortActivorDeactive::Activate==portActiveorDeactive)
                {
                    while select AifOutboundPort
                    {
                              this.portinit();
                              this.deployOutboundPort(AifOutboundPort);

                    }
                }
                else
                {
                    while select AifOutboundPort
                    {
                          this.portinit();
                          this.unDeployOutboundPort(AifOutboundPort);
                    }
                }
            break;
            default:
            throw error(strfmt("@SYS12580",aifPortType));



        }
    }
    catch (Exception::Deadlock)
    {
        retry;
    }
    catch (Exception::UpdateConflict)
    {
        if (appl.ttsLevel() == 0)
        {
            if (xSession::currentRetryCount() >= #RetryNum)
            {
                throw Exception::UpdateConflictNotRecovered;
            }
            else
            {
                retry;
            }
        }
        else
        {
            throw Exception::UpdateConflict;
        }
    }

}

private void undeployInboundPort(AifInboundPort port)
{
        startLengthyOperation();
        AifPortManager::undeployPort(port.Name);
        this.updateServiceHostStatus();
        //this.clearPortWsdlUrl(port);
        endLengthyOperation();
}

private void unDeployOutboundPort(AifOutboundPort port)
{
    startLengthyOperation();
    AifPortManager::undeployPort(port.Name);
    endLengthyOperation();
}
public void updateServiceHostStatus()
{
    // If the map is null, then the IL appDomain does not exist and services are not available
    serviceStatusMap = appl.getServiceHostStatus();
}

static krishhAIFPortsUtil construct()
{
    return new krishhAIFPortsUtil();
}
public static void main(Args _args)
{
    krishhAIFPortsUtil  krishhAIFPortsUtil ;
;
    krishhAIFPortsUtil = krishhAIFPortsUtil  ::construct();

    if (krishhAIFPortsUtil .prompt())
        krishhAIFPortsUtil .run();
}

public static void mainAutoRun(str _args)
{
    krishhAIFPortsUtil aifPortsUtil;
    ;
    aifPortsUtil = krishhAIFPortsUtil ::construct();

    if (!_args)
        return;

    switch(_args)
    {
        case('activate') :
            {
                aifPortsUtil.parmPortActiveorDeactive(AIFPortActivorDeactive::Activate);
            }

        case('deactivate') :
            {
                aifPortsUtil.parmPortActiveorDeactive(AIFPortActivorDeactive::Deactivate);
            }
    }

    aifPortsUtil.run();
}

No comments:

Post a Comment

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.