|
| 1 | +/* |
| 2 | + * Copyright © 2022 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package io.cdap.plugin.salesforce.connector; |
| 17 | + |
| 18 | +import com.sforce.async.AsyncApiException; |
| 19 | +import com.sforce.soap.partner.DescribeGlobalResult; |
| 20 | +import com.sforce.soap.partner.PartnerConnection; |
| 21 | +import com.sforce.soap.partner.QueryResult; |
| 22 | +import com.sforce.soap.partner.sobject.SObject; |
| 23 | +import com.sforce.ws.ConnectionException; |
| 24 | +import io.cdap.cdap.api.annotation.Description; |
| 25 | +import io.cdap.cdap.api.annotation.Name; |
| 26 | +import io.cdap.cdap.api.annotation.Plugin; |
| 27 | +import io.cdap.cdap.api.data.format.StructuredRecord; |
| 28 | +import io.cdap.cdap.api.data.schema.Schema; |
| 29 | +import io.cdap.cdap.etl.api.FailureCollector; |
| 30 | +import io.cdap.cdap.etl.api.batch.BatchSink; |
| 31 | +import io.cdap.cdap.etl.api.batch.BatchSource; |
| 32 | +import io.cdap.cdap.etl.api.connector.BrowseDetail; |
| 33 | +import io.cdap.cdap.etl.api.connector.BrowseEntity; |
| 34 | +import io.cdap.cdap.etl.api.connector.BrowseEntityPropertyValue; |
| 35 | +import io.cdap.cdap.etl.api.connector.BrowseRequest; |
| 36 | +import io.cdap.cdap.etl.api.connector.Connector; |
| 37 | +import io.cdap.cdap.etl.api.connector.ConnectorContext; |
| 38 | +import io.cdap.cdap.etl.api.connector.ConnectorSpec; |
| 39 | +import io.cdap.cdap.etl.api.connector.ConnectorSpecRequest; |
| 40 | +import io.cdap.cdap.etl.api.connector.DirectConnector; |
| 41 | +import io.cdap.cdap.etl.api.connector.PluginSpec; |
| 42 | +import io.cdap.cdap.etl.api.connector.SampleRequest; |
| 43 | +import io.cdap.cdap.etl.api.validation.ValidationException; |
| 44 | +import io.cdap.plugin.common.ConfigUtil; |
| 45 | +import io.cdap.plugin.salesforce.SObjectDescriptor; |
| 46 | +import io.cdap.plugin.salesforce.SalesforceConnectionUtil; |
| 47 | +import io.cdap.plugin.salesforce.SalesforceConstants; |
| 48 | +import io.cdap.plugin.salesforce.SalesforceSchemaUtil; |
| 49 | +import io.cdap.plugin.salesforce.authenticator.AuthenticatorCredentials; |
| 50 | +import io.cdap.plugin.salesforce.plugin.SalesforceConnectorConfig; |
| 51 | +import io.cdap.plugin.salesforce.plugin.sink.batch.SalesforceBatchSink; |
| 52 | +import io.cdap.plugin.salesforce.plugin.sink.batch.SalesforceSinkConfig; |
| 53 | +import io.cdap.plugin.salesforce.plugin.source.batch.MapToRecordTransformer; |
| 54 | +import io.cdap.plugin.salesforce.plugin.source.batch.SalesforceBatchSource; |
| 55 | +import io.cdap.plugin.salesforce.plugin.source.batch.SoapRecordToMapTransformer; |
| 56 | +import io.cdap.plugin.salesforce.plugin.source.batch.util.SalesforceSourceConstants; |
| 57 | +import org.slf4j.Logger; |
| 58 | +import org.slf4j.LoggerFactory; |
| 59 | + |
| 60 | +import java.io.IOException; |
| 61 | +import java.util.ArrayList; |
| 62 | +import java.util.HashMap; |
| 63 | +import java.util.List; |
| 64 | +import java.util.Map; |
| 65 | + |
| 66 | +/** |
| 67 | + * Salesforce Connector Plugin |
| 68 | + */ |
| 69 | +@Plugin(type = Connector.PLUGIN_TYPE) |
| 70 | +@Name(SalesforceConstants.PLUGIN_NAME) |
| 71 | +@Description("Connection to access data in Salesforce SObject.") |
| 72 | +public class SalesforceConnector implements DirectConnector { |
| 73 | + private static final Logger LOG = LoggerFactory.getLogger(SalesforceConnector.class); |
| 74 | + private static final String ENTITY_TYPE_OBJECTS = "object"; |
| 75 | + private static final String LABEL_NAME = "label"; |
| 76 | + private final SalesforceConnectorConfig config; |
| 77 | + private StructuredRecord record; |
| 78 | + |
| 79 | + SalesforceConnector(SalesforceConnectorConfig config) { |
| 80 | + this.config = config; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void test(ConnectorContext connectorContext) throws ValidationException { |
| 85 | + FailureCollector collector = connectorContext.getFailureCollector(); |
| 86 | + config.validate(collector); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public BrowseDetail browse(ConnectorContext connectorContext, BrowseRequest browseRequest) throws IOException { |
| 91 | + AuthenticatorCredentials credentials = new AuthenticatorCredentials(config.getUsername(), config.getPassword(), |
| 92 | + config.getConsumerKey(), |
| 93 | + config.getConsumerSecret(), |
| 94 | + config.getLoginUrl()); |
| 95 | + BrowseDetail.Builder browseDetailBuilder = BrowseDetail.builder(); |
| 96 | + int count = 0; |
| 97 | + try { |
| 98 | + PartnerConnection partnerConnection = SalesforceConnectionUtil.getPartnerConnection(credentials); |
| 99 | + DescribeGlobalResult dgr = partnerConnection.describeGlobal(); |
| 100 | + // Loop through the array to get all the objects. |
| 101 | + for (int i = 0; i < dgr.getSobjects().length; i++) { |
| 102 | + String name = dgr.getSobjects()[i].getName(); |
| 103 | + String label = dgr.getSobjects()[i].getLabel(); |
| 104 | + BrowseEntity.Builder entity = (BrowseEntity.builder(name, name, ENTITY_TYPE_OBJECTS). |
| 105 | + canBrowse(false).canSample(true)); |
| 106 | + entity.addProperty(LABEL_NAME, BrowseEntityPropertyValue.builder(label, BrowseEntityPropertyValue. |
| 107 | + PropertyType.STRING).build()); |
| 108 | + browseDetailBuilder.addEntity(entity.build()); |
| 109 | + count++; |
| 110 | + } |
| 111 | + } catch (ConnectionException e) { |
| 112 | + throw new IOException("Unable to create the connection.", e); |
| 113 | + } |
| 114 | + return browseDetailBuilder.setTotalCount(count).build(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public ConnectorSpec generateSpec(ConnectorContext connectorContext, ConnectorSpecRequest connectorSpecRequest) |
| 119 | + throws IOException { |
| 120 | + ConnectorSpec.Builder specBuilder = ConnectorSpec.builder(); |
| 121 | + Map<String, String> properties = new HashMap<>(); |
| 122 | + properties.put(io.cdap.plugin.common.ConfigUtil.NAME_USE_CONNECTION, "true"); |
| 123 | + properties.put(ConfigUtil.NAME_CONNECTION, connectorSpecRequest.getConnectionWithMacro()); |
| 124 | + String tableName = connectorSpecRequest.getPath(); |
| 125 | + if (tableName != null) { |
| 126 | + properties.put(SalesforceSourceConstants.PROPERTY_SOBJECT_NAME, tableName); |
| 127 | + properties.put(SalesforceSinkConfig.PROPERTY_SOBJECT, tableName); |
| 128 | + } |
| 129 | + AuthenticatorCredentials authenticatorCredentials = config.getAuthenticatorCredentials(); |
| 130 | + try { |
| 131 | + SObjectDescriptor sObjectDescriptor = SObjectDescriptor.fromName(tableName, authenticatorCredentials); |
| 132 | + Schema schema = SalesforceSchemaUtil.getSchema(authenticatorCredentials, sObjectDescriptor); |
| 133 | + specBuilder.setSchema(schema); |
| 134 | + } catch (ConnectionException e) { |
| 135 | + throw new IOException("Unable to generate Schema", e); |
| 136 | + } |
| 137 | + return specBuilder.addRelatedPlugin(new PluginSpec(SalesforceBatchSource.NAME, BatchSource.PLUGIN_TYPE, |
| 138 | + properties)). |
| 139 | + addRelatedPlugin(new PluginSpec(SalesforceBatchSink.PLUGIN_NAME, BatchSink.PLUGIN_TYPE, properties)).build(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public List<StructuredRecord> sample(ConnectorContext connectorContext, SampleRequest sampleRequest) |
| 144 | + throws IOException { |
| 145 | + String object = sampleRequest.getPath(); |
| 146 | + if (object == null) { |
| 147 | + throw new IllegalArgumentException("Path should contain object"); |
| 148 | + } |
| 149 | + try { |
| 150 | + return listObjectDetails(object, sampleRequest.getLimit()); |
| 151 | + } catch (AsyncApiException | ConnectionException e) { |
| 152 | + throw new IOException("unable to fetch records", e); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private List<StructuredRecord> listObjectDetails(String object, int limit) throws AsyncApiException, |
| 157 | + ConnectionException { |
| 158 | + List<StructuredRecord> samples = new ArrayList<>(); |
| 159 | + AuthenticatorCredentials credentials = new AuthenticatorCredentials(config.getUsername(), config.getPassword(), |
| 160 | + config.getConsumerKey(), |
| 161 | + config.getConsumerSecret(), |
| 162 | + config.getLoginUrl()); |
| 163 | + String fields = getObjectFields(object); |
| 164 | + String query = String.format("SELECT %s FROM %s LIMIT %d", fields, object, limit); |
| 165 | + SObjectDescriptor sObjectDescriptor = SObjectDescriptor.fromQuery(query); |
| 166 | + SoapRecordToMapTransformer soapRecordToMapTransformer = new SoapRecordToMapTransformer(); |
| 167 | + PartnerConnection partnerConnection = SalesforceConnectionUtil.getPartnerConnection(credentials); |
| 168 | + QueryResult queryResult = partnerConnection.query(query); |
| 169 | + SObject[] sObjects = queryResult.getRecords(); |
| 170 | + Schema schema = SalesforceSchemaUtil.getSchema(credentials, sObjectDescriptor); |
| 171 | + MapToRecordTransformer transformer = new MapToRecordTransformer(); |
| 172 | + for (int i = 0; i < sObjects.length; i++) { |
| 173 | + record = transformer.transform(schema, soapRecordToMapTransformer.transformToMap(sObjects[i], sObjectDescriptor)); |
| 174 | + samples.add(record); |
| 175 | + } |
| 176 | + |
| 177 | + return samples; |
| 178 | + } |
| 179 | + |
| 180 | + private String getObjectFields(String object) throws ConnectionException { |
| 181 | + SObjectDescriptor sObjectDescriptor = SObjectDescriptor.fromName(object, config.getAuthenticatorCredentials(), |
| 182 | + SalesforceSchemaUtil.COMPOUND_FIELDS); |
| 183 | + List<String> actualFields = sObjectDescriptor.getFieldsNames(); |
| 184 | + String result = String.join(",", actualFields); |
| 185 | + return result; |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments