Package org.apache.camel.component.salesforce.internal.client

Examples of org.apache.camel.component.salesforce.internal.client.SyncResponseCallback


            // use Jackson json
            final ObjectMapper mapper = new ObjectMapper();

            // call getGlobalObjects to get all SObjects
            final Set<String> objectNames = new HashSet<String>();
            final SyncResponseCallback callback = new SyncResponseCallback();
            try {
                getLog().info("Getting Salesforce Objects...");
                restClient.getGlobalObjects(callback);
                if (!callback.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
                    throw new MojoExecutionException("Timeout waiting for getGlobalObjects!");
                }
                final SalesforceException ex = callback.getException();
                if (ex != null) {
                    throw ex;
                }
                final GlobalObjects globalObjects = mapper.readValue(callback.getResponse(),
                        GlobalObjects.class);

                // create a list of object names
                for (SObject sObject : globalObjects.getSobjects()) {
                    objectNames.add(sObject.getName());
                }
            } catch (Exception e) {
                String msg = "Error getting global Objects " + e.getMessage();
                throw new MojoExecutionException(msg, e);
            }

            // check if we are generating POJOs for all objects or not
            if ((includes != null && includes.length > 0)
                    || (excludes != null && excludes.length > 0)
                    || (includePattern != null && !includePattern.trim().isEmpty())
                    || (excludePattern != null && !excludePattern.trim().isEmpty())) {

                getLog().info("Looking for matching Object names...");
                // create a list of accepted names
                final Set<String> includedNames = new HashSet<String>();
                if (includes != null && includes.length > 0) {
                    for (String name : includes) {
                        name = name.trim();
                        if (name.isEmpty()) {
                            throw new MojoExecutionException("Invalid empty name in includes");
                        }
                        includedNames.add(name);
                    }
                }

                final Set<String> excludedNames = new HashSet<String>();
                if (excludes != null && excludes.length > 0) {
                    for (String name : excludes) {
                        name = name.trim();
                        if (name.isEmpty()) {
                            throw new MojoExecutionException("Invalid empty name in excludes");
                        }
                        excludedNames.add(name);
                    }
                }

                // check whether a pattern is in effect
                Pattern incPattern;
                if (includePattern != null && !includePattern.trim().isEmpty()) {
                    incPattern = Pattern.compile(includePattern.trim());
                } else if (includedNames.isEmpty()) {
                    // include everything by default if no include names are set
                    incPattern = Pattern.compile(".*");
                } else {
                    // include nothing by default if include names are set
                    incPattern = Pattern.compile("^$");
                }

                // check whether a pattern is in effect
                Pattern excPattern;
                if (excludePattern != null && !excludePattern.trim().isEmpty()) {
                    excPattern = Pattern.compile(excludePattern.trim());
                } else {
                    // exclude nothing by default
                    excPattern = Pattern.compile("^$");
                }

                final Set<String> acceptedNames = new HashSet<String>();
                for (String name : objectNames) {
                    // name is included, or matches include pattern
                    // and is not excluded and does not match exclude pattern
                    if ((includedNames.contains(name) || incPattern.matcher(name).matches())
                            && !excludedNames.contains(name) && !excPattern.matcher(name).matches()) {
                        acceptedNames.add(name);
                    }
                }
                objectNames.clear();
                objectNames.addAll(acceptedNames);

                getLog().info(String.format("Found %s matching Objects", objectNames.size()));
            } else {
                getLog().warn(String.format("Generating Java classes for all %s Objects, this may take a while...", objectNames.size()));
            }

            // for every accepted name, get SObject description
            final Set<SObjectDescription> descriptions = new HashSet<SObjectDescription>();

            try {
                getLog().info("Retrieving Object descriptions...");
                for (String name : objectNames) {
                    callback.reset();
                    restClient.getDescription(name, callback);
                    if (!callback.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
                        throw new MojoExecutionException("Timeout waiting for getDescription for sObject " + name);
                    }
                    final SalesforceException ex = callback.getException();
                    if (ex != null) {
                        throw ex;
                    }
                    descriptions.add(mapper.readValue(callback.getResponse(), SObjectDescription.class));
                }
            } catch (Exception e) {
                String msg = "Error getting SObject description " + e.getMessage();
                throw new MojoExecutionException(msg, e);
            }
View Full Code Here


    }

    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, Description "
                    + "FROM PushTopic WHERE Name = '" + topicName + "'",
                    callback);

            if (!callback.await(API_TIMEOUT, TimeUnit.SECONDS)) {
                throw new SalesforceException("API call timeout!", null);
            }
            if (callback.getException() != null) {
                throw callback.getException();
            }
            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 query, notifyForFields or notifyForOperations
                if (!query.equals(topic.getQuery())
                        || (config.getNotifyForFields() != null
                                && !config.getNotifyForFields().equals(topic.getNotifyForFields()))
                        || (config.getNotifyForOperations() != null
                                && !config.getNotifyForOperations().equals(topic.getNotifyForOperations()))
                ) {

                    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
                }
            }
        }
View Full Code Here

        topic.setDescription("Topic created by Camel Salesforce component");
        topic.setNotifyForFields(config.getNotifyForFields());
        topic.setNotifyForOperations(config.getNotifyForOperations());

        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);
            }
            if (callback.getException() != null) {
                throw callback.getException();
            }

            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
                }
            }
        }
View Full Code Here

    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());
            topic.setNotifyForOperations(config.getNotifyForOperations());

            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);
            }
            if (callback.getException() != null) {
                throw callback.getException();
            }

        } 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) {
                }
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.camel.component.salesforce.internal.client.SyncResponseCallback

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.