Search in sources :

Example 1 with QueryRecordsPushTopic

use of org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic in project camel by apache.

the class PushTopicHelper method createOrUpdateTopic.

public void createOrUpdateTopic() throws CamelException {
    final String query = config.getSObjectQuery();
    final SyncResponseCallback callback = new SyncResponseCallback();
    // lookup Topic first
    try {
        // use SOQL to lookup Topic, since Name is not an external ID!!!
        restClient.query("SELECT Id, Name, Query, ApiVersion, IsActive, " + "NotifyForFields, NotifyForOperations, NotifyForOperationCreate, " + "NotifyForOperationDelete, NotifyForOperationUndelete, " + "NotifyForOperationUpdate, Description " + "FROM PushTopic WHERE Name = '" + topicName + "'", callback);
        if (!callback.await(API_TIMEOUT, TimeUnit.SECONDS)) {
            throw new SalesforceException("API call timeout!", null);
        }
        final SalesforceException callbackException = callback.getException();
        if (callbackException != null) {
            throw callbackException;
        }
        QueryRecordsPushTopic records = OBJECT_MAPPER.readValue(callback.getResponse(), QueryRecordsPushTopic.class);
        if (records.getTotalSize() == 1) {
            PushTopic topic = records.getRecords().get(0);
            LOG.info("Found existing topic {}: {}", topicName, topic);
            // check if we need to update topic
            final boolean notifyOperationsChanged;
            if (preApi29) {
                notifyOperationsChanged = notEquals(config.getNotifyForOperations(), topic.getNotifyForOperations());
            } else {
                notifyOperationsChanged = notEquals(config.getNotifyForOperationCreate(), topic.getNotifyForOperationCreate()) || notEquals(config.getNotifyForOperationDelete(), topic.getNotifyForOperationDelete()) || notEquals(config.getNotifyForOperationUndelete(), topic.getNotifyForOperationUndelete()) || notEquals(config.getNotifyForOperationUpdate(), topic.getNotifyForOperationUpdate());
            }
            if (!query.equals(topic.getQuery()) || notEquals(config.getNotifyForFields(), topic.getNotifyForFields()) || notifyOperationsChanged) {
                if (!config.isUpdateTopic()) {
                    String msg = "Query doesn't match existing Topic and updateTopic is set to false";
                    throw new CamelException(msg);
                }
                // otherwise update the topic
                updateTopic(topic.getId());
            }
        } else {
            createTopic();
        }
    } catch (SalesforceException e) {
        throw new CamelException(String.format("Error retrieving Topic %s: %s", topicName, e.getMessage()), e);
    } catch (IOException e) {
        throw new CamelException(String.format("Un-marshaling error retrieving Topic %s: %s", topicName, e.getMessage()), e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new CamelException(String.format("Un-marshaling error retrieving Topic %s: %s", topicName, e.getMessage()), e);
    } finally {
        // close stream to close HttpConnection
        if (callback.getResponse() != null) {
            try {
                callback.getResponse().close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) PushTopic(org.apache.camel.component.salesforce.internal.dto.PushTopic) QueryRecordsPushTopic(org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic) QueryRecordsPushTopic(org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic) CamelException(org.apache.camel.CamelException) IOException(java.io.IOException) SyncResponseCallback(org.apache.camel.component.salesforce.internal.client.SyncResponseCallback)

Example 2 with QueryRecordsPushTopic

use of org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic in project camel by apache.

the class StreamingApiIntegrationTest method testSubscribeAndReceive.

@Test
public void testSubscribeAndReceive() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:CamelTestTopic");
    mock.expectedMessageCount(1);
    // assert expected static headers
    mock.expectedHeaderReceived("CamelSalesforceTopicName", "CamelTestTopic");
    mock.expectedHeaderReceived("CamelSalesforceChannel", "/topic/CamelTestTopic");
    Merchandise__c merchandise = new Merchandise__c();
    merchandise.setName("TestNotification");
    merchandise.setDescription__c("Merchandise for testing Streaming API updated on " + ZonedDateTime.now().toString());
    merchandise.setPrice__c(9.99);
    merchandise.setTotal_Inventory__c(1000.0);
    CreateSObjectResult result = template().requestBody("direct:upsertSObject", merchandise, CreateSObjectResult.class);
    assertTrue("Merchandise test record not created", result == null || result.getSuccess());
    try {
        // wait for Salesforce notification
        mock.assertIsSatisfied();
        final Message in = mock.getExchanges().get(0).getIn();
        merchandise = in.getMandatoryBody(Merchandise__c.class);
        assertNotNull("Missing event body", merchandise);
        log.info("Merchandise notification: {}", merchandise.toString());
        assertNotNull("Missing field Id", merchandise.getId());
        assertNotNull("Missing field Name", merchandise.getName());
        // validate dynamic message headers
        assertNotNull("Missing header CamelSalesforceClientId", in.getHeader("CamelSalesforceClientId"));
        assertNotNull("Missing header CamelSalesforceEventType", in.getHeader("CamelSalesforceEventType"));
        assertNotNull("Missing header CamelSalesforceCreatedDate", in.getHeader("CamelSalesforceCreatedDate"));
    } finally {
        // remove the test record
        assertNull(template().requestBody("direct:deleteSObjectWithId", merchandise));
        // remove the test topic
        // find it using SOQL first
        QueryRecordsPushTopic records = template().requestBody("direct:query", null, QueryRecordsPushTopic.class);
        assertEquals("Test topic not found", 1, records.getTotalSize());
        assertNull(template().requestBody("direct:deleteSObject", records.getRecords().get(0)));
    }
}
Also used : CreateSObjectResult(org.apache.camel.component.salesforce.api.dto.CreateSObjectResult) QueryRecordsPushTopic(org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Merchandise__c(org.apache.camel.component.salesforce.dto.generated.Merchandise__c) Test(org.junit.Test)

Aggregations

QueryRecordsPushTopic (org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic)2 IOException (java.io.IOException)1 CamelException (org.apache.camel.CamelException)1 Message (org.apache.camel.Message)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)1 CreateSObjectResult (org.apache.camel.component.salesforce.api.dto.CreateSObjectResult)1 Merchandise__c (org.apache.camel.component.salesforce.dto.generated.Merchandise__c)1 SyncResponseCallback (org.apache.camel.component.salesforce.internal.client.SyncResponseCallback)1 PushTopic (org.apache.camel.component.salesforce.internal.dto.PushTopic)1 Test (org.junit.Test)1