Li Jun
2022-03-24 4452a59757a30ff31fd05c1c6cc8c46725008827
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
/*
 * Author: Bubba Li
 * Created Date: 01/26/2022
 * Purpose: Utility class for PI 
 * Test Class: PIHelper
 * History: 
 *      01/26/2022 - Bubba Li - Initial Code.
 * 
 * */
global without sharing class PIHelper {
    public static Set<String> displayByOrderNumberObject = new set<String>{'Report__c'};
    public static String getObjectKeyPrefix(String objName){
        try{
            schema.sObjectType sObjType = Schema.getGlobalDescribe().get(objName);
            return (sObjType.getDescribe().getKeyPrefix());
        }catch(Exception e){
            system.debug('Exception from get key prefix:'+e.getMessage());
            return '';
        }    
    }
    // confirm file transaction
    // @future(callout =true)
    public static void confirmFileTrans(String module,Integer isSuccess, String   sfRecordId ,String transId ,String token,String transUrl){
        Boolean result =false;
        Transaction_Log__c traLog = new Transaction_Log__c();
        traLog.Module__c = 'ConfirmFileTransaction '+module;
        traLog.Interface_URL__c = transUrl;
        traLog.TransId__c = transId;
        // traLog.SFRecordId__c=sfRecordId;
        Integer MaxLogColumnLength = 131072;
        try {
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint(transUrl);
            request.setMethod('POST');
            request.setHeader('pi-token',token);
            request.setHeader('Content-Type', 'application/json');
            TransRequestBody requestBody =new TransRequestBody();
            requestBody.isSuccess=isSuccess;
            requestBody.sfRecordId=sfRecordId;
            requestBody.txId =transId;
            request.setBody(JSON.serialize(requestBody));
            system.debug('request---'+request.tostring());
            HttpResponse response = http.send(request);
            system.debug('token--'+token);
            system.debug('confirm result--'+response.getBody());
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            result = (Boolean)results.get('success');
            System.debug('result = ' + result);
            traLog.Status__c = 'success';
            String res=response.getBody();
            traLog.Response__c=res.substring(0, (res.length() > MaxLogColumnLength ? MaxLogColumnLength : res.length()));
           
        } catch (Exception e) {  
            traLog.Status__c = 'fail';
            traLog.Response__c = e.getMessage();    
        }
        insert traLog;
    }
     // confirm tx transaction
 
     public static void confirmTrans(String module,Integer isSuccess, String   sfRecordId ,String transId ,String token,String transUrl,List<idList> idList){
         Boolean result =false;
         Transaction_Log__c traLog = new Transaction_Log__c();
         traLog.Module__c = 'Confirm Transaction '+module;
         traLog.Interface_URL__c = transUrl;
         traLog.TransId__c = transId;
         Integer MaxLogColumnLength = 131072;
         if (!String.isEmpty(sfRecordId)) {
            traLog.SFRecordId__c=sfRecordId;
         }else {
            traLog.SFRecordId__c=JSON.serialize(idList);
         }
         
         try {
             Http http = new Http();
             HttpRequest request = new HttpRequest();
             request.setEndpoint(transUrl);
             request.setMethod('POST');
             request.setHeader('pi-token',token);
             request.setHeader('Content-Type', 'application/json');
             TransactionRequestBody requestBody =new TransactionRequestBody();
             requestBody.isSuccess=isSuccess;
             requestBody.sfRecordId=sfRecordId;
             requestBody.idList=idList;
             requestBody.txId =transId;
             request.setBody(JSON.serialize(requestBody));
             system.debug('request---'+request.tostring());
             HttpResponse response = http.send(request);
             system.debug('confirm result--'+response.getBody());
             Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
             result = (Boolean)results.get('success');
             System.debug('result = ' + result);
             traLog.Status__c = 'success';
             String res=response.getBody();
             traLog.Response__c=res.substring(0, (res.length() > MaxLogColumnLength ? MaxLogColumnLength : res.length()));
         } catch (Exception e) {  
             traLog.Status__c = 'fail';
             traLog.Response__c = e.getMessage();    
         }
         insert traLog;
     }
    // Use this log method
    public static void saveTransLog(String module,String awsDataId,String sfId, String transId,String content,String status,String respMsg){
        Transaction_Log__c traLog = new Transaction_Log__c();
        traLog.AWS_Data_Id__c = awsDataId;
        traLog.SFRecordId__c = sfId;
        traLog.Module__c = 'Upsert SF ' + module;
        traLog.TransId__c = transId;
        traLog.Request__c = content;
        traLog.Status__c = status;
        traLog.Response__c = respMsg;
        traLog.Interface_URL__c = traLog.Module__c;
        insert traLog;
    }
    //save qianlima log method add sushanhu 20220324
    // model 0 为文件 1为数据
    public static void insertConfirmTrans(String module,Integer isSuccess, String   sfRecordId ,String transId ,Integer model,String transUrl,List<idList> idList){
        Boolean result =false;
        Transaction_Log__c traLog = new Transaction_Log__c();
        if (model==0) {
            traLog.Module__c = 'ConfirmFileTransaction '+module;
        }else {
            traLog.Module__c = 'Confirm Transaction '+module;
        }
        traLog.Interface_URL__c = transUrl;
        traLog.TransId__c = transId;
        Integer MaxLogColumnLength = 131072;
        if (!String.isEmpty(sfRecordId)) {
           traLog.SFRecordId__c=sfRecordId.substring(0, (sfRecordId.length() > MaxLogColumnLength ? MaxLogColumnLength : sfRecordId.length()));
        }else {
            String sfIds =JSON.serialize(idList);
           traLog.SFRecordId__c=sfIds.substring(0, (sfIds.length() > MaxLogColumnLength ? MaxLogColumnLength : sfIds.length()));
        }
         if(isSuccess==0){
            traLog.Status__c = 'fail';
         } else {
            traLog.Status__c='success';
         }  
        insert traLog;
    }
    public static PIIntegration getPIIntegrationInfo(String sobjectType){
        PIIntegration piIntegration = new PIIntegration();
        //查询url
        PI_Policy_Configuration__c config = [SELECT Full_New_URL__c,Full_Search_URL__c,Full_Update_URL__c,Full_Undelete_URL__c,Full_Read_URL__c,Full_Delete_URL__c,Full_View_Unified_Contact_URL__c,TransactionURL__c FROM PI_Policy_Configuration__c WHERE Sobject_Type__c =:sobjectType];
        System.debug('config = ' + config);
 
        //获取appid和appsecret
        AWS_Integration_Info__mdt awsConfiguration = [SELECT App_Id__c,Max_Query_Number__c,Token_URL__c,App_Secret__c,Host_URL__c FROM AWS_Integration_Info__mdt  WHERE DeveloperName = 'AWS_Default_Configuration'];
        if (awsConfiguration == null) {
            System.debug('AWS_Integration_Info__mdt没配置');
            return null;
        }
        String awsAppId = awsConfiguration.App_Id__c;
        String awsAppSecret = awsConfiguration.App_Secret__c;
 
        System.debug('awsAppId = ' + awsAppId);
        System.debug('awsAppSecret = ' + awsAppSecret);
        System.debug('Host_URL__c = ' + awsConfiguration.Host_URL__c);
        System.debug('Token URL = ' + awsConfiguration.Token_URL__c);
 
        //获取token
        String token = '';
        try{
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            String url = awsConfiguration.Token_URL__c;
            request.setEndpoint(url);
            request.setMethod('GET');
            HttpResponse response = http.send(request);
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            token = (String)results.get('object');
            System.debug('token = ' + token);
        }catch(Exception e){
            Transaction_Log__c traLog = new Transaction_Log__c();
            traLog.Module__c = 'Get Token';
            traLog.Status__c = 'fail';
            traLog.Response__c = e.getMessage();
            traLog.Interface_URL__c = awsConfiguration.Token_URL__c;
            insert traLog;
        }
    
        //Insert Get Token Log
 
        //获取敏感字段
        piIntegration.PIDetails = [select id,PI_Policy_Configuration__r.Full_New_URL__c, Enable_Encrypt__c, SF_Field_API_Name__c,SF_Field_Encrypted_API__c, AWS_Field_API__c,AWS_Encrypted_Field_API__c,Field_Type__c from PI_Field_Policy_Detail__c  where PI_Policy_Configuration_Name__c =:sobjectType and Enable_Encrypt__c=true];
        if(displayByOrderNumberObject.contains(sobjectType)){
            piIntegration.PIDetails = [select id,PI_Policy_Configuration__r.Full_New_URL__c, Enable_Encrypt__c, SF_Field_API_Name__c,SF_Field_Encrypted_API__c, AWS_Field_API__c,AWS_Encrypted_Field_API__c,Field_Type__c from PI_Field_Policy_Detail__c  where PI_Policy_Configuration_Name__c =:sobjectType and Enable_Encrypt__c=true order by Order_Number__c];
        }
        List<String> vLookUpFields = new List<String>();
        List<String> PIFields = new List<String>();
        for (PI_Field_Policy_Detail__c PIDetail : piIntegration.PIDetails) {
            if(PIDetail.Field_Type__c == 'Reference'){
                vLookUpFields.add(PIDetail.SF_Field_API_Name__c);
            }
            PIFields.add(PIDetail.SF_Field_API_Name__c);
        }
 
        System.debug('vLookUpFields = ' + vLookUpFields.toString());
        System.debug('PIFields = ' + PIFields.toString());
 
        //填充数据
        piIntegration.maxQueryNumber = Integer.valueof(awsConfiguration.Max_Query_Number__c);
        piIntegration.newUrl = config.Full_New_URL__c;
        piIntegration.updateUrl = config.Full_Update_URL__c;
        piIntegration.queryUrl = config.Full_Read_URL__c;
        piIntegration.deleteUrl = config.Full_Delete_URL__c;
        piIntegration.undeleteUrl = config.Full_Undelete_URL__c;
        piIntegration.viewUnifiedContactUrl = config.Full_View_Unified_Contact_URL__c;
        piIntegration.transactionURL = config.TransactionURL__c;
        piIntegration.hostUrl = awsConfiguration.Host_URL__c;
        piIntegration.searchUrl = config.Full_Search_URL__c;
        piIntegration.token = token;
        piIntegration.awsAppId = awsAppId;
        piIntegration.awsAppSecret = awsAppSecret;
        piIntegration.vLookUpFields = vLookUpFields;
        piIntegration.PIFields = PIFields;
        piIntegration.sobjectPrefix = getObjectKeyPrefix(sobjectType);
        System.debug('piIntegration' + piIntegration);
        return piIntegration;
    }
    global class PIIntegration{
        public Integer maxQueryNumber{set;get;}
        public String sobjectPrefix{set;get;}
        public String searchUrl{set;get;}
        public String newUrl{set;get;}
        public String updateUrl{set;get;}
        public String queryUrl{set;get;}
        public String deleteUrl{set;get;}
        public String undeleteUrl{set;get;}
        public String viewUnifiedContactUrl{set;get;}
        public String hostUrl{set;get;}
        public String token{set;get;}
        public String awsAppId{set;get;}
        public String awsAppSecret{set;get;}
        public String transactionUrl{set;get;}
        public List<String> vLookUpFields{set;get;}
        public List<String> PIFields{set;get;}
        public List<PI_Field_Policy_Detail__c > PIDetails{set;get;}
    }
    global class TransRequestBody{
        public Integer isSuccess{set;get;}
        public String sfRecordId{set;get;}
        public String txId{set;get;}
        
    }
    global class TransactionRequestBody{
        public Integer isSuccess{set;get;}
        public String sfRecordId{set;get;}
        public String txId{set;get;}
        public List<idList> idList{set;get;} 
    }
    global class idList{
        public String awsId{set;get;}
        public String sfRecordId{set;get;}
    }
}