using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ORiN2.ManagedCAO;
namespace Tester
{
public partial class ControllerForm : Form
{
public static class ExecuteCommand
{
/// 汎用REST
public static readonly string Fetch = "REST";
/// Json文字列登録・転送
public static readonly string RegistJson = "RegistJSON";
/// CSVデータ登録・転送
public static readonly string RegistCsv = "RegistCSV";
/// txtデータ登録・転送
public static readonly string RegistTxt = "RegistTXT";
/// Binデータ登録・転送
public static readonly string RegistBin = "RegistBIN";
/// 最新データ参照
public static readonly string ReferToLatestData = "ReferenceLatestData";
/// データ参照
public static readonly string ReferToPastData = "ReferencePastData";
/// データ取得
public static readonly string RetrieveData = "RetrieveData";
/// 登録データHIT数取得
public static readonly string RetrieveDataCount = "DataCount";
/// Json文字列更新
public static readonly string UpdateJson = "UpdateJSON";
/// CSVデータ更新
public static readonly string UpdateCsv = "UpdateCSV";
/// txtデータ更新
public static readonly string UpdateTxt = "UpdateTXT";
/// Binデータ更新
public static readonly string UpdateBin = "UpdateBIN";
/// データ削除
public static readonly string DeleteData = "DeleteData";
/// リソースメタデータ参照
public static readonly string ReferToMetadata = "ReferenceMetadata";
}
internal CCaoController Ctrl { get; private set; }
private List CommandList { get; set; }
public ControllerForm(CCaoController ctrl)
{
InitializeComponent();
this.Ctrl = ctrl;
this.CommandList = new List();
this.CommandList.Add(new ExecuteModel(ExecuteCommand.Fetch, new RestPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.RegistJson, new RegistJSONPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.RegistCsv, new RegistCSVPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.RegistTxt, new RegistTXTPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.RegistBin, new RegistBINPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.ReferToLatestData, new ReferenceLatestDataPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.ReferToPastData, new ReferencePastDataPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.RetrieveData, new RetrieveDataPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.RetrieveDataCount, new DataCountPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.UpdateJson, new UpdateJSONPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.UpdateCsv, new UpdateCSVPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.UpdateTxt, new UpdateTXTPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.UpdateBin, new UpdateBINPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.DeleteData, new DeleteDataPanel()));
this.CommandList.Add(new ExecuteModel(ExecuteCommand.ReferToMetadata, new ReferenceMetadataPanel()));
cmbCommand.DataSource = this.CommandList;
cmbCommand.SelectedIndex = 0;
}
private class ExecuteModel : IDisposable
{
public ExecuteModel(string command, IExecutePanel panel)
{
this.Command = command;
this.Panel = panel;
}
public string Command { get; private set; }
public IExecutePanel Panel { get; private set; }
public void Dispose()
{
if (this.Panel != null && this.Panel is UserControl)
{
var userCtrl = (UserControl)this.Panel;
if (!userCtrl.IsDisposed) userCtrl.Dispose();
}
}
public override string ToString()
{
return this.Command;
}
}
private void cmbCommand_SelectedIndexChanged(object sender, EventArgs e)
{
var item = cmbCommand.SelectedItem;
if (item != null)
{
var model = (ExecuteModel)item;
var panel = (UserControl)model.Panel;
panel.Dock = DockStyle.Fill;
if (pnlOptionParam.Controls.Count > 0) pnlOptionParam.Controls.RemoveAt(0);
pnlOptionParam.Controls.Add(panel);
}
}
private void ControllerForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.CommandList != null)
{
foreach (var cmd in this.CommandList)
{
cmd.Dispose();
}
}
}
private void btnExecute_Click(object sender, EventArgs e)
{
try
{
var item = cmbCommand.SelectedItem;
if (item != null)
{
var model = (ExecuteModel)item;
var panel = (IExecutePanel)model.Panel;
var data = panel.GetObjectArg();
var res = this.Ctrl.Execute(model.Command, data);
if (res == null)
{
MessageBox.Show(this, "Succeeded", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string str = string.Empty;
if (model.Command == ExecuteCommand.Fetch)
{
var resArr = (object[])res;
str += "■StatusCode" + Environment.NewLine + resArr[0] + Environment.NewLine + Environment.NewLine;
str += "■Header" + Environment.NewLine + resArr[1].ToString();
str += "■Body" + Environment.NewLine + (resArr[2] ?? string.Empty).ToString();
}
else
{
str = res.ToString();
}
using (var resFrm = new ResultForm(str))
{
resFrm.ShowDialog(this);
}
}
}
}
catch (System.Net.HttpListenerException httpEx)
{
MessageBox.Show(this, string.Format("StatusCode : {0}", httpEx.ErrorCode), "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}