-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatterArchiveManager
1369 lines (1221 loc) · 82.9 KB
/
ChatterArchiveManager
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
//
// (c) 2009 Appirio, Inc.
//
// Zantaz Phase2 ChatterArchiveManager
//
//
// 5/25/2011 Parth Tamhaney Original
// 06/10/2011 Parth Tamhaney Changes to include UserStatus type to in retrieving existing feed (line 145)
// Add if clause to get document info, only if its content post (line 583)
// 06/14/2011 Parth Tamhaney Fixed a bug related to ContentDocumentId not being set correctly
// Changed arguments in iArchiveBuilder.setContentDocumentFields() to (Sobject rdmFeed ,RFC2822_Archive__c rfc2822Arch, Map<string,ContentDocumentLink> mapContentLinks,string hostURL);
// Updated method getContentDetails() to return the ContentDocumentLink object instead of a map
// 07/06/2011 Dan - Updated the separator in the "To" field from a comma to a semicolon.
// 07/12/2011 Parth Set Archive_Status__c = STATUS_READY when a RFC Archive Record is updated
// Methods updated: update_RFC2822_Archive_For_Posts( Ln#: 504), update_RFC2822_Archive_For_Comments(Ln#: 742)
//07/12/2011 Parth Set feed id on RFC Archive
// Method create_RFC2822_Archive() line# 609
// Changed return type of iArchiveBuilder.getExistingRFC2822_Archives() from List<RFC2822_Archive__c> to Map<String,RFC2822_Archive__c>
//07/20/2011 Parth Updated Feed Selection query as per v22.
// Removed use of FeedPost and ContentDocumentLink.
// Removed use of Map<string,ContentDocumentLink> mapContentLinks
//07/26/2011 Parth Added future method public static void ArchiveFeedTracking_future() to Archive Tracked Changes feed
// Also added methods getParentSobjectFields() to get the Field metadata
// Updated Method getChatterPostDiv() to add details of the fields updated
//08/03/2011 Parth Updated with ZantazPhase3 Code
//09/22/2011 Manisha Gupta Removed ContentData from feed query (ln# 137)
//11/15/2011 Manisha Gupta changed rfc archive body for campaign. line (line 100,239-256,856-857)
//11/18/2011 Manisha Gupta changed rfc archive body for campaign summary(line 257-260)
// added a new field Campaign Summary in query.(line 245 )
//12/15/2011 : Manisha Gupta : Test Failure fix Line 183
//17 Feb 2012 RA: Zantaz File Enhancement
//10 April 2013 Dinesh Moundekar CRMGPS-3827: Inserted code in initialize_RFC2822_Archive() to capture group Name(Line No 778, 779, 780).
//22 Aug 2013: Aditya Talwar: QC - 1870 Chatter Archival: Group name is not displaying in the outlook email box after local java agent is run for chatter post and comments
//05 Sep 2013: Dinesh M: CRMGPS-4275: Zantaz Implementation - two things missing
//25 Feb 2014: Dinesh M: QC2448, Zantaz-Chatter: Files uploaded in comment section, deleted from comment section do not have the group name associated in outlook mail box
//Jira 4350 : Vipin Makhija : Methods added to create Archieve records for file version change and File deletion as part of ChatterArchiveManager_Batch bach decommisioning
//// VM : QC 2890 changed getChatterPostDiv method to get to fetch the correct file name , replaced contentfilename with title
public without sharing class ChatterArchiveManager {
public interface iArchiveBuilder {
// Method returns list of Parent Fields
String getParentFields();
/*
* This method returns the div tag for primaryMaterial
*/
String getHTMLPrimaryMaterialDivTag(sObject parent);
/*
* This method creates and returns the Subject for the Archive
*/
String getArchiveSubject(sObject parent,sObject createdBy);
/**
* This method return the From field for Chatter Archive
*
**/
String getFromAddress(sObject parent, sObject createdBy );
/**
* This method return the To fields for the RFC Archive
*
**/
String getToAddress(sObject parent, sObject createdBy );
/**
* This method returns the List of existing RFC Archives by matching the source ids
* For General Chatter the Source id is matched with the Post Id
* For RDM Chatter the Source id is the RDM id
*
**/
List<RFC2822_Archive__c>getExistingRFC2822_Archives(Set<String> parentIds , sObject createdBy);
/**
* This method return the Source Id for Chatter Archive
*
**/
string getSourceID(sObject rdmFeed);
/**
* This Method returns the list of Parent Ids
* Parent Ids can be the Post Ids or the Object Ids for which the Chatter is happening
*
*/
Set<String> getParentIds(Set<String> parentIds, Set<String> parentPostIds) ;
/**
* Creates the To Address for the Post
*/
String getFirstToAddressForPost(string strFrom, sObject createdBy,sObject parent );
/**
* This method gets the Existing RFC archive
*/
RFC2822_Archive__c getExistingArchive( Map<String,RFC2822_Archive__c> mapRFC2822_Archive, string parentId, string feedItemId);
/**
* This Method Fills the Content data in the RFC Archive
*
*/
RFC2822_Archive__c setContentDocumentFields(Sobject rdmFeed ,RFC2822_Archive__c rfc2822Arch,string hostURL);
RFC2822_Archive__c setRFCArchiveDocumentFields(RFC2822_Archive__c rfc2822Arch,string filename ,Id contentDocumentId,string hostURL,string mimeType);
}
// END INTERFACE
public static String STATUS_READY = 'Ready';
public ChatterArchiveManager.iArchiveBuilder archiveBuilder;
// 11/15/2011 Manisha Gupta : addded to add campaign details in archive body.
public static String objId ;
public static String oldDesc ;
public static String newDesc ;
public static String oldSumm;
public static String newSumm;
public static boolean st_forInsert = false;
public ChatterArchiveManager(ChatterArchiveManager.iArchiveBuilder builder){
archiveBuilder = builder;
}
public static String MESSAGE_HOST_ADDRESS = getHostIP();// '@cs3.salesforce.com';
public static string getHostURL(){ //https://cs3.salesforce.com
return URL.getSalesforceBaseUrl().toExternalForm();
}
public static string getHostIP(){ // @cs3.salesforce.com
return '@' + URL.getSalesforceBaseUrl().toExternalForm().split('//')[1] ;
}
//This method encodes stringto Base 64
public static string base64Encode(string strData){
if(strData==null){
return '';
}
//return EncodingUtil.base64Encode(Blob.valueOf(EncodingUtil.urlEncode( strData,'UTF-8')));
//Just base64Encode. No need to do URLEncode here.
return EncodingUtil.base64Encode(Blob.valueOf(strData));
}
//This method encodes the RFC2822_Archive__c Body_Plain_Text__c and sets it into Body__c
public static void EncodeData(RFC2822_Archive__c rfc2822Arch){
rfc2822Arch.Body__c = rfc2822Arch.Body_Plain_Text__c;
if(rfc2822Arch.Content_Transfer_Encoding__c == 'base64' ){
rfc2822Arch.Body__c = base64Encode(rfc2822Arch.Body_Plain_Text__c);
}
}
// Method returns the Object name of the Feed object for any object
public static string getFeedSobjectName(string ParentSobjectName){
if(ParentSobjectName.endsWith('__c')){
return ParentSobjectName.substring(0,ParentSobjectName.length()-1) + 'Feed';// 'Research_Document_Metadata__Feed'
}else{
return ParentSobjectName+ 'Feed';// 'UserFeed'
}
}
public static string getFeedQuery(Set<Id> objectIds,string ParentSobjectName, boolean fromTriggerUpdate,ChatterArchiveManager.iArchiveBuilder archiveBuilder){
String feedObjectName = getFeedSobjectName(ParentSobjectName);
system.debug('_____feedObjectName: '+ feedObjectName+'___________ getFeeds_______ parentObjectIds-->'+ objectIds);
string whereCaluse = ' where Id in :objectIds and CreatedById != null ' ;
if(fromTriggerUpdate || (st_forInsert ==true)){
whereCaluse= ' where ParentId in :objectIds ';
}
String query ='SELECT Id, Type, CreatedBy.FirstName, CreatedBy.LastName, CreatedBy.Name,CreatedBy.email, ' +
' Body, Title,CreatedDate, ContentDescription, ContentFileName, LinkUrl, ContentSize, ContentType,RelatedRecordId, ';
query = query + archiveBuilder.getParentFields() ; //getParentFields(ParentSobjectName) ;
query = query + ' (SELECT Id, CommentBody, CreatedDate, CreatedById,CreatedBy.Name, CreatedBy.LastName, CreatedBy.FirstName,CreatedBy.email ' +
' ,CommentType, RelatedRecordId ' + // Added on 16 Feb 2012 RA: Zantaz File Enhancement
' FROM FeedComments ORDER BY CreatedDate Asc ), ' +
' (Select Id, FeedItemId, FieldName, OldValue, NewValue From FeedTrackedChanges ) ' +
' FROM ' + feedObjectName +
whereCaluse + //' where Id in :feedItemIds and CreatedById != null ' +
' Order BY CreatedDate desc ' ;
if(fromTriggerUpdate && (st_forInsert ==false)){
query = query + ' Limit 1';
}
if(ParentSobjectName == 'ContentDocument'){
query = query.replace('Parent.Name,', '');
}
return query;
}
/*
* This method takes the list of ids of feedItems and Parent Object name .
* Returns the list of feeds (List<Sobject>)
*
*/
public static List<Sobject> getFeeds(Set<Id> objectIds,string ParentSobjectName, boolean fromTriggerUpdate,ChatterArchiveManager.iArchiveBuilder archiveBuilder){
string query = getFeedQuery(objectIds,ParentSobjectName,fromTriggerUpdate,archiveBuilder);
List<Sobject> feedSobjectList = new List<Sobject>();
feedSobjectList = Database.query(query);
//Check if The operation is called from Update trigger and the feed is for Update
if(fromTriggerUpdate && (st_forInsert ==false)){
system.debug('________________inside for update');
List<sObject> lstObj =Database.query('Select CreatedDate from ' + ParentSobjectName +' where id in :objectIds ' );
// 12/15/2011 : Manisha Gupta : Test Failure fix
if(lstObj != null && lstObj.size() > 0 && feedSobjectList.size() > 0 ) {
if((((DateTime)feedSobjectList[0].get('CreatedDate')).getTime() - ((DateTime)lstObj[0].get('CreatedDate')).getTime())< 2000 ){
return new List<Sobject>();
}
}
}
return feedSobjectList;
}
// This Method identifies the Sobject type from the parent Id and return the Sobject Name
public static string getParentSobjectTypeName(String feedParentItmId){
Map<String, String> keyPrefixMap = new Map<String, String>{};
Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
string ParentSobjectTypeName ='';
Set<String> keyPrefixSet = gd.keySet();
for(String sObj : keyPrefixSet){
Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe();
String tempName = r.getName();
String tempPrefix = r.getKeyPrefix();
if(tempPrefix<> null && feedParentItmId.startsWith(tempPrefix)){
ParentSobjectTypeName= tempName;
break;
}}
return ParentSobjectTypeName;
}
public static List<Schema.SObjectField> getSobjectFields(String sObjectName){
List<Schema.SObjectField> fields ;
Map<String, String> keyPrefixMap = new Map<String, String>{};
Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
Set<String> keyPrefixSet = gd.keySet();
for(String sObj : keyPrefixSet){
Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe();
String tempName = r.getName();
String tempPrefix = r.getKeyPrefix();
if(tempName == sObjectName){
fields = r.fields.getMap().values();
break;
} }
return fields; }
public static List<Schema.SObjectField> getParentSobjectFields(String feedParentItmId){
List<Schema.SObjectField> fields ;
Map<String, String> keyPrefixMap = new Map<String, String>{};
Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
Set<String> keyPrefixSet = gd.keySet();
for(String sObj : keyPrefixSet){
Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe();
String tempName = r.getName();
String tempPrefix = r.getKeyPrefix();
if(tempPrefix<> null && feedParentItmId.startsWith(tempPrefix)){
fields = r.fields.getMap().values();
break;
} }
return fields; }
// This Method returns the Existing RFC2822_Archive__c
private RFC2822_Archive__c getExistingRFC2822_Archive(Id recordId ){
List<RFC2822_Archive__c> lstTodaysArchives =[Select r.To__c, r.Subject__c, r.SourceID__c, r.Name, r.Message_ID__c, r.MIME_Version__c, r.Id, r.ContentDocumentTitle__c,r.ContentDocumentType__c,r.ContentDocumentName__c,r.From__c, r.Content_Type__c, r.Content_Transfer_Encoding__c, r.Body__c, r.Body_Plain_Text__c, r.Archive_Date__c,Index__c From RFC2822_Archive__c r where id = :recordId limit 1];
return lstTodaysArchives[0];
}
// This method returns the style tag
public static string getHTMLStyleTag(){
return '<style type="text/css">\n' + ' div {padding-left:10px; padding-bottom:5px;}\n' + ' span {padding-right:5px;}\n' + ' .deleted {font-weight:bold;}\n' + '</style>\n';
}
// This method returns the div tag for message id
public static string getHTMLMessageDivTag(string SourceId){
return ' <div id="sourceID">Source Record ID: '+ SourceId + '</div>\n' ;
}
// This method returns the div tag for a Chatter Post
public static string getChatterPostDiv(sObject rdmFeed, string DeleteInfo, string ParentSobjectName,RFC2822_Archive__c rfc2822Arch){
system.debug('InsideThis***');
sObject createdBy = rdmFeed.getSObject('CreatedBy');
string body = '';
/*string divBody =' <div id="' + rdmFeed.get('id') + '" class="chatterPost"> \n' + DeleteInfo + rdmFeed.get('CreatedDate') +' ' + createdBy.get('Name') + ' - @body@\n' + ' </div>\n' ;
if(rdmFeed.get('Body')<> null){
body = (string)rdmFeed.get('Body') + '\n' ;
}*/
string divBody =' <div id="' + rdmFeed.get('id') + '" class="chatterPost"> \n' + DeleteInfo + rdmFeed.get('CreatedDate') +' '+ createdBy.get('Name') ;
//5 Sep 2013: Dinesh M: CRMGPS-4275:Added below 7 Line of code.
String feedObjectName = rdmFeed.getSObjectType().getDescribe().getName();
if(feedObjectName.equalsIgnoreCase('UserFeed')){
if((string)rdmFeed.get('parentId') != UserInfo.getUserId()){
string toUserName = String.ValueOf(rdmFeed.getSObject('Parent').get('Name'));
divBody += ' To '+toUserName;
}
}
divBody += ' - @body@\n' + ' </div>\n' ;
if(rdmFeed.get('Body')<> null){
body = (string)rdmFeed.get('Body') + '\n' ;
}
if(rdmFeed.get('Type') == 'TrackedChange'){
body = ChatterArchiveManager.getFeedPostBody( rdmFeed, DeleteInfo, ParentSobjectName, rfc2822Arch);
}else if(rdmFeed.get('Type') == 'LinkPost'){
body += createdBy.get('Name') + ' posted a link\n ';
body +=(string)rdmFeed.get('Title')+ ' \n ';
body += (string)rdmFeed.get('LinkUrl') + '\n' ;
}else if(rdmFeed.get('Type') == 'ContentPost'){
body += createdBy.get('Name') + ' posted a file\n ';
body +=(string)rdmFeed.get('title')+ ' \n '; // VM : QC 2890 changed from contentfilename to Title to fetch the correct file name
}
divBody = divBody.replace('@body@',body) ;
divBody = divBody.replace('null','');
return divBody;
}
public static Map<string,string> getParentObjectFieldMap(sObject rdmFeed){
Map<string,string> mapFieldAPI_Names_to_Label = new Map<string,string>();
List<Schema.SObjectField> fields = ChatterArchiveManager.getParentSobjectFields((string)rdmFeed.get('parentId'));
for(Schema.SObjectField fld : fields){
mapFieldAPI_Names_to_Label.put(fld.getDescribe().getName(),fld.getDescribe().getLabel());
}
return mapFieldAPI_Names_to_Label;
}
//This method creates the Body of a Status Post
public static string getFeedPostBody(sObject rdmFeed, string DeleteInfo, string ParentSobjectName,RFC2822_Archive__c rfc2822Arch){
string body = '';
sObject createdBy = rdmFeed.getSObject('CreatedBy');
Map<string,string> mapFieldAPI_Names_to_Label = new Map<string,string>();
List<Schema.SObjectField> fields = ChatterArchiveManager.getParentSobjectFields((string)rdmFeed.get('parentId'));
for(Schema.SObjectField fld : fields){
mapFieldAPI_Names_to_Label.put(fld.getDescribe().getName(),fld.getDescribe().getLabel());
}
// For Feed Tracked Changes, the feed body is null
List<FeedTrackedChange> lstFeedTrackedChanges = (List<FeedTrackedChange>)rdmFeed.getSObjects('FeedTrackedChanges');
String parentName = '';
String parentNameLabel ='';
if(ParentSobjectName <> 'ContentDocument' && rdmFeed.getSObject('Parent') <> null){
parentName = String.ValueOf(rdmFeed.getSObject('Parent').get('Name'));
parentNameLabel = String.ValueOf(rdmFeed.getSObject('Parent').getSObjectType().getDescribe().getLabel());
}
if(lstFeedTrackedChanges <> null){
body = getBody(String.ValueOf(rdmFeed.get('parentId')));
//'Update Record ID ' + rdmFeed.get('parentId') + ' with the following changes:\n';
string summary= createdBy.get('Name') + ' changed ';
string body2='';
for(FeedTrackedChange change: lstFeedTrackedChanges){
string fieldLabel = change.FieldName;
List<string> lstString = ChatterArchiveManager.getChangeSummaryAndBody( change.FieldName,ParentSobjectName,parentNameLabel,parentName,rfc2822Arch.ContentDocumentTitle__c,String.valueOf(createdBy.get('Name')),(change.OldValue == null?'blank': String.valueOf(change.OldValue)),(change.NewValue == null?'blank': String.valueOf(change.NewValue)),mapFieldAPI_Names_to_Label);
if((ParentSobjectName == 'ContentDocument' && ( fieldLabel=='contentVersionCreated'||fieldLabel=='created'))
||(fieldLabel=='created') ){
summary = lstString[0];
}else{
//summary += lstString[0];
summary = '' ;
body2 += lstString[1];
}
}
summary = ChatterArchiveManager.RemoveTrailingString(summary,' and ');
//body2 = ChatterArchiveManager.RemoveTrailingString(body2,'\n');
//body = body + summary + '\n' + body2;
body = body + summary + (body2 != '' ? '\n' + body2 : '');
}
return body;
}
public static string RemoveTrailingString(String str1, String str2){
if(str1.endsWith(str2)){
str1 = str1.substring(0,str1.lastIndexOf(str2));
}
return str1;
}
public static List<string> getChangeSummaryAndBody( string fieldLabel,string ParentSobjectTypeName, string ParentSobjectLabel,string ParentName,string ContentDocTitle, string createdBy,string oldVal,string newVal,Map<string,string> mapFieldAPI_Names_to_Label){
List<string> lstString = new List<string>();
string summary ='';
string body2 ='';
if(fieldLabel.contains('.')){
fieldLabel = mapFieldAPI_Names_to_Label.get(fieldLabel.split('\\.')[1] );
}
if(ParentSobjectTypeName == 'ContentDocument' && fieldLabel=='contentVersionCreated'){
summary = ChatterArchiveManager.getContentVersionSummary(ContentDocTitle,createdBy,true);
}else if(ParentSobjectTypeName == 'ContentDocument' && fieldLabel=='created') {
summary = ChatterArchiveManager.getContentVersionSummary(ContentDocTitle,createdBy,false);
}else if(fieldLabel=='created'){
String label = ParentSobjectLabel;
summary = ChatterArchiveManager.fieldCreatedSummary(ParentName,createdBy,label);
if(ParentSobjectTypeName == 'Campaign'){
//11/18/2011 Manisha Gupta : added a new filed Campaign Summary in query.
Campaign newCampaign = [select name,startDate,EndDate,Description,campaign_summary__c from campaign where id=:objId] ;
summary += '\n' ;
system.debug('^^^^^^^^^^^^^^^^^' + newCampaign) ;
if(newCampaign != null) {
system.debug('^^^^^^^^^^^^^^^^^') ;
if(newCampaign.StartDate != null) {
summary += 'Campaign Start date : ' + newCampaign.StartDate +'\n';
}
if(newCampaign.EndDate != null) {
summary += 'Campaign End date : ' + newCampaign.EndDate +'\n';
}
if(newCampaign.Description != null && newCampaign.Description != '') {
summary += 'Campaign Description : ' + newCampaign.Description +'\n';
}
// 11/18/2011 Manisha Gupta : added campaign summary in archive body
if(newCampaign.campaign_summary__c != null) {
summary += 'Campaign Summary : ' + newCampaign.campaign_summary__c +'\n';
}
}
}
}else{
if(ParentSobjectTypeName == 'Campaign'){
//Campaign newCampaign = [select name,startDate,EndDate,Description,campaign_summary__c from campaign where id=:objId] ;
if (fieldLabel=='Description' ){
newVal = (newDesc == null?'blank': String.valueOf(newDesc));
oldVal = (oldDesc == null?'blank': String.valueOf(oldDesc));
}
else if (fieldLabel=='Campaign Summary' ){
newVal = (newSumm == null?'blank': String.valueOf(newSumm));
oldVal = (oldSumm == null?'blank': String.valueOf(oldSumm));
}
}
body2 += ChatterArchiveManager.getFieldUpdatedBody(fieldLabel,oldVal,newVal);
summary += fieldLabel;
summary += ChatterArchiveManager.getFieldUpdatedSummary(fieldLabel,oldVal,newVal);
}
lstString.add(summary);
lstString.add(body2);
return lstString;
}
public static String getFieldUpdatedSummary(String fieldLabel,string oldVal,string newVal) {
string summary = '';
if(OldVal == 'blank' && NewVal == 'blank'){
summary += ' and ';
}else{
summary += ' from ' + oldVal + ' to ' + newVal + ' and ';
}
return summary;
}
public static String getFieldUpdatedBody(String fieldLabel,string oldVal,string newVal) {
string body2='';
if(OldVal == 'blank' && NewVal == 'blank'){
body2 += 'Changed ' + fieldLabel + '.\n';
}else{
body2 += 'Original ' + fieldLabel + ' value: ' ;
body2 += oldVal + '\n' ;
body2 += 'Updated '+ fieldLabel + ' value: ';
body2 += newVal + '\n' ;
}
return body2;
}
public static String fieldCreatedSummary(String parentName,String createdBy,String Label) {
String summary = parentName + ' - ' + createdBy + ' created this ' + Label + '.';
return summary;
}
public static String getBody(String parentId) {
String body = 'Updated Record ID ' + parentId + ' with the following changes:\n';
return body;
}
public static String getContentVersionSummary(String Title,String UpdatedBy , boolean isUpdated) {
String summary = '';
if(isUpdated) {
summary += Title + ' - ' + UpdatedBy + ' uploaded a new version of this file.';
} else {
summary += Title + ' - ' + UpdatedBy + ' created this file.';
}
return summary;
}
// This method returns the span tag for a deleted Post/comment
public static string getDeleteInfoSpanTag(boolean forDelete){
if(forDelete) {
return '<span class="deleted">Deleted on ' + DateTime.now() + '</span>' ;
}else{
return '';
}
}
// This method returns the span tag for a referenced Post
//RA: Zantaz File Enhancement 17 Feb 2012
private string getPostReferenceSpanTag(sObject rdmFeed, boolean isForCmtAttachment){
DateTime createdDt = (DateTime)rdmFeed.get('CreatedDate');
if(createdDt < Date.today() || isForCmtAttachment){ //RA: Zantaz File Enhancement 17 Feb 2012
return ' <span class="reference">Included for Context</span> ';
}else{
return '';
}
}
// This method returns the div tag for a Chatter comment
//RA: Zantaz File Enhancement 17 Feb 2012
public static string getSingleChatterCommentDiv(FeedComment feedCmt, string DeleteInfo, boolean isForCmtAttachment){
// Added on 16 Feb 2012 RA: Zantaz File Enhancement
//return ' <div id="' + feedCmt.id + '" class="chatterPost"> \n' + DeleteInfo + feedCmt.CreatedDate +' ' + feedCmt.CreatedBy.Name + ' - ' +feedCmt.CommentBody +'\n' + ' </div>\n ' ;
string commentDiv = ' <div id="' + feedCmt.id + '" class="chatterPost"> \n' + DeleteInfo + feedCmt.CreatedDate +' ' + feedCmt.CreatedBy.Name + ' - ' +feedCmt.CommentBody +'\n' + ' @UploadedFileName </div>\n ' ;
string uploadedFileName = '';
if(feedCmt.CommentType == 'ContentComment' && isForCmtAttachment){//RA: Zantaz File Enhancement 17 Feb 2012
List<ContentVersion> lstCv = [select id, title from ContentVersion where id =: feedCmt.RelatedRecordId];
Integer listsize = lstCv.size();
uploadedFileName = lstCv.size() > 0? feedCmt.CreatedBy.Name + ' posted a file \n' + lstCv[listsize-1].title + ' \n ' : '';
}
commentDiv =commentDiv.replace('null',''); //VM : Added as part of QC 2922
return commentDiv.replace('@UploadedFileName', uploadedFileName);
}
//This method returns the div tags for a all Chatter comments
public string getAllChatterCommentDiv(sObject rdmFeed,FeedComment currentFeedCmt,string DeleteInfo){
List<FeedComment> lstFeedComments = (List<FeedComment>)rdmFeed.getSObjects('FeedComments');
string comments = '';
for(FeedComment feedCmt : lstFeedComments){
if(feedCmt.id == currentFeedCmt.id){
comments += getSingleChatterCommentDiv(feedCmt,DeleteInfo, false); //RA: Zantaz File Enhancement 17 Feb 2012
}else{
comments += getSingleChatterCommentDiv(feedCmt,'', false); //RA: Zantaz File Enhancement 17 Feb 2012
}
}
return comments;
}
// This method returns the div tag for a Chatter Post and comment
//RA: Zantaz File Enhancement 17 Feb 2012
private string getChatterPostCommentDiv(sObject rdmFeed,FeedComment feedCmt, string referenceSpan ,string DeleteInfo,string ParentSobjectName,RFC2822_Archive__c rfc2822Arch,boolean addAllComments, boolean isForCmtAttachment){
system.debug('********' + rdmFeed.get('Type'));
sobject createdBy = rdmFeed.getSObject('CreatedBy');
string body='';
String divBody=' <div id="' + rdmFeed.get('id') + '" class="chatterPost"> \n' +
referenceSpan + rdmFeed.get('CreatedDate') +' ' + createdBy.get('Name') + ' - @body@\n' +
(addAllComments==true? getAllChatterCommentDiv(rdmFeed,feedCmt,DeleteInfo) : getSingleChatterCommentDiv(feedCmt,DeleteInfo, isForCmtAttachment) ) + //RA: Zantaz File Enhancement 17 Feb 2012
' </div>\n' ;
if(rdmFeed.get('Body') <> null){
body = (string)rdmFeed.get('Body');
}
if(rdmFeed.get('Type') == 'TrackedChange'){
body = ChatterArchiveManager.getFeedPostBody( rdmFeed, '', ParentSobjectName, rfc2822Arch);
} else if(rdmFeed.get('Type') == 'LinkPost'){
body += createdBy.get('Name') + ' posted a link\n ';
body +=(string)rdmFeed.get('Title')+ ' \n ';
body += (string)rdmFeed.get('LinkUrl') + '\n' ;
}else if(rdmFeed.get('Type') == 'ContentPost'){
body += createdBy.get('Name') + ' posted a file\n ';
body +=(string)rdmFeed.get('ContentFileName')+ ' \n ';
system.debug ((string)rdmFeed.get('ContentFileName') + '******');
}
divBody = divBody.replace('@body@',body );
divBody = divBody.replace('null',''); //VM : Added as part of QC 2922
return divBody;
}
/*
* This Method Generates the RFC2822_Archive for a list of Feed Items
* If an RFC2822_Archive exists for the Parent object for that day, then it is updated else create a new RFC2822_Archive object
*/
public void ArchivePostToRFC2822(Map<Id,FeedItem> mapRDMFeedItems,boolean forDelete,string ParentSobjectName){
Set<Id> newFeedItems = mapRDMFeedItems.keySet();
//Retrieve the Feeds for the parent object
List<Sobject> rdmFeedList = ChatterArchiveManager.getFeeds( newFeedItems,ParentSobjectName,false,this.archiveBuilder);
if(rdmFeedList.size()>0){
ArchiveToRFC2822( rdmFeedList, forDelete, ParentSobjectName);
}
// UpdateContentVersionFields(rdmFeedList[0]);
}
/*
* This Method Generates the RFC2822_Archive for a list of Feed Items
* If an RFC2822_Archive exists for the Parent object for that day, then it is updated else create a new RFC2822_Archive object
*/
public void ArchiveToRFC2822(List<Sobject> rdmFeedList,boolean forDelete,string ParentSobjectName){
String DocumentLink='';
//Set of Parent Ids
Set<String> parentIds = new Set<String>();
Set<String> parentPostIds = new Set<String>();
Sobject createdBy ;
for(Sobject rdmFeed : rdmFeedList){
parentPostIds.add((string)rdmFeed.get('Id'));
parentIds.add((string)rdmFeed.get('ParentId'));
createdBy = rdmFeed.getSObject('CreatedBy');
system.debug('rdmFeedddd____'+rdmFeed);
}
//Get the Parent Ids to be used- ObjectFeed Id or Object Id
parentIds = archiveBuilder.getParentIds( parentIds, parentPostIds);//getParentIds( parentIds, ParentSobjectName, parentPostIds);
//Get Existing RFC2822_Archive__c for the current day
List<RFC2822_Archive__c> lstTodaysArchives = archiveBuilder.getExistingRFC2822_Archives(parentIds,createdBy);
//Loop Over all Feed to Create/Update the RFC2822_Archive
//If No RFC2822_Archive exists, create new else update it
List<RFC2822_Archive__c> lstRFCToInsert = new List<RFC2822_Archive__c>();
for(Sobject rdmFeed : rdmFeedList){
string parentID = (string)rdmFeed.get('ParentId');
RFC2822_Archive__c rfc2822Arch = new RFC2822_Archive__c();
if(lstTodaysArchives.size()>0){
rfc2822Arch= lstTodaysArchives[0];//archiveBuilder.getExistingArchive(mapRFC2822_Archive, parentId, (string)rdmFeed.get('Id'));
system.debug('==========archive list TODAY==============' + lstTodaysArchives[0]) ;
}
if(rfc2822Arch.id == null){
System.debug('_____________Inside ArchivePostToRFC2822 ____________________CREATE NEW');
rfc2822Arch = create_RFC2822_Archive_For_Posts( rfc2822Arch, parentID, rdmFeed ,forDelete,ParentSobjectName,0,true) ;
}else if(rfc2822Arch.Archive_Date__c <> Date.Today()){
//Create Copy of Existing Archive
System.debug('_____________Inside ArchivePostToRFC2822 ____________________COPY FROM EXISTING');
lstTodaysArchives = copyRFC2822_Archive(lstTodaysArchives);
rfc2822Arch= lstTodaysArchives[0];
System.debug('_____________Inside ArchivePostToRFC2822 ____________________COPY FROM EXISTING____' + rfc2822Arch);
rfc2822Arch = update_RFC2822_Archive_For_Posts( rfc2822Arch, parentID, rdmFeed ,forDelete,ParentSobjectName) ;
}
else{
System.debug('_____________Inside ArchivePostToRFC2822 ____________________UPDATE');
rfc2822Arch = update_RFC2822_Archive_For_Posts( rfc2822Arch, parentID, rdmFeed ,forDelete,ParentSobjectName) ;
}
lstRFCToInsert.add(rfc2822Arch);
}
}
// This method Creates a new RFC2822_Archive__c in case of Feed Post updates
private RFC2822_Archive__c create_RFC2822_Archive_For_Posts( RFC2822_Archive__c rfc2822Arch,string parentID,sObject rdmFeed ,boolean forDelete, string ParentSobjectName,Integer index,boolean addContentLink ){
sObject createdBy = rdmFeed.getSObject('CreatedBy');
sObject parent = null; //rdmFeed.getSObject('Parent');
//Create Subject
String sub = archiveBuilder.getArchiveSubject(parent,createdBy);//getArchiveSubject(parent,ParentSobjectName,createdBy);
//Create From
string strFrom = archiveBuilder.getFromAddress(parent,createdBy);//getFromAddress(parent,ParentSobjectName,createdBy);
//Create To
String strTo = archiveBuilder.getFirstToAddressForPost( strFrom, createdBy , parent );//getFirstToAddressForPost( strFrom, ParentSobjectName, createdBy );
//Create the Archive
rfc2822Arch = create_RFC2822_Archive(rfc2822Arch, parentID, sub, strFrom,strTo,rdmFeed,index,addContentLink);
rfc2822Arch = setBodyFields( archiveBuilder, rfc2822Arch, rdmFeed , forDelete, ParentSobjectName, addContentLink, parent );
update rfc2822Arch;
return rfc2822Arch;
}
public static RFC2822_Archive__c setBodyFields(ChatterArchiveManager.iArchiveBuilder archiveBuilder, RFC2822_Archive__c rfc2822Arch,sObject rdmFeed ,boolean forDelete, string ParentSobjectName,boolean addContentLink,sObject parent ){
system.debug((string)rdmFeed.get('ContentFileName') + '**FileName3');
String styleTag = ChatterArchiveManager.getHTMLStyleTag();
String divMessageId = ChatterArchiveManager.getHTMLMessageDivTag(rfc2822Arch.SourceID__c);
//QC - 1870: 22 Aug 2013
String feedObjectName = rdmFeed.getSObjectType().getDescribe().getName();
if(feedObjectName.equalsIgnoreCase('CollaborationGroupFeed')){
string groupName = String.ValueOf(rdmFeed.getSObject('Parent').get('Name'));
if(!String.isEmpty(groupName)){
divMessageId += ' <div id="groupName">Group Name: '+ groupName + '</div>\n' ;
}
}
String divPrimaryMaterial = archiveBuilder.getHTMLPrimaryMaterialDivTag(parent );//getHTMLPrimaryMaterialDivTag(parent,ParentSobjectName );
String divChatterFeed = ' <div id="chatterFeed">\n @chatterPosts </div>';
String DeleteInfo = ChatterArchiveManager.getDeleteInfoSpanTag(forDelete);
String divChatterPost = ChatterArchiveManager.getChatterPostDiv(rdmFeed,DeleteInfo,ParentSobjectName,rfc2822Arch) ;
divChatterFeed = divChatterFeed.replace('@chatterPosts', divChatterPost);
string plainBody = styleTag + '<div id="archiveBody">\n'+ divMessageId + divPrimaryMaterial + divChatterFeed + '</div>';
rfc2822Arch.Body_Plain_Text__c = plainBody;
EncodeData(rfc2822Arch);
return rfc2822Arch;
}
// This method Updates the RFC2822_Archive__c
private RFC2822_Archive__c update_RFC2822_Archive_For_Posts( RFC2822_Archive__c rfc2822Arch,string parentID,sObject rdmFeed , boolean forDelete,string ParentSobjectName){
sObject createdBy = rdmFeed.getSObject('CreatedBy');
string email = (string)createdBy.get('email');
if(!(rfc2822Arch.To__c.indexOf( email) >= 0)){
rfc2822Arch.To__c = rfc2822Arch.To__c + ';' + '"' + createdBy.get('lastname') + (createdBy.get('firstname') <> null && createdBy.get('firstname') <> '' ? ', ' + createdBy.get('firstname'):'') + '" <' + createdBy.get('email') + '>';
}
String DeleteInfo = ChatterArchiveManager.getDeleteInfoSpanTag(forDelete);
String divChatterPost = ChatterArchiveManager.getChatterPostDiv(rdmFeed,DeleteInfo,ParentSobjectName,rfc2822Arch) ;
integer ind = rfc2822Arch.Body_Plain_Text__c.indexOf('<div id="' + rdmFeed.get('id') +'"');
if(ind < 0){
//If Div does not exists then we need to add div fro the new post
//rfc2822Arch.Body_Plain_Text__c = rfc2822Arch.Body_Plain_Text__c.replace(' ', divChatterPost +' ');
boolean isAppended = checkLengthAndAppend( rfc2822Arch, ' ', divChatterPost +' ');
if(!isAppended){
Integer index = rfc2822Arch.Index__c.intValue() ;
rfc2822Arch = new RFC2822_Archive__c();
return create_RFC2822_Archive_For_Posts( rfc2822Arch, parentID, rdmFeed, forDelete,ParentSobjectName,index+1,false);
}
}else{
//If the div for feed already exists, it means its being updated for delete operation
integer ind2 = rfc2822Arch.Body_Plain_Text__c.indexOf('\n',ind);
String subString = rfc2822Arch.Body_Plain_Text__c.subString(ind,ind2 +1 );
String subString2 = subString + DeleteInfo;
//rfc2822Arch.Body_Plain_Text__c = rfc2822Arch.Body_Plain_Text__c.replace(subString,subString2);
boolean isAppended = checkLengthAndAppend( rfc2822Arch, subString, subString2);
if(!isAppended){
Integer index = rfc2822Arch.Index__c.intValue();
rfc2822Arch = new RFC2822_Archive__c();
return create_RFC2822_Archive_For_Posts( rfc2822Arch, parentID, rdmFeed , forDelete,ParentSobjectName,index+1,false);
}
}
EncodeData(rfc2822Arch);
rfc2822Arch.Archive_Status__c = STATUS_READY;
update rfc2822Arch;
return rfc2822Arch;
}
// This method Creates a new RFC2822_Archive__c in case of Feed Comment updates
public void ArchiveCommentsToRFC2822(Map<Id,FeedComment> mapRDMFeedComments,boolean forDelete, string ParentSobjectName){
Set<String> parentIds = new Set<String>();
Set<String> parentPostIds = new Set<String>();
// Map<String,RFC2822_Archive__c> mapRFC2822_Archive = new Map<String,RFC2822_Archive__c>() ;
Set<Id> newFeedItems = new Set<Id>() ;
for(FeedComment feedCmt: mapRDMFeedComments.values()){
parentIds.add(feedCmt.parentId);
newFeedItems.add(feedCmt.FeedItemId );
}
//Get Feed List
List<sObject> rdmFeedList = ChatterArchiveManager.getFeeds( newFeedItems, ParentSobjectName,false,this.archiveBuilder);
system.debug('RA_rdmFeedList ---> ' + rdmFeedList);
Map<String,sObject> mapFeedItem_RDMFeed = new Map<String,sObject>() ;
String DocumentLink;
sObject createdBy;
System.debug('ArchiveCommentsToRFC2822__________________-rdmFeedList________________'+ rdmFeedList);
for(sObject rdmFeed : rdmFeedList){
mapFeedItem_RDMFeed.put((string)rdmFeed.get('Id'),rdmFeed);
parentPostIds.add((string)rdmFeed.get('Id'));
createdBy = rdmFeed.getSObject('CreatedBy');
}
system.debug('RA_mapFeedItem_RDMFeed ---> ' + mapFeedItem_RDMFeed);
parentIds = archiveBuilder.getParentIds(parentIds, parentPostIds) ;//getParentIds(parentIds, ParentSobjectName, parentPostIds) ;
//Get Existing Archives for the current day
List<RFC2822_Archive__c> lstTodaysArchives = archiveBuilder.getExistingRFC2822_Archives(parentIds,createdBy);
List<RFC2822_Archive__c> lstRFCToInsert = new List<RFC2822_Archive__c>();
system.debug('RA_mapRDMFeedComments ---> ' + mapRDMFeedComments);
for(FeedComment feedCmt: mapRDMFeedComments.values()){
string parentID = feedCmt.parentId;
RFC2822_Archive__c rfc2822Arch = new RFC2822_Archive__c();
sObject rdmFeed = mapFeedItem_RDMFeed.get(feedCmt.FeedItemId);
if(lstTodaysArchives.size()>0){
rfc2822Arch= lstTodaysArchives[0];//archiveBuilder.getExistingArchive(mapRFC2822_Archive, parentId, (string)rdmFeed.get('Id'));
}
/*if(rdmFeed == null)
continue;*/
FeedComment currentFeedCommentDetails;
system.debug('RA_rdmFeed ---> ' + rdmFeed);
sObject[] feedCmts = rdmFeed.getSobjects('FeedComments');
system.debug('feedCmt.id---> ' + feedCmt.id);
for(sObject objComment: feedCmts){
FeedComment cmt = (FeedComment)objComment;
system.debug('cmt.id ---> ' + cmt.id);
if(cmt.id == feedCmt.id){
currentFeedCommentDetails = cmt;
break;
}
}
//17 Feb 2012: RA : ADD CONFITION TO IDENTIFY THAT ANY ATTACHMENT IS AVAILABLE WITH COMMENT OR NOT
if(currentFeedCommentDetails.CommentType == 'ContentComment' && forDelete == false){
CreateRFCArchiveForCommentAttachment(rdmFeed, ParentSobjectName, currentFeedCommentDetails);
}
if(rfc2822Arch.id == null){
//RA: Zantaz File Enhancement 17 Feb 2012
rfc2822Arch = create_RFC2822_Archive_For_Comments( rfc2822Arch, currentFeedCommentDetails, rdmFeed,forDelete, ParentSobjectName,0,true,true, false) ;
}else if(rfc2822Arch.Archive_Date__c <> Date.Today()){
//rfc2822Arch exists for a previous day
System.debug('_____________Inside ArchivePostToRFC2822 ____________________COPY FROM EXISTING');
//Create Copy of Existing Archive
lstTodaysArchives = copyRFC2822_Archive(lstTodaysArchives);
rfc2822Arch= lstTodaysArchives[0];
System.debug('_____________Inside ArchivePostToRFC2822 ____________________COPY FROM EXISTING____' + rfc2822Arch);
rfc2822Arch = update_RFC2822_Archive_For_Comments( rfc2822Arch, currentFeedCommentDetails, rdmFeed,forDelete, ParentSobjectName) ;
} else {
//Update Body
rfc2822Arch = update_RFC2822_Archive_For_Comments( rfc2822Arch, currentFeedCommentDetails, rdmFeed,forDelete, ParentSobjectName) ;
}
lstRFCToInsert.add(rfc2822Arch);
}
}
// This Method Creates A Copy of the RFC2822_Archive
public List<RFC2822_Archive__c> copyRFC2822_Archive(List<RFC2822_Archive__c> lst_rfc2822Arch){
List<RFC2822_Archive__c> lst_Copy = new List<RFC2822_Archive__c>();
List<RFC2822_Archive__c> lst_Copy_update = new List<RFC2822_Archive__c>();
for(RFC2822_Archive__c rfc2822Arch : lst_rfc2822Arch){
RFC2822_Archive__c copy = new RFC2822_Archive__c();
sObject obj = (sObject)rfc2822Arch;
//opt_preserve_id = false, opt_IsDeepClone = false, opt_preserve_readonly_timestamps = false, opt_preserve_autonumber false
copy = rfc2822Arch.clone(false,false,false,false);
copy.ContentDocument_ID__c = null;
copy.ContentDocumentLink__c ='';
copy.ContentDocumentTitle__c ='';
copy.ContentDocumentName__c='';
copy.ContentDocumentType__c ='';
copy.Message_ID__c = 'Salesforce-GPS'+System.Label.sndbx +'-'+ DateTime.Now().format('yyyyMMdd') +'Z'+ DateTime.Now().format('hhmmss') + '-I-[objectID]' + MESSAGE_HOST_ADDRESS ;
lst_Copy.add(copy);
}
insert lst_Copy;
for(RFC2822_Archive__c copy : lst_Copy){
copy = getExistingRFC2822_Archive(copy.id );
copy.Message_ID__c = copy.Message_ID__c.replace('[objectID]', copy.Id);
lst_Copy_update.add(copy);
}
update lst_Copy_update;
return lst_Copy_update;
}
public static RFC2822_Archive__c initialize_RFC2822_Archive(RFC2822_Archive__c rfc2822Arch,string parentID,string sub,string strFrom, string strTo,sObject rdmFeed,Integer index , boolean addContentLink,ChatterArchiveManager.iArchiveBuilder archiveBuilder){
//Set feed id
rfc2822Arch.Index__c = index;
rfc2822Arch.Feed_Id__c = (string)rdmFeed.get('Id');
rfc2822Arch.SourceID__c = archiveBuilder.getSourceID(rdmFeed) ;// getSourceID(rdmFeed,ParentSobjectName) // parentID;
rfc2822Arch.Subject__c = sub;
rfc2822Arch.Body__c = '';
rfc2822Arch.Body_Plain_Text__c ='';
// Value Changed From 'GPSCommunication.' To 'Salesforce-' + System.Label.sndbx +'-' 18-May-2011
// 07-14-11 Dan Value Changed From 'Salesforce-' To 'Salesforce-GPS-' + System.Label.sndbx +'-'
rfc2822Arch.Message_ID__c = 'Salesforce-GPS'+System.Label.sndbx +'-'+ DateTime.Now().format('yyyyMMdd') +'Z'+ DateTime.Now().format('hhmmss') + '-I-[objectID]' + ChatterArchiveManager.MESSAGE_HOST_ADDRESS ;
rfc2822Arch.From__c = strFrom;
rfc2822Arch.To__c = strTo ;//'"' + feedCmt.CreatedBy.lastname + (feedCmt.CreatedBy.firstname <> null && feedCmt.CreatedBy.firstname <> '' ? ', ' + feedCmt.CreatedBy.firstname:'') + '" <' + feedCmt.CreatedBy.email + '>';
//if((string)rdmFeed.get('Type') == 'ContentPost' ){
if((string)rdmFeed.get('ContentFileName')<> null && addContentLink == true){
rfc2822Arch = archiveBuilder.setContentDocumentFields(rdmFeed,rfc2822Arch, ChatterArchiveManager.getHostURL());
}
ContentVersion doc = ChatterArchiveManager.getContentDocument(parentID);
if(doc <> null){
string mimetype= getMimeType(doc.PathOnClient);
//rfc2822Arch= archiveBuilder.setRFCArchiveDocumentFields( rfc2822Arch, doc.Title , doc.Id, getHostURL(),(string)rdmFeed.get('ContentType'));
rfc2822Arch= archiveBuilder.setRFCArchiveDocumentFields( rfc2822Arch, doc.PathOnClient , doc.Id, ChatterArchiveManager.getHostURL(),mimetype);
}
String feedObjectName = rdmFeed.getSObjectType().getDescribe().getName();
rfc2822Arch.FeedObjectName__c = feedObjectName;
if(feedObjectName.equalsIgnoreCase('CollaborationGroupFeed')){
rfc2822Arch.GroupName__c = String.ValueOf(rdmFeed.getSObject('Parent').get('Name'));//CRMGPS-3827
}
return rfc2822Arch;
}
// This method Fills the RFC2822 archive
private RFC2822_Archive__c create_RFC2822_Archive(RFC2822_Archive__c rfc2822Arch,string parentID,string sub,string strFrom, string strTo,sObject rdmFeed,Integer index , boolean addContentLink){
//Set feed id
rfc2822Arch = ChatterArchiveManager.initialize_RFC2822_Archive( rfc2822Arch, parentID, sub, strFrom, strTo, rdmFeed, index , addContentLink,this.archiveBuilder);
system.debug('------rfc2822Arch'+rfc2822Arch);
insert rfc2822Arch;
rfc2822Arch = getExistingRFC2822_Archive(rfc2822Arch.id );
rfc2822Arch.Message_ID__c = rfc2822Arch.Message_ID__c.replace('[objectID]', rfc2822Arch.Id);
return rfc2822Arch;
}
// This Method creates new RFC2822_Archive__c in case of Feed Comment updates
//RA: Zantaz File Enhancement 17 Feb 2012
private RFC2822_Archive__c create_RFC2822_Archive_For_Comments( RFC2822_Archive__c rfc2822Arch,FeedComment feedCmt,sObject rdmFeed, boolean forDelete,string ParentSobjectName,Integer index, boolean addContentLink,boolean addAllComments, boolean isForCmtAttachment){
string parentID = (string)rdmFeed.get('parentId');
sObject objParent ;
system.debug('_________ rdmFeed.getSObjectType().getDescribe().getName()_______' + rdmFeed.getSObjectType().getDescribe().getName());
if(rdmFeed.getSObjectType().getDescribe().getName() <>'ContentDocumentFeed'){
objParent = rdmFeed.getSObject('Parent');
}
sObject createdBy = rdmFeed.getSObject('CreatedBy');
String sub = archiveBuilder.getArchiveSubject(objParent,createdBy);//getArchiveSubject(objParent,ParentSobjectName,createdBy);
string strFrom = archiveBuilder.getFromAddress(objParent,createdBy);//getFromAddress(objParent,ParentSobjectName,createdBy);
String strTo = '"' + feedCmt.CreatedBy.lastname + (feedCmt.CreatedBy.firstname <> null && feedCmt.CreatedBy.firstname <> '' ? ', ' + feedCmt.CreatedBy.firstname:'') + '" <' + feedCmt.CreatedBy.email + '>';
//rfc2822Arch = create_RFC2822_Archive(rfc2822Arch, parentID, sub, strFrom,strTo,rdmFeed ,index, addContentLink);
rfc2822Arch = ChatterArchiveManager.initialize_RFC2822_Archive( rfc2822Arch, parentID, sub, strFrom, strTo, rdmFeed, index , addContentLink,this.archiveBuilder);
String styleTag = ChatterArchiveManager.getHTMLStyleTag();
String divMessageId = ChatterArchiveManager.getHTMLMessageDivTag(rfc2822Arch.SourceID__c);
//25 Feb 2014: Dinesh M: QC2448, Zantaz-Chatter: Files uploaded in comment section, deleted from comment section do not have the group name associated in outlook mail box
String feedObjectName = rdmFeed.getSObjectType().getDescribe().getName();
if(feedObjectName.equalsIgnoreCase('CollaborationGroupFeed')){
string groupName = String.ValueOf(rdmFeed.getSObject('Parent').get('Name'));
if(isForCmtAttachment && !String.isEmpty(groupName)){
divMessageId += ' <div id="groupName">Group Name: '+ groupName + '</div>\n' ;
}
}
String divPrimaryMaterial = archiveBuilder.getHTMLPrimaryMaterialDivTag(objParent );//getHTMLPrimaryMaterialDivTag(objParent,ParentSobjectName );
String divChatterFeed = ' <div id="chatterFeed">\n @chatterPosts </div>';
String DeleteInfo = ChatterArchiveManager.getDeleteInfoSpanTag(forDelete);
String referenceSpan = getPostReferenceSpanTag(rdmFeed, isForCmtAttachment); //RA: Zantaz File Enhancement 17 Feb 2012
String divChatterPost = getChatterPostCommentDiv( rdmFeed, feedCmt, referenceSpan , DeleteInfo, ParentSobjectName, rfc2822Arch,addAllComments, isForCmtAttachment) ; //RA: Zantaz File Enhancement 17 Feb 2012
divChatterFeed = divChatterFeed.replace('@chatterPosts', divChatterPost);
string plainBody = styleTag + '<div id="archiveBody">\n'+ divMessageId + divPrimaryMaterial + divChatterFeed + '</div>';
rfc2822Arch.Body_Plain_Text__c = plainBody;
EncodeData(rfc2822Arch);
//RA: Zantaz File Enhancement 17 Feb 2012
if(isForCmtAttachment){
rfc2822Arch.For_Comment_with_File__c = true;
rfc2822Arch.ContentDocument_ID__c = feedCmt.RelatedRecordId;
string query2 = 'Select ContentUrl, FileType, PathOnClient, Title, Id From ContentVersion where id= \'' + feedCmt.RelatedRecordId + '\' ';
List<ContentVersion> lstVer = new List<ContentVersion>();
try{
lstVer = Database.Query(query2);
}catch(Exception e){
lstVer = new List<ContentVersion>();
}
if(lstVer.size() > 0){
string mimetype= getMimeType(lstVer[0].PathOnClient);
rfc2822Arch= archiveBuilder.setRFCArchiveDocumentFields( rfc2822Arch, lstVer[0].PathOnClient , lstVer[0].Id, ChatterArchiveManager.getHostURL(),mimetype);
}
}
insert rfc2822Arch;
rfc2822Arch = getExistingRFC2822_Archive(rfc2822Arch.id );
rfc2822Arch.Message_ID__c = rfc2822Arch.Message_ID__c.replace('[objectID]', rfc2822Arch.Id);
update rfc2822Arch;
/*
15 Feb 2012 Rahul A: For Archiving Chatter File Enhancement
TO DO: Create New RFC record for attached file of comments
*/
return rfc2822Arch;
}
//This method Updates the RFC2822_Archive__c in case of Comment updates
private RFC2822_Archive__c update_RFC2822_Archive_For_Comments( RFC2822_Archive__c rfc2822Arch,FeedComment feedCmt,sObject rdmFeed , boolean forDelete,string ParentSobjectName){
//TO Field : Update the To Field
if(rfc2822Arch.To__c <> null && !(rfc2822Arch.To__c.indexOf( feedCmt.CreatedBy.email )>=0)){
rfc2822Arch.To__c = rfc2822Arch.To__c + ';' + '"' + feedCmt.CreatedBy.Lastname + (feedCmt.CreatedBy.firstname <> null && feedCmt.CreatedBy.firstname <> '' ? ', ' + feedCmt.CreatedBy.firstname:'') + '" <' + feedCmt.CreatedBy.email + '>';
}
//Check if the Record is being updated due to deletion of a comment
String DeleteInfo = ChatterArchiveManager.getDeleteInfoSpanTag(forDelete);
//Create the div element for the comment update
String divChatterPost = getSingleChatterCommentDiv(feedCmt,DeleteInfo, false) ; //RA: Zantaz File Enhancement 17 Feb 2012
/*
15 Feb 2012 Rahul A: For Archiving Chatter File Enhancement
TO DO: Create a new RFC for attached file with comments
*/
//Create the Span Element for the post that the comment refers to
String referenceSpan = getPostReferenceSpanTag(rdmFeed, false); //RA: Zantaz File Enhancement 17 Feb 2012
integer ind = rfc2822Arch.Body_Plain_Text__c.indexOf('<div id="' + rdmFeed.get('id') +'"');
if(ind < 0){
//The Post was not created today so we need to create the Div for Reference feed and add comment div top it
divChatterPost = getChatterPostCommentDiv( rdmFeed, feedCmt, referenceSpan , DeleteInfo,ParentSobjectName, rfc2822Arch,false, false) ;
// rfc2822Arch.Body_Plain_Text__c = rfc2822Arch.Body_Plain_Text__c.replace(' ', divChatterPost +' ');
boolean isAppended = checkLengthAndAppend( rfc2822Arch, ' ', divChatterPost +' ');
if(!isAppended){
Integer index = rfc2822Arch.Index__c.intValue();
rfc2822Arch = new RFC2822_Archive__c();
//RA: Zantaz File Enhancement 17 Feb 2012
return create_RFC2822_Archive_For_Comments( rfc2822Arch, feedCmt, rdmFeed, forDelete, ParentSobjectName,index+1, false,false, false);
}
}else{
ind = rfc2822Arch.Body_Plain_Text__c.indexOf('<div id="' + feedCmt.id +'"');
system.debug('______RA_forDelete_____ : '+ forDelete + ' ___ index_____: '+ind);
if(forDelete && ind > 0){
integer ind2 = rfc2822Arch.Body_Plain_Text__c.indexOf('\n',ind);
String subString = rfc2822Arch.Body_Plain_Text__c.subString(ind,ind2 +1 );
String subString2 = subString + DeleteInfo;
// rfc2822Arch.Body_Plain_Text__c = rfc2822Arch.Body_Plain_Text__c.replace(subString,subString2);
boolean isAppended = checkLengthAndAppend( rfc2822Arch, subString, subString2);
if(!isAppended){
Integer index = rfc2822Arch.Index__c.intValue();
rfc2822Arch = new RFC2822_Archive__c();
//RA: Zantaz File Enhancement 17 Feb 2012
return create_RFC2822_Archive_For_Comments( rfc2822Arch, feedCmt, rdmFeed , forDelete, ParentSobjectName,index+1, false,false, false);
}
}else{
ind = rfc2822Arch.Body_Plain_Text__c.indexOf('<div id="' + rdmFeed.id +'"');
integer ind2 = rfc2822Arch.Body_Plain_Text__c.indexOf(' ',ind);
String subString = rfc2822Arch.Body_Plain_Text__c.subString(ind,ind2);
String subString2 = subString + divChatterPost;
//rfc2822Arch.Body_Plain_Text__c = rfc2822Arch.Body_Plain_Text__c.replace(subString,subString2);
boolean isAppended = checkLengthAndAppend( rfc2822Arch, subString, subString2);
if(!isAppended){
Integer index = rfc2822Arch.Index__c.intValue();
rfc2822Arch = new RFC2822_Archive__c();
//RA: Zantaz File Enhancement 17 Feb 2012
return create_RFC2822_Archive_For_Comments( rfc2822Arch, feedCmt, rdmFeed , forDelete, ParentSobjectName,index+1,false,false, false);
}
}
}
EncodeData(rfc2822Arch);
rfc2822Arch.Archive_Status__c = STATUS_READY;
update rfc2822Arch;
return rfc2822Arch;
}
public static boolean checkLengthAndAppend(RFC2822_Archive__c rfc2822Arch, string subString, string subString2){
System.debug('_____________Inside checkLengthAndAppend_____ ');
if(checkLength( rfc2822Arch, subString2)){
rfc2822Arch.Body_Plain_Text__c = rfc2822Arch.Body_Plain_Text__c.replace(subString,subString2);
EncodeData(rfc2822Arch);
System.debug('_____________Inside checkLengthAndAppend --subString__' + subString);
System.debug('_____________Inside checkLengthAndAppend --subString2__' + subString2);
System.debug('_____________Inside checkLengthAndAppend_____ return true');
return true;
}else{
System.debug('_____________Inside checkLengthAndAppend_____ return false');
return false;
}
}
private static boolean checkLength( RFC2822_Archive__c rfc2822Arch, string string2){
Integer fieldLength = RFC2822_Archive__c.Body__c.getDescribe().getLength();
if(Test.isRunningTest()){
fieldLength= 1000;
}
System.debug('_____________Inside checkLength ____________________ ' + fieldLength);
String orgText = rfc2822Arch.Body_Plain_Text__c;
if(rfc2822Arch.Content_Transfer_Encoding__c == 'base64' ){
orgText = base64Encode(rfc2822Arch.Body_Plain_Text__c);
string2 = base64Encode(string2);