More Related Content
Similar to XTW_Import (20)
XTW_Import
- 1. using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading; //.Tasks;
using System.Xml;
namespace FileInFileOut3
{
class Program
{
private ArrayList logList = new ArrayList();
static int Main(string[] args)
{
Program prog = new Program();
String[] pathArgs = prog.GetConfig(args[0]);
string inputPath = (string)pathArgs[0]; //InputPath
string outputPath = (string)pathArgs[1]; //OutputPath
String week1File = (String)pathArgs[2];
String week2File = (String)pathArgs[3];
bool success = prog.PrepareWageFiles(inputPath, outputPath, week1File, week2File);
if (success == true)
{
Console.WriteLine("Merge success.");
return 0;
}
else
{
Console.WriteLine("Merge failure.");
return 1;
}
}
private string CheckTaxWageValueFormat(string valueString, int numchar)
{
string outstring = "";
int valnum = valueString.Length;
if (numchar - valnum == 5)
{
valueString = valueString.Insert(0, "00.00");
- 2. }
if (numchar - valnum == 4)
{
valueString = valueString.Insert(0, "00.0");
}
if (numchar - valnum == 3)
{
valueString = valueString.Insert(0, "00.");
}
if (numchar - valnum == 2)
{
valueString = valueString.Insert(0, "00");
}
if (numchar - valnum == 1)
{
valueString = valueString.Insert(0, "0");
}
outstring = valueString;
valueString = "";
numchar = 0;
valnum = 0;
return outstring;
}
private bool Error()
{
return false;
}
private String[] GetConfig(String configPath)
{
XmlDocument xDoc;
String[] filepathString;
xDoc = new XmlDocument();
xDoc.Load(configPath + "/XTW_Import_settings.xml");
if (!xDoc.DocumentElement.IsEmpty)
{
XmlNode rootSegments = xDoc.DocumentElement.SelectSingleNode("DataFiles");
XmlNode inNode = rootSegments.ChildNodes[0];
XmlNode outNode = rootSegments.ChildNodes[1];
XmlNode Week1Node = rootSegments.ChildNodes[2];
XmlNode Week2Node = rootSegments.ChildNodes[3];
- 3. if (!String.IsNullOrEmpty(inNode.InnerText) & !String.IsNullOrEmpty(outNode.InnerText) & !String.IsNullOrEmpty(Week1Node.InnerText) &
!String.IsNullOrEmpty(Week2Node.InnerText))
{
filepathString = new String[] { inNode.InnerText, outNode.InnerText, Week1Node.InnerText, Week2Node.InnerText };
logList.Add("XML Settings document loaded in GetConfig ( ): ");
logList.Add(" Qualified name of XmlDocument node: " + xDoc.Name);
logList.Add(" Input Path : " + inNode.InnerText);
logList.Add(" Output Path: " + outNode.InnerText);
logList.Add("");
return filepathString;
}
else
{
return new String[] { };
}
}
else
{
logList.Add("XML Settings document in GetConfig ( ) failed to load.");
logList.Add("");
return new String[] { };
}
}
private DateTime GetPreviousSunday(DateTime inDate)
{
if (inDate.DayOfWeek == DayOfWeek.Sunday)
{
return inDate.Date;
}
else
{
DateTime newDate = inDate.AddDays(-1);
DateTime sundayDate = GetPreviousSunday(newDate);
return sundayDate;
}
}
private ArrayList MergeWageFiles(SortedDictionary<string, string> week1Dict, SortedDictionary<string, string> week2Dict, string endDate)
{
ArrayList outList = new ArrayList();
- 4. outList.Clear();
string basestring = " {0}XTWSCR {1} {2}{3}{4}{5}{6} ";
SortedDictionary<string, string> enumDict;
if (week1Dict == null)
{
enumDict = week2Dict;
}
else
{
enumDict = week1Dict;
}
foreach (KeyValuePair<string, string> kvp in enumDict)
{
List<string> lx = new List<string>(7);
string keyHash = kvp.Key;
string week1Hash;
string week2Hash;
if (week1Dict != null)
{
week1Hash = kvp.Value;
if (week2Dict.ContainsKey(keyHash))
{
week2Hash = week2Dict[keyHash];
}
else
{
week2Hash = ",,";
}
}
else
{
week2Hash = kvp.Value;
week1Hash = ",,";
}
ArrayList keyList = SplitHashWeekKey(keyHash);
ArrayList val2List = SplitHashWeekValue(week2Hash);
ArrayList val1List = SplitHashWeekValue(week1Hash);
lx.Add((string)keyList[0]);
lx.Add((string)keyList[1]);
bool isEndDateNull = true;
bool isTip1Null = true;
bool isTip2Null = true;
- 5. bool isWage1Null = true;
bool isWage2Null = true;
if (!string.IsNullOrEmpty(endDate))
{
lx.Add(endDate);
isEndDateNull = false;
}
else
{
lx.Add("Date Error");
}
if (val1List[1] != null & !string.IsNullOrEmpty((string)val1List[1])) //Tip 1
{
lx.Add(CheckTaxWageValueFormat((string)val1List[1], 5));
isTip1Null = false;
}
else
{
lx.Add("00.00");
}
if (val1List[2] != null & !string.IsNullOrEmpty((string)val1List[2])) //Wage 1
{
lx.Add(CheckTaxWageValueFormat((string)val1List[2], 5));
isWage1Null = false;
}
else
{
lx.Add("00.00");
}
if (val2List[1] != null & !string.IsNullOrEmpty((string)val2List[1])) //Tip 2
{
lx.Add(CheckTaxWageValueFormat((string)val2List[1], 5));
isTip2Null = false;
}
else
{
lx.Add("00.00");
}
if (val2List[2] != null & !string.IsNullOrEmpty((string)val2List[2])) //Wage 2
{
lx.Add(CheckTaxWageValueFormat((string)val2List[2], 5));
isWage2Null = false;
- 6. }
else
{
lx.Add("00.00");
}
string concat = "";
concat = string.Format(basestring, lx[0], lx[1], lx[2], lx[3], lx[5], lx[4], lx[6]);
outList.Add(concat);
if (isEndDateNull == true | isTip1Null == true | isTip2Null == true | isWage1Null == true | isWage2Null == true)
{
logList.Add("");
logList.Add("Null Data: ");
logList.Add(" Organization: " + (string)keyList[0]);
logList.Add(" EmployeeId : " + (string)keyList[1]);
if (isTip1Null == true)
{
logList.Add(" Week 1 Tip : NULL");
}
if (isTip2Null == true)
{
logList.Add(" Week 2 Tip : NULL");
}
if (isWage1Null == true)
{
logList.Add(" Week 1 Wage : NULL");
}
if (isWage2Null == true)
{
logList.Add(" Week 2 Wage : NULL");
}
logList.Add("");
}
lx.Clear();
}
return outList;
}
private bool PrepareWageFiles(String inPath, String outPath, String fileWeek1, String fileWeek2)
{
bool isSuccess = false;
DateTime week2Date = GetPreviousSunday(DateTime.Now);
- 7. DateTime week1Date = week2Date.AddDays(-7);
logList.Add("Current Date: " + DateTime.Now.ToString());
logList.Add("");
logList.Add("");
string dateFormatString = "{0:MM-dd-yyyy}";
string currentDate = string.Format(dateFormatString, DateTime.Now);
DirectoryInfo diSource = new DirectoryInfo(inPath);
inPath = diSource.FullName;
try
{
ArrayList concatList1;
ArrayList concatList2;
if (diSource.Exists)
{
logList.Add("Input / Output Path : " + diSource.FullName);
logList.Add("Input / Output Path Exists: TRUE");
logList.Add("");
logList.Add("");
logList.Add("Week 1 Input File Name: " + fileWeek1);
logList.Add("Week 2 Input File Name: " + fileWeek2);
logList.Add("");
logList.Add("");
FileInfo[] fis1 = diSource.GetFiles(fileWeek1);
FileInfo[] fis2 = diSource.GetFiles(fileWeek2);
if (fis1.Length < 1)
{
if (fis2.Length < 1)
{
logList.Add("Week 1 and Week 2 wage files (" + fileWeek1 + " and " + fileWeek2 + ") are missing.");
logList.Add("");
}
else
{
logList.Add("Week 1 wage file (" + fileWeek1 + ") is missing.");
logList.Add("");
}
}
else if (fis2.Length < 1)
{
logList.Add("Week 2 wage file (" + fileWeek2 + ") is missing.");
logList.Add("");
}
- 8. else if (fis1.Length > 1 | fis2.Length > 1)
{
logList.Add("Multiple wage files present in folder: " + inPath);
logList.Add("");
}
else
{
string week1EndDate = string.Format(dateFormatString, week1Date);
string week2EndDate = string.Format(dateFormatString, week2Date);
logList.Add("End Week 1 Date: " + week1EndDate);
logList.Add("End Week 2 Date: " + week2EndDate);
logList.Add("");
SortedDictionary<string, string> week1SD = ReadWeek(fis1[0], week1Date);
SortedDictionary<string, string> week2SD = ReadWeek(fis2[0], week2Date);
concatList1 = MergeWageFiles(week1SD, week2SD, week2EndDate);
foreach (string keyString in week1SD.Keys)
{
if (week2SD.ContainsKey(keyString))
{
week2SD.Remove(keyString);
}
}
concatList2 = MergeWageFiles(null, week2SD, week2EndDate);
string filename = outPath + "tip_wage_summary_" + currentDate + ".txt";
using (StreamWriter sw = File.CreateText(filename))
{
foreach (string entryString in concatList1)
{
sw.WriteLine(entryString);
}
foreach (string entryString in concatList2)
{
sw.WriteLine(entryString);
}
}
logList.Add("");
logList.Add("Output Summary File: " + filename);
}
isSuccess = true;
}
else
{
- 9. isSuccess = false;
}
}
catch (Exception ex)
{
logList.Add("");
logList.Add("--------------------------------------------------");
logList.Add("");
logList.Add("Exception from outer Try / Catch: ");
logList.Add(" Exception Message: " + ex.Message);
logList.Add(" Exception Data: " + ex.Data);
logList.Add(" Exception HelpLink: " + ex.HelpLink);
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("");
isSuccess = false;
Console.WriteLine("Error: " + ex.Message);
Console.Read();
}
try
{
string logname = outPath + "tip_wage_LogFile_" + currentDate + ".txt";
logList.Add("Output Log File : " + logname);
logList.Add("");
using (StreamWriter sw = File.CreateText(logname))
{
foreach (string entryString in logList)
{
sw.WriteLine(entryString);
}
}
isSuccess = true;
}
catch (Exception ex)
{
logList.Add("");
logList.Add("--------------------------------------------------");
logList.Add("");
- 10. 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 Inner Exception: " + ex.InnerException);
logList.Add(" Exception Source: " + ex.Source);
logList.Add(" Exception StackTrace: " + ex.StackTrace);
logList.Add(" Exception TargetSite: " + ex.TargetSite);
logList.Add("");
isSuccess = false;
Console.WriteLine("Error: " + ex.Message);
Console.Read();
}
return isSuccess;
}
private DateTime ReadDateDSV(string hashValue)
{
string[] dashString = new string[] { "-" };
string[] dashSplit = hashValue.Split(dashString, StringSplitOptions.None);
DateTime currentDate;
try
{
currentDate = Convert.ToDateTime(dashSplit[1] + "/" + dashSplit[2] + "/" + dashSplit[0]);
}
catch (Exception)
{
currentDate = new DateTime(1900, 01, 01);
logList.Add("Null date found in record. Check data!");
logList.Add(" : ");
}
return currentDate.Date;
}
private SortedDictionary<string, string> ReadWeek(FileInfo files, DateTime weekDate)
{
SortedDictionary<string, string> employeeRecord = new SortedDictionary<string, string>();
employeeRecord.Clear();
int skipRecord = 0;
StreamReader reads = new StreamReader(files.FullName);
- 11. while (reads.Peek() >= 0)
{
string lineString = reads.ReadLine();
if (skipRecord > 0)
{
string[] splitter = new string[] { "," };
string[] splitLine = lineString.Split(splitter, StringSplitOptions.None);
DateTime inDate = ReadDateDSV(splitLine[3]);
string dateFormatString = "{0:MM-dd-yyyy}";
string dateString = string.Format(dateFormatString, weekDate);
string inKey = (string)splitLine[0] + "," + (string)splitLine[2];
string tipsString = CheckTaxWageValueFormat((string)splitLine[4], 5);
string wageString = CheckTaxWageValueFormat((string)splitLine[5], 5);
SortedDictionary<string, string>.KeyCollection keyCollx = employeeRecord.Keys;
bool IsKeyInList = false;
foreach (string keyString in keyCollx)
{
if (keyString == inKey)
{
IsKeyInList = true;
}
}
if (IsKeyInList)
{
employeeRecord[inKey] = splitLine[3] + ",ERROR,ERROR";
logList.Add("");
logList.Add("Error: File contains more than one record for the Key for the week date.");
logList.Add(" Week Date : " + dateString);
logList.Add(" Key : " + inKey);
logList.Add(" Record 1 Data: " + (string)employeeRecord[inKey]);
logList.Add(" Record 2 Data: " + splitLine[3] + "," + tipsString + "," + wageString);
logList.Add("");
}
else
{
employeeRecord.Add(inKey, (string)splitLine[3] + "," + tipsString + "," + wageString);
}
Array.Clear(splitter, 0, splitter.Length);
Array.Clear(splitLine, 0, splitLine.Length);
}
lineString = "";
skipRecord++;
- 12. }
reads.Close();
return employeeRecord;
}
private ArrayList SplitHashWeekKey(string keyHash)
{
string[] splitter = new string[] { "," };
string[] splitLine = keyHash.Split(splitter, StringSplitOptions.None);
ArrayList fieldList = new ArrayList();
fieldList.Clear();
fieldList.Add(splitLine[0]);
fieldList.Add(splitLine[1]);
Array.Clear(splitter, 0, splitter.Length);
Array.Clear(splitLine, 0, splitLine.Length);
return fieldList;
}
private ArrayList SplitHashWeekValue(string valueHash)
{
string[] splitter = new string[] { "," };
string[] splitLine = valueHash.Split(splitter, StringSplitOptions.None);
ArrayList fieldList = new ArrayList();
fieldList.Clear();
for (int i = 0; i < splitLine.Length; i++)
{
fieldList.Add(splitLine[i]);
}
Array.Clear(splitter, 0, splitter.Length);
Array.Clear(splitLine, 0, splitLine.Length);
return fieldList;
}
}
}