Li Jun
2022-04-06 fb04e7c01d119c60632b4298d18fd93f3ccb3d79
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
/*
 * Author: Bubba Li
 * Created Date: 02/08/2022
 * Purpose: SearchContactController
 * Test Class: SearchContactController
 * History: 
 *      02/08/2022 - Bubba Li - Initial Code.
 * 
 * */
public without sharing class SearchContactController {
    public String searchKeyWord{set;get;}
    public String staticResource {get; set;}
    public String contactAWSIds {set;get;}
    public String contactsInfo {set;get;}
    public String PIPL_Search_Contact_Label{set;get;}
    public String aId{set;get;}
    public static Boolean checkNullString(String inputString){
        if(String.isEmpty(inputString)||String.isBlank(inputString)){
            return true;
        }
        return false;
    }
    public SearchContactController() {
        String accountId = ApexPages.currentPage().getParameters().get('accountId');
        searchKeyWord = ApexPages.currentPage().getParameters().get('searchContactKeyWord');
        aId = accountId;
        PIPL_Search_Contact_Label = Label.PIPL_Search_Contact_Label;
        //1. Query Contact by accountId
        List<Contact> conList = new List<Contact>();
        system.debug('Account Id from Front-end:'+accountId);
        if(checkNullString(accountId)&&checkNullString(searchKeyWord)){
            conList = new List<Contact>();
        }else{
            if(checkNullString(accountId)){
                conList = new List<Contact>(); 
            }else {
                conList = new List<Contact>([select Id,AWS_Data_Id__c,Account.Name from Contact  where AccountId=:accountId and AWS_Data_Id__c!='']); 
            }
            
        }    
        //2. Prepare the Contact Info
        Map<String,Contact> awsIdToContactMap = new Map<String,Contact>();
        List<String> conAWSIds = new List<String>();
        for(Contact con:conList){
            conAWSIds.add(con.AWS_Data_Id__c);
            awsIdToContactMap.put(con.AWS_Data_Id__c,con);
        }
        contactsInfo = JSON.serialize(awsIdToContactMap);
        contactAWSIds = JSON.serialize(conAWSIds);
        staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Contact'));        
    }
 
    @RemoteAction
    public static Response searchContacts(String awsContactIds,String searchContactName,String accountId) {
        system.debug('awsContactIds = ' + awsContactIds);
        Response resp = new Response();
        resp.status = 'fail';
        Map<String,Contact> awsIdToContactMapTemp = new Map<String,Contact>();
        if(!checkNullString(awsContactIds)){
            List<String> awsDataIds = (List<String>) JSON.deserialize(awsContactIds, List<String>.class);
            List<Contact> conListTemp = new List<Contact>();
            if(!checkNullString(accountId)){
                conListTemp = new List<Contact>([select Id,AWS_Data_Id__c,Account.Name from Contact where AccountId=:accountId and AWS_Data_Id__c in:awsDataIds]);
            }else {
                conListTemp = new List<Contact>([select Id,AWS_Data_Id__c,Account.Name from Contact where AWS_Data_Id__c in:awsDataIds]);
            }
            for(Contact con:conListTemp){
                awsIdToContactMapTemp.put(con.AWS_Data_Id__c,con);
            }
        }
        System.debug('awsIdToContactMapTemp = ' + awsIdToContactMapTemp);
        Map<String,Contact> noPIContactMapTemp = new Map<String,Contact>();
        List<Contact> partnerContactList = AWSServiceTool.getNoPIContact(searchContactName,accountId);
        System.debug('partnerContactList = ' + partnerContactList);            
        if(partnerContactList.size()>0){
            for(Contact con:partnerContactList){
                noPIContactMapTemp.put(con.Id,con);
            }               
        }
        if(awsIdToContactMapTemp.keySet().size()>0 ||noPIContactMapTemp.keySet().size()>0){
            resp.status = 'success';
            resp.message = JSON.serialize(awsIdToContactMapTemp);// PI contact info
            resp.noPIContactList = JSON.serialize(noPIContactMapTemp);//NoPI contact info
        }
        return resp;
    }
 
    @RemoteAction
    public static Response searchContactsNoPI(String contactName) {
        Response resp = new Response();
        resp.status = 'fail';
        List<Contact> conListTemp = new List<Contact>([select Id,Name,Account.Name,Phone,Email,MobilePhone from Contact where Name like :contactName]);
        if(conListTemp.size() > 0){
            resp.status = 'success';
            resp.message = JSON.serialize(conListTemp);
        }
        return resp;
    }
 
    public class Response{
        public String message{set;get;}
        public String status{set;get;}
        public String noPIContactList{set;get;}
    }
}