Search in sources :

Example 1 with DescribeSObjectResult

use of com.sforce.soap.partner.DescribeSObjectResult in project pentaho-kettle by pentaho.

the class SalesforceConnection method query.

public void query(boolean specifyQuery) throws KettleException {
    if (getBinding() == null) {
        throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.Exception.CanNotGetBiding"));
    }
    try {
        if (!specifyQuery) {
            // check if we can query this Object
            DescribeSObjectResult describeSObjectResult = getBinding().describeSObject(getModule());
            if (describeSObjectResult == null) {
                throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.ErrorGettingObject"));
            }
            if (!describeSObjectResult.isQueryable()) {
                throw new KettleException(BaseMessages.getString(PKG, "SalesforceInputDialog.ObjectNotQueryable", module));
            }
            if (this.recordsFilter == SalesforceConnectionUtils.RECORDS_FILTER_UPDATED || this.recordsFilter == SalesforceConnectionUtils.RECORDS_FILTER_DELETED) {
                // The object must be replicateable
                if (!describeSObjectResult.isReplicateable()) {
                    throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.Error.ObjectNotReplicateable", getModule()));
                }
            }
        }
        if (getSQL() != null && log.isDetailed()) {
            log.logDetailed(BaseMessages.getString(PKG, "SalesforceInput.Log.SQLString") + " : " + getSQL());
        }
        switch(this.recordsFilter) {
            case SalesforceConnectionUtils.RECORDS_FILTER_UPDATED:
                // Updated records ...
                GetUpdatedResult updatedRecords = getBinding().getUpdated(getModule(), this.startDate, this.endDate);
                if (updatedRecords.getIds() != null) {
                    int nr = updatedRecords.getIds().length;
                    if (nr > 0) {
                        String[] ids = updatedRecords.getIds();
                        // We can pass a maximum of 2000 object IDs
                        if (nr > SalesforceConnectionUtils.MAX_UPDATED_OBJECTS_IDS) {
                            this.sObjects = new SObject[nr];
                            List<String> list = new ArrayList<String>();
                            int desPos = 0;
                            for (int i = 0; i < nr; i++) {
                                list.add(updatedRecords.getIds()[i]);
                                if (i % SalesforceConnectionUtils.MAX_UPDATED_OBJECTS_IDS == 0 || i == nr - 1) {
                                    SObject[] s = getBinding().retrieve(this.fieldsList, getModule(), list.toArray(new String[list.size()]));
                                    System.arraycopy(s, 0, this.sObjects, desPos, s.length);
                                    desPos += s.length;
                                    s = null;
                                    list = new ArrayList<String>();
                                }
                            }
                        } else {
                            this.sObjects = getBinding().retrieve(this.fieldsList, getModule(), ids);
                        }
                        if (this.sObjects != null) {
                            this.queryResultSize = this.sObjects.length;
                        }
                    }
                }
                break;
            case SalesforceConnectionUtils.RECORDS_FILTER_DELETED:
                // Deleted records ...
                GetDeletedResult deletedRecordsResult = getBinding().getDeleted(getModule(), this.startDate, this.endDate);
                DeletedRecord[] deletedRecords = deletedRecordsResult.getDeletedRecords();
                if (log.isDebug()) {
                    log.logDebug(toString(), BaseMessages.getString(PKG, "SalesforceConnection.DeletedRecordsFound", String.valueOf(deletedRecords == null ? 0 : deletedRecords.length)));
                }
                if (deletedRecords != null && deletedRecords.length > 0) {
                    getDeletedList = new HashMap<String, Date>();
                    for (DeletedRecord dr : deletedRecords) {
                        getDeletedList.put(dr.getId(), dr.getDeletedDate().getTime());
                    }
                    this.qr = getBinding().queryAll(getSQL());
                    this.sObjects = getQueryResult().getRecords();
                    if (this.sObjects != null) {
                        this.queryResultSize = this.sObjects.length;
                    }
                }
                break;
            default:
                // return query result
                this.qr = isQueryAll() ? getBinding().queryAll(getSQL()) : getBinding().query(getSQL());
                this.sObjects = getQueryResult().getRecords();
                this.queryResultSize = getQueryResult().getSize();
                break;
        }
        if (this.sObjects != null) {
            this.recordsCount = this.sObjects.length;
        }
    } catch (Exception e) {
        log.logError(Const.getStackTracker(e));
        throw new KettleException(BaseMessages.getString(PKG, "SalesforceConnection.Exception.Query"), e);
    }
}
Also used : GetUpdatedResult(com.sforce.soap.partner.GetUpdatedResult) KettleException(org.pentaho.di.core.exception.KettleException) DeletedRecord(com.sforce.soap.partner.DeletedRecord) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) ArrayList(java.util.ArrayList) Date(java.util.Date) SOAPException(javax.xml.soap.SOAPException) KettleException(org.pentaho.di.core.exception.KettleException) ConnectionException(com.sforce.ws.ConnectionException) IOException(java.io.IOException) SObject(com.sforce.soap.partner.sobject.SObject) GetDeletedResult(com.sforce.soap.partner.GetDeletedResult)

