Search in sources :

Example 6 with DescribeSObjectResult

use of com.sforce.soap.partner.DescribeSObjectResult in project teiid by teiid.

the class SalesForceMetadataProcessor method getColumnsAndRelationships.

private void getColumnsAndRelationships(List<String> names) throws TranslatorException {
    try {
        DescribeSObjectResult[] objectMetadatas = connection.getObjectMetaData(names.toArray(new String[names.size()]));
        for (DescribeSObjectResult objectMetadata : objectMetadatas) {
            getRelationships(objectMetadata);
            Table table = this.tableMap.get(objectMetadata.getName());
            boolean hasUpdateableColumn = addColumns(objectMetadata, table);
            // Some SF objects return true for isUpdateable() but have no updateable columns.
            if (objectMetadata.isDeletable() || (hasUpdateableColumn && (objectMetadata.isUpdateable() || objectMetadata.isCreateable()))) {
                table.setSupportsUpdate(true);
            }
        }
    } catch (ResourceException e) {
        throw new TranslatorException(e);
    }
    names.clear();
}
Also used : Table(org.teiid.metadata.Table) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) ResourceException(javax.resource.ResourceException) TranslatorException(org.teiid.translator.TranslatorException)

Example 7 with DescribeSObjectResult

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

the class SalesforceSourceOrSink method guessSchema.

@Override
public Schema guessSchema(String soqlQuery) throws IOException {
    SoqlQuery query = SoqlQuery.getInstance();
    query.init(soqlQuery);
    SchemaBuilder.FieldAssembler fieldAssembler = SchemaBuilder.record("GuessedSchema").fields();
    DescribeSObjectResult describeSObjectResult = null;
    try {
        describeSObjectResult = connect(null).connection.describeSObject(query.getDrivingEntityName());
    } catch (ConnectionException e) {
        throw new RuntimeException(e.getMessage());
    }
    Schema entitySchema = SalesforceAvroRegistry.get().inferSchema(describeSObjectResult);
    for (FieldDescription fieldDescription : query.getFieldDescriptions()) {
        Schema.Field schemaField = entitySchema.getField(fieldDescription.getSimpleName());
        SchemaBuilder.FieldBuilder builder = fieldAssembler.name(fieldDescription.getFullName());
        Schema fieldType = null;
        if (schemaField != null) {
            Map<String, Object> props = schemaField.getObjectProps();
            for (Map.Entry<String, Object> entry : props.entrySet()) {
                builder.prop(entry.getKey(), String.valueOf(entry.getValue()));
            }
            fieldType = schemaField.schema();
        } else {
            fieldType = DEFAULT_GUESS_SCHEMA_TYPE;
        }
        builder.type(fieldType).noDefault();
    }
    return (Schema) fieldAssembler.endRecord();
}
Also used : DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) Schema(org.apache.avro.Schema) FieldDescription(org.talend.components.salesforce.soql.FieldDescription) SchemaBuilder(org.apache.avro.SchemaBuilder) SoqlQuery(org.talend.components.salesforce.soql.SoqlQuery) Map(java.util.Map) ConnectionException(com.sforce.ws.ConnectionException)

Example 8 with DescribeSObjectResult

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

the class SalesforceSourceOrSink method getSchema.

protected Schema getSchema(PartnerConnection connection, String module) throws IOException {
    try {
        DescribeSObjectResult[] describeSObjectResults = new DescribeSObjectResult[0];
        describeSObjectResults = connection.describeSObjects(new String[] { module });
        return SalesforceAvroRegistry.get().inferSchema(describeSObjectResults[0]);
    } catch (ConnectionException e) {
        throw new IOException(e);
    }
}
Also used : DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) IOException(java.io.IOException) ConnectionException(com.sforce.ws.ConnectionException)

Example 9 with DescribeSObjectResult

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

the class SalesforceAvroRegistryTest method testInferSchemaDescribeSObjectResult.

/**
 * Tests that the {@link SalesforceAvroRegistry} has added support to get a {@link Schema} that describes
 * {@link DescribeSObjectResult} objects.
 */
