Search in sources :

Example 16 with Field

use of com.sforce.soap.partner.Field in project components by Talend.

the class SalesforceAvroRegistryStringTest method testLocationTypeFilter.

@Test
public void testLocationTypeFilter() throws Exception {
    DescribeSObjectResult describeSObjectResult = new DescribeSObjectResult();
    Field location = new Field();
    location.setName("location");
    location.setType(FieldType.location);
    describeSObjectResult.setFields(new Field[] { location });
    Schema schema = SalesforceAvroRegistryString.get().inferSchema(describeSObjectResult);
    assertThat(0, is(schema.getFields().size()));
}
Also used : Field(com.sforce.soap.partner.Field) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) Schema(org.apache.avro.Schema) Test(org.junit.Test)

Example 17 with Field

use of com.sforce.soap.partner.Field in project components by Talend.

the class SalesforceAvroRegistryStringTest method testAddressTypeFilter.

@Test
public void testAddressTypeFilter() throws Exception {
    DescribeSObjectResult describeSObjectResult = new DescribeSObjectResult();
    Field address = new Field();
    address.setName("address");
    address.setType(FieldType.address);
    describeSObjectResult.setFields(new Field[] { address });
    Schema schema = SalesforceAvroRegistryString.get().inferSchema(describeSObjectResult);
    assertThat(0, is(schema.getFields().size()));
}
Also used : Field(com.sforce.soap.partner.Field) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) Schema(org.apache.avro.Schema) Test(org.junit.Test)

Example 18 with Field

use of com.sforce.soap.partner.Field in project components by Talend.

the class SalesforceAvroRegistryString method inferSchemaDescribeSObjectResult.

private Schema inferSchemaDescribeSObjectResult(DescribeSObjectResult in) {
    List<Schema.Field> fields = new ArrayList<>();
    for (Field field : in.getFields()) {
        // filter the invalid compound columns for salesforce bulk query api
        if (// no address
        field.getType() == FieldType.address || // no location
        field.getType() == FieldType.location || // no picklist that has a parent
        (field.getType() == FieldType.picklist && StringUtils.isNotBlank(field.getCompoundFieldName()))) {
            continue;
        }
        Schema.Field avroField = new Schema.Field(field.getName(), salesforceField2AvroTypeSchema(field), null, field.getDefaultValueFormula());
        Schema avroFieldSchema = avroField.schema();
        if (avroFieldSchema.getType() == Schema.Type.UNION) {
            for (Schema schema : avroFieldSchema.getTypes()) {
                if (avroFieldSchema.getType() != Schema.Type.NULL) {
                    avroFieldSchema = schema;
                    break;
                }
            }
        }
        if (AvroUtils.isSameType(avroFieldSchema, AvroUtils._string())) {
            if (field.getLength() != 0) {
                avroField.addProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH, String.valueOf(field.getLength()));
            }
            if (field.getPrecision() != 0) {
                avroField.addProp(SchemaConstants.TALEND_COLUMN_PRECISION, String.valueOf(field.getPrecision()));
            }
        } else {
            if (field.getPrecision() != 0) {
                avroField.addProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH, String.valueOf(field.getPrecision()));
            }
            if (field.getScale() != 0) {
                avroField.addProp(SchemaConstants.TALEND_COLUMN_PRECISION, String.valueOf(field.getScale()));
            }
        }
        if (field.getReferenceTo() != null && field.getReferenceTo().length > 0 && field.getRelationshipName() != null) {
            avroField.addProp(SalesforceSchemaConstants.REF_MODULE_NAME, field.getReferenceTo()[0]);
            avroField.addProp(SalesforceSchemaConstants.REF_FIELD_NAME, field.getRelationshipName());
        }
        switch(field.getType()) {
            case date:
                avroField.addProp(SchemaConstants.TALEND_COLUMN_PATTERN, "yyyy-MM-dd");
                break;
            case datetime:
                avroField.addProp(SchemaConstants.TALEND_COLUMN_PATTERN, "yyyy-MM-dd'T'HH:mm:ss'.000Z'");
                break;
            case time:
                avroField.addProp(SchemaConstants.TALEND_COLUMN_PATTERN, "HH:mm:ss.SSS'Z'");
                break;
            default:
                break;
        }
        if (avroField.defaultVal() != null) {
            avroField.addProp(SchemaConstants.TALEND_COLUMN_DEFAULT, String.valueOf(avroField.defaultVal()));
        }
        fields.add(avroField);
    }
    return Schema.createRecord(in.getName(), null, null, false, fields);
}
Also used : Field(com.sforce.soap.partner.Field) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList)