Example 2 with DescribeSObjectResult

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

the class SalesforceAvroRegistryStringTest method testPickList.

@Test
public void testPickList() throws Exception {
    DescribeSObjectResult describeSObjectResult = new DescribeSObjectResult();
    Field pickList = new Field();
    pickList.setName("pickList");
    pickList.setType(FieldType.picklist);
    describeSObjectResult.setFields(new Field[] { pickList });
    Schema schema = SalesforceAvroRegistryString.get().inferSchema(describeSObjectResult);
    assertThat(1, 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 3 with DescribeSObjectResult

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

the class SalesforceAvroRegistryStringTest method testPickListWithParent.

@Test
public void testPickListWithParent() throws Exception {
    DescribeSObjectResult describeSObjectResult = new DescribeSObjectResult();
    Field pickList = new Field();
    pickList.setName("pickList");
    pickList.setType(FieldType.picklist);
    pickList.setCompoundFieldName("parent");
    describeSObjectResult.setFields(new Field[] { pickList });
    Schema schema = SalesforceAvroRegistryString.get().inferSchema(describeSObjectResult);
    assertTrue(schema.getFields().isEmpty());
}
Also used : Field(com.sforce.soap.partner.Field) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) Schema(org.apache.avro.Schema) Test(org.junit.Test)

Example 4 with DescribeSObjectResult

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

the class SalesforceDataprepSource method guessSchema.

@Override
public Schema guessSchema(String soqlQuery) throws IOException {
    SoqlQuery query = SoqlQuery.getInstance();
    query.init(soqlQuery);
    List<FieldDescription> fieldDescriptions = query.getFieldDescriptions();
    String drivingEntityName = query.getDrivingEntityName();
    DescribeSObjectResult describeSObjectResult = null;
    try {
        describeSObjectResult = connectionHolder.connection.describeSObject(drivingEntityName);
    } catch (ConnectionException e) {
        throw new RuntimeException(e);
    }
    Schema runtimeSchema = SalesforceAvroRegistryString.get().inferSchema(describeSObjectResult);
    Schema newSchema = Schema.createRecord("GuessedSchema", runtimeSchema.getDoc(), runtimeSchema.getNamespace(), runtimeSchema.isError());
    List<Schema.Field> newFieldList = new ArrayList<>();
    for (FieldDescription fieldDescription : fieldDescriptions) {
        Schema.Field runtimeField = runtimeSchema.getField(fieldDescription.getFullName());
        if (runtimeField != null) {
            Schema.Field newField = new Schema.Field(runtimeField.name(), runtimeField.schema(), runtimeField.doc(), runtimeField.defaultVal(), runtimeField.order());
            newField.getObjectProps().putAll(runtimeField.getObjectProps());
            for (Map.Entry<String, Object> entry : runtimeField.getObjectProps().entrySet()) {
                newField.addProp(entry.getKey(), entry.getValue());
            }
            newFieldList.add(newField);
        } else {
            Schema.Field newField = new Schema.Field(fieldDescription.getFullName(), AvroUtils._string(), null, (String) null);
            newFieldList.add(newField);
        }
    }
    newSchema.setFields(newFieldList);
    for (Map.Entry<String, Object> entry : runtimeSchema.getObjectProps().entrySet()) {
        newSchema.addProp(entry.getKey(), entry.getValue());
    }
    return newSchema;
}
Also used : DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) FieldDescription(org.talend.components.salesforce.soql.FieldDescription) SoqlQuery(org.talend.components.salesforce.soql.SoqlQuery) Map(java.util.Map) ConnectionException(com.sforce.ws.ConnectionException)

Example 5 with DescribeSObjectResult

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

the class SalesforceDataprepSource method getEndpointSchema.

@Override
public Schema getEndpointSchema(RuntimeContainer container, String schemaName) throws IOException {
    try {
        DescribeSObjectResult[] describeSObjectResults = new DescribeSObjectResult[0];
        describeSObjectResults = connectionHolder.connection.describeSObjects(new String[] { schemaName });
        return SalesforceAvroRegistryString.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)

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