-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataLoader.cs
More file actions
5293 lines (4329 loc) · 216 KB
/
DataLoader.cs
File metadata and controls
5293 lines (4329 loc) · 216 KB
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Company2SF.Salesforce;
using Company2SF.EMSC2SF;
using Metaphone;
using GenericLibrary;
using System.Text;
using System.IO;
namespace Company2SF
{
public static class SalesForceExtensions
{
public static bool IsADuplicateOf( this Contact c, Contact objNewContact )
{
// discard if both first/last names don't match
if( !c.FirstName.Equals( objNewContact.FirstName )
|| !c.LastName.Equals( objNewContact.LastName ) )
return false;
// discard if middle name or SSN are not blank and don't match
if( !c.Middle_Name__c.IsNullOrBlank() && !objNewContact.Middle_Name__c.IsNullOrBlank()
&& !c.Middle_Name__c.NullAwareEquals( objNewContact.Middle_Name__c ) )
return false;
if( !c.SSN__c.IsNullOrBlank() && !objNewContact.SSN__c.IsNullOrBlank()
&& !c.SSN__c.NullAwareEquals( objNewContact.SSN__c ) )
return false;
// name matches, so we test email, home/mobile phones, birthdate, address, city, ME nbr, work phone
// (any matches are flagged as duplicate)
if( c.Email != null && c.Email.NotNullBlankAndEquals( objNewContact.Email ) )
return true;
if( c.HomePhone != null && ( c.HomePhone.NotNullBlankAndEquals( objNewContact.HomePhone )
|| c.HomePhone.NotNullBlankAndEquals( objNewContact.MobilePhone ) ) )
return true;
if( c.MobilePhone != null && ( c.MobilePhone.NotNullBlankAndEquals( objNewContact.HomePhone )
|| c.MobilePhone.NotNullBlankAndEquals( objNewContact.MobilePhone ) ) )
return true;
if( c.Birthdate != null && c.Birthdate.NotNullAndEquals( objNewContact.Birthdate ) )
return true;
// check the main and other address line #1
string strOtherStreet = ( objNewContact.OtherStreet ?? "" ).Replace( ".", "" );
string strAddress1 = objNewContact.Address_Line_1__c.Replace( ".", "" );
if( c.OtherStreet != null )
{
string strCOthStr= c.OtherStreet.Replace( ".", "" );
if( ( strCOthStr.IsEqualOrPartiallyMatchedTo( strOtherStreet )
|| strCOthStr.IsEqualOrPartiallyMatchedTo( strAddress1 ) ) )
return true;
}
if( c.Address_Line_1__c != null )
{
string strCAddr1 = c.Address_Line_1__c.Replace( ".", "" );
if( ( strCAddr1.IsEqualOrPartiallyMatchedTo( strAddress1 )
|| strCAddr1.IsEqualOrPartiallyMatchedTo( strOtherStreet ) ) )
return true;
}
// check main and other city
string strOtherCity = ( objNewContact.OtherCity ?? "" ).Replace( ".", "" );
string strCity = objNewContact.City__c.Replace( ".", "" );
if( c.OtherCity != null )
{
string strCOthCty = c.OtherCity.Replace( ".", "" );
if( ( strCOthCty.IsEqualOrPartiallyMatchedTo( strOtherCity )
|| strCOthCty.IsEqualOrPartiallyMatchedTo( strCity ) ) )
return true;
}
if( c.City__c != null )
{
string strCCty = c.City__c.Replace( ".", "" );
if( ( strCCty.IsEqualOrPartiallyMatchedTo( strCity )
|| strCCty.IsEqualOrPartiallyMatchedTo( strOtherCity ) ) )
return true;
}
// check me number
if( c.MeNumber__c != null && ( c.MeNumber__c.NotNullBlankAndEquals( objNewContact.MeNumber__c )
|| c.MeNumber__c.NotNullBlankAndEquals( objNewContact.MeNumber__c ) ) )
return true;
if( c.Work_Phone__c != null && c.Work_Phone__c.NotNullBlankAndEquals( objNewContact.Work_Phone__c ) )
return true;
// if name matches but middle name and ssn cant be compared
// and neither email, phones and birth dt match, then we cannot conclude it is a duplicate
return false;
}
public static bool IsAMADuplicateOf( this Contact c, Contact objAMAContact )
{
// compare MeNumber
if( !c.MeNumber__c.IsNullOrBlank() )
{
// same MeNumber means duplicate
if( objAMAContact.MeNumber__c.Equals( c.MeNumber__c ) )
return true;
else
return false;
}
// compare both first/last names
if( !c.FirstName.Equals( objAMAContact.FirstName )
|| !c.LastName.Equals( objAMAContact.LastName ) )
return false;
if( c.HomePhone != null && ( c.HomePhone.NotNullBlankAndEquals( objAMAContact.HomePhone )
|| c.HomePhone.NotNullBlankAndEquals( objAMAContact.MobilePhone ) ) )
return true;
if( c.Birthdate != null && c.Birthdate.NotNullAndEquals( objAMAContact.Birthdate ) )
return true;
string strOtherStreet = objAMAContact.OtherStreet.Replace( ".", "" );
string strAddress1 = objAMAContact.Address_Line_1__c.Replace( ".", "" );
if( c.OtherStreet != null )
{
string strCOthStr= c.OtherStreet.Replace( ".", "" );
if( ( strCOthStr.IsEqualOrPartiallyMatchedTo( strOtherStreet )
|| strCOthStr.IsEqualOrPartiallyMatchedTo( strAddress1 ) ) )
return true;
}
if( c.Address_Line_1__c != null )
{
string strCAddr1 = c.Address_Line_1__c.Replace( ".", "" );
if( ( strCAddr1.IsEqualOrPartiallyMatchedTo( strAddress1 )
|| strCAddr1.IsEqualOrPartiallyMatchedTo( strOtherStreet ) ) )
return true;
}
// if name matches but middle name and ssn cant be compared
// and neither email, phones and birth dt match, then we cannot conclude it is a duplicate
return false;
}
}
public class DataLoader
{
public ApiService objAPI;
public ApiService API
{
set
{
objAPI = value;
objAPI.OnError = HandleError;
objAPI.OnReportStatus = ReportStatus;
// auto configure API according to the setting in the web.config
strInstance = System.Configuration.ConfigurationManager.AppSettings[ "Instance" ].ToLower();
switch( strInstance )
{
case "dev1":
objAPI.ConnectionString = "SalesforceDEV1";
break;
case "test1":
objAPI.ConnectionString = "SalesforceLogin";
break;
case "prod":
if( Properties.Settings.Default.Company2SF_EMSC_SF_SforceService.StartsWith( "https://login.salesforce.com" ) )
{
objAPI.ConnectionString = "SalesforcePRODUCTION";
}
else
lblError.Text = "ERROR: In order to connect to Production, please switch the Salesforce URL to https://login.salesforce.com in the web.config.";
break;
}
}
get { return objAPI; }
}
DBAccess objDB;
string strInstance = "";
string strAppPath = HttpContext.Current.Request.PhysicalApplicationPath;
bool bMassDataLoad = false;
System.Web.UI.WebControls.TextBox tbStatus;
System.Web.UI.WebControls.Label lblError;
public DataLoader( string strPath
, System.Web.UI.WebControls.TextBox tbStatusParam
, System.Web.UI.WebControls.Label lblErrorParam
, System.Web.UI.HtmlControls.HtmlGenericControl OpenCSVListParam )
{
strAppPath = strPath; //Request.PhysicalApplicationPath
tbStatus = tbStatusParam;
lblError = lblErrorParam;
objDB = new DBAccess();
objDB.ErrorLabel = lblErrorParam;
Company2SFUtils.OpenCSVList = OpenCSVListParam;
}
public bool HandleError( string strErrorMessage, string strCommand )
{
// report error then save it to a file
tbStatus.Text = string.Concat( tbStatus.Text, "\r\n\r\n** ERROR: ", objAPI.ErrorMessage
, " - Error happened during execution of ", strCommand, " **" );
Company2SFUtils.SaveStatusCSV( "", tbStatus, true );
// do not cancel
return false;
}
public bool ReportStatus( params string[] strStatus )
{
return Company2SFUtils.ReportStatus( strStatus );
}
public void RemoveAMADuplicates()
{
string strFirstName = "", strLastName = "";
bool bContinue = true;
while( bContinue )
{
List<Contact> objContacts = Company2SFUtils.GetProvidersFromSF( objAPI, lblError
, " Birthdate, Email, Address_Line_1__c, City__c, OtherStreet, OtherCity, HomePhone, MobilePhone, MeNumber__c, Work_Phone__c, SSN__C, AMAOnly__c "
, " FirstName > '" + strFirstName + "' AND LastName > '" + strLastName + "'", " LastName, FirstName, AMAOnly__c LIMIT 2000 " );
if( objContacts.Count == 0 )
break;
strFirstName = objContacts.Last().FirstName;
strLastName = objContacts.Last().LastName;
List<Contact> objProvs4Upd = new List<Contact>();
Contact objPrevious = objContacts.First();
foreach( Contact objProv in objContacts.Skip( 1 ) )
{
if( objProv.LastName.Equals( objPrevious.LastName )
&& objProv.FirstName.Equals( objPrevious.FirstName )
&& ( objProv.SSN__c.NotNullAndEquals( objPrevious.SSN__c )
|| objProv.HomePhone.NotNullAndEquals( objPrevious.HomePhone )
|| objProv.Email.NotNullAndEquals( objPrevious.Email )
|| objProv.Birthdate.NotNullAndEquals( objPrevious.Birthdate )
) )
{
Contact objAMAContact = null, objCompanyContact = null;
if( objProv.AMAOnly__c.NotNullAndEquals( "1" ) )
{
objAMAContact = objProv;
objCompanyContact = objPrevious;
}
else
if( objPrevious.AMAOnly__c.NotNullAndEquals( "1" ) )
{
objAMAContact = objPrevious;
objCompanyContact = objProv;
}
else
// ignore if neither was from AMA
continue;
// copy provider to list of contacts for update
objAMAContact.LastName = objAMAContact.LastName + " (DUPLICATE)";
objAMAContact.Name = null;
objProvs4Upd.Add( objAMAContact );
// copy MENumber if available
if( !objAMAContact.MeNumber__c.NotNullAndEquals( "" ) && objCompanyContact.MeNumber__c.NotNullAndEquals( "" ) )
{
objCompanyContact.MeNumber__c = objAMAContact.MeNumber__c;
objCompanyContact.Name = null;
objProvs4Upd.Add( objCompanyContact );
}
continue;
}
objPrevious = objProv;
}
// update providers
if( objProvs4Upd.Count > 0 )
{
SaveResult[] objResults = objAPI.Update( objProvs4Upd.ToArray<sObject>() );
}
}
}
public void RemoveDuplicateNotes()
{
string strSOQL = "SELECT ID, ParentID FROM NOTE WHERE Title like 'SystemName Note:%' "
+ " order by ParentId , Title DESC";
List<Note> objNotes = objAPI.Query<Note>( strSOQL );
// if no notes came back, then we're finished
if( objNotes.Count == 0 )
return;
List<string> objIDList = new List<string>( 4000 );
string strLastParent = "";
foreach( Note objNote in objNotes )
// compare to the previous parent to see if note is duplicate
if( strLastParent.Equals( objNote.ParentId ) )
{
// add duplicate to the list
objIDList.Add( objNote.Id );
// every 4k rows, submit deletes
if( objIDList.Count > 3999 )
{
// delete duplicates
DeleteResult[] objDelResult = objAPI.Delete( objIDList.ToArray() );
// reset list
objIDList = new List<string>( 4000 );
}
}
else
strLastParent = objNote.ParentId;
// final deletes submission
if( objIDList.Count > 0 )
{
// delete duplicates
DeleteResult[] objDelResult = objAPI.Delete( objIDList.ToArray() );
}
}
public void LoadCredentialsReportedWithErrors()
{
// load Company credentials
DataTable objDT = objDB.GetDataTableFromSQLFile( "SQLAllCredentials.txt" );
// load SF agencies
string strSOQL =
"select Id, Name, SystemName_Agency_Match__c, Code__c, City__c from TableStoringAgenciesThatGiveCredentials order by Name";
List<Credential_Agency__c> objAgencies = objAPI.Query<Credential_Agency__c>( strSOQL );
// load SF providers (non-AMA)
List<Contact> objProviders = Company2SFUtils.GetProvidersFromSF( objAPI, lblError
, null, " RecruitingID__c != null ", " RecruitingID__c, PhysicianNumber__c DESC " );
List<Credential__c> objNewCredentials = new List<Credential__c>(); ;
string strFileName = string.Concat( strAppPath, "_CandidatePendingRows.txt" );
StreamReader objSR = new StreamReader( strFileName );
string strLine = "", strPreviousRecrID = "0", strPreviousAgency = "";
while( ( strLine = objSR.ReadLine() ) != null )
{
string[] strData = strLine.Split( '\t' );
if( strData.Count() < 2 )
continue;
if( strData[ 1 ].Contains( ".0 not found" ) )
{
// this should be a case of a duplicate provider that was removed
// so we need to FIND the provider by name and update the credential using the new recr id
// parse the recruiting id (example "Recr ID: 106532.0 not found")
string strRecrID = strData[ 1 ].Substring( 9 );
int iPos = strRecrID.IndexOf( ".0" );
strRecrID = strRecrID.Substring( 0, iPos );
// skip repeated recr IDs
if( strPreviousRecrID.Equals( strRecrID ) )
continue;
strPreviousRecrID = strRecrID;
// get provider name
string[] strName = strData[ 0 ].Split( "|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries );
if( strName.Count() < 2 )
{
ReportStatus( "Blank provider name: ", strData[0], strData[1] );
continue;
}
string strFirstName = strName[ 0 ];
string strLastName = strName[ 1 ];
// if provider not found, skip
Contact objProv = objProviders.FirstOrDefault( c => c.FirstName.Equals( strFirstName )
&& c.LastName.Equals( strLastName ) );
if( objProv == null )
{
ReportStatus( "Provider name not found: ", strFirstName, " ", strLastName );
continue;
}
// get the credentials for the recr ID
DataRow[] objRows = objDT.Select( "RecruitingID = " + strRecrID );
foreach( DataRow objDR in objRows )
{
Credential__c objCred = objDR.ConvertTo<Credential__c>();
string strCredName = string.Concat( objDR[ "FirstName" ].ToString(), " ", objDR[ "LastName" ].ToString()
, "-", objCred.Name );
//string strAgency = objCred.Credential_Agency__c;
//DataRow[] objAgencyFound = objDTAgencies.Select(
// string.Concat( "Company_Agency_Match__c = '", strAgency, "'" ) );
//if( objAgencyFound.Count() > 0 )
// if( !objAgencyFound[ 0 ][ "DuplicateOfCode" ].ToString().Equals( "" ) )
// strAgency = objAgencyFound[ 0 ][ "DuplicateOfCode" ].ToString();
//objCred.Credential_Agency__c = strAgency;
// set the external ids in the proper places
if( !objCred.Credential_Agency__c.Equals( "" ) )
{
objCred.Credential_Agency__r = new Credential_Agency__c();
objCred.Credential_Agency__r.Company_Agency_Match__c = objCred.Credential_Agency__c;
}
objCred.Credential_Agency__c = null;
if( !objCred.Credential_Sub_Type__c.Equals( "" ) )
{
objCred.Credential_Sub_Type__r = new Credential_Subtype__c();
objCred.Credential_Sub_Type__r.Company_SubType_Match__c = objCred.Credential_Sub_Type__c;
}
objCred.Credential_Sub_Type__c = null;
// use the recr id of the provider found with the same name
objCred.Contact__r = new Contact();
objCred.Contact__r.RecruitingID__c = objProv.RecruitingID__c;
objCred.Contact__r.RecruitingID__cSpecified = true;
// if name too long, reduce to just the credential number and truncate
if( strCredName.Length > 80 )
strCredName = objCred.Name;
objCred.Name = strCredName.Left( 80 );
objNewCredentials.Add( objCred );
// each 200 credentials, submit update to SalesForce
if( objNewCredentials.Count > 200 )
{
UpsertResult[] objResults = objAPI.Upsert( "Name", objNewCredentials.ToArray<sObject>() );
// create CSV file / set the Ids in the list of candidates
Company2SFUtils.ReportErrorsToHistoryFile( objResults, objNewCredentials );
objNewCredentials = new List<Credential__c>();
}
}
}
if( strData[ 1 ].Contains( "Tried to change to contact " ) )
{
// this should be a case of a provider with same name or an actual duplicate provider
// so we need to either CHANGE the credential name or
// FIND the right credential to update (the one attached to the contact that has physician nbr)
// parse first, last name - example: |Jonathan|Hart|-Certification NPDB
string[] strName = strData[ 0 ].Split( "| ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries );
if( strName.Count() < 2 )
{
ReportStatus( "Blank provider name: ", strData[ 0 ], strData[ 1 ] );
continue;
}
string strFirstName = strName[ 0 ];
string strLastName = strName[ 1 ];
// parse cert name/number
int iPos = strData[ 0 ].IndexOf( "-" );
string strCertName = strData[ 0 ].Substring( iPos + 1 );
if( strCertName.Length == 0 )
continue;
// parse contact id - example: Tried to change to contact 003E0000006g8a2IAA from 003E0000006VCEIIA4
string strContactId = strData[ 1 ].ParseFromTo( "contact ", " from " );
if( strContactId.Length != 18 )
continue;
// find the provider by the id
Contact objProv = objProviders.FirstOrDefault( c => c.Id.Equals( strContactId ) );
if( objProv == null )
continue;
// find the credential
DataRow[] objRows = objDT.Select( "Name = '" + strCertName
+ "' and FirstName = '" + strFirstName + "' and LastName = '" + strLastName + "'"
+ " and RecruitingID = " + objProv.RecruitingID__c );
foreach( DataRow objDR in objRows )
{
Credential__c objCred = objDR.ConvertTo<Credential__c>();
string strCredName = string.Concat( objDR[ "FirstName" ].ToString()
, " ", objDR[ "MiddleName" ].ToString()
, " ", objDR[ "LastName" ].ToString()
, "-", objDR[ "Credential_Type__c" ].ToString(), " / ", objCred.Name, " (2)" );
objCred.Name = strCredName;
// set the external ids in the proper places
if( !objCred.Credential_Agency__c.Equals( "" ) )
{
objCred.Credential_Agency__r = new Credential_Agency__c();
objCred.Credential_Agency__r.Company_Agency_Match__c = objCred.Credential_Agency__c;
}
objCred.Credential_Agency__c = null;
if( !objCred.Credential_Sub_Type__c.Equals( "" ) )
{
objCred.Credential_Sub_Type__r = new Credential_Subtype__c();
objCred.Credential_Sub_Type__r.Company_SubType_Match__c = objCred.Credential_Sub_Type__c;
}
objCred.Credential_Sub_Type__c = null;
// use the contact id parsed
objCred.Contact__c = strContactId;
// if name too long, reduce to just the credential number and truncate
if( strCredName.Length > 80 )
strCredName = objCred.Name;
objCred.Name = strCredName.Left( 80 );
objNewCredentials.Add( objCred );
// each 200 credentials, submit update to SalesForce
if( objNewCredentials.Count > 200 )
{
UpsertResult[] objResults = objAPI.Upsert( "Name", objNewCredentials.ToArray<sObject>() );
// create CSV file / set the Ids in the list of candidates
Company2SFUtils.ReportErrorsToHistoryFile( objResults, objNewCredentials );
objNewCredentials = new List<Credential__c>();
}
}
}
if( strData[ 1 ].Contains( "Missing agency" ) )
{
// this should be a case of a missing agency
// so we need to reload the credentials after these agencies are created
// parse the agency id (example: "Missing agency: board118")
string strAgencyID = strData[ 1 ].Substring( 16 );
//int iPos = strAgencyID.IndexOfAny( "0123456789".ToCharArray() );
//string strAgencyType = strAgencyID.Substring( 0, iPos );
//strAgencyID = strAgencyID.Substring( iPos, strAgencyID.Length - iPos );
// skip repeated Agency IDs
if( strPreviousAgency.Equals( strAgencyID ) )
continue;
strPreviousAgency = strAgencyID;
// note: license227 doesn't exist
// get the credentials for the recr ID
DataRow[] objRows = objDT.Select( "Credential_Agency__c = '" + strAgencyID + "'" );
foreach( DataRow objDR in objRows )
{
Credential__c objCred = objDR.ConvertTo<Credential__c>();
string strId = objDR[ "RecruitingID" ].ToString();
string strCredName = string.Concat( objDR[ "FirstName" ].ToString(), " ", objDR[ "LastName" ].ToString()
, "-", objCred.Name );
//string strAgency = objCred.Credential_Agency__c;
//DataRow[] objAgencyFound = objDTAgencies.Select(
// string.Concat( "Company_Agency_Match__c = '", strAgency, "'" ) );
//if( objAgencyFound.Count() > 0 )
// if( !objAgencyFound[ 0 ][ "DuplicateOfCode" ].ToString().Equals( "" ) )
// strAgency = objAgencyFound[ 0 ][ "DuplicateOfCode" ].ToString();
//objCred.Credential_Agency__c = strAgency;
// set the external ids in the proper places
if( !objCred.Credential_Agency__c.Equals( "" ) )
{
objCred.Credential_Agency__r = new Credential_Agency__c();
objCred.Credential_Agency__r.Company_Agency_Match__c = objCred.Credential_Agency__c;
}
objCred.Credential_Agency__c = null;
if( !objCred.Credential_Sub_Type__c.Equals( "" ) )
{
objCred.Credential_Sub_Type__r = new Credential_Subtype__c();
objCred.Credential_Sub_Type__r.Company_SubType_Match__c = objCred.Credential_Sub_Type__c;
}
objCred.Credential_Sub_Type__c = null;
// use the recr id of the provider found with the same name
objCred.Contact__r = new Contact();
objCred.Contact__r.RecruitingID__c = Convert.ToDouble( strId );
objCred.Contact__r.RecruitingID__cSpecified = true;
// if name too long, reduce to just the credential number and truncate
if( strCredName.Length > 80 )
strCredName = objCred.Name;
objCred.Name = strCredName.Left( 80 );
objNewCredentials.Add( objCred );
// each 200 credentials, submit update to SalesForce
if( objNewCredentials.Count > 200 )
{
UpsertResult[] objResults = objAPI.Upsert( "Name", objNewCredentials.ToArray<sObject>() );
// create CSV file / set the Ids in the list of candidates
Company2SFUtils.ReportErrorsToHistoryFile( objResults, objNewCredentials );
objNewCredentials = new List<Credential__c>();
}
}
}
}
objSR.Dispose();
// submit last update to SalesForce
if( objNewCredentials.Count > 0 )
{
UpsertResult[] objResults = objAPI.Upsert( "Name", objNewCredentials.ToArray<sObject>() );
// create CSV file / set the Ids in the list of candidates
Company2SFUtils.ReportErrorsToHistoryFile( objResults, objNewCredentials );
}
return;
}
public void LoadMissingCredentials()
{
// get list of agencies from Company free from duplicates
DataTable objDTAgencies = objDB.GetDataTableFromSQLFile( "SQLAllAgencies.txt" );
Company2SFUtils.FlagDupesByAddressCityName( objDTAgencies );
bool bContinueProcessing = true;
string strLastRecrId = "0", strCurrentRecrId = "0";
int iCount = 0;
while( bContinueProcessing )
{
string strCondition = string.Concat( " AND p.ID > ", strLastRecrId, " " );
DataTable objDT = objDB.GetDataTableFromSQLFile( "SQLAllCredentials.txt", null, strCondition );
if( !objDB.ErrorMessage.Equals( "" ) )
{
// interrupt process
ReportStatus( objDB.ErrorMessage );
bContinueProcessing = false;
break;
}
if( objDT.Rows.Count == 0 )
{
// interrupt process because there are no more rows
ReportStatus( "No more Job App rows found." );
bContinueProcessing = false;
break;
}
// get recr IDs to retrieve equivalent rows from SF
strLastRecrId = objDT.Rows[ objDT.Rows.Count -1 ][ "RecruitingID" ].ToString();
strCurrentRecrId = objDT.Rows[ 0 ][ "RecruitingID" ].ToString();
// retrieve equivalent credentials from SF
string strSOQL =
"select Id, Name, Contact__r.RecruitingID__c, Physician_Number__c, TableStoringAgencyGivingCredentials.Company_Agency_Match__c "
+ ", Credential_Number__c , Credential_Type__c, TableStoringSubTypesOfCredentials.Company_SubType_Match__c, State__c from Credential__c "
+ " where Contact__r.RecruitingID__c <= " + strLastRecrId
+ " and Contact__r.RecruitingID__c >= " + strCurrentRecrId
+ " order by Contact__r.RecruitingID__c, Name DESC ";
List<Credential__c> objCredentials = objAPI.Query<Credential__c>( strSOQL );
if( !objAPI.ErrorMessage.Equals( "" ) )
{
lblError.Text = objAPI.ErrorMessage;
return;
}
//if( objCredentials.Count == 0 )
// break;
iCount += objDT.Rows.Count;
ReportStatus( objDT.Rows.Count.ToString(), " Credential rows retrieved. Total ", iCount.ToString(), " processed.\r\n" );
List<Credential__c> objNewCredentials = new List<Credential__c>();
// skip existing credentials and add missing ones *******************
foreach( DataRow objDR in objDT.Rows )
{
Credential__c objCred = objDR.ConvertTo<Credential__c>();
string strId = objDR[ "RecruitingID" ].ToString();
string strAgency = objCred.Credential_Agency__c;
string strNbr = objCred.Credential_Number__c;
string strType = objCred.Credential_Type__c;
string strSubType = objCred.Credential_Sub_Type__c;
string strState = objCred.State__c;
string strName = string.Concat( objDR[ "FirstName" ].ToString(), " ", objDR[ "LastName" ].ToString()
, "-", objCred.Name );
// prepend name to the credential record name
//objCred.Name = strName;
// find the non-duplicate equivalent of the agency
// (example: License105 should become 240 Medical Board of CA)
DataRow[] objAgencyFound = objDTAgencies.Select(
string.Concat( "Company_Agency_Match__c = '", strAgency, "'" ) );
if( objAgencyFound.Count() > 0 )
if( ! objAgencyFound[ 0 ][ "DuplicateOfCode" ].ToString().Equals( "" ) )
strAgency = objAgencyFound[ 0 ][ "DuplicateOfCode" ].ToString();
objCred.Credential_Agency__c = strAgency;
// if credential exists, skip it
Credential__c objFound = objCredentials.FirstOrDefault( c =>
c.Contact__r.RecruitingID__c.ToString().NotNullAndEquals( strId )
&& c.Credential_Type__c.NotNullAndEquals( strType )
&& ( c.Credential_Number__c != null ?
c.Credential_Number__c.NotNullAndEquals( strNbr )
: strNbr.Equals( "" )
)
&& ( c.State__c != null ?
c.State__c.NotNullAndEquals( strState )
: strState.Equals( "" )
)
&& ( c.Credential_Agency__r != null ?
c.Credential_Agency__r.Company_Agency_Match__c.NotNullAndEquals( strAgency )
: strAgency.Equals( "" )
)
&& ( c.Credential_Sub_Type__r != null ?
c.Credential_Sub_Type__r.Company_SubType_Match__c.NotNullAndEquals( strSubType )
: strSubType.Equals( "" )
)
);
// if found, skip
if( objFound != null )
continue;
// check whether the name of the credential has already been used (example: licenses for recr Id 22497)
objFound = objNewCredentials.FirstOrDefault( c => c.Name.Equals( strName ) );
if( objFound != null )
{
// try adding the subtype (it may be different subtype with same nbr - example: recr Id 22497)
if( !objCred.Credential_Sub_Type__c.Equals( "" ) )
{
strName = string.Concat( objDR[ "FirstName" ].ToString()
, " ", objDR[ "LastName" ].ToString()
, "-", objCred.Name, "/", objCred.Credential_Sub_Type__c );
objFound = objNewCredentials.FirstOrDefault( c => c.Name.Equals( strName ) );
}
// if available, insert middle name to avoid duplicates
if( objFound != null && ! objDR[ "MiddleName" ].IsNullOrBlank() )
{
strName = string.Concat( objDR[ "FirstName" ].ToString(), " ", objDR[ "MiddleName" ].ToString()
, " ", objDR[ "LastName" ].ToString()
, "-", objCred.Name );
objFound = objNewCredentials.FirstOrDefault( c => c.Name.Equals( strName ) );
}
// if name is still duplicate, add year or " #2"
if( objFound != null )
{
if( objCred.From__c != null )
strName = string.Concat( strName, " - "
, ( (DateTime) objCred.From__c ).Year.ToString() );
else
strName = string.Concat( strName, " #2" );
}
}
// set the external ids in the proper places
if( ! objCred.Credential_Agency__c.Equals( "" ) )
{
objCred.Credential_Agency__r = new Credential_Agency__c();
objCred.Credential_Agency__r.Company_Agency_Match__c = objCred.Credential_Agency__c;
}
objCred.Credential_Agency__c = null;
if( ! objCred.Credential_Sub_Type__c.Equals( "" ) )
{
objCred.Credential_Sub_Type__r = new Credential_Subtype__c();
objCred.Credential_Sub_Type__r.Company_SubType_Match__c = objCred.Credential_Sub_Type__c;
}
objCred.Credential_Sub_Type__c = null;
objCred.Contact__r = new Contact();
objCred.Contact__r.RecruitingID__c = Convert.ToDouble( strId );
objCred.Contact__r.RecruitingID__cSpecified = true;
// if name too long, reduce to just the credential number and truncate
if( strName.Length > 80 )
strName = objCred.Name;
objCred.Name = strName.Left( 80 );
// if credential doesn't exist, add it to the list for upsertion
objNewCredentials.Add( objCred );
}
// upsert missing credentials
//SaveResult[] objUpdResult = objAPI.Insert( objNewCredentials.ToArray() );
UpsertResult[] objResults = objAPI.Upsert( "Name", objNewCredentials.ToArray<sObject>() );
// create CSV file / set the Ids in the list of candidates
Company2SFUtils.ReportErrorsToHistoryFile( objResults, objNewCredentials );
// after the last one was processed, stop
if( objCredentials.Count == 1 )
bContinueProcessing = false;
}
return;
}
public void CleanCandidates()
{
int iCount = 0, iUpdCount = 0;
while( iCount > 0 )
{
List<Candidate__c> objCandies = objAPI.Query<Candidate__c>(
"select Id, Name, OwnerId, Contact__c, Facility_Name__c, Contact__r.RecruitingID__c from Candidate__c "
+ " order by Contact__c, Facility_Name__c" );
if( !objAPI.ErrorMessage.Equals( "" ) )
{
lblError.Text = objAPI.ErrorMessage;
return;
}
if( objCandies.Count == 0 )
return;
iCount = 0;
iUpdCount = 0;
List<string> strIds = new List<string>( 2000 );
List<Candidate__c> objCandies4Upd = new List<Candidate__c>( 2000 );
string strLastKey = "";
Candidate__c objPreviousCandidate = null;
foreach( Candidate__c objCandidate in objCandies )
{
if( iCount == 2000 )
{
// if array is full, submit the deletes and reset the array
DeleteResult[] objDelResult = objAPI.Delete( strIds.ToArray() );
iCount = 0;
strIds = new List<string>( 2000 );
}
if( iUpdCount == 2000 )
{
// if array is full, submit the updates and reset the array
SaveResult[] objSaveResult = objAPI.Update( objCandies4Upd.ToArray() );
iUpdCount = 0;
objCandies4Upd = new List<Candidate__c>( 2000 );
}
// check whether this is a duplicate (when contact and facility are the same as previous)
if( strLastKey.Equals( objCandidate.Contact__c + objCandidate.Facility_Name__c ) )
{
// decide whether to delete the current record or the previous
if( objCandidate.OwnerId.Equals( "005E0000000hMcKIAU" ) ) // "005E0000000hMcKIAU" = Batch Load User
{
// add Candidate to the list to delete
strIds.Add( objCandidate.Id );
iCount++;
// correct the name of previous Candidate record
Candidate__c objCandCopy = new Candidate__c();
objCandCopy.Id = objPreviousCandidate.Id;
objCandCopy.Name = objCandidate.Name;
objCandies4Upd.Add( objCandCopy );
iUpdCount++;
continue;
}
}
strLastKey = objCandidate.Contact__c + objCandidate.Facility_Name__c;
objPreviousCandidate = objCandidate;
}
if( iCount > 0 )
{
// submit last batch of deletes
DeleteResult[] objDelResult = objAPI.Delete( strIds.ToArray() );
}
if( iUpdCount > 0 )
{
// if array is full, submit the updates and reset the array
SaveResult[] objSaveResult = objAPI.Update( objCandies4Upd.ToArray() );
}
}
}
public void CleanJobAppsByRecrID()
{
bool bContinueProcessing = true;
string strLastRecrId = "0";
while( bContinueProcessing )
{
List<Job_Application__c> objJobApps = objAPI.Query<Job_Application__c>(
"select Id, Name, Contact__r.FirstName, Contact__r.LastName, Contact__r.RecruitingID__c, Contact__r.Degree__c, LastModifiedDate "
+ "from Job_Application__c "
+ "where Contact__r.RecruitingID__c != null and Contact__r.RecruitingID__c >= " + strLastRecrId
+ " order by Contact__r.RecruitingID__c, LastModifiedDate DESC, Name DESC LIMIT 2000 " );
// where Name like 'a0iE0%'
if( !objAPI.ErrorMessage.Equals( "" ) )
{
lblError.Text = objAPI.ErrorMessage;
return;
}
if( objJobApps.Count == 0 )
break;
List<string> strIds = new List<string>( 2000 );
List<Job_Application__c> objJAForUpdate = new List<Job_Application__c>( 2000 );
DateTime dtCutOff = Convert.ToDateTime( "9/27/2011" );
Job_Application__c objPrevious = null;
foreach( Job_Application__c objJA in objJobApps )
{
// if record is older than 9/27/11, check if it is a duplicate
DateTime dtModified = (DateTime) objJA.LastModifiedDate;
if( dtModified.CompareTo( dtCutOff ) < 0 )
{
// if it is a duplicate, collect for removal
if( objJA.Contact__r.RecruitingID__c.ToString().Equals( strLastRecrId ) )
{
// add Job App to the list to delete
strIds.Add( objJA.Id );
// correct the Provider Name & Degree on the previous record
objPrevious.Provider_Name_Degree__c =
string.Concat( objJA.Contact__r.LastName, ", ", objJA.Contact__r.FirstName
, ", ", objJA.Contact__r.Degree__c );
// make a copy of previous record for the update
// because SalesForce doesn't accept more than one reference in Contact__r f/update
Job_Application__c objJACopy = new Job_Application__c();
objJACopy.Id = objPrevious.Id;
objJACopy.Provider_Name_Degree__c = objPrevious.Provider_Name_Degree__c;
objJAForUpdate.Add( objJACopy );
continue;
}
}
#region Previous Clean up
//// previous clean up
//// if it is a duplicate that starts with a0iE0, collect for removal
//if( !objJA.Name.StartsWith( "Job App" ) )
//{
// if( objJA.Contact__r.RecruitingID__c.ToString().Equals( strLastRecrId ) )
// {
// // add Job App to the list to delete
// strIds.Add( objJA.Id );
// continue;
// }
// // it is not a duplicate, so we correct the name
// objJA.Name = string.Concat( "Job Application for ", objJA.Contact__r.FirstName, " ", objJA.Contact__r.LastName );
// // make a copy of object because SalesForce doesn't accept more than one reference in Contact__r f/update
// Job_Application__c objJACopy = new Job_Application__c();
// objJACopy.Id = objJA.Id;
// objJACopy.Name = objJA.Name;
// objJAForUpdate.Add( objJACopy );
//}
#endregion
strLastRecrId = objJA.Contact__r.RecruitingID__c.ToString();
objPrevious = objJA;
}
DeleteResult[] objDelResult = objAPI.Delete( strIds.ToArray() );
SaveResult[] objUpdResult = objAPI.Update( objJAForUpdate.ToArray() );
// after the last one was processed, stop
if( objJobApps.Count == 1 )
bContinueProcessing = false;
}
return;
}
public void CleanJobAppsByMENbr()
{
ReportStatus( "Starting Job App clean up." );
bool bContinueProcessing = true;
int iCount = 0;
string strLastMENumber = "";
while( bContinueProcessing )