using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsApplication
private string inputFileName;
private string logFileName;
BITCollege_OM.BITCollege_OMContext db = new BITCollege_OM.BITCollege_OMContext();
private void processErrors(IEnumerable<XElement> beforeQuery, IEnumerable<XElement> afterQuery, string message)
IEnumerable<XElement> differQuery = beforeQuery.Except(afterQuery);
foreach (XElement xele in differQuery)
srLog.WriteLine("\r\n"+"------------ERROR-----------");
srLog.WriteLine("File: " + this.inputFileName);
srLog.WriteLine("Program: " + xele.Element("program"));
srLog.WriteLine("Student Number: " + xele.Element("student_no"));
srLog.WriteLine("Course Number: " + xele.Element("course_no"));
srLog.WriteLine("Registration Number: " + xele.Element("registration_no"));
srLog.WriteLine("Type: " + xele.Element("type"));
srLog.WriteLine("Grade: " + xele.Element("grade"));
srLog.WriteLine("Notes: " + xele.Element("notes"));
srLog.WriteLine("Nodes: " + xele.Elements().Count());
srLog.WriteLine(message + "\r\n");
srLog.WriteLine("----------------------------" + "\r\n");
private bool processHeader()
bool isDateTested = false;
bool isProgramTested = false;
bool isCheckSumTested = false;
string[] message = new string[3];
XDocument xDocumentObject = XDocument.Load(inputFileName);
XElement xElement = xDocumentObject.Element("student_update");
XAttribute attributeDate = xElement.Attribute("date");
if (attributeDate.Value == System.DateTime.Today.ToString("yyyy-MM-dd"))
message[0] = attributeDate.Value + " attribute is not current date.";
srLog.WriteLine(message[0]);
XAttribute attributeProgram = xElement.Attribute("program");
var queryProgram = (from ResultsProg in db.Programs
where ResultsProg.ProgramAcronym == attributeProgram.Value
select ResultsProg).ToList();
message[1] = attributeProgram.Value + " attribute does not exist in the in the Program Entity class";
srLog.WriteLine(message[1]);
XAttribute attributeChecksum = xElement.Attribute("checksum");
IEnumerable<XElement> filteredElements = xDocumentObject.Descendants().Where(d => d.Name == "student_no");
foreach (XElement xele in filteredElements)
checkSum += Convert.ToInt32(xele.Value);
if (checkSum.ToString() == attributeChecksum.Value)
message[2] = attributeChecksum.Value + " attribute does not match the sum of all student_no elements in the file.";
isCheckSumTested = false;
srLog.WriteLine(message[2]);
if (!isDateTested) throw new Exception(message[0]);
if (!isProgramTested) throw new Exception(message[1]);
if (!isCheckSumTested) throw new Exception(message[2]);
if (isDateTested && isProgramTested && isCheckSumTested)
private void processDetails()
XDocument xDocumentObject = XDocument.Load(inputFileName);
IEnumerable<XElement> transactionElements = xDocumentObject.Descendants("transaction");
XElement xElement = xDocumentObject.Element("student_update");
XAttribute attributeProgram = xElement.Attribute("program");
IEnumerable<XElement> query1 = transactionElements.Where(obj => obj.Nodes().Count() == 7);
processErrors(transactionElements, query1, "There are no 7 child elements.");
IEnumerable<XElement> query2 = query1.Where(obj => obj.Element("program").Value == attributeProgram.Value);
processErrors(query1, query2, "Program node value and program attribute do not match.");
IEnumerable<XElement> query3 = query2.Where(obj => int.TryParse(obj.Element("type").Value, out tempInt));
processErrors(query2, query3, "Type node value is not numeric.");
IEnumerable<XElement> query4 = query3.Where(obj => double.TryParse(obj.Element("grade").Value, out tempDouble) || Equals(obj.Element("grade").Value, "*"));
processErrors(query3, query4, "Grade node value is not numeric or is not *");
IEnumerable<XElement> query5 = query4.Where(obj => int.Parse(obj.Element("type").Value) == 1 || int.Parse(obj.Element("type").Value) == 2);
processErrors(query4, query5, "Type node value is not 1 or 2.");
IEnumerable<XElement> query6 = query5.Where(obj => (int.Parse(obj.Element("type").Value) == 1 && obj.Element("grade").Value == "*") ||
((int.Parse(obj.Element("type").Value) == 2) && double.Parse(obj.Element("grade").Value) >= 0) && double.Parse(obj.Element("grade").Value) <= 100);
processErrors(query5, query6, "Type node should be 1 and grade should be *. Or Type node should be 2 and grade should be fro, 0 to 100.");
IEnumerable<long> queryVerifyStudentNumber = (from nodeElements in db.Students
select nodeElements.StudentNumber);
IEnumerable<XElement> query7 = query6.Where(obj => queryVerifyStudentNumber.Contains(int.Parse(obj.Element("student_no").Value)));
processErrors(query6, query7, "Student number does not exist in database.");
IEnumerable<string> queryVerifyCourseNumber = (from nodeElements in db.Courses
select nodeElements.CourseNumber);
IEnumerable<XElement> query8 = query7.Where(obj => queryVerifyCourseNumber.Contains(obj.Element("course_no").Value) ||
(obj.Element("course_no").Value == "*" && obj.Element("type").Value == "2"));
processErrors(query7, query8, "Course number does not exist in database or wrong combination of course_no and type.");
IEnumerable<long> queryVerifyRegistrationNumber = (from nodeElements in db.Registrations
select nodeElements.RegistrationNumber);
IEnumerable<XElement> query9 = query8.Where(obj => (obj.Element("registration_no").Value == "*" && obj.Element("type").Value == "1") ||
queryVerifyRegistrationNumber.Contains(int.Parse(obj.Element("registration_no").Value)));
processErrors(query8, query9, "Student number does not exist in database.");
processTransactions(query9);
private void processTransactions(IEnumerable<XElement> transactionRecords)
ServiceReference.CollegeRegistrationClient registration = new ServiceReference.CollegeRegistrationClient();
foreach (XElement xele in transactionRecords)
if (xele.Element("type").Value == "1")
int studentNo = int.Parse(xele.Element("student_no").Value);
string courseNo = xele.Element("course_no").Value;
var queryStudent = from student in db.Students
where student.StudentNumber == studentNo
var queryCourse = from course in db.Courses
where course.CourseNumber == courseNo
int returnErr = registration.registerCourse(queryStudent.SingleOrDefault().StudentId, queryCourse.SingleOrDefault().CourseId, xele.Element("notes").Value);
srLog.WriteLine("Successful Registration student " + xele.Element("student_no").Value + " course " + xele.Element("course_no").Value + "\r\n");
srLog.WriteLine("ERROR: " + Utility.BusinessRules.registerError(returnErr) + "\r\n");
else if (xele.Element("type").Value == "2")
int regNo = int.Parse(xele.Element("registration_no").Value);
var queryRegistration = from reg in db.Registrations
where reg.RegistrationNumber == regNo
registration.updateGrade(double.Parse(xele.Element("grade").Value), queryRegistration.SingleOrDefault().RegistrationId, xele.Element("notes").Value);
srLog.WriteLine("grade " + xele.Element("grade").Value + " applied to student " + xele.Element("student_no").Value + " for registration " + xele.Element("registration_no").Value + "\r\n");
public string writeLogData()
if (File.Exists(this.inputFileName))
File.Move(this.inputFileName, "COMPLETE-" + this.inputFileName);
this.logData = File.ReadAllText(this.logFileName);
public void processTransmission(string institution, string key)
string FYear = DateTime.Today.Year.ToString();
string FDay = DateTime.Now.DayOfYear.ToString();
string FInstitution = institution;
this.inputFileName = FYear + "-" + FDay + "-" + FInstitution + ".xml";
string extension = this.inputFileName.Substring(inputFileName.LastIndexOf('.') + 1);
if (extension.ToLower() == "xml")
string[] numbers = inputFileName.Split('-');
FInstitution = numbers[2].Substring(0, numbers[2].LastIndexOf('.'));
logFileName = "LOG " + FYear + "-" + FDay + "-" + FInstitution + ".txt";
srLog = new StreamWriter(logFileName);
if (File.Exists(this.inputFileName))
srLog.WriteLine(e.Message);
else srLog.WriteLine("\u2639" + "File " + "\"" + this.inputFileName + "\"" + " does not exist." + "\r\n");