-
Notifications
You must be signed in to change notification settings - Fork 40
/
SyncManager.cs
564 lines (496 loc) · 20.4 KB
/
SyncManager.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
using QbSync.QbXml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace QbSync.WebConnector
{
public class SyncManager
{
protected IAuthenticator authenticator;
public SyncManager(IAuthenticator authenticator)
{
this.authenticator = authenticator;
StepSync = new List<StepQueryResponse>();
}
internal List<StepQueryResponse> StepSync
{
get;
set;
}
public IVersionValidator VersionValidator
{
get;
set;
}
/// <summary>
/// Authenticate a login/password and return important information regarding if more requests
/// should be executed immediately.
/// </summary>
/// <param name="login">Login.</param>
/// <param name="password">Password.</param>
/// <returns>Array of 4 strings. 0: ticket; 1: nvu if invalid user, or empty string if valid; 2: time to wait in seconds before coming back; 3: not used</returns>
public virtual string[] Authenticate(string login, string password)
{
try
{
var authenticatedTicket = authenticator.GetAuthenticationFromLogin(login, password);
if (authenticatedTicket == null)
{
throw new Exception("GetAuthenticationFromLogin must return a ticket.");
}
try
{
LogMessage(authenticatedTicket, LogMessageType.Authenticate, LogDirection.In, authenticatedTicket.Ticket, login, password);
var ret = new string[4];
ret[0] = authenticatedTicket.Ticket;
ret[2] = string.Empty;
ret[3] = string.Empty; // Not used
if (authenticatedTicket.Authenticated == false)
{
ret[1] = "nvu"; // Invalid user
}
else
{
var waitTime = GetWaitTime(authenticatedTicket);
if (waitTime == 0)
{
ret[1] = GetCompanyFile(authenticatedTicket);
}
else
{
ret[1] = "none"; // No work is necessary
ret[2] = waitTime.ToString();
}
}
LogMessage(authenticatedTicket, LogMessageType.Authenticate, LogDirection.Out, authenticatedTicket.Ticket, ret);
return ret;
}
catch (Exception ex)
{
throw new QbSyncException(authenticatedTicket, ex);
}
}
catch (QbSyncException ex)
{
OnException(ex.Ticket, ex);
}
catch (Exception ex)
{
OnException(null, ex);
}
finally
{
SaveChanges();
}
return null;
}
/// <summary>
/// Returns the server version to the Web Connector.
/// </summary>
/// <returns>Server version.</returns>
public virtual string ServerVersion()
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
/// <summary>
/// Indicates which version the Web Connector is using.
/// </summary>
/// <param name="version">Web Connector Client version.</param>
/// <returns>An empty string if everything is fine, W:<message> if warning; E:<message> if error.</returns>
public virtual string ClientVersion(string version)
{
var requiredVersion = GetMinimumRequiredVersion();
var receivedVersion = new Version(version);
if (receivedVersion < requiredVersion)
{
return "W:You must update this WebConnector to a newer version.";
}
return string.Empty;
}
/// <summary>
/// The Web Connector is asking what has to be done to its database. Return an XML command.
/// </summary>
/// <param name="ticket">The ticket.</param>
/// <param name="strHCPResponse">First time use, an XML containing information about the QuickBooks client database.</param>
/// <param name="strCompanyFileName">The QuickBooks file opened on the client.</param>
/// <param name="qbXMLCountry">Country code.</param>
/// <param name="qbXMLMajorVers">QbXml Major Version.</param>
/// <param name="qbXMLMinorVers">QbXml Minor Version.</param>
/// <returns></returns>
public virtual string SendRequestXML(string ticket, string strHCPResponse, string strCompanyFileName, string qbXMLCountry, int qbXMLMajorVers, int qbXMLMinorVers)
{
try
{
var authenticatedTicket = authenticator.GetAuthenticationFromTicket(ticket);
try
{
LogMessage(authenticatedTicket, LogMessageType.Send, LogDirection.In, ticket, strHCPResponse, strCompanyFileName, qbXMLCountry, qbXMLMajorVers.ToString(), qbXMLMinorVers.ToString());
if (!string.IsNullOrWhiteSpace(strHCPResponse))
{
ProcessClientInformation(authenticatedTicket, strHCPResponse);
}
var result = string.Empty;
if (authenticatedTicket != null)
{
// Check the version, if we can't have the minimum version, we must fail.
if (VersionValidator != null && !VersionValidator.ValidateVersion(authenticatedTicket.Ticket, qbXMLCountry, qbXMLMajorVers, qbXMLMinorVers))
{
result = string.Empty;
}
else
{
StepQueryResponse stepQueryResponse = null;
while ((stepQueryResponse = FindStep(authenticatedTicket.CurrentStep)) != null)
{
stepQueryResponse.SetOptions(GetOptions(authenticatedTicket));
result = stepQueryResponse.SendXML(authenticatedTicket);
if (result == null)
{
authenticatedTicket.CurrentStep = FindNextStepName(authenticatedTicket.CurrentStep);
}
else
{
break;
}
}
// If we don't have more steps to execute, let's return nothing.
if (result == null)
{
result = string.Empty;
}
}
}
LogMessage(authenticatedTicket, LogMessageType.Send, LogDirection.Out, ticket, result);
return result;
}
catch (Exception ex)
{
throw new QbSyncException(authenticatedTicket, ex);
}
}
catch (QbSyncException ex)
{
OnException(ex.Ticket, ex);
}
catch (Exception ex)
{
OnException(null, ex);
}
finally
{
SaveChanges();
}
return null;
}
/// <summary>
/// Response from the Web Connector based on the previous comamnd sent.
/// </summary>
/// <param name="ticket">The ticket.</param>
/// <param name="response">The XML response.</param>
/// <param name="hresult">Code in case of an error.</param>
/// <param name="message">Human message in case of an error.</param>
/// <returns></returns>
public virtual int ReceiveRequestXML(string ticket, string response, string hresult, string message)
{
try
{
var authenticatedTicket = authenticator.GetAuthenticationFromTicket(ticket);
try
{
LogMessage(authenticatedTicket, LogMessageType.Receive, LogDirection.In, ticket, response, hresult, message);
var result = -1;
if (authenticatedTicket != null)
{
StepQueryResponse stepQueryResponse = FindStep(authenticatedTicket.CurrentStep);
if (stepQueryResponse != null)
{
stepQueryResponse.SetOptions(GetOptions(authenticatedTicket));
result = stepQueryResponse.ReceiveXML(authenticatedTicket, response, hresult, message);
if (result >= 0)
{
var stepName = stepQueryResponse.GotoStep();
// We go to the next step if we are asked to
if (!string.IsNullOrEmpty(stepName))
{
authenticatedTicket.CurrentStep = stepName;
}
else if (stepQueryResponse.GotoNextStep())
{
authenticatedTicket.CurrentStep = FindNextStepName(authenticatedTicket.CurrentStep);
}
}
}
}
LogMessage(authenticatedTicket, LogMessageType.Receive, LogDirection.Out, ticket, result.ToString());
return result;
}
catch (Exception ex)
{
throw new QbSyncException(authenticatedTicket, ex);
}
}
catch (QbSyncException ex)
{
OnException(ex.Ticket, ex);
}
catch (Exception ex)
{
OnException(null, ex);
}
finally
{
SaveChanges();
}
return -1;
}
/// <summary>
/// Gets the last error that happened. This method is called only if an error is found.
/// </summary>
/// <param name="ticket">The ticket.</param>
/// <returns>Tell the Web Connector what is the error.</returns>
public virtual string GetLastError(string ticket)
{
try
{
var authenticatedTicket = authenticator.GetAuthenticationFromTicket(ticket);
try
{
LogMessage(authenticatedTicket, LogMessageType.GetError, LogDirection.In, ticket);
var result = string.Empty;
if (authenticatedTicket != null)
{
if (VersionValidator != null && !VersionValidator.IsValidTicket(authenticatedTicket.Ticket))
{
result = string.Format("The server requires QbXml version {0}.{1} or higher. Please upgrade QuickBooks.", QbXmlRequest.VERSION.Major, QbXmlRequest.VERSION.Minor);
}
else
{
StepQueryResponse stepQueryResponse = FindStep(authenticatedTicket.CurrentStep);
if (stepQueryResponse != null)
{
result = stepQueryResponse.LastError(authenticatedTicket);
}
}
}
LogMessage(authenticatedTicket, LogMessageType.GetError, LogDirection.Out, ticket, result);
return result;
}
catch (Exception ex)
{
throw new QbSyncException(authenticatedTicket, ex);
}
}
catch (QbSyncException ex)
{
OnException(ex.Ticket, ex);
}
catch (Exception ex)
{
OnException(null, ex);
}
finally
{
SaveChanges();
}
return null;
}
/// <summary>
/// An error happened with the Web Conenctor.
/// </summary>
/// <param name="ticket">The ticket.</param>
/// <param name="hresult">Code in case of an error.</param>
/// <param name="message">Human message in case of an error.</param>
/// <returns>Tell the Web Connector what is the error.</returns>
public virtual string ConnectionError(string ticket, string hresult, string message)
{
try
{
var authenticatedTicket = authenticator.GetAuthenticationFromTicket(ticket);
try
{
LogMessage(authenticatedTicket, LogMessageType.Error, LogDirection.In, ticket, hresult, message);
var result = "done";
if (authenticatedTicket != null)
{
}
LogMessage(authenticatedTicket, LogMessageType.Error, LogDirection.Out, ticket, result);
return result;
}
catch (Exception ex)
{
throw new QbSyncException(authenticatedTicket, ex);
}
}
catch (QbSyncException ex)
{
OnException(ex.Ticket, ex);
}
catch (Exception ex)
{
OnException(null, ex);
}
finally
{
SaveChanges();
}
return null;
}
/// <summary>
/// Closing the conneciton.
/// </summary>
/// <param name="ticket">The ticket</param>
/// <returns>String to display to the user.</returns>
public virtual string CloseConnection(string ticket)
{
try
{
var authenticatedTicket = authenticator.GetAuthenticationFromTicket(ticket);
try
{
LogMessage(authenticatedTicket, LogMessageType.Close, LogDirection.In, ticket);
var result = "Invalid Ticket";
if (authenticatedTicket != null)
{
result = "Sync Completed";
}
LogMessage(authenticatedTicket, LogMessageType.Error, LogDirection.Out, ticket, result);
return result;
}
catch (Exception ex)
{
throw new QbSyncException(authenticatedTicket, ex);
}
}
catch (QbSyncException ex)
{
OnException(ex.Ticket, ex);
}
catch (Exception ex)
{
OnException(null, ex);
}
finally
{
SaveChanges();
}
return null;
}
/// <summary>
/// Registers a step to use.
/// </summary>
/// <param name="stepQueryResponse">Step.</param>
public void RegisterStep(StepQueryResponse stepQueryResponse)
{
StepSync.Add(stepQueryResponse);
}
/// <summary>
/// Called when an exception occurs.
/// </summary>
/// <param name="ticket">The ticket if found. It might be null if no ticket has been provided or if the code failed when trying to create the authenticated ticket.</param>
/// <param name="exception">Exception.</param>
protected internal virtual void OnException(AuthenticatedTicket ticket, Exception exception)
{
}
/// <summary>
/// Indicates if there is anything to be done with the current ticket.
/// Return 0 if you have work to do immediately. Otherwise return the number of seconds
/// when you want the Web Connector to come back.
/// </summary>
/// <returns>Number of seconds to wait before the Web Connector comes back, or 0 to do some work right now.</returns>
protected internal virtual int GetWaitTime(AuthenticatedTicket authenticatedTicket)
{
return 0;
}
/// <summary>
/// Logs messages to a database.
/// </summary>
/// <param name="authenticatedTicket">The ticket which has the ticket string. The ticket could also be not authenticated if an error happened.</param>
/// <param name="messageType">Type of message.</param>
/// <param name="direction">Direction of the message (In the WebService, or Out the WebService).</param>
/// <param name="arguments">Other arguments to save.</param>
protected internal virtual void LogMessage(AuthenticatedTicket authenticatedTicket, LogMessageType messageType, LogDirection direction, string ticket, params string[] arguments)
{
}
/// <summary>
/// Implement this function in order to save the states to your database.
/// </summary>
protected internal virtual void SaveChanges()
{
}
/// <summary>
/// Returns the minimum version the Web Connector has to support.
/// </summary>
/// <returns>Minimum version.</returns>
protected internal virtual Version GetMinimumRequiredVersion()
{
return new Version(2, 1, 0, 30);
}
/// <summary>
/// Returns the path where the client company file is located.
/// Override this method if you wish to open a different file than the current one open on the client.
/// </summary>
/// <param name="authenticatedTicket">The ticket.</param>
/// <returns>Path where company file is located.</returns>
protected internal virtual string GetCompanyFile(AuthenticatedTicket authenticatedTicket)
{
return string.Empty; // Use the company that is opened on the client.
}
/// <summary>
/// Processes the response sent for the first time by the WebConnector.
/// </summary>
/// <param name="authenticatedTicket">The ticket.</param>
/// <param name="response">First Message.</param>
protected virtual void ProcessClientInformation(AuthenticatedTicket authenticatedTicket, string response)
{
}
/// <summary>
/// Gets the options for the SyncManager.
/// </summary>
/// <param name="authenticatedTicket">The ticket.</param>
/// <returns>Options.</returns>
protected internal virtual QbXmlResponseOptions GetOptions(AuthenticatedTicket authenticatedTicket)
{
return null;
}
/// <summary>
/// Finds the step based on its name.
/// </summary>
/// <param name="step">Step name.</param>
/// <returns>The step associated with the name.</returns>
protected internal StepQueryResponse FindStep(string step)
{
if (step == AuthenticatedTicket.InitialStep)
{
return StepSync.FirstOrDefault();
}
return StepSync.FirstOrDefault(s => s.Name == step);
}
/// <summary>
/// Finds the next step after the step name.
/// </summary>
/// <param name="step">Step name.</param>
/// <returns>The next step name or null if none.</returns>
protected internal string FindNextStepName(string step)
{
if (step == AuthenticatedTicket.InitialStep)
{
if (StepSync.Count >= 2)
{
return StepSync[1].Name;
}
}
for (var i = 0; i < StepSync.Count; i++)
{
if (StepSync[i].Name == step)
{
if (i + 1 < StepSync.Count)
{
return StepSync[i + 1].Name;
}
}
}
return null;
}
}
}