Example 19 with Field

use of com.sforce.soap.partner.Field in project components by Talend.

the class SalesforceSourceOrSinkTest method testGuessSchema.

/**
 * Checks {@link SalesforceSourceOrSink#guessSchema(String)} returns the
 * {@link org.apache.avro.Schema} with date and string type
 */
@Test
public void testGuessSchema() throws Exception {
    String field0Name = "Id";
    String field1Name = "LastModifiedDate";
    String field2Name = "LastActivityDate";
    String drivingEntity = "Account";
    String soql = new StringBuilder().append("SELECT").append(" ").append(field0Name).append(",").append(SPACE).append(field1Name).append(",").append(SPACE).append(field2Name).append(SPACE).append("FROM").append(SPACE).append(drivingEntity).toString();
    final PartnerConnection partnerConnectionMock = Mockito.mock(PartnerConnection.class);
    class SalesforceSourceOrSinkChild extends SalesforceSourceOrSink {

        @Override
        protected ConnectionHolder connect(RuntimeContainer container) {
            ConnectionHolder connectionHolder = new ConnectionHolder();
            connectionHolder.connection = partnerConnectionMock;
            return connectionHolder;
        }
    }
    Field field0 = new Field();
    field0.setName(field0Name);
    field0.setType(FieldType.string);
    Field field1 = new Field();
    field1.setName(field1Name);
    field1.setType(FieldType.date);
    Field field2 = new Field();
    field2.setName(field2Name);
    field2.setType(FieldType.date);
    Field[] fields = new Field[3];
    fields[0] = field0;
    fields[1] = field1;
    fields[2] = field2;
    DescribeSObjectResult describeSObjectResult = new DescribeSObjectResult();
    describeSObjectResult.setFields(fields);
    Mockito.when(partnerConnectionMock.describeSObject(drivingEntity)).thenReturn(describeSObjectResult);
    SalesforceSourceOrSinkChild salesforceSourceOrSinkChild = new SalesforceSourceOrSinkChild();
    salesforceSourceOrSinkChild.initialize(runtimeContainerMock, properties);
    Schema resultSchema = salesforceSourceOrSinkChild.guessSchema(soql);
    LOGGER.debug("result schema: " + resultSchema.toString());
    Assert.assertEquals("GuessedSchema", resultSchema.getName());
    Assert.assertEquals(field0Name, resultSchema.getFields().get(0).name());
    Assert.assertEquals(field1Name, resultSchema.getFields().get(1).name());
    Assert.assertEquals("java.util.Date", AvroUtils.unwrapIfNullable(resultSchema.getFields().get(1).schema()).getProp("java-class"));
    Assert.assertEquals("yyyy-MM-dd", resultSchema.getFields().get(1).getProp(SchemaConstants.TALEND_COLUMN_PATTERN));
    Assert.assertEquals(field2Name, resultSchema.getFields().get(2).name());
    Assert.assertEquals("java.util.Date", AvroUtils.unwrapIfNullable(resultSchema.getFields().get(2).schema()).getProp("java-class"));
    Assert.assertEquals("yyyy-MM-dd", resultSchema.getFields().get(2).getProp(SchemaConstants.TALEND_COLUMN_PATTERN));
}
Also used : Field(com.sforce.soap.partner.Field) PartnerConnection(com.sforce.soap.partner.PartnerConnection) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) Schema(org.apache.avro.Schema) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder) Test(org.junit.Test)

Aggregations

Field (com.sforce.soap.partner.Field)19 Schema (org.apache.avro.Schema)12 Test (org.junit.Test)11 DescribeSObjectResult (com.sforce.soap.partner.DescribeSObjectResult)8 ArrayList (java.util.ArrayList)6 FieldType (com.sforce.soap.partner.FieldType)2 KettleException (org.pentaho.di.core.exception.KettleException)2 SalesforceConnection (org.pentaho.di.trans.steps.salesforce.SalesforceConnection)2 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)2 PartnerConnection (com.sforce.soap.partner.PartnerConnection)1 XmlObject (com.sforce.ws.bind.XmlObject)1 HashSet (java.util.HashSet)1 Type (org.apache.avro.Schema.Type)1 TableItem (org.eclipse.swt.widgets.TableItem)1 SourceToTargetMapping (org.pentaho.di.core.SourceToTargetMapping)1 RowMeta (org.pentaho.di.core.row.RowMeta)1 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1 ValueMetaNone (org.pentaho.di.core.row.value.ValueMetaNone)1 SalesforceInputField (org.pentaho.di.trans.steps.salesforceinput.SalesforceInputField)1