/* * 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 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 ''; } } // 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; } 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,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,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); System.debug('response = ' + response); Map results = (Map) JSON.deserializeUntyped(response.getBody()); token = (String)results.get('object'); System.debug('token = ' + token); }catch(Exception e){ // System.debug(e.getMessage()); // System.debug(e.getStackTraceString()); 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]; List vLookUpFields = new List(); List PIFields = new List(); 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.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.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 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 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 vLookUpFields{set;get;} public List PIFields{set;get;} public List PIDetails{set;get;} } }