-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocation.cs
800 lines (714 loc) · 31.4 KB
/
Location.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
// <copyright file="Location.cs" company="Engage Software">
// Copyright (c) 2004-2008
// by Engage Software ( http://www.engagesoftware.com )
// </copyright>
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
namespace Engage.Dnn.Locator
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using DotNetNuke.Common.Lists;
using DotNetNuke.Common.Utilities;
using Engage.Data;
/// <summary>
/// Represents a location, the main component in Engage: Locator
/// </summary>
public class Location
{
/// <summary>
/// Backing field for <see cref="Address"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string address;
/// <summary>
/// Backing field for <see cref="Address2"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string address2;
/// <summary>
/// Backing field for <see cref="Approved"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private bool approved;
/// <summary>
/// Backing field for <see cref="AverageRating"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private float averageRating;
/// <summary>
/// Backing field for <see cref="City"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string city;
/// <summary>
/// Backing field for <see cref="CountryId"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int countryId;
/// <summary>
/// Backing field for <see cref="CsvLineNumber"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int csvLineNumber;
/// <summary>
/// Backing field for <see cref="ExternalIdentifier"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string externalIdentifier;
/// <summary>
/// Backing field for <see cref="LastUpdateDate"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private DateTime lastUpdateDate;
/// <summary>
/// Backing field for <see cref="Latitude"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private double latitude;
/// <summary>
/// Backing field for <see cref="LocationDetails"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string locationDetails;
/// <summary>
/// Backing field for <see cref="LocationId"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int locationId = -1;
/// <summary>
/// Backing field for <see cref="LocationTypeId"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int locationTypeId;
/// <summary>
/// Backing field for <see cref="Longitude"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private double longitude;
/// <summary>
/// Backing field for <see cref="Name"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string name;
/// <summary>
/// Backing field for <see cref="Phone"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string phone;
/// <summary>
/// Backing field for <see cref="PortalId"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int portalId;
/// <summary>
/// Backing field for <see cref="PostalCode"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string postalCode;
/// <summary>
/// Backing field for <see cref="RegionId"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int? regionId;
/// <summary>
/// Backing field for <see cref="RegionAbbreviation"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string regionAbbreviation;
/// <summary>
/// Backing field for <see cref="RegionName"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string regionName;
/// <summary>
/// Backing field for <see cref="Website"/>
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string website;
/// <summary>
/// Backing field for <see cref="Distance"/>
/// </summary>
private double? distance;
/// <summary>
/// Backing field for <see cref="QueryIndex"/>
/// </summary>
private int? queryIndex;
/// <summary>
/// Gets or sets the location id.
/// </summary>
/// <value>The location id.</value>
public int LocationId
{
[DebuggerStepThrough]
get { return this.locationId; }
[DebuggerStepThrough]
set { this.locationId = value; }
}
/// <summary>
/// Gets or sets the public-facing ID for this location.
/// </summary>
/// <value>The external identifier.</value>
public string ExternalIdentifier
{
[DebuggerStepThrough]
get { return this.externalIdentifier; }
[DebuggerStepThrough]
set { this.externalIdentifier = value; }
}
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name of this location.</value>
public string Name
{
[DebuggerStepThrough]
get { return this.name; }
[DebuggerStepThrough]
set { this.name = value; }
}
/// <summary>
/// Gets or sets the latitude.
/// </summary>
/// <value>The latitude.</value>
public double Latitude
{
[DebuggerStepThrough]
get { return this.latitude; }
[DebuggerStepThrough]
set { this.latitude = value; }
}
/// <summary>
/// Gets or sets the longitude.
/// </summary>
/// <value>The longitude.</value>
public double Longitude
{
[DebuggerStepThrough]
get { return this.longitude; }
[DebuggerStepThrough]
set { this.longitude = value; }
}
/// <summary>
/// Gets or sets the country id.
/// </summary>
/// <value>The country id.</value>
public int CountryId
{
[DebuggerStepThrough]
get { return this.countryId; }
[DebuggerStepThrough]
set { this.countryId = value; }
}
/// <summary>
/// Gets or sets the region id.
/// </summary>
/// <value>The region id.</value>
public int? RegionId
{
[DebuggerStepThrough]
get { return this.regionId; }
[DebuggerStepThrough]
set { this.regionId = value; }
}
/// <summary>
/// Gets the region abbreviation.
/// </summary>
/// <value>The region abbreviation.</value>
public string RegionAbbreviation
{
[DebuggerStepThrough]
get { return this.regionAbbreviation; }
}
/// <summary>
/// Gets the region name.
/// </summary>
/// <value>The region name.</value>
public string RegionName
{
[DebuggerStepThrough]
get { return this.regionName; }
}
/// <summary>
/// Gets or sets the city.
/// </summary>
/// <value>The city in which this location resides.</value>
public string City
{
[DebuggerStepThrough]
get { return this.city; }
[DebuggerStepThrough]
set { this.city = value; }
}
/// <summary>
/// Gets or sets the first line of the address.
/// </summary>
/// <value>The first line of the address.</value>
public string Address
{
[DebuggerStepThrough]
get { return this.address; }
[DebuggerStepThrough]
set { this.address = value; }
}
/// <summary>
/// Gets or sets the second line of the address.
/// </summary>
/// <value>The second line of the address.</value>
public string Address2
{
[DebuggerStepThrough]
get { return this.address2; }
[DebuggerStepThrough]
set { this.address2 = value; }
}
/// <summary>
/// Gets the third line of an address, containing city, state (if applicable) and postal code.
/// </summary>
/// <value>The third line of the address.</value>
public string Address3
{
get
{
ListEntryInfo region = this.RegionId.HasValue ? new ListController().GetListEntryInfo(this.RegionId.Value) : null;
return string.Format("{0}, {1}{2}", this.City, (region != null ? region.Value + " " : string.Empty), this.PostalCode);
}
}
/// <summary>
/// Gets the full address, a combination of <see cref="Address"/> and <see cref="Address2"/>.
/// </summary>
/// <value>The full address.</value>
public string FullAddress
{
get
{
return this.address + (this.address2.Length > 0 ? " " + this.address2 : string.Empty);
}
}
/// <summary>
/// Gets or sets the postal code.
/// </summary>
/// <value>The postal code.</value>
public string PostalCode
{
[DebuggerStepThrough]
get { return this.postalCode; }
[DebuggerStepThrough]
set { this.postalCode = value; }
}
/// <summary>
/// Gets or sets the phone number for this location.
/// </summary>
/// <value>The phone number.</value>
public string Phone
{
[DebuggerStepThrough]
get { return this.phone; }
[DebuggerStepThrough]
set { this.phone = value; }
}
/// <summary>
/// Gets or sets the location details.
/// </summary>
/// <value>The location details.</value>
public string LocationDetails
{
[DebuggerStepThrough]
get { return this.locationDetails; }
[DebuggerStepThrough]
set { this.locationDetails = value; }
}
/// <summary>
/// Gets or sets the location type id.
/// </summary>
/// <value>The location type id.</value>
public int LocationTypeId
{
[DebuggerStepThrough]
get { return this.locationTypeId; }
[DebuggerStepThrough]
set { this.locationTypeId = value; }
}
/// <summary>
/// Gets or sets the portal id.
/// </summary>
/// <value>The portal id.</value>
public int PortalId
{
[DebuggerStepThrough]
get { return this.portalId; }
[DebuggerStepThrough]
set { this.portalId = value; }
}
/// <summary>
/// Gets or sets the website URL.
/// </summary>
/// <value>The website URL.</value>
public string Website
{
[DebuggerStepThrough]
get { return this.website; }
[DebuggerStepThrough]
set { this.website = value; }
}
/// <summary>
/// Gets or sets the last update date.
/// </summary>
/// <value>The last update date.</value>
public DateTime LastUpdateDate
{
get
{
return this.lastUpdateDate == DateTime.MinValue ? DateTime.Now : this.lastUpdateDate;
}
[DebuggerStepThrough]
set
{
this.lastUpdateDate = value;
}
}
/// <summary>
/// Gets or sets the line number on which this location was located when imported.
/// </summary>
/// <value>The CSV line number.</value>
public int CsvLineNumber
{
[DebuggerStepThrough]
get { return this.csvLineNumber; }
[DebuggerStepThrough]
set { this.csvLineNumber = value; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Location"/> is approved.
/// </summary>
/// <value><c>true</c> if approved; otherwise, <c>false</c>.</value>
public bool Approved
{
[DebuggerStepThrough]
get { return this.approved; }
[DebuggerStepThrough]
set { this.approved = value; }
}
/// <summary>
/// Gets the average rating.
/// </summary>
/// <value>The average rating.</value>
public float AverageRating
{
[DebuggerStepThrough]
get { return this.averageRating; }
[DebuggerStepThrough]
private set { this.averageRating = value; }
}
/// <summary>
/// Gets the distance of this location from the search query, or <c>null</c> if this location was not returned by a search query.
/// </summary>
/// <value>The distance of this location from the search query.</value>
public double? Distance
{
[DebuggerStepThrough]
get { return this.distance; }
}
/// <summary>
/// Gets the index of this location within the search query, or <c>null</c> is this location was not returned by a search query.
/// </summary>
/// <value>The index of this location within the search query.</value>
public double? QueryIndex
{
[DebuggerStepThrough]
get { return this.queryIndex; }
}
public static DataTable GetCountriesList(int portalId)
{
return Data.DataProvider.Instance().GetCountriesList(portalId);
}
public static DataTable GetFilesToImport()
{
return Data.DataProvider.Instance().GetFilesToImport();
}
public static void UpdateImportedLocationRow(int fileId)
{
Data.DataProvider.Instance().UpdateImportedLocationRow(fileId);
}
/// <summary>
/// Gets the location with the given ID.
/// </summary>
/// <param name="locationId">The location ID.</param>
/// <returns>The location with the given ID, or <c>null</c> if no location with that ID exists</returns>
public static Location GetLocation(int locationId)
{
using (IDataReader reader = Data.DataProvider.Instance().GetLocation(locationId))
{
if (reader.Read())
{
return Load(reader);
}
}
return null;
}
/// <summary>
/// Gets a list of locations in the given portal.
/// </summary>
/// <param name="portalId">The portal ID.</param>
/// <param name="approvedOnly">if set to <c>true</c>, the list only includes approved locations; otherwise, the list includes approved and unapproved locations.</param>
/// <returns>A list of <see cref="Location"/>s</returns>
public static LocationCollection GetLocations(int portalId, bool approvedOnly)
{
// TODO: Refactor this so it doesn't go to the database for each location!
LocationCollection locations = new LocationCollection();
foreach (DataRow row in GetLocations(portalId, approvedOnly, "Name", null, null).Rows)
{
locations.Add(GetLocation(Convert.ToInt32(row["LocationId"], CultureInfo.InvariantCulture)));
}
return locations;
}
/// <summary>
/// Gets a list of locations in the given portal.
/// </summary>
/// <param name="portalId">The portal ID.</param>
/// <param name="approvedOnly">if set to <c>true</c>, the list only includes approved locations; otherwise, the list includes approved and unapproved locations.</param>
/// <param name="sortColumn">The name of the column by which the results should be sorted.</param>
/// <param name="pageIndex">Index of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <param name="pageSize">Size of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <returns>A list of <see cref="Location"/>s</returns>
public static DataTable GetLocations(int portalId, bool approvedOnly, string sortColumn, int? pageIndex, int? pageSize)
{
return Data.DataProvider.Instance().GetAllLocations(portalId, approvedOnly, sortColumn, pageIndex, pageSize);
}
/// <summary>
/// Gets a list of the locations ordered by their distance from a given location.
/// </summary>
/// <param name="latitude">The latitude of the search location.</param>
/// <param name="longitude">The longitude of the search location.</param>
/// <param name="portalId">The portal ID.</param>
/// <param name="locationTypeIds">An array of IDs of the types of locations to include in the results.</param>
/// <param name="pageIndex">Index of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <param name="pageSize">Size of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <returns>A list of <see cref="Location"/>s</returns>
public static LocationCollection GetAllLocationsByDistance(double latitude, double longitude, int portalId, int[] locationTypeIds, int? pageIndex, int? pageSize)
{
return GetAllLocationsByDistance(latitude, longitude, null, portalId, locationTypeIds, pageIndex, pageSize);
}
/// <summary>
/// Gets a list of the locations within a given mile radius, ordered by their distance from a given location.
/// </summary>
/// <param name="latitude">The latitude of the search location.</param>
/// <param name="longitude">The longitude of the search location.</param>
/// <param name="radius">The radius (in miles) around the search location for which to return results.</param>
/// <param name="portalId">The portal ID.</param>
/// <param name="locationTypeIds">An array of IDs of the types of locations to include in the results.</param>
/// <param name="pageIndex">Index of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <param name="pageSize">Size of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <returns>A list of <see cref="Location"/>s</returns>
/// <exception cref="DBException">Data reader did not have the expected structure. An error must have occurred in the query.</exception>
public static LocationCollection GetAllLocationsByDistance(double latitude, double longitude, int? radius, int portalId, int[] locationTypeIds, int? pageIndex, int? pageSize)
{
using (IDataReader reader = Data.DataProvider.Instance().GetAllLocationsByDistance(latitude, longitude, radius, portalId, locationTypeIds, pageIndex, pageSize))
{
return FillPagedCollection(reader);
}
}
/// <summary>
/// Gets all of the locations in the given list of type.
/// </summary>
/// <param name="portalId">The portal ID.</param>
/// <param name="locationTypeIds">An array of IDs of the types of locations to include in the results.</param>
/// <returns>A list of <see cref="Location"/>s</returns>
public static LocationCollection GetAllLocationsByType(int portalId, int[] locationTypeIds)
{
return GetAllLocationsByType(portalId, locationTypeIds, null, null);
}
/// <summary>
/// Gets all of the locations in the given list of type.
/// </summary>
/// <param name="portalId">The portal ID.</param>
/// <param name="locationTypeIds">An array of IDs of the types of locations to include in the results.</param>
/// <param name="pageIndex">Index of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <param name="pageSize">Size of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <returns>A list of <see cref="Location"/>s</returns>
public static LocationCollection GetAllLocationsByType(int portalId, int[] locationTypeIds, int? pageIndex, int? pageSize)
{
using (IDataReader reader = Data.DataProvider.Instance().GetAllLocationsByType(portalId, locationTypeIds, pageIndex, pageSize))
{
return FillPagedCollection(reader);
}
}
/// <summary>
/// Gets all of the locations located in a given country.
/// </summary>
/// <param name="countryId">The country ID.</param>
/// <param name="portalId">The portal ID.</param>
/// <returns>A list of all the <see cref="Location"/>s in the given country</returns>
public static LocationCollection GetLocationsByCountry(int countryId, int portalId)
{
return GetLocationsByCountry(countryId, portalId, null, null);
}
/// <summary>
/// Gets all of the locations located in a given country.
/// </summary>
/// <param name="countryId">The country ID.</param>
/// <param name="portalId">The portal ID.</param>
/// <param name="pageIndex">Index of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <param name="pageSize">Size of the page, if returning a partial list; otherwise <c>null</c>.</param>
/// <returns>A list of all the <see cref="Location"/>s in the given country</returns>
public static LocationCollection GetLocationsByCountry(int countryId, int portalId, int? pageIndex, int? pageSize)
{
using (IDataReader reader = Data.DataProvider.Instance().GetLocationsByCountry(countryId, portalId, pageIndex, pageSize))
{
return FillPagedCollection(reader);
}
}
/// <summary>
/// Deletes a location.
/// </summary>
/// <param name="locationId">The location ID.</param>
public static void DeleteLocation(int locationId)
{
Data.DataProvider.Instance().DeleteLocation(locationId);
}
/// <summary>
/// Adds the given <paramref name="comment"/> to the location with the given ID.
/// </summary>
/// <param name="locationId">The location ID.</param>
/// <param name="comment">The comment text.</param>
/// <param name="submittedBy">The user ID of the commenter.</param>
/// <param name="approved">if set to <c>true</c> the comment is approved; otherwise it is waiting for approval.</param>
public static void InsertComment(int locationId, string comment, string submittedBy, bool approved)
{
Data.DataProvider.Instance().InsertComment(locationId, comment, submittedBy, approved);
}
public static DataTable GetNewSubmittedComments(int portalId, bool approved)
{
return Data.DataProvider.Instance().GetNewSubmittedComments(portalId, approved);
}
/// <summary>
/// Loads the <see cref="Location"/> instance contained in the specified <paramref name="row"/>.
/// </summary>
/// <param name="row">The data reader for the <see cref="Location"/> instance.</param>
/// <returns>A <see cref="Location"/> instance instantiated from the given <paramref name="row"/></returns>
public static Location Load(IDataReader row)
{
return Load(row, row.GetSchemaTable().Select("ColumnName = 'Distance'").Length == 1, row.GetSchemaTable().Select("ColumnName = 'Index'").Length == 1);
}
/// <summary>
/// Loads the <see cref="Location"/> instance contained in the specified <paramref name="row"/>.
/// </summary>
/// <param name="row">The data record for the <see cref="Location"/> instance.</param>
/// <param name="containsDistanceColumn">if set to <c>true</c> <paramref name="row"/> contains the Distance column.</param>
/// <param name="containsIndexColumn">if set to <c>true</c> <paramref name="row"/> contains the Index column.</param>
/// <returns>
/// A <see cref="Location"/> instance instantiated from the given <paramref name="row"/>
/// </returns>
public static Location Load(IDataRecord row, bool containsDistanceColumn, bool containsIndexColumn)
{
Location loc = new Location();
loc.address = row["Address"].ToString();
loc.address2 = row["Address2"].ToString();
loc.city = row["City"].ToString();
loc.locationDetails = row["LocationDetails"].ToString();
loc.externalIdentifier = row["ExternalIdentifier"].ToString();
loc.latitude = Convert.ToDouble(row["Latitude"], CultureInfo.InvariantCulture);
loc.longitude = Convert.ToDouble(row["Longitude"], CultureInfo.InvariantCulture);
loc.locationId = (int)row["LocationId"];
loc.locationTypeId = (int)row["LocationTypeId"];
loc.name = row["Name"].ToString();
loc.website = row["Website"].ToString();
loc.phone = row["Phone"].ToString();
loc.postalCode = row["PostalCode"].ToString();
loc.regionName = row["StateName"].ToString();
loc.regionAbbreviation = row["Abbreviation"].ToString();
loc.regionId = row["RegionId"] as int?;
loc.approved = (bool)row["Approved"];
loc.averageRating = row["AverageRating"] is DBNull ? 0 : Convert.ToSingle(row["AverageRating"], CultureInfo.InvariantCulture);
loc.distance = containsDistanceColumn ? (double?)row["Distance"] : null;
loc.queryIndex = containsIndexColumn ? (int?)row["Index"] : null;
return loc;
}
/// <summary>
/// Saves this instance.
/// </summary>
public void Save()
{
this.lastUpdateDate = DateTime.Now;
if (this.locationId == -1)
{
this.locationId = Data.DataProvider.Instance().SaveLocation(this);
}
else
{
Data.DataProvider.Instance().UpdateLocation(this);
}
}
/// <summary>
/// Saves this instance as a temp location. This is only used when importing locations.
/// </summary>
/// <param name="successful">if set to <c>true</c> the import for this location was successful.</param>
public void SaveTemp(bool successful)
{
if (this.locationId != -1)
{
this.locationId = Data.DataProvider.Instance().SaveTempLocation(this, successful);
}
}
/// <summary>
/// Gets the comments for this instance.
/// </summary>
/// <param name="onlyApproved">if set to <c>true</c> only gets the approved comments; otherwise gets approved and unapproved.</param>
/// <returns>A list of comments</returns>
public DataSet GetComments(bool onlyApproved)
{
return Data.DataProvider.Instance().GetComments(this.locationId, onlyApproved);
}
/// <summary>
/// Gets the attributes for this instance.
/// </summary>
/// <returns>A list of <see cref="Attribute"/>s</returns>
public List<Attribute> GetAttributes()
{
return Attribute.GetAttributes(this.locationTypeId, this.locationId);
}
/// <summary>
/// Fills a <see cref="LocationCollection"/> using the given <paramref name="reader"/>, for queries that contain the total records as the first result.
/// </summary>
/// <param name="reader">A representation of the list of locations returned by a query.</param>
/// <returns>A list of locations as represented by the given <paramref name="reader"/></returns>
/// <exception cref="DBException">Data reader did not have the expected structure. An error must have occurred in the query.</exception>
private static LocationCollection FillPagedCollection(IDataReader reader)
{
if (reader.Read())
{
LocationCollection locations = new LocationCollection(reader.GetInt32(0));
if (reader.NextResult())
{
return FillCollection(reader, locations);
}
}
throw new DBException("Data reader did not have the expected structure. An error must have occurred in the query.");
}
/// <summary>
/// Fills the given <see cref="LocationCollection"/> with the given <paramref name="reader"/>.
/// </summary>
/// <param name="reader">A representation of the list of locations returned by a query.</param>
/// <param name="locations">The list of locations to fill, instantiated and with its <see cref="LocationCollection.TotalRecords"/> property set, if necessary.</param>
/// <returns>A list of locations as represented by the given <paramref name="reader"/></returns>
/// <exception cref="ArgumentNullException"><c>locations</c> is null.</exception>
private static LocationCollection FillCollection(IDataReader reader, LocationCollection locations)
{
if (locations == null)
{
throw new ArgumentNullException("locations");
}
while (reader.Read())
{
locations.Add(Load(reader));
}
return locations;
}
}
}