Más contenido relacionado
La actualidad más candente (20)
Similar a WOTC_Import (20)
WOTC_Import
- 1. using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace WOTC_Import
{
class Program
{
XmlDocument xDoc;
ArrayList logList = new ArrayList();
static int Main(string[] args)
{
Program prog = new Program();
args = new String[] { "./" };
String[] pathArgs = prog.GetConfig(args[0]);
String inPath = (string)pathArgs[0];
string outPath = (string)pathArgs[1];
string inSsnData = (string)pathArgs[2];
string inIdsData = (string)pathArgs[3];
bool IsSuccess = false;
try
{
IsSuccess = prog.MergeDataFiles(inPath, outPath, inSsnData, inIdsData);
}
catch (Exception ex)
{
Console.WriteLine("WOTC Failure: " + ex.Message);
}
if (IsSuccess == true)
{
Console.WriteLine("Merge success.");
return 0;
}
else
{
Console.WriteLine("Merge failure.");
return 1;
- 2. }
}
private DataTable CreateDataTable(int Columns)
{
DataTable tabled = new DataTable();
DataColumn idColumn = new DataColumn();
idColumn.DataType = System.Type.GetType("System.Int32");
idColumn.ColumnName = "cid";
idColumn.AutoIncrement = true;
tabled.Columns.Add(idColumn);
for (int i = 0; i < Columns; i++)
{
DataColumn dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
tabled.Columns.Add(dc);
}
DataColumn[] keys = new DataColumn[2];
keys[0] = tabled.Columns[0];
keys[1] = tabled.Columns[1];
tabled.PrimaryKey = keys;
return tabled;
}
private String[] GetConfig(String configPath)
{
String[] filepathString = new String[] { };
xDoc = new XmlDocument();
xDoc.Load(configPath + "/WOTC_Import_settings.xml");
if (!xDoc.DocumentElement.IsEmpty)
{
XmlNode rootSegments = xDoc.DocumentElement.SelectSingleNode("DataFiles");
XmlNode inNode = rootSegments.ChildNodes[0];
XmlNode outNode = rootSegments.ChildNodes[1];
XmlNode ssnNode = rootSegments.ChildNodes[2];
XmlNode idsNode = rootSegments.ChildNodes[3];
if (!String.IsNullOrWhiteSpace(ssnNode.InnerText) & !String.IsNullOrWhiteSpace(idsNode.InnerText) & !String.IsNullOrWhiteSpace(outNode.InnerText))
{
filepathString = new String[] { inNode.InnerText, outNode.InnerText, ssnNode.InnerText, idsNode.InnerText };
logList.Add("XML Settings document loaded in GetConfig ( ): ");
logList.Add(" Qualified name of XmlDocument node: " + xDoc.Name);
logList.Add(" SSS File : " + ssnNode.InnerText);
- 3. logList.Add(" Ids File : " + idsNode.InnerText);
logList.Add(" Output Path: " + outNode.InnerText);
logList.Add("");
return filepathString;
}
else
{
logList.Add("XML Settings document NOT loaded in GetConfig ( ).");
logList.Add("");
return new String[] { };
}
}
else
{
logList.Add("XML Settings document in GetConfig ( ) failed to load.");
logList.Add("");
}
return filepathString;
}
private bool MergeDataFiles(string inPathWOTC, String outPathMerge, string inXSSNXT, string inIds)
{
bool IsSuccessful = false;
logList.Add("Current Date: " + DateTime.Now.ToString());
logList.Add("");
logList.Add("");
if (!string.IsNullOrWhiteSpace(inPathWOTC) & !String.IsNullOrWhiteSpace(outPathMerge) & !string.IsNullOrWhiteSpace(inXSSNXT) & !String.IsNullOrWhiteSpace(inIds))
{
DirectoryInfo diPathWOTC = new DirectoryInfo(inPathWOTC);
DirectoryInfo diPathOut = new DirectoryInfo(outPathMerge);
if (diPathWOTC.Exists & diPathOut.Exists)
{
FileInfo[] fiSSN = diPathWOTC.GetFiles(inXSSNXT);
FileInfo[] fiIds = diPathWOTC.GetFiles(inIds);
if (fiSSN.Length < 1)
{
if (fiIds.Length < 1)
{
logList.Add("SSM and EmployeeId files (" + inXSSNXT + " and " + inIds + ") are missing.");
logList.Add("");
}
else
- 4. {
logList.Add("SSN file (" + inXSSNXT + ") is missing.");
logList.Add("");
}
}
else if (fiIds.Length < 1)
{
logList.Add("Ids file (" + fiIds + ") is missing.");
logList.Add("");
}
else if (fiSSN.Length > 1 | fiIds.Length > 1)
{
logList.Add("Multiple data files exist at each input location:");
logList.Add(inPathWOTC);
logList.Add("");
}
else
{
logList.Add("Paths/Files exist: ");
logList.Add(" Employee (SSN) Data: " + inPathWOTC + @"/" + inXSSNXT);
logList.Add(" Organization Data : " + inPathWOTC + @"/" + inIds);
logList.Add(" Output Path : " + outPathMerge + @"/");
logList.Add("");
DataTable matchTable = CreateDataTable(11);
DataTable nomatchTable = CreateDataTable(8);
DataTable tableWOTC = ReadCSVData(fiSSN[0]);
logList.Add("Employee (SSN) Data includes " + tableWOTC.Rows.Count + " records.");
logList.Add("");
DataTable tableXSSNXT = ReadFixedWidthData(fiIds[0]);
logList.Add("Organization Data includes " + tableXSSNXT.Rows.Count + " records.");
logList.Add("");
if (tableWOTC.Rows.Count > 0 & tableXSSNXT.Rows.Count > 0)
{
int matchHeadCount = 0;
int nomatchHeadCount = 0;
for (int i = 0; i < tableWOTC.Rows.Count; i++)
{
DataRow drWOTC = tableWOTC.Rows[i];
string keystringWOTC = (string)drWOTC[1];
string ssnfieldname = tableXSSNXT.Columns[1].ColumnName;
string ssnfieldvalueWOTC = (string)drWOTC[1];
string lnmfieldname = tableXSSNXT.Columns[4].ColumnName;
- 5. string lnmfieldvalueWOTC = (string)drWOTC[2];
string sortorder = ssnfieldname + " ASC";
DataRow[] drXSSNXT;
drXSSNXT = null;
String ssnQuery = ssnfieldname + "=" + "'" + ssnfieldvalueWOTC + "'";
drXSSNXT = tableXSSNXT.Select(ssnQuery, sortorder);
if (drXSSNXT.Length == 1)
{
String xssnxtlnmString = (String)drXSSNXT[0][4];
String xssnxtlnmLower = xssnxtlnmString.ToLower();
String xssnxtlnmTrim = xssnxtlnmLower.TrimEnd(new Char[] { ' ' });
if (xssnxtlnmTrim != lnmfieldvalueWOTC)
{
String wotclnmLower = lnmfieldvalueWOTC.ToLower();
String wotclnmTrim = wotclnmLower.TrimEnd(new Char[] { ' ' });
String wotclnmReplaceSQ = wotclnmTrim.Replace(''', ' ');
String wotclnmReplaceH = wotclnmReplaceSQ.Replace('-', ' ');
String wotclnmReplace1 = wotclnmReplaceH.Replace(" jr.", "");
String wotclnmReplace2 = wotclnmReplace1.Replace(" ii", "");
String wotclnmReplace3 = wotclnmReplace2.Replace(" iii", "");
String wotclnmReplace4 = wotclnmReplace3.Replace(" iv", "");
String wotclnmReplace5 = wotclnmReplace4.Replace(". ", "");
if (xssnxtlnmTrim != wotclnmReplace5)
{
String wotclnmSub = "";
String xssnxtlnmSub = "";
if (wotclnmReplace5.Length < xssnxtlnmTrim.Length)
{
xssnxtlnmSub = xssnxtlnmTrim.Substring(0, wotclnmReplace5.Length);
wotclnmSub = wotclnmReplace5;
}
else if (xssnxtlnmTrim.Length < wotclnmReplace4.Length)
{
wotclnmSub = wotclnmReplace5.Substring(0, xssnxtlnmTrim.Length);
xssnxtlnmSub = xssnxtlnmTrim;
}
if (xssnxtlnmSub != wotclnmSub)
{
logList.Add("Matching SSN with different last names: ");
logList.Add(" SSN : " + ssnfieldvalueWOTC);
logList.Add(" Last Name (WOTC) : " + wotclnmReplace4);
- 6. logList.Add(" Last Name (XSSNXT: " + (String)drXSSNXT[0][4]);
logList.Add("");
tableXSSNXT.Rows.Remove(drXSSNXT[0]);
drXSSNXT = tableXSSNXT.Select(ssnQuery, sortorder);
}
}
}
}
else if (drXSSNXT.Length > 1)
{
for (int drInt = 0; drInt < drXSSNXT.Length; drInt++)
{
tableXSSNXT.Rows.Remove(drXSSNXT[drInt]);
drXSSNXT = tableXSSNXT.Select(ssnQuery, sortorder);
logList.Add("Matching SSN ( * MULTIPLE * ) with different last names: ");
logList.Add(" SSN : " + ssnfieldvalueWOTC);
logList.Add(" Last Name (WOTC) : " + lnmfieldvalueWOTC);
logList.Add(" Last Name (XSSNXT: " + (String)drXSSNXT[0][4]);
logList.Add("");
//String idslnmString = ( String ) drXSSNXT[drInt][4];
//String idslnmStringLower = idslnmString.ToLower ( );
//String idslnmStringTrim = idslnmStringLower.TrimEnd ( new Char[] { ' ' } );
//if ( idslnmStringTrim != wotclnmTrim )
//{
//}
}
}
if (drXSSNXT.Length > 0)
{
for (int n = 0; n < drXSSNXT.Length; n++)
{
if (matchHeadCount == 0)
{
DataRow headDR = matchTable.NewRow();
//headDR[0] = "ID";
headDR[1] = "SSN";
headDR[2] = "ORGANIZATIONID";
headDR[3] = "EMPLOYEEID";
headDR[4] = "LASTNAME";
headDR[5] = "FIRSTNAME";
headDR[6] = "MIDDLENAME";
- 7. headDR[7] = "DATASCREENED";
headDR[8] = "ELIGIBLE";
headDR[9] = "TARGETGROUP";
headDR[10] = "JOBSTARTDATE";
headDR[11] = "MISSING8850";
matchTable.Rows.Add(headDR);
matchHeadCount++;
}
DataRow nowDR = drXSSNXT[n];
DataRow matchDR = matchTable.NewRow();
matchDR[1] = drWOTC[1];
matchDR[2] = nowDR[2];
matchDR[3] = nowDR[3];
matchDR[4] = drWOTC[2];
matchDR[5] = drWOTC[3];
matchDR[6] = nowDR[6];
matchDR[7] = drWOTC[4];
matchDR[8] = drWOTC[5];
matchDR[9] = drWOTC[6];
matchDR[10] = drWOTC[7];
matchDR[11] = drWOTC[8];
matchTable.Rows.Add(matchDR);
}
}
else
{
if (nomatchHeadCount == 0)
{
DataRow headDR = nomatchTable.NewRow();
//headDR[0] = "ID";
headDR[1] = "SSN";
headDR[2] = "LASTNAME";
headDR[3] = "FIRSTNAME";
headDR[4] = "DATASCREENED";
headDR[5] = "ELIGIBLE";
headDR[6] = "TARGETGROUP";
headDR[7] = "JOBSTARTDATE";
headDR[8] = "MISSING8850";
nomatchTable.Rows.Add(headDR);
nomatchHeadCount++;
}
- 8. DataRow nomatchDR = nomatchTable.NewRow();
nomatchDR[1] = drWOTC[1];
nomatchDR[2] = drWOTC[2];
nomatchDR[3] = drWOTC[3];
nomatchDR[4] = drWOTC[4];
nomatchDR[5] = drWOTC[5];
nomatchDR[6] = drWOTC[6];
nomatchDR[7] = drWOTC[7];
nomatchDR[8] = drWOTC[8];
nomatchTable.Rows.Add(nomatchDR);
}
}
logList.Add("Data includes " + (matchTable.Rows.Count - 1) + " matching records.");
logList.Add("Data includes " + (nomatchTable.Rows.Count - 1) + " non-matching records.");
logList.Add("");
try
{
string filename = outPathMerge + @"/WOTC_Import_matching.txt";
using (StreamWriter sw = File.CreateText(filename))
{
foreach (DataRow row in matchTable.Rows)
{
sw.WriteLine(row[0] + "," + row[1] + "," + row[2] + "," + row[3] + "," + row[4] + "," + row[5] + "," + row[6] + "," + row[7] + "," + row[8] +
"," + row[9] + "," + row[10] + "," + row[11]);
}
}
logList.Add("Output data file for matching records: " + filename + ": ");
logList.Add("");
filename = outPathMerge + @"/WOTC_Import_nonmatching.txt";
using (StreamWriter sw = File.CreateText(filename))
{
foreach (DataRow row in nomatchTable.Rows)
{
sw.WriteLine(row[0] + "," + row[1] + "," + row[2] + "," + row[3] + "," + row[4] + "," + row[5] + "," + row[6] + "," + row[7] + "," + row[8]);
}
}
logList.Add("Output data file for non-matching records: " + filename + ": ");
logList.Add("");
IsSuccessful = true;
}
catch (Exception ex)
{
- 9. logList.Add("");
logList.Add("--------------------------------------------------");
logList.Add("");
logList.Add("Exception from Input Data StreamWriter Try / Catch: ");
logList.Add(" Exception Message: " + ex.Message);
logList.Add(" Exception Data: " + ex.Data);
logList.Add(" Exception HelpLink: " + ex.HelpLink);
logList.Add(" Exception HResult: " + ex.HResult);
logList.Add(" Exception Inner Exception: " + ex.InnerException);
logList.Add(" Exception Source: " + ex.Source);
logList.Add(" Exception StackTrace: " + ex.StackTrace);
logList.Add(" Exception TargetSite: " + ex.TargetSite);
logList.Add("");
}
}
else
{
logList.Add("Input data tables contain no matching records;");
logList.Add("");
}
}
}
else
{
if (!diPathWOTC.Exists)
{
logList.Add("Input paths were not found: " + inPathWOTC);
logList.Add("");
}
else if (!diPathOut.Exists)
{
logList.Add(" Data output path not found: " + outPathMerge);
logList.Add("");
}
else
{
logList.Add(" Un-specified data path error.");
logList.Add("");
}
}
- 10. }
else
{
if (string.IsNullOrWhiteSpace(inPathWOTC))
{
logList.Add("Input path is empty: + inPathWOTC ");
logList.Add("");
}
else
{
logList.Add(" Un-specified data input path error.");
logList.Add("");
}
}
try
{
string logname = outPathMerge + @"/LogFile_WOTC_Import_" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ".txt";
logList.Add("Output Log File : " + logname);
logList.Add("");
using (StreamWriter sw = File.CreateText(logname))
{
foreach (string entryString in logList)
{
sw.WriteLine(entryString);
}
}
}
catch (Exception ex)
{
logList.Add("");
logList.Add("--------------------------------------------------");
logList.Add("");
logList.Add("Exception from LogFIle StreamWriter Try / Catch: ");
logList.Add(" Exception Message: " + ex.Message);
logList.Add(" Exception Data: " + ex.Data);
logList.Add(" Exception HelpLink: " + ex.HelpLink);
logList.Add(" Exception HResult: " + ex.HResult);
logList.Add(" Exception Inner Exception: " + ex.InnerException);
logList.Add(" Exception Source: " + ex.Source);
logList.Add(" Exception StackTrace: " + ex.StackTrace);
logList.Add(" Exception TargetSite: " + ex.TargetSite);
logList.Add("");
- 11. }
return IsSuccessful;
}
private DataTable ReadCSVData(FileInfo infoFile)
{
DataTable ssnTable = CreateDataTable(8);
int skipRecord = 0;
StreamReader reads = new StreamReader(infoFile.FullName);
while (reads.Peek() >= 0)
{
string lineString = reads.ReadLine();
if (skipRecord > 0) //data includes a header row
{
string[] csvSplit = new string[] { "," };
string[] splitLine = lineString.Split(csvSplit, StringSplitOptions.None);
string[] dsvSplit = new string[] { "-" };
string[] splitSSN = splitLine[0].Split(dsvSplit, StringSplitOptions.None);
DataRow dr = ssnTable.NewRow();
dr[1] = splitSSN[0] + splitSSN[1] + splitSSN[2];
dr[2] = splitLine[1];
dr[3] = splitLine[2];
dr[4] = splitLine[3];
dr[5] = splitLine[4];
dr[6] = splitLine[5];
dr[7] = splitLine[6];
dr[8] = splitLine[7];
ssnTable.Rows.Add(dr);
}
else
{
logList.Add("Header row of 'WOTC_File.csv' file has been skipped (this line should only appear once in log file).");
logList.Add("");
skipRecord++;
}
}
return ssnTable;
}
private DataTable ReadFixedWidthData(FileInfo infoFile)
{
- 12. DataTable employeeData = CreateDataTable(6);
StreamReader reads = new StreamReader(infoFile.FullName);
while (reads.Peek() >= 0)
{
string lineString = reads.ReadLine();
string ssnString = ReadLine(lineString, 0, 9); //lineString.Substring ( 0, 9 );
string orgString = ReadLine(lineString, 9, 6); //lineString.Substring ( 9, 6 );
string empString = ReadLine(lineString, 15, 10); //lineString.Substring ( 15, 10 );
string lnmString = ReadLine(lineString, 25, 20); //lineString.Substring ( 25, 19 );
string fnmString = ReadLine(lineString, 45, 15); //lineString.Substring ( 45, 14 );
string mnmString = ReadLine(lineString, 60, 15); //lineString.Substring ( 60, (lineString.Length -60) );
DataRow dr = employeeData.NewRow();
dr[1] = ssnString;
dr[2] = orgString;
dr[3] = empString;
dr[4] = lnmString;
dr[5] = fnmString;
dr[6] = mnmString;
employeeData.Rows.Add(dr);
}
return employeeData;
}
private string ReadLine(string stringLine, int iStart, int iLength)
{
string outString = "";
if (stringLine.Length >= iStart)
{
if (stringLine.Length >= iStart + iLength)
{
outString = stringLine.Substring(iStart, iLength);
}
else
{
outString = stringLine.Substring(iStart, (stringLine.Length - iStart));
}
}
else
{
outString = "";
}
return outString;