//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// .NET Service Configuration V2.0 for Design Considerations for Service-Oriented Applications based on Windows Communication Foundation. Created with Microsoft .NET Framework 3.5 and Microsoft Visual Studio. Copyright 2008, Microsoft Corporation.
using System;
using System.Web;
using Trade.ConfigClient;
using Trade.ConfigServiceDataContract;
using Trade.StockTraderWebApplicationSettings;
namespace Trade.Web
{
public partial class ConfigurationAdvanced : System.Web.UI.Page
{
public void Page_Init(object sender, EventArgs e)
{
LoadDropDownList();
}
public void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadCurrentSettings();
UpdateBsURL();
UpdateOpsURL();
}
else
{
var target = Request.Form["__EVENTTARGET"];
if (target.Equals(this.BsName.UniqueID))
UpdateBsURL();
else if (target.Equals(this.OpsName.UniqueID))
UpdateOpsURL();
}
}
///
/// Loads the dropdown lists with the business services and order processing services
///
private void LoadDropDownList()
{
ConfigServiceClient configClient = new ConfigServiceClient();
//Populate the dropdown lists
BsName.DataSource = configClient.GetBSLocations();
BsName.DataTextField = "ServiceName";
BsName.DataValueField = "ServiceName";
BsName.DataBind();
OpsName.DataSource = configClient.GetOPSLocations();
OpsName.DataTextField = "ServiceName";
OpsName.DataValueField = "ServiceName";
OpsName.DataBind();
}
///
/// Sets the BS and OPS dropdowns to the current values from the configuration service
///
private void LoadCurrentSettings()
{
ConfigServiceClient configClient = new ConfigServiceClient();
//Select the current configuration settings as the default values in the dropdown list
ClientConfigRequest existingClientConfig = new ClientConfigRequest();
existingClientConfig.ClientName = Settings.CLIENT_LABEL;
BsName.SelectedValue = configClient.GetClientConfig(existingClientConfig).BSName;
BSConfigRequest existingBsConfig = new BSConfigRequest();
existingBsConfig.BSName = BsName.SelectedValue;
OpsName.SelectedValue = configClient.GetBSConfig(existingBsConfig).OPSName;
}
private void UpdateBsURL()
{
ConfigServiceClient configClient = new ConfigServiceClient();
var bsLocations = configClient.GetBSLocations();
foreach (ServiceLocation s in bsLocations)
{
if (s.ServiceName.Equals(BsName.SelectedValue))
{
BsURL.Text = s.ServiceURL;
BsSec.Checked = s.Sec;
break;
}
}
}
private void UpdateOpsURL()
{
ConfigServiceClient configClient = new ConfigServiceClient();
var opsLocations = configClient.GetOPSLocations();
foreach (ServiceLocation s in opsLocations)
{
if (s.ServiceName.Equals(OpsName.SelectedValue))
{
OpsURL.Text = s.ServiceURL;
OpsSec.Checked = s.Sec;
break;
}
}
}
private const string BsNameCacheKey = "BSNAME";
private const string OpsNameCacheKey = "OPSNAME";
protected void SetButton_Click(object sender, EventArgs e)
{
try
{
// create client
ConfigServiceClient configClient = new ConfigServiceClient();
// create and update new BS
ServiceLocation newBs = new ServiceLocation();
newBs.ServiceName = this.BsName.SelectedValue;
newBs.ServiceURL = this.BsURL.Text;
newBs.Sec = this.BsSec.Checked;
// send ws call
configClient.SetServiceLocation(newBs);
// create and update new OPS
ServiceLocation newOps = new ServiceLocation();
newOps.ServiceName = this.OpsName.SelectedValue;
newOps.ServiceURL = this.OpsURL.Text;
newOps.Sec = this.OpsSec.Checked;
// send ws call
configClient.SetServiceLocation(newOps);
// Set Client Endpoint Configuration
ClientToBS clientRequest = new ClientToBS();
clientRequest.Client = Settings.CLIENT_LABEL;
clientRequest.Bs = newBs.ServiceName;
// send ws call
configClient.SetClientToBS(clientRequest);
if (HttpRuntime.Cache[BsNameCacheKey] != null)
{
HttpRuntime.Cache.Remove(BsNameCacheKey);
}
HttpRuntime.Cache.Insert(BsNameCacheKey, clientRequest.Bs);
// Set BS Endpoint Configuration
BSToOPS bsRequest = new BSToOPS();
bsRequest.Bs = newBs.ServiceName;
bsRequest.Ops = newOps.ServiceName;
// send ws call
configClient.SetBSToOPS(bsRequest);
if (HttpRuntime.Cache[OpsNameCacheKey] != null)
{
HttpRuntime.Cache.Remove(OpsNameCacheKey);
}
HttpRuntime.Cache.Insert(OpsNameCacheKey, bsRequest.Ops);
ConfirmationText.Text = ("Configuration successfully changed to: "+Settings.CLIENT_LABEL+" -> " + clientRequest.Bs + " -> " + bsRequest.Ops);
ConfirmationText.ForeColor = System.Drawing.Color.Green;
ConfirmationText.Visible = true;
}
catch
{
ConfirmationText.Text = ("Unsuccessful Configuration");
ConfirmationText.ForeColor = System.Drawing.Color.Red;
ConfirmationText.Visible = true;
throw;
}
UpdateBsURL();
UpdateOpsURL();
}
protected void RestoreDefaults_Click(object sender, EventArgs e)
{
BsName.SelectedValue = Settings.BS_LABEL;
OpsName.SelectedValue = Settings.OPS_LABEL;
UpdateBsURL();
UpdateOpsURL();
}
protected void BackButton_Click(object sender, EventArgs e)
{
Response.Redirect(Settings.PAGE_CONFIGURATION, true);
}
}
}