@Test
public void testInferSchemaDescribeSObjectResult() {
    Schema s;
    // Setup.
    {
        Field booleanField = new Field();
        booleanField.setName("valid");
        booleanField.setType(FieldType._boolean);
        Field defaultField = new Field();
        defaultField.setName("def");
        defaultField.setType(FieldType._boolean);
        defaultField.setDefaultValueFormula(Boolean.TRUE.toString());
        Field dateField = new Field();
        dateField.setName("date");
        dateField.setType(FieldType.date);
        Field stringWithLengthField = new Field();
        stringWithLengthField.setName("string_with_length");
        stringWithLengthField.setType(FieldType.string);
        stringWithLengthField.setLength(20);
        Field numberWithScaleAndPrecisionField = new Field();
        numberWithScaleAndPrecisionField.setName("number_with_scale_and_precision");
        numberWithScaleAndPrecisionField.setType(FieldType._double);
        numberWithScaleAndPrecisionField.setPrecision(10);
        numberWithScaleAndPrecisionField.setScale(2);
        Field doubleWithNullable = new Field();
        doubleWithNullable.setName("double_with_nullable");
        doubleWithNullable.setType(FieldType._double);
        doubleWithNullable.setPrecision(18);
        doubleWithNullable.setScale(15);
        doubleWithNullable.setNillable(true);
        DescribeSObjectResult dsor = new DescribeSObjectResult();
        dsor.setName("MySObjectRecord");
        dsor.setFields(new Field[] { booleanField, defaultField, dateField, stringWithLengthField, numberWithScaleAndPrecisionField, doubleWithNullable });
        s = sRegistry.inferSchema(dsor);
    }
    assertThat(s.getType(), is(Schema.Type.RECORD));
    assertThat(s.getName(), is("MySObjectRecord"));
    assertThat(s.getFields(), hasSize(6));
    assertThat(s.getObjectProps().keySet(), empty());
    // Check out the field.
    Schema.Field f = s.getFields().get(0);
    assertThat(f.name(), is("valid"));
    assertThat(f.schema().getType(), is(Schema.Type.BOOLEAN));
    assertThat(f.schema().getObjectProps().keySet(), empty());
    f = s.getField("def");
    assertThat(f.name(), is("def"));
    assertThat(f.schema().getType(), is(Schema.Type.BOOLEAN));
    assertThat(f.getObjectProps().keySet(), containsInAnyOrder(TALEND_COLUMN_DEFAULT));
    assertThat(f.getProp(TALEND_COLUMN_DEFAULT), is(Boolean.TRUE.toString()));
    f = s.getField("date");
    assertThat(f.name(), is("date"));
    assertTrue(AvroUtils.isSameType(f.schema(), AvroUtils._date()));
    assertThat(f.getObjectProps().keySet(), containsInAnyOrder(SchemaConstants.TALEND_COLUMN_PATTERN));
    assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_PATTERN), is("yyyy-MM-dd"));
    f = s.getField("string_with_length");
    assertThat(f.name(), is("string_with_length"));
    assertTrue(AvroUtils.isSameType(f.schema(), AvroUtils._string()));
    assertThat(f.getObjectProps().keySet(), containsInAnyOrder(SchemaConstants.TALEND_COLUMN_DB_LENGTH));
    assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH), is("20"));
    f = s.getField("number_with_scale_and_precision");
    assertThat(f.name(), is("number_with_scale_and_precision"));
    assertTrue(AvroUtils.isSameType(f.schema(), AvroUtils._double()));
    assertThat(f.getObjectProps().keySet(), containsInAnyOrder(SchemaConstants.TALEND_COLUMN_DB_LENGTH, SchemaConstants.TALEND_COLUMN_PRECISION));
    assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH), is("10"));
    assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_PRECISION), is("2"));
    f = s.getField("double_with_nullable");
    assertThat(f.name(), is("double_with_nullable"));
    assertThat(f.schema().getType(), is(Schema.Type.UNION));
    assertThat(f.schema().getTypes(), containsInAnyOrder(AvroUtils._double(), Schema.create(Schema.Type.NULL)));
    assertThat(f.getObjectProps().keySet(), containsInAnyOrder(SchemaConstants.TALEND_COLUMN_DB_LENGTH, SchemaConstants.TALEND_COLUMN_PRECISION));
    assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH), is("18"));
    assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_PRECISION), is("15"));
}
Also used : Field(com.sforce.soap.partner.Field) Schema(org.apache.avro.Schema) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) Test(org.junit.Test)

Example 10 with DescribeSObjectResult

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

the class SalesforceAvroRegistryTest method testInferSchemaWithReferenceField.

@Test
public void testInferSchemaWithReferenceField() {
    Field referenceField = new Field();
    referenceField.setName("reference");
    referenceField.setType(FieldType.string);
    referenceField.setReferenceTo(new String[] { "SomeRecord" });
    referenceField.setRelationshipName("relationship");
    DescribeSObjectResult dsor = new DescribeSObjectResult();
    dsor.setName("MySObjectRecord");
    dsor.setFields(new Field[] { referenceField });
    Schema schema = sRegistry.inferSchema(dsor);
    Schema.Field field = schema.getField("reference");
    assertThat(field.schema().getType(), is(Schema.Type.STRING));
    assertThat(field.getProp(SalesforceSchemaConstants.REF_MODULE_NAME), is("SomeRecord"));
    assertThat(field.getProp(SalesforceSchemaConstants.REF_FIELD_NAME), is("relationship"));
}
Also used : Field(com.sforce.soap.partner.Field) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) Schema(org.apache.avro.Schema) Test(org.junit.Test)

Aggregations

DescribeSObjectResult (com.sforce.soap.partner.DescribeSObjectResult)14 Schema (org.apache.avro.Schema)10 Field (com.sforce.soap.partner.Field)8 Test (org.junit.Test)8 ConnectionException (com.sforce.ws.ConnectionException)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 FieldDescription (org.talend.components.salesforce.soql.FieldDescription)2 SoqlQuery (org.talend.components.salesforce.soql.SoqlQuery)2 DeletedRecord (com.sforce.soap.partner.DeletedRecord)1 GetDeletedResult (com.sforce.soap.partner.GetDeletedResult)1 GetUpdatedResult (com.sforce.soap.partner.GetUpdatedResult)1 PartnerConnection (com.sforce.soap.partner.PartnerConnection)1 SObject (com.sforce.soap.partner.sobject.SObject)1 Date (java.util.Date)1 ResourceException (javax.resource.ResourceException)1 SOAPException (javax.xml.soap.SOAPException)1 SchemaBuilder (org.apache.avro.SchemaBuilder)1 KettleException (org.pentaho.di.core.exception.KettleException)1