@@ -28,7 +28,10 @@ public void MapContactFormData( IQueryable<Row> tableData, long totalRows = 0 )
2828 var importedCommunicationCount = new CommunicationService ( lookupContext ) . Queryable ( ) . Count ( c => c . ForeignKey != null ) ;
2929 var importedNoteCount = new NoteService ( lookupContext ) . Queryable ( ) . Count ( n => n . ForeignKey != null ) ;
3030
31+ var prayerRequestors = new Dictionary < int , Person > ( ) ;
32+
3133 var communicationList = new List < Communication > ( ) ;
34+ var prayerList = new List < PrayerRequest > ( ) ;
3235 var noteList = new List < Note > ( ) ;
3336
3437 if ( totalRows == 0 )
@@ -49,7 +52,9 @@ public void MapContactFormData( IQueryable<Row> tableData, long totalRows = 0 )
4952 var individualId = row [ "ContactIndividualID" ] as int ? ;
5053 var createdDate = row [ "ContactActivityDate" ] as DateTime ? ;
5154 var modifiedDate = row [ "ContactDatetime" ] as DateTime ? ;
55+ var approvalDate = row [ "ContactFormLastUpdatedDate" ] as DateTime ? ;
5256 var itemType = row [ "ContactFormName" ] as string ;
57+ var itemStatus = row [ "ContactStatus" ] as string ;
5358 var itemCaption = row [ "ContactItemName" ] as string ;
5459 var noteText1 = row [ "ContactNote" ] as string ;
5560 var noteText2 = row [ "ContactItemNote" ] as string ;
@@ -87,9 +92,27 @@ public void MapContactFormData( IQueryable<Row> tableData, long totalRows = 0 )
8792
8893 communicationList . Add ( communication ) ;
8994 }
95+ else if ( ! string . IsNullOrWhiteSpace ( itemCaption ) && itemCaption . EndsWith ( "Prayer Request" , StringComparison . CurrentCultureIgnoreCase ) )
96+ {
97+ // create a prayer request
98+ Person requestor = null ;
99+ prayerRequestors . TryGetValue ( personKeys . PersonId , out requestor ) ;
100+ if ( requestor == null )
101+ {
102+ requestor = lookupContext . People . FirstOrDefault ( p => p . Id . Equals ( personKeys . PersonId ) ) ;
103+ prayerRequestors . Add ( personKeys . PersonId , requestor ) ;
104+ }
105+
106+ var request = AddPrayerRequest ( lookupContext , null , personKeys . PersonAliasId , requestor . FirstName , requestor . LastName , requestor . Email , itemText ?? itemCaption , string . Empty ,
107+ ! itemStatus . Equals ( "Closed" , StringComparison . CurrentCultureIgnoreCase ) , false , createdDate ?? modifiedDate , approvalDate , itemForeignKey . ToString ( ) , userPersonAliasId ) ;
108+ if ( request != null )
109+ {
110+ prayerList . Add ( request ) ;
111+ }
112+ }
90113 else
91114 {
92- //strip campus from note type
115+ //strip campus from type
93116 var campusId = GetCampusId ( itemType ) ;
94117 if ( campusId . HasValue )
95118 {
@@ -113,10 +136,12 @@ public void MapContactFormData( IQueryable<Row> tableData, long totalRows = 0 )
113136 else if ( completedItems % ReportingNumber < 1 )
114137 {
115138 SaveCommunications ( communicationList ) ;
139+ SavePrayerRequests ( prayerList ) ;
116140 SaveNotes ( noteList ) ;
117141 ReportPartialProgress ( ) ;
118142
119143 communicationList . Clear ( ) ;
144+ prayerList . Clear ( ) ;
120145 noteList . Clear ( ) ;
121146 }
122147 }
@@ -125,6 +150,7 @@ public void MapContactFormData( IQueryable<Row> tableData, long totalRows = 0 )
125150 if ( communicationList . Any ( ) || noteList . Any ( ) )
126151 {
127152 SaveCommunications ( communicationList ) ;
153+ SavePrayerRequests ( prayerList ) ;
128154 SaveNotes ( noteList ) ;
129155 }
130156
@@ -142,6 +168,8 @@ public void MapIndividualContactNotes( IQueryable<Row> tableData, long totalRows
142168
143169 var importedNotes = new NoteService ( lookupContext ) . Queryable ( ) . Where ( n => n . ForeignId != null )
144170 . ToDictionary ( n => n . ForeignId , n => n . Id ) ;
171+ var importedRequests = new PrayerRequestService ( lookupContext ) . Queryable ( ) . Where ( r => r . ForeignId != null )
172+ . ToDictionary ( r => r . ForeignId , r => r . Id ) ;
145173
146174 var noteList = new List < Note > ( ) ;
147175 int ? confidentialNoteTypeId = null ;
@@ -174,26 +202,38 @@ public void MapIndividualContactNotes( IQueryable<Row> tableData, long totalRows
174202 }
175203
176204 var noteId = 0 ;
177- if ( importedNotes . ContainsKey ( itemForeignKey ) )
178- {
179- noteId = importedNotes [ itemForeignKey ] ;
180- }
205+ var noteEntityId = personKeys . PersonId ;
206+ var noteEntityTypeId = PersonEntityTypeId ;
207+ var noteTypeId = PersonalNoteTypeId ;
181208
182209 // add a confidential note
183210 if ( ! string . IsNullOrWhiteSpace ( confidentialText ) )
184211 {
185- var confidential = AddEntityNote ( lookupContext , PersonEntityTypeId , personKeys . PersonId , string . Empty , confidentialText , false , false ,
212+ var confidential = AddEntityNote ( lookupContext , noteEntityTypeId , noteEntityId , string . Empty , confidentialText , false , false ,
186213 "Confidential Note" , confidentialNoteTypeId , false , createdDate , itemForeignKey . ToString ( ) ) ;
187214 confidentialNoteTypeId = confidential . NoteTypeId ;
188215
189216 noteList . Add ( confidential ) ;
190217 }
191218
192- // add a normal note
219+ // this is new or an update to timeline note
220+ if ( importedNotes . ContainsKey ( itemForeignKey ) )
221+ {
222+ noteId = importedNotes [ itemForeignKey ] ;
223+ }
224+ // note this as a prayer request comment
225+ else if ( importedRequests . ContainsKey ( itemForeignKey ) )
226+ {
227+ noteEntityTypeId = PrayerRequestTypeId ;
228+ noteEntityId = importedRequests [ itemForeignKey ] ;
229+ noteTypeId = PrayerNoteTypeId ;
230+ }
231+
232+ // add the note text
193233 if ( ! string . IsNullOrWhiteSpace ( noteText ) )
194234 {
195- var note = AddEntityNote ( lookupContext , PersonEntityTypeId , personKeys . PersonId , string . Empty , noteText , false , false ,
196- null , PersonalNoteTypeId , false , createdDate , itemForeignKey . ToString ( ) ) ;
235+ var note = AddEntityNote ( lookupContext , noteEntityTypeId , noteEntityId , string . Empty , noteText , false , false ,
236+ null , noteTypeId , false , createdDate , itemForeignKey . ToString ( ) ) ;
197237 note . Id = noteId ;
198238
199239 noteList . Add ( note ) ;
@@ -309,12 +349,32 @@ public void MapNotes( IQueryable<Row> tableData, long totalRows = 0 )
309349 /// <param name="communicationList">The communication list.</param>
310350 private static void SaveCommunications ( List < Communication > communicationList )
311351 {
312- var rockContext = new RockContext ( ) ;
313- rockContext . WrapTransaction ( ( ) =>
352+ if ( communicationList . Count > 0 )
314353 {
315- rockContext . Communications . AddRange ( communicationList ) ;
316- rockContext . SaveChanges ( DisableAuditing ) ;
317- } ) ;
354+ var rockContext = new RockContext ( ) ;
355+ rockContext . WrapTransaction ( ( ) =>
356+ {
357+ rockContext . Communications . AddRange ( communicationList ) ;
358+ rockContext . SaveChanges ( DisableAuditing ) ;
359+ } ) ;
360+ }
361+ }
362+
363+ /// <summary>
364+ /// Saves the prayer requests.
365+ /// </summary>
366+ /// <param name="prayerList">The prayer list.</param>
367+ private static void SavePrayerRequests ( List < PrayerRequest > prayerList )
368+ {
369+ if ( prayerList . Count > 0 )
370+ {
371+ var rockContext = new RockContext ( ) ;
372+ rockContext . WrapTransaction ( ( ) =>
373+ {
374+ rockContext . PrayerRequests . AddRange ( prayerList ) ;
375+ rockContext . SaveChanges ( DisableAuditing ) ;
376+ } ) ;
377+ }
318378 }
319379
320380 /// <summary>
@@ -323,25 +383,28 @@ private static void SaveCommunications( List<Communication> communicationList )
323383 /// <param name="noteList">The note list.</param>
324384 private static void SaveNotes ( List < Note > noteList )
325385 {
326- var rockContext = new RockContext ( ) ;
327- rockContext . WrapTransaction ( ( ) =>
386+ if ( noteList . Count > 0 )
328387 {
329- rockContext . Configuration . AutoDetectChangesEnabled = false ;
330- rockContext . Notes . AddRange ( noteList . Where ( n => n . Id == 0 ) ) ;
331-
332- foreach ( var note in noteList . Where ( n => n . Id > 0 ) )
388+ var rockContext = new RockContext ( ) ;
389+ rockContext . WrapTransaction ( ( ) =>
333390 {
334- var existingNote = rockContext . Notes . FirstOrDefault ( n => n . Id == note . Id ) ;
335- if ( existingNote != null )
391+ rockContext . Configuration . AutoDetectChangesEnabled = false ;
392+ rockContext . Notes . AddRange ( noteList . Where ( n => n . Id == 0 ) ) ;
393+
394+ foreach ( var note in noteList . Where ( n => n . Id > 0 ) )
336395 {
337- existingNote . Text += note . Text ;
338- rockContext . Entry ( existingNote ) . State = EntityState . Modified ;
396+ var existingNote = rockContext . Notes . FirstOrDefault ( n => n . Id == note . Id ) ;
397+ if ( existingNote != null )
398+ {
399+ existingNote . Text += note . Text ;
400+ rockContext . Entry ( existingNote ) . State = EntityState . Modified ;
401+ }
339402 }
340- }
341403
342- rockContext . ChangeTracker . DetectChanges ( ) ;
343- rockContext . SaveChanges ( DisableAuditing ) ;
344- } ) ;
404+ rockContext . ChangeTracker . DetectChanges ( ) ;
405+ rockContext . SaveChanges ( DisableAuditing ) ;
406+ } ) ;
407+ }
345408 }
346409 }
347410}
0 commit comments