-
Notifications
You must be signed in to change notification settings - Fork 9
/
03_PageXmlExport.cs
189 lines (157 loc) · 6.86 KB
/
03_PageXmlExport.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Linq;
namespace LessonsSamples.Lesson7.CohesionCoupling
{
// One step towards better cohesion by grouping some of the fields in one,
// and extract the file write functionality out
public class PageXmlExport_3
{
private readonly IPageFileWriter fileWriter;
private readonly int maxSalesOrders; // used in 3/5 methods
private readonly bool addCustomerDetails; // used in 2/5 methods
private readonly ICrmService crmService; // used in 2/5 methods
private readonly ILocationService locationService; // used in 2/5 methods
public PageXmlExport_3(
IPageFileWriter fileWriter,
int maxSalesOrders,
bool addCustomerDetails,
ICrmService crmService,
ILocationService locationService)
{
this.fileWriter = fileWriter;
this.maxSalesOrders = maxSalesOrders;
this.addCustomerDetails = addCustomerDetails;
this.crmService = crmService;
this.locationService = locationService;
}
public bool ExportCustomerPage(string customerName)
{
PageXml content = new PageXml {Customer = new CustomerXml {Name = customerName}};
using (EfRepository repository = new EfRepository())
{
if (maxSalesOrders > 0)
{
var orders = repository.GetEntities<Order>()
.Where(o => o.Customer.CompanyName == content.Customer.Name)
.OrderBy(o => o.OrderDate)
.Take(maxSalesOrders);
//enrich content with orders
// ...
}
if (addCustomerDetails)
{
var customer = repository.GetEntities<Customer>()
.Where(c => c.CompanyName == customerName);
// enrich content with customer data
// ...
}
}
return fileWriter.WriteFile(content, "CustomerPage");
}
public bool ExportCustomerPageWithExternalData(
string customerName,
PageData externalData)
{
PageXml content = new PageXml {Customer = new CustomerXml {Name = customerName}};
if (externalData.CustomerData != null)
{
// enrich with externalData.CustomerData
// ...
}
else
{
CustomerInfo customerData = crmService.GetCustomerInfo(content.Customer.Name);
}
using (EfRepository repository = new EfRepository())
{
if (maxSalesOrders > 0)
{
var orders = repository.GetEntities<Order>()
.Where(o => o.Customer.CompanyName == content.Customer.Name)
.OrderBy(o => o.OrderDate)
.Take(maxSalesOrders);
//enrich content with orders
// ...
}
if (addCustomerDetails)
{
var customer = repository.GetEntities<Customer>()
.Where(c => c.CompanyName == customerName);
// enrich content by merging the external customer data with what read from DB
// ...
}
}
if (locationService != null)
{
foreach (var address in content.Customer.Addresses)
{
Coordinates coordinates = locationService.GetCoordinates(address.City, address.Street, address.Number);
if (coordinates != null)
address.Coordinates = string.Format("{0},{1}", coordinates.Latitude, coordinates.Longitude);
}
}
return fileWriter.WriteFile(content);
}
public bool ExportOrders(string customerName)
{
PageXml content = new PageXml {Customer = new CustomerXml {Name = customerName}};
using (EfRepository repository = new EfRepository())
{
var orders = repository.GetEntities<Order>()
.Where(o => o.Customer.CompanyName == content.Customer.Name)
.OrderBy(o => o.ApprovedAmmount)
.ThenBy(o => o.OrderDate);
//enrich content with orders
}
return fileWriter.WriteFile(content);
}
public IEnumerable<PageXml> GetPagesFromOrders(IEnumerable<Order> orders)
{
Dictionary<string, IEnumerable<Order>> customerOrders = GroupOrdersByCustomer(orders);
foreach (var customerName in customerOrders.Keys)
{
PageXml content = new PageXml {Customer = new CustomerXml {Name = customerName}};
if (crmService != null)
{
CustomerInfo customerData = crmService.GetCustomerInfo(content.Customer.Name);
//enrich with data from crm
}
var recentOrders = customerOrders[customerName]
.OrderBy(o => o.OrderDate)
.Take(maxSalesOrders);
foreach (var order in recentOrders)
{
// enrich content with orders
// ...
}
if (locationService != null)
{
foreach (var address in content.Customer.Addresses)
{
Coordinates coordinates = locationService.GetCoordinates(address.City, address.Street, address.Number);
if (coordinates != null)
address.Coordinates = string.Format("{0},{1}", coordinates.Latitude, coordinates.Longitude);
}
}
yield return content;
}
}
public bool ExportPagesFromOrders(IEnumerable<Order> orders)
{
IEnumerable<PageXml> pages = GetPagesFromOrders(orders);
foreach (var pageXml in pages)
{
bool wasWritten = fileWriter.WriteFile(pageXml, "OrdersPage");
if (!wasWritten)
return false;
}
return true;
}
private Dictionary<string, IEnumerable<Order>> GroupOrdersByCustomer(IEnumerable<Order> orders)
{
// group orders by customer name and return them in a dictionary, ordered by OrderDate
throw new NotImplementedException();
}
}
}