public void MigrateProtocolSettings(Services.TpmContext cloudContext, Services.OnewayAgreement cloudOnewayAgreement, Server.ProtocolSettings serverProtocolSettings, Services.BusinessProfile senderProfile, string agreementName, out MigrationStatus migrationStatus)
        {
            this.envelopeOverridesMigrator   = new EnvelopeOverridesMigrator(cloudContext);
            this.validationOverridesMigrator = new ValidationOverridesMigrator(cloudContext);
            Services.ProtocolSettings cloudProtocolSettings;
            switch (serverProtocolSettings.ProtocolName)
            {
            case AppConstants.X12ProtocolName:
                cloudProtocolSettings = CreateX12ProtocolSettings((Server.X12ProtocolSettings)serverProtocolSettings);
                migrationStatus       = MigrationStatus.Succeeded;
                break;

            case AppConstants.AS2ProtocolName:
                cloudProtocolSettings = CreateAS2ProtocolSettings((Server.AS2ProtocolSettings)serverProtocolSettings);
                CleanAs2ProtocolSettings((Services.AS2ProtocolSettings)cloudProtocolSettings, senderProfile);
                this.UpdateStatus(agreementName, out migrationStatus);
                break;

            case AppConstants.EdifactProtocolName:
                cloudProtocolSettings = CreateEdifactProtocolSettings((Server.EDIFACTProtocolSettings)serverProtocolSettings);
                migrationStatus       = MigrationStatus.Succeeded;
                break;

            default:
                throw new NotSupportedException();
            }

            cloudContext.AddToProtocolSettings(cloudProtocolSettings);
            cloudProtocolSettings.OnewayAgreement = cloudOnewayAgreement;
            cloudContext.SetLink(cloudProtocolSettings, "OnewayAgreement", cloudOnewayAgreement);
            cloudOnewayAgreement.ProtocolSettings = cloudProtocolSettings;
            cloudContext.SetLink(cloudOnewayAgreement, "ProtocolSettings", cloudProtocolSettings);

            var serverX12ProtocolSettings = serverProtocolSettings as Server.X12ProtocolSettings;
            var cloudX12ProtocolSettings  = cloudProtocolSettings as Services.X12ProtocolSettings;

            if (serverX12ProtocolSettings != null)
            {
                this.envelopeOverridesMigrator.MigrateAllX12EnvelopeOverrides(serverX12ProtocolSettings, cloudX12ProtocolSettings);
                this.validationOverridesMigrator.MigrateAllX12ValidationOverrides(serverX12ProtocolSettings, cloudX12ProtocolSettings);
            }

            var serverEdifactProtocolSettings = serverProtocolSettings as Server.EDIFACTProtocolSettings;
            var cloudEdifactProtocolSettings  = cloudProtocolSettings as Services.EDIFACTProtocolSettings;

            if (serverEdifactProtocolSettings != null)
            {
                this.envelopeOverridesMigrator.MigrateAllEdifactEnvelopeOverrides(serverEdifactProtocolSettings, cloudEdifactProtocolSettings);
                this.validationOverridesMigrator.MigrateAllEdifactValidationOverrides(serverEdifactProtocolSettings, cloudEdifactProtocolSettings);
            }
        }
        public void MigrateOnewayAgreement(
            Services.TpmContext cloudContext,
            Server.OnewayAgreement serverOnewayAgreement,
            Server.QualifierIdentity serverSenderBusinessIdentity,
            Server.QualifierIdentity serverReceiverBusinessIdentity,
            Services.Agreement cloudAgreement,
            string onewayAgreementType,
            out MigrationStatus migrationStatus)
        {
            Services.OnewayAgreement cloudOnewayAgreement = new Services.OnewayAgreement();
            cloudContext.AddToOnewayAgreements(cloudOnewayAgreement);
            cloudContext.RelateEntities(
                cloudOnewayAgreement,
                cloudAgreement,
                onewayAgreementType == "OnewayAgreementAToB" ? "AgreementAsAToB" : "AgreementAsBToA",
                onewayAgreementType,
                Services.RelationshipCardinality.OneToOne);

            this.LinkBusinessProfilesToOnewayAgreement(
                cloudContext,
                cloudOnewayAgreement,
                cloudAgreement.BusinessProfileA,
                cloudAgreement.BusinessProfileB,
                serverSenderBusinessIdentity,
                serverReceiverBusinessIdentity,
                onewayAgreementType);

            // Migrate send and receive protocol settings
            Server.ProtocolSettings serverProtocolSettings;
            switch (cloudAgreement.ProtocolName)
            {
            case AppConstants.X12ProtocolName:
                serverProtocolSettings = serverOnewayAgreement.GetProtocolSettings <Server.X12ProtocolSettings>();
                break;

            case AppConstants.AS2ProtocolName:
                serverProtocolSettings = serverOnewayAgreement.GetProtocolSettings <Server.AS2ProtocolSettings>();
                break;

            case AppConstants.EdifactProtocolName:
                serverProtocolSettings = serverOnewayAgreement.GetProtocolSettings <Server.EDIFACTProtocolSettings>();
                break;

            default:
                throw new NotSupportedException("Migration of  X12, AS2, EDIFACT agreements only is supported");
            }

            this.protocolSettingsMigrator.MigrateProtocolSettings(cloudContext, cloudOnewayAgreement, serverProtocolSettings, onewayAgreementType == "OnewayAgreementAToB" ? cloudAgreement.BusinessProfileA : cloudAgreement.BusinessProfileB, cloudAgreement.Name, out migrationStatus);
        }
        private void LinkBusinessProfilesToOnewayAgreement(
            Services.TpmContext cloudContext,
            Services.OnewayAgreement cloudOnewayAgreement,
            Services.BusinessProfile agreementBusinessProfileA,
            Services.BusinessProfile agreementBusinessProfileB,
            Server.QualifierIdentity serverAgreementProfileAIdentity,
            Server.QualifierIdentity serverAgreementProfileBIdentity,
            string onewayAgreementType)
        {
            Services.BusinessProfile senderProfile, receiverProfile;
            if (onewayAgreementType == "OnewayAgreementAToB")
            {
                senderProfile   = agreementBusinessProfileA;
                receiverProfile = agreementBusinessProfileB;
            }
            else
            {
                senderProfile   = agreementBusinessProfileB;
                receiverProfile = agreementBusinessProfileA;
            }

            Services.BusinessIdentity cloudSenderBusinessIdentity, cloudReceiverBusinessIdentity;
            if (!TryGetCloudBusinessIdentity(senderProfile, serverAgreementProfileAIdentity, out cloudSenderBusinessIdentity) ||
                !TryGetCloudBusinessIdentity(receiverProfile, serverAgreementProfileBIdentity, out cloudReceiverBusinessIdentity))
            {
                throw new TpmMigrationException(string.Format(
                                                    CultureInfo.InvariantCulture,
                                                    "Business identities do not exist: {0}, {1}; {2}, {3}",
                                                    serverAgreementProfileAIdentity.Qualifier,
                                                    serverAgreementProfileAIdentity.Value,
                                                    serverAgreementProfileBIdentity.Qualifier,
                                                    serverAgreementProfileBIdentity.Value));
            }

            cloudContext.RelateEntities(cloudOnewayAgreement, cloudSenderBusinessIdentity, "SenderBusinessIdentity", "OnewayAgreementSender", Services.RelationshipCardinality.ManyToOne);
            cloudContext.RelateEntities(cloudOnewayAgreement, cloudReceiverBusinessIdentity, "ReceiverBusinessIdentity", "OnewayAgreementReceiver", Services.RelationshipCardinality.ManyToOne);
        }