forked from ZochNet/QBMigrationTool
/
VehicleDAL.cs
108 lines (93 loc) · 3.9 KB
/
VehicleDAL.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using rototrack_data_access;
using rototrack_model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace QBMigrationTool
{
class VehicleDAL
{
public static XmlDocument BuildQueryRequest(string activeStatus, string fromModifiedDate, string toModifiedDate)
{
XmlDocument doc = XmlUtils.MakeRequestDocument();
XmlElement parent = XmlUtils.MakeRequestParentElement(doc);
XmlElement queryElement = doc.CreateElement("VehicleQueryRq");
parent.AppendChild(queryElement);
queryElement.AppendChild(XmlUtils.MakeSimpleElem(doc, "ActiveStatus", activeStatus));
queryElement.AppendChild(XmlUtils.MakeSimpleElem(doc, "FromModifiedDate", fromModifiedDate));
queryElement.AppendChild(XmlUtils.MakeSimpleElem(doc, "ToModifiedDate", toModifiedDate));
return doc;
}
public static void HandleResponse(string response)
{
WalkVehicleQueryRs(response);
}
private static void WalkVehicleQueryRs(string response)
{
//Parse the response XML string into an XmlDocument
XmlDocument responseXmlDoc = new XmlDocument();
responseXmlDoc.LoadXml(response);
//Get the response for our request
XmlNodeList VehicleQueryRsList = responseXmlDoc.GetElementsByTagName("VehicleQueryRs");
if (VehicleQueryRsList.Count == 1) //Should always be true since we only did one request in this sample
{
XmlNode responseNode = VehicleQueryRsList.Item(0);
//Check the status code, info, and severity
XmlAttributeCollection rsAttributes = responseNode.Attributes;
string statusCode = rsAttributes.GetNamedItem("statusCode").Value;
string statusSeverity = rsAttributes.GetNamedItem("statusSeverity").Value;
string statusMessage = rsAttributes.GetNamedItem("statusMessage").Value;
//status code = 0 all OK, > 0 is warning
if (Convert.ToInt32(statusCode) >= 0)
{
XmlNodeList VehicleRetList = responseNode.SelectNodes("//VehicleRet");//XPath Query
for (int i = 0; i < VehicleRetList.Count; i++)
{
XmlNode VehicleRet = VehicleRetList.Item(i);
WalkVehicleRet(VehicleRet);
}
}
}
}
private static void WalkVehicleRet(XmlNode VehicleRet)
{
if (VehicleRet == null) return;
Vehicle v = null;
RotoTrackDb db = new RotoTrackDb();
string ListID = VehicleRet.SelectSingleNode("./ListID").InnerText;
if (db.Vehicles.Any(f => f.QBListId == ListID))
{
v = db.Vehicles.First(f => f.QBListId == ListID);
}
else
{
v = new Vehicle();
db.Vehicles.Add(v);
}
v.QBListId = ListID;
if (VehicleRet.SelectSingleNode("./Name") != null)
{
string Name = VehicleRet.SelectSingleNode("./Name").InnerText;
v.Name = Name;
}
string IsActive = "false";
if (VehicleRet.SelectSingleNode("./IsActive") != null)
{
IsActive = VehicleRet.SelectSingleNode("./IsActive").InnerText;
}
v.IsActive = (IsActive == "true") ? true : false;
if (VehicleRet.SelectSingleNode("./Desc") != null)
{
string Desc = VehicleRet.SelectSingleNode("./Desc").InnerText;
v.Description = Desc;
}
if (v != null)
{
db.SaveChanges();
}
}
}
}