Search in sources :

Example 1 with PushTopic

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

the class PushTopicHelper method createTopic.

private void createTopic() throws CamelException {
    final PushTopic topic = new PushTopic();
    topic.setName(topicName);
    topic.setApiVersion(Double.valueOf(config.getApiVersion()));
    topic.setQuery(config.getSObjectQuery());
    topic.setDescription("Topic created by Camel Salesforce component");
    topic.setNotifyForFields(config.getNotifyForFields());
    if (preApi29) {
        topic.setNotifyForOperations(config.getNotifyForOperations());
    } else {
        topic.setNotifyForOperationCreate(config.getNotifyForOperationCreate());
        topic.setNotifyForOperationDelete(config.getNotifyForOperationDelete());
        topic.setNotifyForOperationUndelete(config.getNotifyForOperationUndelete());
        topic.setNotifyForOperationUpdate(config.getNotifyForOperationUpdate());
    }
    LOG.info("Creating Topic {}: {}", topicName, topic);
    final SyncResponseCallback callback = new SyncResponseCallback();
    try {
        restClient.createSObject(PUSH_TOPIC_OBJECT_NAME, new ByteArrayInputStream(OBJECT_MAPPER.writeValueAsBytes(topic)), 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;
        }
        CreateSObjectResult result = OBJECT_MAPPER.readValue(callback.getResponse(), CreateSObjectResult.class);
        if (!result.getSuccess()) {
            final SalesforceException salesforceException = new SalesforceException(result.getErrors(), HttpStatus.BAD_REQUEST_400);
            throw new CamelException(String.format("Error creating Topic %s: %s", topicName, result.getErrors()), salesforceException);
        }
    } catch (SalesforceException e) {
        throw new CamelException(String.format("Error creating Topic %s: %s", topicName, e.getMessage()), e);
    } catch (IOException e) {
        throw new CamelException(String.format("Un-marshaling error creating Topic %s: %s", topicName, e.getMessage()), e);
    } catch (InterruptedException e) {
        throw new CamelException(String.format("Un-marshaling error creating Topic %s: %s", topicName, e.getMessage()), e);
    } finally {
        if (callback.getResponse() != null) {
            try {
                callback.getResponse().close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : PushTopic(org.apache.camel.component.salesforce.internal.dto.PushTopic) QueryRecordsPushTopic(org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) CreateSObjectResult(org.apache.camel.component.salesforce.api.dto.CreateSObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) CamelException(org.apache.camel.CamelException) IOException(java.io.IOException) SyncResponseCallback(org.apache.camel.component.salesforce.internal.client.SyncResponseCallback)

Example 2 with PushTopic

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

the class PushTopicHelper method updateTopic.

private void updateTopic(String topicId) throws CamelException {
    final String query = config.getSObjectQuery();
    LOG.info("Updating Topic {} with Query [{}]", topicName, query);
    final SyncResponseCallback callback = new SyncResponseCallback();
    try {
        // update the query, notifyForFields and notifyForOperations fields
        final PushTopic topic = new PushTopic();
        topic.setQuery(query);
        topic.setNotifyForFields(config.getNotifyForFields());
        if (preApi29) {
            topic.setNotifyForOperations(config.getNotifyForOperations());
        } else {
            topic.setNotifyForOperationCreate(config.getNotifyForOperationCreate());
            topic.setNotifyForOperationDelete(config.getNotifyForOperationDelete());
            topic.setNotifyForOperationUndelete(config.getNotifyForOperationUndelete());
            topic.setNotifyForOperationUpdate(config.getNotifyForOperationUpdate());
        }
        restClient.updateSObject("PushTopic", topicId, new ByteArrayInputStream(OBJECT_MAPPER.writeValueAsBytes(topic)), 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;
        }
    } catch (SalesforceException e) {
        throw new CamelException(String.format("Error updating topic %s with query [%s] : %s", topicName, query, e.getMessage()), e);
    } catch (InterruptedException e) {
        // reset interrupt status
        Thread.currentThread().interrupt();
        throw new CamelException(String.format("Error updating topic %s with query [%s] : %s", topicName, query, e.getMessage()), e);
    } catch (IOException e) {
        throw new CamelException(String.format("Error updating topic %s with query [%s] : %s", topicName, query, e.getMessage()), e);
    } finally {
        if (callback.getResponse() != null) {
            try {
                callback.getResponse().close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : PushTopic(org.apache.camel.component.salesforce.internal.dto.PushTopic) QueryRecordsPushTopic(org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) ByteArrayInputStream(java.io.ByteArrayInputStream) CamelException(org.apache.camel.CamelException) IOException(java.io.IOException) SyncResponseCallback(org.apache.camel.component.salesforce.internal.client.SyncResponseCallback)

Example 3 with PushTopic

use of org.apache.camel.component.salesforce.internal.dto.PushTopic 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)

Aggregations

IOException (java.io.IOException)3 CamelException (org.apache.camel.CamelException)3 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)3 SyncResponseCallback (org.apache.camel.component.salesforce.internal.client.SyncResponseCallback)3 PushTopic (org.apache.camel.component.salesforce.internal.dto.PushTopic)3 QueryRecordsPushTopic (org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CreateSObjectResult (org.apache.camel.component.salesforce.api.dto.CreateSObjectResult)1