public interface IValidator
{
DirectoryInfo OutputDirectoryFromClient { get; set; }
DirectoryInfo OutputDirectoryOnServer { get; set; }
DirectoryInfo WorkbenchDirectoryFromClient {
get; set; }
DirectoryInfo WorkbenchDirectoryOnServer { get; set; }
FileInfo FileToValidate { get; set; }
FileInfo FmeBatch { get; set; }
FileInfo FmeWorkbench { get; set; }
FileInfo PsExec { get; set; }
FileInfo FmeExec { get; set; }
string UserName { get; set; }
string Password { get; set; }
string ServerName { get; set; }
string StandardError { get; }
string StandardOutput { get; }
void Validate();
}
public class Validator : IValidator
{
public DirectoryInfo OutputDirectoryFromClient { get; set; }
public DirectoryInfo OutputDirectoryOnServer { get; set; }
public DirectoryInfo WorkbenchDirectoryFromClient { get; set; }
public DirectoryInfo WorkbenchDirectoryOnServer { get; set; }
public FileInfo FmeBatch { get; set; }
public FileInfo FileToValidate { get; set; }
public FileInfo FmeWorkbench { get; set; }
public FileInfo PsExec { get; set; }
public FileInfo FmeExec { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string ServerName { get; set; }
public string StandardError { get; private set; }
public string StandardOutput { get; private set; }
private void CreateSourceCopy()
{
File.Copy(FileToValidate.FullName,
WorkbenchDirectoryFromClient.FullName + "source.sdf");
}
private void RemoveSourceCopy()
{
if (File.Exists(WorkbenchDirectoryFromClient.FullName
+ "source.sdf"))
File.Delete(WorkbenchDirectoryFromClient.FullName
+ "source.sdf");
}
private void RemoveOutput()
{
if (File.Exists(OutputDirectoryFromClient.FullName
+ "validated.sdf"))
File.Delete(OutputDirectoryFromClient.FullName
+ "validated.sdf");
}
public void Validate()
{
RemoveSourceCopy();
RemoveOutput();
CreateSourceCopy();
var arg = ServerName + @" cmd.exe -u " + UserName + @" -p " + Password + @" /c " + FmeBatch.FullName;
var p = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
FileName =
PsExec.FullName,
Arguments = arg,
CreateNoWindow = true
}
};
using (p)
{
p.Start();
StandardOutput = p.StandardOutput.ReadToEnd();
StandardError = p.StandardError.ReadToEnd();
//One should
add an amount of time before we exit as this could lock up your app if the
process never exits.
p.WaitForExit();
}
RemoveSourceCopy();
}
}
public class ValidatorViewModel : ViewModelBase
{
public List<string> ValidationErrors { get; set; }
private string _standardOutput;
public string
StandardOutput { get { return _standardOutput; } set { _standardOutput = value; OnPropertyChanged("StandardOutput"); } }
private string _standardError;
public string StandardError { get { return _standardError; } set { _standardError = value; OnPropertyChanged("StandardError"); } }
#region
Constructor
public ValidatorViewModel(IValidator validator)
{
Validator = validator;
ValidationErrors = new List<string>();
}
#endregion
#region
Properties & Variables
public IValidator Validator { get; private set; }
private string _sourceFileName;
public string SourceFileName { get { return _sourceFileName; } set { _sourceFileName = value; OnPropertyChanged("SourceFileName"); } }
#endregion
#region
Commands
private RelayCommand _sourceUploadCommand;
public ICommand SourceUploadCommand
{
get
{
return _sourceUploadCommand ??
(_sourceUploadCommand = new RelayCommand(param => SourceUploadExecute(),
param => CanSourceUploadExecute()));
}
}
private void SourceUploadExecute()
{
try
{
var ofd = new OpenFileDialog
{
InitialDirectory = "c:\\",
Filter = "sdf
files (*.sdf)|*.sdf|All files (*.*)|*.*",
RestoreDirectory = true
};
if (ofd.ShowDialog() == DialogResult.OK)
{
Validator.FileToValidate = new FileInfo(ofd.FileName);
SourceFileName = Validator.FileToValidate.FullName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private bool CanSourceUploadExecute()
{
return true;
}
private RelayCommand _validateUploadCommand;
public ICommand ValidateUploadCommand
{
get
{
return _validateUploadCommand ??
(_validateUploadCommand = new RelayCommand(param => ValidateUploadExecute(),
param => CanValidateUploadExecute()));
}
}
private void ValidateUploadExecute()
{
try
{
try
{
Validator.Validate();
StandardError = Validator.StandardError;
StandardOutput = Validator.StandardOutput;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private bool CanValidateUploadExecute()
{
ValidationErrors.Clear();
if
(Validator.OutputDirectoryFromClient == null)
{
ValidationErrors.Add("OutputDirectoryFromClient is not set.");
return false;
}
if
(Validator.OutputDirectoryOnServer == null)
{
ValidationErrors.Add("OutputDirectoryOnServer is not set.");
return false;
}
if (Validator.WorkbenchDirectoryOnServer
== null)
{
ValidationErrors.Add("WorkbenchDirectoryOnServer is not set.");
return false;
}
if (Validator.FileToValidate == null)
{
ValidationErrors.Add("FileToValidate is not set.");
return false;
}
if (Validator.FmeWorkbench == null)
{
ValidationErrors.Add("FmeWorkbench is not set.");
return false;
}
if (Validator.PsExec == null)
{
ValidationErrors.Add("PsExec is not set.");
return false;
}
if (Validator.FmeExec == null)
{
ValidationErrors.Add("FmeExec is not set.");
return false;
}
if (Validator.FmeBatch == null)
{
ValidationErrors.Add("FmeBatch is not set.");
return false;
}
if (!Directory.Exists(Validator.OutputDirectoryFromClient.FullName))
{
ValidationErrors.Add("Invalid Output Directory From Client Path");
return false;
}
return true;
}
private RelayCommand _openDestinationCommand;
public ICommand OpenDestinationCommand
{
get
{
return _openDestinationCommand ??
(_openDestinationCommand = new RelayCommand(param =>
OpenDestinationExecute(),
param => CanOpenDestinationExecute()));
}
}
private void OpenDestinationExecute()
{
try
{
Process.Start("explorer.exe",
Validator.OutputDirectoryFromClient.FullName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private bool CanOpenDestinationExecute()
{
return true;
}
#endregion
}
public TrackValidatorViewModel() : base(new Validator())
{
Validator.OutputDirectoryFromClient = new DirectoryInfo(@"\\hictblockap022\FME\TrackValidator\");
Validator.OutputDirectoryOnServer = new DirectoryInfo(@"E:\FME\TrackValidator\");
Validator.WorkbenchDirectoryFromClient = new DirectoryInfo(@"\\hictblockap022\FME\Infranet\TrackValidator\");
Validator.WorkbenchDirectoryOnServer = new DirectoryInfo(@"E:\FME\Infranet\TrackValidator\");
Validator.FmeBatch = new FileInfo(Validator.WorkbenchDirectoryOnServer
+ "trackvalidator.bat");
Validator.FmeWorkbench = new FileInfo(Validator.WorkbenchDirectoryOnServer
+ "trackvalidator.fmw");
Validator.PsExec = new FileInfo(@"C:\Users\Public\Documents\Infranet\PSTools\PsExec.exe");
Validator.FmeExec = new FileInfo(@"C:\Program Files\FME\fme.exe");
Validator.UserName = "CXP8200";
Validator.Password = "password";
Validator.ServerName = @"\\hictblockap022";
}
public class SwitchValidatorViewModel : ValidatorViewModel
{
public SwitchValidatorViewModel() : base(new Validator())
{
Validator.OutputDirectoryFromClient = new DirectoryInfo(@"\\hictblockap022\FME\SwitchValidator\");
Validator.OutputDirectoryOnServer = new DirectoryInfo(@"E:\FME\SwitchValidator\");
Validator.WorkbenchDirectoryFromClient = new DirectoryInfo(@"\\hictblockap022\FME\Infranet\SwitchValidator\");
Validator.WorkbenchDirectoryOnServer = new DirectoryInfo(@"E:\FME\Infranet\SwitchValidator\");
Validator.FmeBatch = new FileInfo(Validator.WorkbenchDirectoryOnServer
+ "switchvalidator.bat");
Validator.FmeWorkbench = new FileInfo(Validator.WorkbenchDirectoryOnServer
+ "switchvalidator.fmw");
Validator.PsExec = new FileInfo(@"C:\Users\Public\Documents\Infranet\PSTools\PsExec.exe");
Validator.FmeExec = new FileInfo(@"C:\Program Files\FME\fme.exe");
Validator.UserName = "CXP8200";
Validator.Password = "password";
Validator.ServerName = @"\\hictblockap022";
}
}
<UserControl x:Class="BRail.InfraNet.TrackValidator.TrackValidatorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Grid.Row="0" Command="{Binding SourceUploadCommand}" Content="Open Source" Margin="0,5,5,5"/>
<Button Grid.Column="1" Grid.Row="0" Command="{Binding ValidateUploadCommand}" Content="Validate Source" Margin="5"/>
<Button Grid.Column="2" Grid.Row="0" Command="{Binding OpenDestinationCommand}" Content="Open Result" Margin="5,5,0,5"/>
<TextBox Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" Margin="0,5,0,0" IsReadOnly="True" Height="25" TextWrapping="Wrap" Text="{Binding SourceFileName, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top"/>
<TextBox Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="3" Margin="0,5,0,0" TextWrapping="Wrap" Text="{Binding StandardError}" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Height="Auto"/>
</Grid>
</UserControl>
<Window x:Class="BRail.InfraNet.TrackValidator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BRail.InfraNet.TrackValidator"
Title="Master Validator" Height="Auto" Width="Auto">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border BorderBrush="LightBlue" BorderThickness="5" Margin="5" Grid.Column="0">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Height="20" TextAlignment="Center" Text="TrackValidator" Background="LightBlue" FontStyle="Italic" FontWeight="Bold"/>
<local:TrackValidatorView Grid.Row="1" Margin="0,5,0,0" DataContext="{Binding Source={StaticResource TrackValidatorViewModel}}"/>
</Grid>
</Border>
<Border BorderBrush="LightBlue" BorderThickness="5" Margin="5" Grid.Column="1">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Height="20" TextAlignment="Center" Text="SwitchValidator" Background="LightBlue" FontStyle="Italic" FontWeight="Bold"/>
<local:TrackValidatorView Grid.Row="1" Margin="0,5,0,0" DataContext="{Binding Source={StaticResource SwitchValidatorViewModel}}"/>
</Grid>
</Border>
</Grid>
</Window>
No comments:
Post a Comment