/// <summary>
 /// Refreshes the Config of the OPS when running in-process with BSL.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="configService"></param>
 /// <param name="csUser"></param>
 /// <returns>Int success code.</returns>
 public int refreshOPSConfig(ConfigurationKeyValues key, ConfigurationService configService, bool notifyNodes, ServiceUsers csUser)
 {
     int returnCode= ConfigUtility.CLUSTER_UPDATE_FULL_SUCCESS;
     string value = key.ConfigurationKeyValue;
     string targetHostID = null;
     if (ConfigUtility.onAzure)
         targetHostID = Trade.Utility.StockTraderUtility.ORDER_PROCESSOR_SERVICE_AZURE;
     else
         targetHostID = Trade.Utility.StockTraderUtility.ORDER_PROCESSOR_SERVICE_SELFHOST;
     List<TraverseNode> traversePath = configService.getTraversePath(null, targetHostID, StockTraderUtility.ORDER_PROCESSOR_SERVICE_CONFIG, csUser);
     key = configService.getServiceConfigurationKey(targetHostID, StockTraderUtility.ORDER_PROCESSOR_SERVICE_CONFIG, key.ConfigurationKeyFieldName, traversePath, csUser);
     if (key != null)
     {
         key.ConfigurationKeyValue = value;
         traversePath = configService.getTraversePath(null, targetHostID, StockTraderUtility.ORDER_PROCESSOR_SERVICE_CONFIG, csUser);
         if (traversePath!=null || Trade.BusinessServiceConfigurationSettings.Settings.ORDER_PROCESSING_MODE.Equals(StockTraderUtility.OPS_INPROCESS))
             returnCode = configService.receiveConfigurationKey(targetHostID, StockTraderUtility.ORDER_PROCESSOR_SERVICE_CONFIG, key, key, notifyNodes, ConfigUtility.UPDATE_KEY_VALUE, traversePath, csUser);
     }
     return returnCode;
 }
        /// <summary>
        /// This method performs the logic required when a specific configuration settings key is changed.  This includes 
        /// potentially updating configuration database, notifying nodes, and performing configuration actions.
        /// </summary>
        /// <param name="targetHostNameIdentifier">The name of the target host we are seeking, as specified in the target's HOST_NAME_IDENTIFIER settings key.</param>
        /// <param name="targetConfigServiceName">The fully qualified name of the Configuration Service implementation class for the host we are seeking, used in
        /// conjunction with targetHostNameIdentifier to uniquely identify a target host.</param>
        /// <param name="oldKey">The key prior to update.</param>
        /// <param name="newKey">The updated key.</param>
        /// <param name="notifyNodes">If true, key update will be flowed to peer nodes.</param>
        /// <param name="action">Action to perform.</param>
        /// <param name="traversePath">Network path to the target host.</param>
        /// <param name="csUser">User to authenticate.</param>
        /// <returns>Int representing success code.</returns>
        public override int receiveConfigurationKey(string targetHostNameIdentifier, string targetConfigServiceNameIdentifier, ConfigurationKeyValues oldKey, ConfigurationKeyValues newKey, bool notifyNodes, string action, List<TraverseNode> traversePath, ServiceUsers csUser)
        {
            ServiceConfigHelper configHelper = new ServiceConfigHelper(settingsInstance);
            bool success = true;
            object theSettingsInstance = new Trade.StockTraderWebApplicationSettings.Settings();
            IConfigurationActions configActions;
            int returnCode = ConfigUtility.CLUSTER_UPDATE_FULL_SUCCESS;
            try
            {
                if (!(configHelper.setServiceRights(ref csUser, ConfigUtility.CONFIG_ADMIN_RIGHTS) >= ConfigUtility.CONFIG_ADMIN_RIGHTS))
                    return ConfigUtility.CLUSTER_UPDATE_FAIL_AUTHENTICATION;
                configActions = (IConfigurationActions)ConfigUtility.reflectGetField(settingsInstance, "thisServiceConfigActions");
                if ((action != ConfigUtility.ADD_KEY && action != ConfigUtility.REMOVE_KEY) && (traversePath == null || traversePath.Count <= 1 || traversePath[0] == null))
                {
                    if (targetHostNameIdentifier.Equals(StockTraderUtility.WEB_APPLICATION) || targetHostNameIdentifier.Equals(StockTraderUtility.WEB_APPLICATION_Azure))
                    {
                        if (!configActions.validateConfigurationKey(settingsInstance, newKey))
                            return ConfigUtility.CLUSTER_UPDATE_FAIL_VALIDATION;
                    }
                }
                ConfigUtility.writeConfigConsoleMessage("\nReceived Request for Configuration Key Change, Action: " + action + ". Keyname: " + oldKey.ConfigurationKeyDisplayName + "\n", EventLogEntryType.Information, true, settingsInstance);
                ConfigUtility.writeConfigConsoleMessage("Value: " + newKey.ConfigurationKeyValue + "\n\n", EventLogEntryType.Information, true, settingsInstance);
                if (traversePath == null || traversePath.Count <= 1 || traversePath[0] == null)
                {
                    bool SHARESERVICECONFIGURATIONDATA = (bool)ConfigUtility.reflectGetField(settingsInstance, "SHARESERVICECONFIGURATIONDATA");
                    if (!SHARESERVICECONFIGURATIONDATA)
                    {
                        List<ServiceUsers> thisServiceUsers = (List<ServiceUsers>)ConfigUtility.reflectGetField(settingsInstance, "thisServiceUsers");
                        ServiceUsers userLocalAdmin = thisServiceUsers.Find(delegate(ServiceUsers userExist) { return csUser.UserId.ToLower().Equals("localadmin") && userExist.Password.Equals(csUser.Password) && csUser.Rights == ConfigUtility.CONFIG_ADMIN_RIGHTS; });
                        if (userLocalAdmin == null)
                            return ConfigUtility.CLUSTER_UPDATE_FULL_SUCCESS;
                    }
                    switch (targetHostNameIdentifier)
                    {
                        case StockTraderUtility.IISHOST_BUSINESS_SERVICES:
                            {
                                theSettingsInstance = new Trade.BusinessServiceConfigurationSettings.Settings();
                                Trade.BusinessServiceHostConfigurationImplementation.ConfigurationService configService = new Trade.BusinessServiceHostConfigurationImplementation.ConfigurationService();
                                int bslRetCode = configService.receiveConfigurationKey(targetHostNameIdentifier, targetConfigServiceNameIdentifier, oldKey, newKey, notifyNodes, action, traversePath, csUser);
                                if (bslRetCode == ConfigUtility.CLUSTER_UPDATE_FULL_SUCCESS)
                                    success = true;
                                break;
                            }
                        case StockTraderUtility.ORDER_PROCESSOR_SERVICE_SELFHOST:
                            {
                                theSettingsInstance = new Trade.BusinessServiceConfigurationSettings.Settings();
                                Trade.BusinessServiceHostConfigurationImplementation.ConfigurationService configService = new Trade.BusinessServiceHostConfigurationImplementation.ConfigurationService();
                                int bslRetCode = configService.receiveConfigurationKey(targetHostNameIdentifier, targetConfigServiceNameIdentifier, oldKey, newKey, notifyNodes, action, traversePath, csUser);
                                if (bslRetCode == ConfigUtility.CLUSTER_UPDATE_FULL_SUCCESS)
                                    success = true;
                                break;
                            }

                        case StockTraderUtility.AZURE_BUSINESS_SERVICES:
                            {
                                goto case StockTraderUtility.IISHOST_BUSINESS_SERVICES;
                            }

                        case StockTraderUtility.ORDER_PROCESSOR_SERVICE_AZURE:
                            {
                                goto case StockTraderUtility.ORDER_PROCESSOR_SERVICE_SELFHOST;
                            }

                        default:
                            {
                                if (notifyNodes)
                                    success = configHelper.sendConfigurationKeyUpdateLocal(oldKey, newKey, action, csUser);
                                break;
                            }
                    }
                    if (success)
                    {
                        if (targetHostNameIdentifier.Equals(StockTraderUtility.WEB_APPLICATION) || targetHostNameIdentifier.Equals(StockTraderUtility.WEB_APPLICATION_Azure))
                            success = configActions.checkChangedSetting(theSettingsInstance, newKey, notifyNodes, csUser);
                        if (notifyNodes && success)
                        {
                            RemoteNotifications remoteNotify = new RemoteNotifications(settingsInstance);
                            return remoteNotify.sendKeyUpdateToClusterMembers(targetHostNameIdentifier, targetConfigServiceNameIdentifier, oldKey, newKey, action);
                        }
                    }
                    else
                        returnCode = ConfigUtility.CLUSTER_UPDATE_FAIL_PERSISTED;
                }
                else
                {
                    if (traversePath != null && traversePath.Count > 1)
                    {
                        RemoteNotifications remoteKeyUpdate = new RemoteNotifications(settingsInstance);
                        ConfigUtility.writeConfigConsoleMessage("     Forwarding Key to Remote Connected Service: " + traversePath[traversePath.Count - 1].MyNode.Address + "\n", EventLogEntryType.Information, true, settingsInstance);
                        return (remoteKeyUpdate.sendConfigurationKeyUpdateRemote(targetHostNameIdentifier, targetConfigServiceNameIdentifier, oldKey, newKey, notifyNodes, action, traversePath, csUser));
                    }
                }
            }
            catch (Exception e)
            {
                ServiceConfigHelper.logServiceOperationException(csUser, e.ToString(), settingsInstance);
                throw;
            }
            return (returnCode);
        }
  /// <summary>
  /// Not part of service contract; special for Web app to return a single requested specific configurationkey 
  /// from Business Service when running BSL in-process.
  /// </summary>
 /// <param name="targetHostNameIdentifier"></param>
 /// <param name="targetConfigServiceNameIdentifier"></param>
 /// <param name="settingsClassFieldName"></param>
 /// <param name="traversePath"></param>
 /// <param name="csUser"></param>
 /// <returns></returns>
  private ConfigurationKeyValues callBSLConfigKeyLocal(string targetHostNameIdentifier, string targetConfigServiceNameIdentifier, string settingsClassFieldName, List<TraverseNode> traversePath, ServiceUsers csUser)
  {
      Trade.BusinessServiceHostConfigurationImplementation.ConfigurationService BSLConfig = new Trade.BusinessServiceHostConfigurationImplementation.ConfigurationService();
      return BSLConfig.getServiceConfigurationKey(targetHostNameIdentifier, targetConfigServiceNameIdentifier, settingsClassFieldName, traversePath, csUser);
  }
        /// <summary>
        /// This method returns the composite configuration data for a service--the primary method
        /// used to collect and display the data for a series of connected services making up a composite 
        /// application. 
        /// </summary>
        /// <param name="targetHostNameIdentifier">The name of the target host we are seeking, as specified in the target's HOST_NAME_IDENTIFIER settings key.</param>
        /// <param name="targetConfigServiceName">The fully qualified name of the Configuration Service implementation class for the host we are seeking, used in
        /// conjunction with targetHostNameIdentifier to uniquely identify a target host.</param>
        /// <param name="configurationLevel">The config key level to return.</param>
        /// <param name="probeDeeper">If true, one layer deeper in the connected services tree will be probed as well.</param>
        /// <param name="traversePath">The network traverse path to the target.</param>
        /// <param name="csUser">User to authenticate.</param>
        /// <returns>Linked List of ServiceConfigurationData.</returns>
        public override List<ServiceConfigurationData> getServiceConfiguration(string targetHostNameIdentifier, string targetConfigServiceName, int configurationLevel, bool probeDeeper, List<TraverseNode> traversePath, ServiceUsers csUser)
        {
            ServiceConfigHelper configHelper = new ServiceConfigHelper(settingsInstance);
            object bslSettings = new Trade.BusinessServiceConfigurationSettings.Settings();
            List<ServiceConfigurationData> returnCompositeConfigurationData = null;
            try
            {
                if (!(configHelper.setServiceRights(ref csUser, ConfigUtility.CONFIG_CONNECTED_SERVICE_RIGHTS)>=ConfigUtility.CONFIG_CONNECTED_SERVICE_RIGHTS))
                {
                    returnCompositeConfigurationData = new List<ServiceConfigurationData>();
                    return returnCompositeConfigurationData;
                }
                if (traversePath == null || traversePath.Count <= 1 || traversePath[0] == null)
                {
                    //Step 1:  Get list of all currently configured connected services.  We need to adjust this list based on
                    //the OrderMode setting, since we do not want to query for and return connected data for the Order Processor Service
                    //if we are not running Order Processing remotely. 
                    List<ConnectedServices> retrieveConnectedServiceList = new List<ConnectedServices>();
                    if (Trade.StockTraderWebApplicationSettings.Settings.connectedServices != null && Trade.StockTraderWebApplicationSettings.Settings.connectedServices.Count > 0)
                        retrieveConnectedServiceList.AddRange(Trade.StockTraderWebApplicationSettings.Settings.connectedServices);

                    //Step 2: Here we will remove the Order Processor Service if we are not running in a remote mode. 
                    switch (Trade.StockTraderWebApplicationSettings.Settings.ACCESS_MODE)
                    {
                        case StockTraderUtility.BSL_INPROCESS:
                            {
                                //In this case, we want no Business Service connections--so remove by contract name.
                                retrieveConnectedServiceList.RemoveAll(delegate(ConnectedServices cs) { return (cs.ServiceContract.Equals(Trade.StockTraderWebApplicationSettings.Settings.BSL_CLIENT_CONNECTED_SERVICE_CONTRACT)); });
                                break;
                            }

                        default:
                            {
                                break;
                            }
                    }
                    returnCompositeConfigurationData = configHelper.getMyServiceConfiguration(targetHostNameIdentifier, targetConfigServiceName, configurationLevel, csUser, retrieveConnectedServiceList, probeDeeper);
                    //Now another special case for the Business Services app: it can run OPS Services in-process.  In this case,
                    //we will add data for OPS Services by invoking the config service in-process, and pointing to the OPS self-host repository.
                    if (Trade.StockTraderWebApplicationSettings.Settings.ACCESS_MODE == StockTraderUtility.BSL_INPROCESS)
                    {
                        string theTargetHost = null;
                        string theTargetConfig = null;
                        List<ServiceConfigurationData> bslConfigData = null;
                        switch (targetHostNameIdentifier)
                        {
                            case StockTraderUtility.IISHOST_BUSINESS_SERVICES:
                                {
                                    theTargetHost = targetHostNameIdentifier;
                                    theTargetConfig = targetConfigServiceName;
                                    break;
                                }
                            case StockTraderUtility.AZURE_BUSINESS_SERVICES:
                                {
                                    goto case StockTraderUtility.IISHOST_BUSINESS_SERVICES;
                                }
                            case StockTraderUtility.ORDER_PROCESSOR_SERVICE_SELFHOST:
                                {
                                    goto case StockTraderUtility.IISHOST_BUSINESS_SERVICES;
                                }
                            case StockTraderUtility.ORDER_PROCESSOR_SERVICE_AZURE:
                                {
                                    goto case StockTraderUtility.IISHOST_BUSINESS_SERVICES;
                                }
                            default:
                                {
                                    theTargetHost = StockTraderUtility.WEB_APPLICATION;
                                    theTargetConfig = StockTraderUtility.WEBAPPLICATION_CONFIG;
                                    break;
                                }
                        }
                        Trade.BusinessServiceHostConfigurationImplementation.ConfigurationService bslServiceConfig = new Trade.BusinessServiceHostConfigurationImplementation.ConfigurationService();
                        bslConfigData = bslServiceConfig.getServiceConfiguration(theTargetHost, theTargetConfig, configurationLevel, false, traversePath, csUser);
                        if (bslConfigData != null)
                        {
                            for (int i = 0; i < bslConfigData.Count; i++)
                            {
                                bslConfigData[i].InProcessHost = Trade.StockTraderWebApplicationSettings.Settings.HOST_NAME_IDENTIFIER;
                            }
                            returnCompositeConfigurationData.AddRange(bslConfigData);
                        }
                    }
                }
                else
                {
                    if (traversePath != null && traversePath.Count > 1)
                    {
                        ConfigUtility.writeConfigConsoleMessage("-----> Forwarding Request to Remote Connected Service: " + traversePath[traversePath.Count - 1].MyNode.Address + "\n", EventLogEntryType.Information, true, settingsInstance);
                        RemoteNotifications remoteNotify = new RemoteNotifications(settingsInstance);
                        returnCompositeConfigurationData = remoteNotify.getRemoteServiceConfiguration(targetHostNameIdentifier, targetConfigServiceName, configurationLevel, probeDeeper, traversePath, csUser, false, null);
                    }
                }
            }
            catch (Exception e)
            {
                ServiceConfigHelper.logServiceOperationException(csUser, e.ToString(), settingsInstance);
                throw;
            }
            return returnCompositeConfigurationData;
        }