Apex Regex Validation Factory

Validation Factory

public with sharing class Regex_Validator {

    public static Boolean isValidEmail(String emailAddress) {
        String emailRegex = '^[a-zA-Z0-9._}{+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$';
        return String.isBlank(emailAddress) ? false : Pattern.compile(emailRegex).matcher(emailAddress).matches();
    }

    public static Boolean isValidPassword(String password){
        String passwordRegex = '^.*(?=.{8,})(?=.*\\d)(?=.*[a-zA-Z])|(?=.{8,})(?=.*\\d)(?=.*[!@#$%^&])|(?=.{8,})(?=.*[a-zA-Z])(?=.*[!@#$%^&]).*$';
        return String.isBlank(password) ? false : Pattern.compile(passwordRegex).matcher(password).matches();
    }

    public static Boolean isValidDate(String dateString){
        String dateRegex = '^\\d{4}-\\d{2}-\\d{2}$';
        return String.isBlank(dateString) ? false : Pattern.compile(dateRegex).matcher(dateString).matches();
    }

    public static Boolean isValidSSN(String ssn){
        String ssnRegex = '[0-9]{3}[-]?[0-9]{2}[-]?[0-9]{4}';
        return String.isBlank(ssn) ? false : Pattern.compile(ssnRegex).matcher(ssn).matches();
    }

    public static Boolean isValidPhone(String phone){
        String phoneRegex='^([0-9\\(\\)\\/\\+ \\-]*)$';
        return String.isBlank(phone) ? false : Pattern.compile(phoneRegex).matcher(phone).matches();
    }

    public static Boolean isValidZipCode(String zipCode){
        String zipRegex='^\\d{5}(?:[-\\s]?\\d{4})?$';
        return String.isBlank(zipCode) ? false : Pattern.compile(zipRegex).matcher(zipCode).matches();
    }

    public static Boolean isValidNumber(String num) {
         String numberRegex = '^[0-9]\\d*(\\.\\d+)?$';
     return String.isBlank(num) ? false : Pattern.compile(numberRegex).matcher(num).matches();
    }

    public static Boolean isValidSfId(String id){
       String idRegex = '[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}';
       return String.isBlank(id) ? false : Pattern.compile(idRegex).matcher(id).matches();
    }
}

Validation Factory Test Class

@isTest
private class Regex_Validator_Test {

    public static User setupUser(){
        User user = (User)SmartFactory.createSObject('User');
        return user;
    }

    @isTest static void test_isValidEmail() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidEmail('test@mail.com'),true);
            System.assertEquals(Regex_Validator.isValidEmail('tmichels+5@mygmail.com'),true);
            System.assertEquals(Regex_Validator.isValidEmail('testmail.com'),false);
            System.assertEquals(Regex_Validator.isValidEmail('tes13_?//@tmail.com'),false);
            System.assertEquals(Regex_Validator.isValidEmail('testmail'),false);
            System.assertEquals(Regex_Validator.isValidEmail('!@#$%%^&*()'),false);
            System.assertEquals(Regex_Validator.isValidEmail(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidPassword() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidPassword('Password123!'),true);
            System.assertEquals(Regex_Validator.isValidPassword('password123!'),true);
            System.assertEquals(Regex_Validator.isValidPassword('password!'),true);
            System.assertEquals(Regex_Validator.isValidPassword('Password123'),false);
            System.assertEquals(Regex_Validator.isValidPassword('blah'),false);
            System.assertEquals(Regex_Validator.isValidPassword('!@#$%%^&*()'),false);
            System.assertEquals(Regex_Validator.isValidPassword('!@#$%%^&*()'),false);
            System.assertEquals(Regex_Validator.isValidPassword(null),false);
      test.stopTest();
    }

    @isTest static void test_isValidDate() {
        test.startTest();
                System.assertEquals(Regex_Validator.isValidDate('01/01/2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('1/1/2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('01-01-2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('2016-01-01'),true);
                System.assertEquals(Regex_Validator.isValidDate('01.01.2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('1.1.2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('33/01/2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('01/32/2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('01/01/3016'),false);
                System.assertEquals(Regex_Validator.isValidDate('adasdsad'),false);
                System.assertEquals(Regex_Validator.isValidDate('!@#$%%^&*()'),false);
                System.assertEquals(Regex_Validator.isValidDate(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidSSN() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidSSN('222-22-2222'),true);
            System.assertEquals(Regex_Validator.isValidSSN('222-2-2222'),false);
            System.assertEquals(Regex_Validator.isValidSSN('22-22-2222'),false);
            System.assertEquals(Regex_Validator.isValidSSN('22-22-222'),false);
            System.assertEquals(Regex_Validator.isValidSSN('222222222'),true);
            System.assertEquals(Regex_Validator.isValidSSN('2222222'),false);
            System.assertEquals(Regex_Validator.isValidSSN('asdsad'),false);
            System.assertEquals(Regex_Validator.isValidSSN('!@#$%%^&*()'),false);
            System.assertEquals(Regex_Validator.isValidSSN(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidPhone() {
        test.startTest();
                System.assertEquals(Regex_Validator.isValidPhone('(408)421-3722'),true);
                System.assertEquals(Regex_Validator.isValidPhone('4084213722'),true);
                System.assertEquals(Regex_Validator.isValidPhone('+1(408)421-3722'),true);
                System.assertEquals(Regex_Validator.isValidPhone('(333)222-999'),true);
                System.assertEquals(Regex_Validator.isValidPhone('3332229999'),true);
                System.assertEquals(Regex_Validator.isValidPhone('asdsadsad'),false);
                System.assertEquals(Regex_Validator.isValidPhone('!@#$$%%@'),false);
                System.assertEquals(Regex_Validator.isValidPhone(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidZipCode() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidZipCode('95070'),true);
            System.assertEquals(Regex_Validator.isValidZipCode('95070-2344'),true);
            System.assertEquals(Regex_Validator.isValidZipCode('95070-234'),false);
            System.assertEquals(Regex_Validator.isValidZipCode('950702344'),true);
            System.assertEquals(Regex_Validator.isValidZipCode('99999'),true);
            System.assertEquals(Regex_Validator.isValidZipCode('999'),false);
            System.assertEquals(Regex_Validator.isValidZipCode('9a9n9'),false);
            System.assertEquals(Regex_Validator.isValidZipCode('asdasd'),false);
            System.assertEquals(Regex_Validator.isValidZipCode('!@#$$%%@'),false);
            System.assertEquals(Regex_Validator.isValidZipCode(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidNumber() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidNumber('95070'),true);
            System.assertEquals(Regex_Validator.isValidNumber('-99'),false);
            System.assertEquals(Regex_Validator.isValidNumber('asdasd'),false);
            System.assertEquals(Regex_Validator.isValidNumber('!@#$$%%@'),false);
            System.assertEquals(Regex_Validator.isValidNumber(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidSfId(){
      test.startTest();
          System.assertEquals(Regex_Validator.isValidSfId('32423324'),false);
          System.assertEquals(Regex_Validator.isValidSfId('00363000005eIbz'),true);
          System.assertEquals(Regex_Validator.isValidSfId('00363000005eIbzAlz'),true);
          System.assertEquals(Regex_Validator.isValidSfId(null),false);
      test.stopTest();
    }
}

Apex Get Enum from String

Get Enum from String
  public Enum EventType { USER_LOGIN, USER_LOGOUT, USER_COMPLETE_FORM, USER_CLICK }

  static Map<String, EventType> nameToEvenType {
    get {
        Map<String, EventType> eventTypeValues = new Map<String, EventType>();
        if (nameToEvenType == null){
          for (EventType enumValue : EventType.values()){
              eventTypeValues.put(String.valueOf(enumValue).toUpperCase(), enumValue);
          }
        }
        return eventTypeValues;
    }
    private set;
}

public static EventType getEventType(String name){
    return String.isBlank(name) ? null : nameToEvenType.get(name.toUpperCase());
}

Test Class
@IsTest
public static void testNameToEvenType(){
 	Test.startTest();
      System.assertEquals(Events.EventType.USER_LOGIN, Events.getEventType('USER_LOGIN'));
      System.assertEquals(Events.EventType.USER_LOGOUT, Events.getEventType('USER_LOGOUT'));
      System.assertEquals(Events.EventType.USER_COMPLETE_FORM, Events.getEventType('USER_COMPLETE_FORM'));
      System.assertEquals(Events.EventType.USER_CLICK, Events.getEventType('USER_CLICK'));
    Test.stopTest(); 
}

Apex Advanced Selector Queries

Sub Queries

public Contact getRelatedDetails(Id userId){
       fflib_QueryFactory qf = new fflib_QueryFactory(Contact.sObjectType);
       qf.selectField('name').selectField('Id').setCondition('Id In (Select ContactId from User where Id=:userId)');
       qf.subselectQuery('Goals__r').selectField('Id');
       qf.subselectQuery('Account_Client__r').selectField('Id').selectField('External_Id__c').selectField('Institution_Account__c').setCondition('Status__c=\'Active\'');
       Contact bpContact = Database.query(qf.toSOQL());
       return bpContact;
     }

Pagination with Ordering

  public List<Goal__c> selectByUserIdAndPageNumber(Id userId, Integer pageNumber){
     Integer calcOffset = pageNumber * 200;
     fflib_QueryFactory goalQueryFactory = newQueryFactory();
     fflib_QueryFactory.Ordering ordering = new fflib_QueryFactory.Ordering(Goal__c.CreatedDate, fflib_QueryFactory.SortOrder.DESCENDING, false);
     goalQueryFactory.addOrdering(ordering);
     List<fflib_QueryFactory.Ordering> orderings = goalQueryFactory.getOrderings();
     orderings.remove(0);
     String queryWithOffSet = goalQueryFactory.setCondition('Client__c in (Select ContactId from User where Id=:userId)').setLimit(maxGoalsPerPage).toSOQL() + ' offset ' + calcOffset;
     return (List<Goal__c>) Database.query(queryWithOffSet);
}

Aggregate Query

public List<AggregateResult> getAggregateDetails(List<Id> securityIds){
    String selectedACLevel = 'Level_1__c';

    String formatQuery = String.format('Select {0} sumOfAllocPercent, {1} from Classification__c where Security__c IN :securityIds Group By {2}', new String[]{'sum(Asset_Percent__c)', selectedACLevel, selectedACLevel});
    List<AggregateResult> aggregateResultForAC= (List<AggregateResult>) Database.query(formatQuery);

    return aggregateResultForAC;
}

Implementing Mocking for Apex Tests

Trying to mock a actual database query can be hard. Using the fflib_Mocks jar you can generator Selector Mocks: More info here:

https://github.com/financialforcedev/fflib-apex-mocks

1. Create interface class that extends fflib_ISObjectSelector to mock the Selector 

2. Create interfacemocks.properties file in root of your project

3. Run java -jar apex-mocks-generator-4.0.0.jar “{path}/src/classes” “{path}/interfacemocks.properties” “fflib_Mocks” “{path}/src/classes” “30.0” to generate mocking files

4. Used mocked selector to write stub selector

5. Run mock generator as maven job

1. Create interface class that extends fflib_ISObjectSelector to mock the Selector 
public interface System_Selector extends fflib_ISObjectSelector {
    List<Object> selectByUserId(Id userId);
}

2. Create interfacemocks.properties file in root of your project

System_Selector=Contact:fflib_SObjectMocks.SObjectSelector
3. Run java -jar apex-mocks-generator-4.0.0.jar "{path}/src/classes" "{path}/interfacemocks.properties" "fflib_Mocks" "{path}/src/classes" "30.0" to generate mocking files
/* Generated by apex-mocks-generator version 4.0.1 */
@isTest
public class Selector_Mocks{
    public class Contact extends fflib_SObjectMocks.SObjectSelector implements System_Selector{
        private fflib_ApexMocks mocks;

        public Contact(fflib_ApexMocks mocks){
            super(mocks);
            this.mocks = mocks;
        }
        
        public List<Object> selectByUserId(Id userId){
            return (List<Object>) mocks.mockNonVoidMethod(this, 'selectByUserId', new List<Type> {System.Type.forName('Id')}, new List<Object> {userId});
        }
    }
}

4. Used mocked selector to write stub selector

  @isTest static void testContactSelector(){
        // Create mocks
  		fflib_ApexMocks mocks = new fflib_ApexMocks();
  		fflib_ISObjectUnitOfWork uowMock = new fflib_SObjectMocks.SObjectUnitOfWork(mocks);

  		System_Selector selectorMock = new System_Mocks.Contact(mocks);

  		// Given
  		mocks.startStubbing();
        List<Contact> testContactList = new List<Contact> {
  			new Contact(
  				Id = fflib_IDGenerator.generate(Contact.SObjectType),
  				FirstName = 'Test FirstName',
  				LastName = 'Test LastName',
  				Email = 'test@mail.com') };

        Set<Id> testContactSet = new Map<Id, Contact>(testContactList).keySet();
        User testUserId = new User(	Id = fflib_IDGenerator.generate(User.SObjectType),
  				FirstName = 'Test FirstName',
  				LastName = 'Test LastName',
  				Email = 'test@mail.com' );
        mocks.when(selectorMock.sObjectType()).thenReturn(Contact.SObjectType);
    		mocks.when(selectorMock.selectByUserId(testUserId.Id)).thenReturn(testContactList);
  		mocks.stopStubbing();

      System_Rest_App.Selector.setMock(selectorMock);

      // When
      new System_Mocks.Contact(mocks).selectByUserId(testUserId.Id);

      // Then
      ((System_Selector)
        mocks.verify(selectorMock)).selectByUserId(testUserId.Id);
    }

5. Run mock generator as maven job – ant generate.mocks

  <target name="generate.mocks">
		<java classname="com.financialforce.apexmocks.ApexMockGenerator">
			<classpath>
				<pathelement location="${basedir}/lib/apex-mocks-generator-4.0.1.jar"/>
			</classpath>
			<arg value="${basedir}/src/classes"/>
			<arg value="${basedir}/interfacemocks.properties"/>
			<arg value="Selector_Mocks"/>
			<arg value="${basedir}/src/classes"/>
		</java>
	</target>

Salesforce Chatter Attaching Files to SObject

There are different ways you can use salesforce to attach a file to an SObject using Chatter.

  • Apex Code
  • Chatter REST Api
    public static void createAttachmentFeed(List<Attachment> attachments){
      for (Attachment attachment : attachments){
        // ContentVersion is how you upload a file!
       ContentVersion version = new ContentVersion();
       version.Title=attachment.Name;
       version.PathOnClient = '\\' + attachment.Name;
       version.VersionData = attachment.Body;
       version.NetWorkId = brightPlanWebCustomSettings.Community_Id__c;
       insert version;

       // After you insert the ContentVersion object, a base 'ContentDocument' is established
       // The ID of the ContentDocument is what you need to attach the file to the Chatter post.
       version = [SELECT ID,ContentDocumentId FROM ContentVersion WHERE ID=:version.id];

       // Form a basic post attached to our own feed.
       ConnectApi.FeedItemInput feedItem = new ConnectApi.FeedItemInput();
       feedItem.subjectId = attachment.ParentId; // This can also be an objectID to post the file to.

       // Now connect the feeditem to our already uploaded file.
       feedItem.capabilities = new ConnectAPI.FeedElementCapabilitiesInput();
       feedItem.capabilities.files = new ConnectAPI.FilesCapabilityInput();
       feedItem.capabilities.files.items = new List<ConnectAPI.FileIdInput>();
       ConnectAPI.FileIdInput attachFile = new ConnectAPI.FileIDInput();

       //**** Here is where we attach the specific file to the post!
       attachFile.id = version.contentDocumentid;
       feedItem.capabilities.files.items.add(attachFile);

       // Execute the posting
       ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(brightPlanWebCustomSettings.Community_Id__c, feedItem);
      }
    }

Some limitations using this is using Blob for version.VersionData = attachment.Body; will run into Apex String length exceeds maximum: 6000000.

Chatter Rest API

POST https://cs43.salesforce.com/services/data/v35.0/connect/communities/0DB63000000003jGAA/chatter/feed-elements/batch

HEADER

Authorization         Bearer {sessionToken}

Content-Type          multipart/form-data; boundary=a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq

Format is as follows:

{salesforceOrgUrl}/services/data/v35.0/connect/communities/{salesforceCommunityId}chatter/feed-elements/batch

The subjectId is the SObject Id to which you want to link the file, is this case we use our Case Id.

--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Type: application/json; charset=UTF-8
Content-Disposition: form-data; name="json"
{
"inputs": [
{
"binaryPartNames": [
"file1"
],
"richInput": {
"subjectId": "50063000003YlSe",
"capabilities": {
"content": {
"title": "file1.pdf"
}
},
"feedElementType": "FeedItem"
}
},
{
"binaryPartNames": [
"file2"
],
"richInput": {
"subjectId": "50063000003YlSe",
"capabilities": {
"content": {
"title": "file2.pdf"
}
},
"feedElementType": "FeedItem"
}
}
]
}
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Type: application/octet-stream; charset=ISO-8859-1
Content-Disposition: form-data; name="file1"; filename="file1.pdf"

...contents of file1.pdf...

--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Disposition: form-data; name="file2"; filename="file2.pdf"
Content-Type: application/octet-stream; charset=ISO-8859-1
...contents of file2.pdf...
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq--

Benefits you can batch upload files without hitting an Apex limits.

Both the Chatter and Apex solutions can be used by Community Users to upload files.

Run Spring Boot Scheduler as Heroku Worker

1. Setup Application
2. Setup Scheduler
3. Add Spring Boot dependency
4. Add Procfile
5. Scale Heroku worker

1. Setup Application

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * Created by tmichels on 9/20/15.
 */

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

2. Setup Scheduler

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * Created by tmichels on 9/18/15.
 */

@Component
public class Scheduler {

    private static final Logger logger = LoggerFactory.getLogger(Scheduler.class);

    @Scheduled(cron = "0 0 13 * * *")
    public void buyHighPercentageReturnNotes(){
       System.out.println("Scheduler is running");
    }
}

3. Add Spring Boot dependency

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.6.RELEASE</version>
    </parent>
  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
      </dependency>
  </dependencies>

4. Add Procfile

worker: java $JAVA_OPTS -jar target/*.jar

5. Scale Heroku worker

heroku ps:scale worker=1

Microsoft Bing Adwords Java Integration

Step 1: Setup Bing BulkServiceManager Configuration

package com.integration.adwords.config;

import com.microsoft.bingads.*;
import com.microsoft.bingads.bulk.BulkServiceManager;
import com.microsoft.bingads.campaignmanagement.ICampaignManagementService;
import com.microsoft.bingads.internal.OAuthService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by tmichels on 7/31/15.
 */
@Configuration
public class BingAdwordsConfig {

    @Value("${BING_ADS_CLIENTID}")
    private String bingAdsClientId;

    @Value("${BING_ADS_CLIENT_SECRET}")
    private String bingAdsClientSecret;

    @Value("${BING_ADS_ACCOUNTID}")
    private String bingAdsAccountId;

    @Value("${BING_ADS_CUSTOMERID}")
    private String bingAdsCustomerId;

    @Value("${BING_ADS_DEVELOPER_TOKEN}")
    private String bingAdsDeveloperToken;

    @Value("${BING_ADS_USERNAME}")
    private String bingAdsUsername;

    @Value("${BING_ADS_PASSWORD}")
    private String bingAdsPassword;

    @Value("${BING_ADS_REFRESH_TOKEN}")
    private String bingAdsRefreshToken;


    @Bean
    public OAuthDesktopMobileAuthCodeGrant oAuthWebAuthCodeGrant() throws MalformedURLException {
        OAuthDesktopMobileAuthCodeGrant oAuthWebAuthCodeGrant = new OAuthDesktopMobileAuthCodeGrant(bingAdsClientId, bingAdsRefreshToken);
        return oAuthWebAuthCodeGrant;
    }
    
    @Bean
    public AuthorizationData authorizationData(OAuthDesktopMobileAuthCodeGrant authentication){
        AuthorizationData authorizationData = new AuthorizationData();
        authorizationData.setAuthentication(authentication);
        authorizationData.setCustomerId(new Long(bingAdsCustomerId));
        authorizationData.setAccountId(new Long(bingAdsAccountId));
        authorizationData.setDeveloperToken(bingAdsDeveloperToken);
        return authorizationData;
    }

    @Bean
    public BulkServiceManager bulkServiceManager(AuthorizationData authorizationData){
        BulkServiceManager bulkServiceManager = new BulkServiceManager(authorizationData);
        bulkServiceManager.setStatusPollIntervalInMilliseconds(5000);
        return bulkServiceManager;
    }
}

Step 2: Setup Bing Adwords Service

package com.integration.adwords.service;

import com.microsoft.bingads.AuthorizationData;
import com.microsoft.bingads.bulk.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

/**
 * Created by tmichels on 7/31/15.
 */

@Service
public class BingAdwordService {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(BingAdwordService.class);

    private static final String OUTPUTFILENAME="bingAdwords.csv";
    private static final String FILEDIRECTORY="adwords/bing/";

    @Autowired
    BulkServiceManager bulkService;

    public void submitAndDownloadWithBulkServiceManager() throws IOException, URISyntaxException {
        try {
            logger.info("Start BingAdwordService submitAndDownloadWithBulkServiceManager");
            SubmitDownloadParameters submitDownloadParameters = new SubmitDownloadParameters();
            submitDownloadParameters.setCampaignIds(null);
            ArrayList<DataScope> dataScope = new ArrayList<DataScope>();
            dataScope.add(DataScope.ENTITY_DATA);
            submitDownloadParameters.setDataScope(dataScope);
            submitDownloadParameters.setFileType(DownloadFileType.CSV);
            submitDownloadParameters.setLastSyncTimeInUTC(null);
            ArrayList<BulkDownloadEntity> bulkDownloadEntities = new ArrayList<BulkDownloadEntity>();
            bulkDownloadEntities.add(BulkDownloadEntity.CAMPAIGNS);
            bulkDownloadEntities.add(BulkDownloadEntity.AD_GROUPS);
            bulkDownloadEntities.add(BulkDownloadEntity.KEYWORDS);
            bulkDownloadEntities.add(BulkDownloadEntity.ADS);
            submitDownloadParameters.setEntities(bulkDownloadEntities);

            BulkDownloadOperation bulkDownloadOperation = bulkService.submitDownloadAsync(submitDownloadParameters, null).get();

            BulkOperationStatus<DownloadStatus> downloadStatus;
            int waitTime = 5000;

            for (int i = 0; i < 24; i++) {
                Thread.sleep(waitTime);

                downloadStatus = bulkDownloadOperation.getStatusAsync(null).get();

                if (downloadStatus.getStatus() == DownloadStatus.COMPLETED) {
                    break;
                }
            }

            File resultFile = bulkDownloadOperation.downloadResultFileAsync(
                    new File(FILEDIRECTORY),
                    OUTPUTFILENAME,
                    true,
                    true,  // Set this value true if you want to overwrite the same file.
                    null).get();

            logger.info("Download BingAdwords file success %s\n", resultFile.getName());

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

Apache Camel Kafka Spring Integration

1. Configure Camel Kafka Component

package com.integration.camel.component;

import org.apache.camel.component.kafka.KafkaComponent;
import org.apache.camel.component.kafka.KafkaConfiguration;
import org.apache.camel.component.kafka.KafkaEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 9/3/15.
 */

@Configuration
public class KafkaCamelComponent {
    
    @Bean
    public KafkaEndpoint kafkaEndpoint(){
        KafkaEndpoint kafkaEndpoint = new KafkaEndpoint();
        kafkaEndpoint.setZookeeperHost("localhost");
        kafkaEndpoint.setZookeeperPort(2181);
        kafkaEndpoint.setTopic("test");
        return kafkaEndpoint;
    }

    @Bean
    public KafkaComponent kafkaComponent(KafkaEndpoint kafkaEndpoint){
        KafkaComponent kafkaComponent = new KafkaComponent();
        kafkaComponent.setEndpointClass(kafkaEndpoint.getClass());
        return kafkaComponent;
    }
}

2. Configure Kafka Consume and Producer Route

package com.integration.camel.route;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 9/3/15.
 */

@Configuration
public class KafkaCamelRoute {

    @Bean(name = "KafkaRouteProducer")
    public RouteBuilder kafkaRouteProducer() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:kafkaRoute").to("kafka:localhost:9092?topic=test&zookeeperHost=localhost&zookeeperPort=2181&groupId=group1&serializerClass=kafka.serializer.StringEncoder").bean(kafkaOutputBean.class);
            }
        };
    }

    @Bean(name="KafkaRouteConsumer")
    public RouteBuilder kafkaRouteConsumer() {
        return new RouteBuilder() {
            public void configure() {
                from("kafka:localhost:9092?topic=test&zookeeperHost=localhost&zookeeperPort=2181&groupId=group1&serializerClass=kafka.serializer.StringEncoder").bean(kafkaOutputBean.class);
            }
        };
    }

    public static class kafkaOutputBean {
        public void printKafkaBody(String body) {
            System.out.println("KafkaBody result >>>>> " + body);
        }
    }
}

3. Start Kafka test by adding routes to CamelContext and starting

package com.integration.camel.route;

import com.integration.camel.component.KafkaCamelComponent;
import com.integration.camel.context.CamelContextConfig;
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.kafka.KafkaComponent;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by tmichels on 9/3/15.
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {KafkaCamelComponent.class,
                                KafkaCamelRoute.class,
                                CamelContextConfig.class})
public class KafkaCamelIntegrationTest {

    @Autowired
    CamelContext camelContext;

    @Autowired
    KafkaComponent kafkaComponent;

    @Autowired
    @Qualifier("KafkaRouteProducer")
    RouteBuilder kafkaRouteProducer;

    @Autowired
    @Qualifier("KafkaRouteConsumer")
    RouteBuilder kafkaRouteConsumer;

    @EndpointInject(uri = "direct:kafkaRoute")
    ProducerTemplate kafkaProducer;
    
    @Before
    public void before() throws Exception {
        camelContext.addRoutes(kafkaRouteProducer);
        camelContext.addRoutes(kafkaRouteConsumer);
    }

    @Test
    public void testKafkaRoute(){
        kafkaProducer.sendBody("direct:KafkaRouteProducer", "testKafkaMessage");
        try {
            camelContext.start();
            camelContext.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Google Adwords Java Integration

Step 1: Create Google AdwordSession configuration

package com.integration.adwords.config;

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.exception.OAuthException;
import com.google.api.ads.common.lib.exception.ValidationException;
import com.google.api.client.auth.oauth2.Credential;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 7/31/15.
 */

@Configuration
public class GoogleAdwordsConfig {

    @Value("${GOOGLE_ADWORD_CLIENTID}")
    private String googleAdWordClientId;

    @Value("${GOOGLE_ADWORD_SECRET}")
    private String googleAdwordSecet;

    @Value("${GOOGLE_ADWORDS_DEV_TOKEN}")
    private String googleAdwordDevToken;

    @Value("${GOOGLE_ADWORDS_REFRESH_TOKEN}")
    private String googleAdwordRefreshToken;

    @Value("${GOOGLE_ADWORDS_USER_AGENT}")
    private String googleAdwordUserAgent;

    @Value("${GOOGLE_ADWORDS_CLIENT_CUSTOMER_ID}")
    private String googleAdwordClientCustomerId;

    @Bean
    public Credential credential() throws ValidationException, OAuthException {
        Credential credential = new OfflineCredentials.Builder()
                                .forApi(OfflineCredentials.Api.ADWORDS)
                                .withClientSecrets(googleAdWordClientId, googleAdwordSecet)
                                .withRefreshToken(googleAdwordRefreshToken)
                                .build()
                                .generateCredential();
        return credential;
    }

    @Bean
    public AdWordsSession adWordsSession(Credential credential) throws ValidationException {
        AdWordsSession adWordsSession = new AdWordsSession.Builder()
                                        .withDeveloperToken(googleAdwordDevToken)
                                        .withOAuth2Credential(credential)
                                        .withClientCustomerId(googleAdwordClientCustomerId)
                                        .withUserAgent(googleAdwordUserAgent)


                .build();
        return adWordsSession;
    }
}

2. Create AdWord Service to export KeyWords, Campaigns, AdGroups and Ads.

package com.integration.adwords.service;

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.utils.v201506.SelectorBuilder;
import com.google.api.ads.adwords.axis.v201506.cm.*;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.selectorfields.v201502.cm.CampaignField;
import com.google.api.ads.adwords.lib.selectorfields.v201506.cm.AdGroupAdField;
import com.google.api.ads.adwords.lib.selectorfields.v201506.cm.AdGroupCriterionField;
import com.google.api.ads.adwords.lib.selectorfields.v201506.cm.AdGroupField;
import com.integration.file.FileConverter;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tmichels on 7/31/15.
 */

@Service
public class GoogleAdwordService {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(GoogleAdwordService.class);

    private static final int PAGE_SIZE = 100;

    private static final String CAMPAIGNFILENAME="adwords/google/googleAdwordCampaign.csv";
    private static final String KEYWORDFILENAME="adwords/google/googleAdwordKeywords.csv";
    private static final String ADGROUPFILENAME="adwords/google/googleAdwordAdGroup.csv";
    private static final String ADSFILENAME="adwords/google/googleAdwordAds.csv";

    @Autowired
    AdWordsSession adWordsSession;

    @Autowired
    FileConverter fileConverter;

    public void getCampaigns() throws RemoteException {
        List<String> campaignColumns = new ArrayList<>();
        List<String> campaignData = new ArrayList<>();

        // Get the CampaignService.
        CampaignServiceInterface campaignService =
                new AdWordsServices().get(adWordsSession, CampaignServiceInterface.class);

        int offset = 0;

        // Create selector.
        SelectorBuilder builder = new SelectorBuilder();
        Selector selector = builder
                .fields(CampaignField.Id,
                        CampaignField.Name,
                        CampaignField.AdServingOptimizationStatus,
                        CampaignField.AdvertisingChannelSubType,
                        CampaignField.AdvertisingChannelType,
                        CampaignField.BiddingStrategyId,
                        CampaignField.BiddingStrategyName,
                        CampaignField.BiddingStrategyType,
                        CampaignField.BidType,
                        CampaignField.BudgetId,
                        CampaignField.BudgetName,
                        CampaignField.BudgetReferenceCount,
                        CampaignField.BudgetStatus,
                        CampaignField.DeliveryMethod,
                        CampaignField.Eligible,
                        CampaignField.EndDate,
                        CampaignField.FrequencyCapMaxImpressions,
                        CampaignField.Labels,
                        CampaignField.StartDate,
                        CampaignField.TrackingUrlTemplate,
                        CampaignField.UrlCustomParameters)
                .orderAscBy(CampaignField.Id)
                .offset(offset)
                .limit(PAGE_SIZE)
                .build();

        campaignColumns.add(CampaignField.Id.toString());
        campaignColumns.add(CampaignField.Name.toString());
        campaignColumns.add(CampaignField.AdServingOptimizationStatus.toString());
        campaignColumns.add(CampaignField.AdvertisingChannelSubType.toString());
        campaignColumns.add(CampaignField.AdvertisingChannelType.toString());
        campaignColumns.add(CampaignField.BiddingStrategyId.toString());
        campaignColumns.add(CampaignField.BiddingStrategyName.toString());
        campaignColumns.add(CampaignField.BiddingStrategyType.toString());
        campaignColumns.add(CampaignField.BidType.toString());
        campaignColumns.add(CampaignField.BudgetId.toString());
        campaignColumns.add(CampaignField.BudgetName.toString());
        campaignColumns.add(CampaignField.BudgetReferenceCount.toString());
        campaignColumns.add(CampaignField.BudgetStatus.toString());
        campaignColumns.add(CampaignField.DeliveryMethod.toString());
        campaignColumns.add(CampaignField.Eligible.toString());
        campaignColumns.add(CampaignField.EndDate.toString());
        campaignColumns.add(CampaignField.FrequencyCapMaxImpressions.toString());
        campaignColumns.add(CampaignField.Labels.toString());
        campaignColumns.add(CampaignField.StartDate.toString());
        campaignColumns.add(CampaignField.TrackingUrlTemplate.toString());
        campaignColumns.add(CampaignField.UrlCustomParameters.toString());

        CampaignPage page = null;
        do {
            // Get all campaigns.
            page = campaignService.get(selector);

            // Display campaigns.
            if (page.getEntries() != null) {
                for (Campaign campaign : page.getEntries()) {
                    campaignData.add(campaign.getId().toString());
                    campaignData.add(campaign.getName());
                    campaignData.add(campaign.getAdServingOptimizationStatus()!=null? campaign.getAdServingOptimizationStatus().toString() : "");
                    campaignData.add(campaign.getAdvertisingChannelSubType() != null ? campaign.getAdvertisingChannelSubType().toString() : "");
                    campaignData.add(campaign.getAdvertisingChannelType().toString());
                    campaignData.add(campaign.getBiddingStrategyConfiguration().getBiddingStrategyId() != null ? campaign.getBiddingStrategyConfiguration().getBiddingStrategyId().toString() : "");
                    campaignData.add(campaign.getBiddingStrategyConfiguration().getBiddingStrategyName());
                    campaignData.add(campaign.getBiddingStrategyConfiguration().getBiddingStrategyType() != null ? campaign.getBiddingStrategyConfiguration().getBiddingStrategyType().getValue() : "");
                    campaignData.add(campaign.getBudget().getBudgetId() != null ? campaign.getBudget().getBudgetId().toString() : "");
                    campaignData.add(campaign.getBudget().getName());
                    campaignData.add(campaign.getBudget().getReferenceCount() != null ? campaign.getBudget().getReferenceCount().toString() : "");
                    campaignData.add(campaign.getBudget().getStatus() != null ? campaign.getBudget().getStatus().getValue() : "");
                    campaignData.add(campaign.getBudget().getDeliveryMethod() != null ? campaign.getBudget().getDeliveryMethod().getValue() : "");
                    campaignData.add(campaign.getBudget().getIsExplicitlyShared() != null ? campaign.getBudget().getIsExplicitlyShared().toString() : "");
                    campaignData.add(campaign.getEndDate());
                    campaignData.add(campaign.getFrequencyCap()!=null && campaign.getFrequencyCap().getImpressions() != null ? campaign.getFrequencyCap().getImpressions().toString() : "");
                    campaignData.add(campaign.getLabels() != null ? campaign.getLabels().toString() : "");
                    campaignData.add(campaign.getStartDate());
                    campaignData.add(campaign.getTrackingUrlTemplate());
                    campaignData.add(campaign.getUrlCustomParameters()!=null &&  campaign.getUrlCustomParameters().getParameters() != null ? campaign.getUrlCustomParameters().getParameters().toString() : "");
                }
            } else {
                logger.info("No campaigns were found.");
            }

            offset += PAGE_SIZE;
            selector = builder.increaseOffsetBy(PAGE_SIZE).build();
        } while (offset < page.getTotalNumEntries());

        fileConverter.convertToCSV(campaignData, campaignColumns, CAMPAIGNFILENAME);
    }

    public void getKeyWords() throws RemoteException {
        List<String> campaignColumns = new ArrayList<>();
        List<String> campaignData = new ArrayList<>();

        // Get the AdGroupCriterionService.
        AdGroupCriterionServiceInterface adGroupCriterionService =
                new AdWordsServices().get(adWordsSession, AdGroupCriterionServiceInterface.class);

        int offset = 0;
        boolean morePages = true;

        // Create selector.
        SelectorBuilder builder = new SelectorBuilder();
        Selector selector = builder
                .fields(
                        AdGroupCriterionField.Id,
                        AdGroupCriterionField.AdGroupId,
                        AdGroupCriterionField.KeywordMatchType,
                        AdGroupCriterionField.KeywordText)
                .offset(offset)
                .limit(PAGE_SIZE)
                .equals(AdGroupCriterionField.CriteriaType, "KEYWORD")
                .build();

        campaignColumns.add(AdGroupCriterionField.Id.toString());
        campaignColumns.add(AdGroupCriterionField.AdGroupId.toString());
        campaignColumns.add(AdGroupCriterionField.KeywordMatchType.toString());
        campaignColumns.add(AdGroupCriterionField.KeywordText.toString());

        while (morePages) {
            AdGroupCriterionPage page = adGroupCriterionService.get(selector);

            if (page.getEntries() != null && page.getEntries().length > 0) {
                for (AdGroupCriterion adGroupCriterionResult : page.getEntries()) {
                    campaignData.add(adGroupCriterionResult.getAdGroupId().toString());
                    campaignData.add(adGroupCriterionResult.getCriterion().getId().toString());
                    campaignData.add(((Keyword) adGroupCriterionResult.getCriterion()).getText());
                    campaignData.add(((Keyword) adGroupCriterionResult.getCriterion()).getMatchType().toString());
                }
            } else {
                logger.info("No keywords were found.");
            }
            offset += PAGE_SIZE;
            selector = builder.increaseOffsetBy(PAGE_SIZE).build();
            morePages = offset < page.getTotalNumEntries();
        }

        fileConverter.convertToCSV(campaignData, campaignColumns, KEYWORDFILENAME);
    }

    public void getTextAds() throws RemoteException {
        List<String> campaignColumns = new ArrayList<>();
        List<String> campaignData = new ArrayList<>();

        AdGroupAdServiceInterface adGroupAdService =
                new AdWordsServices().get(adWordsSession, AdGroupAdServiceInterface.class);

        int offset = 0;
        boolean morePages = true;

        // Create selector.
        SelectorBuilder builder = new SelectorBuilder();
        Selector selector = builder
                .fields(AdGroupAdField.Id,
                        AdGroupAdField.AdGroupId,
                        AdGroupAdField.Status)
                .orderAscBy(AdGroupAdField.Id)
                .offset(offset)
                .limit(PAGE_SIZE)
                .in(AdGroupAdField.Status, "ENABLED", "PAUSED", "DISABLED")
                .build();

        campaignColumns.add(AdGroupAdField.Id.toString());
        campaignColumns.add(AdGroupAdField.AdGroupId.toString());
        campaignColumns.add(AdGroupAdField.Status.toString());

        while (morePages) {
            AdGroupAdPage page = adGroupAdService.get(selector);

            if (page.getEntries() != null && page.getEntries().length > 0) {
                for (AdGroupAd adGroupAd : page.getEntries()) {
                    campaignData.add(adGroupAd.getAd().getId().toString());
                    campaignData.add(adGroupAd.getAd().getAdType().toString());
                    campaignData.add(adGroupAd.getStatus().toString());
                }
            } else {
                logger.info("No text ads were found");
            }

            offset += PAGE_SIZE;
            selector = builder.increaseOffsetBy(PAGE_SIZE).build();
            morePages = offset < page.getTotalNumEntries();
        }

        fileConverter.convertToCSV(campaignData, campaignColumns, ADSFILENAME);

    }
    public void getAdGroups() throws RemoteException {
        List<String> campaignColumns = new ArrayList<>();
        List<String> campaignData = new ArrayList<>();

        AdGroupServiceInterface adGroupService = new AdWordsServices().get(adWordsSession, AdGroupServiceInterface.class);

        int offset = 0;
        boolean morePages = true;

        // Create selector.
        SelectorBuilder builder = new SelectorBuilder();
        Selector selector = builder
                .fields(AdGroupField.Id,
                        AdGroupField.Name,
                        AdGroupField.Status)
                .orderAscBy(AdGroupField.Name)
                .offset(offset)
                .limit(PAGE_SIZE)
                .build();

        campaignColumns.add(AdGroupField.Id.toString());
        campaignColumns.add(AdGroupField.Name.toString());
        campaignColumns.add(AdGroupField.Status.toString());

        while (morePages) {
            AdGroupPage page = adGroupService.get(selector);
            // Display ad groups.
            if (page.getEntries() != null) {
                for (AdGroup adGroup : page.getEntries()) {
                    campaignData.add(adGroup.getId().toString());
                    campaignData.add(adGroup.getName().toString());
                    campaignData.add(adGroup.getStatus()!=null ? adGroup.getStatus().getValue() : "");
                }
            } else {
                logger.info("No group ads were found.");
            }

            offset += PAGE_SIZE;
            selector = builder.increaseOffsetBy(PAGE_SIZE).build();
            morePages = offset < page.getTotalNumEntries();
        }
        fileConverter.convertToCSV(campaignData, campaignColumns, ADGROUPFILENAME);
    }
}

Apache Camel Salesforce Integration

1. Setup SalesforceLoginConfig
2. Setup SalesforceCamelEndpointConfig
3. Setup SalesforceCamelComponent
4. Setup SalesforceCamelRouteConfig
5. Run SalesforceCamelIntegrationTest

1. Setup SalesforceLoginConfig

package com.sforce.core.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.component.salesforce.SalesforceComponent;
import org.apache.camel.component.salesforce.SalesforceEndpointConfig;
import org.apache.camel.component.salesforce.SalesforceLoginConfig;
import org.apache.camel.impl.DefaultCamelContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 4/8/15.
 */

@Configuration
public class SalesforceCamelConfig {

    @Value("${SF_USERNAME}")
    private String username;

    @Value("${SF_PASSWORD}")
    private String password;

    @Value("${SF_TOKEN}")
    private String token;

    @Value("${SF_BASE_URL}")
    private String url;

    @Value("${SF_VERSION}")
    private String version;

    @Value("${SF_CLIENT_ID}")
    private String clientId;

    @Value("${SF_CLIENT_SECRET}")
    private String clientSecret;


    @Bean
    public SalesforceLoginConfig salesforceLoginConfig(){
        SalesforceLoginConfig salesforceLoginConfig = new SalesforceLoginConfig();
        salesforceLoginConfig.setUserName(username);
        salesforceLoginConfig.setPassword(password+token);
        salesforceLoginConfig.setLoginUrl(url);
        salesforceLoginConfig.setClientId(clientId);
        salesforceLoginConfig.setClientSecret(clientSecret);
        salesforceLoginConfig.setLazyLogin(false);
        return salesforceLoginConfig;
    }
}

2. Setup SalesforceCamelEndpointConfig

package com.sforce.core.camel;

import org.apache.camel.Endpoint;
import org.apache.camel.component.salesforce.SalesforceComponent;
import org.apache.camel.component.salesforce.SalesforceEndpoint;
import org.apache.camel.component.salesforce.SalesforceEndpointConfig;
import org.apache.camel.component.salesforce.internal.OperationName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 4/13/15.
 */

@Configuration
public class SalesforceCamelEndpointConfig {
    
    @Bean
    public SalesforceEndpointConfig salesforceEndpointConfig(){
        SalesforceEndpointConfig salesforceEndpointConfig = new SalesforceEndpointConfig();
        salesforceEndpointConfig.setApiVersion("33.0");
        return salesforceEndpointConfig;
    }
}

3. Setup SalesforceCamelComponent

package com.sforce.core.camel;


import org.apache.camel.component.salesforce.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Created by tmichels on 4/13/15.
 */

@Configuration
@Import({SalesforceCamelEndpointConfig.class, SalesforceCamelConfig.class})
public class SalesforceCamelComponent {

    @Autowired
    SalesforceEndpointConfig salesforceEndpointConfig;

    @Autowired
    SalesforceLoginConfig salesforceLoginConfig;

    @Bean
    public SalesforceComponent salesforceComponent(){
        SalesforceComponent salesforceComponent = new SalesforceComponent();
        salesforceComponent.setConfig(salesforceEndpointConfig);
        salesforceComponent.setLoginConfig(salesforceLoginConfig);
        salesforceComponent.setPackages("com.sfore.core.camel");
        return salesforceComponent;
    }
}

4. Setup SalesforceCamelRouteConfig

package com.sforce.core.camel;

import com.sforce.core.config.LcGroovyPropertiesConfig;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.salesforce.SalesforceComponent;
import org.apache.camel.spring.SpringCamelContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
import org.apache.camel.spring.javaconfig.Main;

/**
 * Created by tmichels on 4/10/15.
 */

@Configuration
@ComponentScan({"com.sforce.core.camel"})
public class SalesforceCamelRouteConfig extends SingleRouteCamelConfiguration implements InitializingBean {

    @Autowired
    SalesforceComponent salesforceCamelComponent;

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.run(args);
    }


    @Override
    protected CamelContext createCamelContext() throws Exception {
        SpringCamelContext camelContext = new SpringCamelContext();
        camelContext.setApplicationContext(getApplicationContext());
        camelContext.addComponent("salesforce", salesforceCamelComponent);
        camelContext.addRoutes(route());
        camelContext.start();
        return camelContext;
    }


    @Override
    public void afterPropertiesSet() throws Exception {}

    @Bean
    @Override
    public RouteBuilder route() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:getBasicInfo").to("salesforce:getBasicInfo").bean(BasicInfoBean.class);
            }
        };
    }

    public static class BasicInfoBean {
        public void someMethod(String body) {
            System.out.println(">>>>>>> Salesforce Basics Info: " + body);
        }
    }
}

5. Run SalesforceCamelIntegrationTest

package com.sforce.core.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by tmichels on 7/27/15.
 */

@ContextConfiguration(classes = {SalesforceCamelRouteConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class SalesforceCamelIntegrationTest {

    @Autowired
    CamelContext camelContext;

    @EndpointInject(uri = "direct:getBasicInfo")
    ProducerTemplate producer;

    @Test
    public void testCamelContext() throws Exception {
        String body = "Account";
        producer.sendBody(body);
    }
}

OUTPUT:

>>>>>>> Salesforce Basics Info: {"objectDescribe":{"name":"Account","label":"Account","updateable":true,"keyPrefix":"001","custom":false,"urls":{"sobject":"/services/data/v33.0/sobjects/Account","describe":"/services/data/v33.0/sobjects/Account/describe","rowTemplate":"/services/data/v33.0/sobjects/Account/{ID}","approvalLayouts":"/services/data/v33.0/sobjects/Account/describe/approvalLayouts","quickActions":"/services/data/v33.0/sobjects/Account/quickActions","listviews":"/services/data/v33.0/sobjects/Account/listviews","layouts":"/services/data/v33.0/sobjects/Account/describe/layouts","compactLayouts":"/services/data/v33.0/sobjects/Account/describe/compactLayouts"},"searchable":true,"labelPlural":"Accounts","layoutable":true,"activateable":false,"createable":true,"deprecatedAndHidden":false,"deletable":true,"customSetting":false,"feedEnabled":true,"mergeable":true,"queryable":true,"replicateable":true,"retrieveable":true,"undeletable":true,"triggerable":true},"recentItems":[{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhUzlIAF"},"Name":"Pope John XXIII Regional High School","Id":"001W000000JhUzlIAF"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhZFQIA3"},"Name":"A PLAZA DRIVING SCHOOL","Id":"001W000000JhZFQIA3"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JgxxXIAR"},"Name":"Advanced Reproductive Care, Inc. (ARC)","Id":"001W000000JgxxXIAR"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JrwqvIAB"},"Name":"name","Id":"001W000000JrwqvIAB"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhXSnIAN"},"Name":"Stella Niagara Education Park","Id":"001W000000JhXSnIAN"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jr1WOIAZ"},"Name":"A J DIANA SONS INC","Id":"001W000000Jr1WOIAZ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jr1TIIAZ"},"Name":"A J DIANA SONS COMPANY","Id":"001W000000Jr1TIIAZ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jr1WNIAZ"},"Name":"A J DIANA SONS INC","Id":"001W000000Jr1WNIAZ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JqzmSIAR"},"Name":"A J DIANA SONS INC","Id":"001W000000JqzmSIAR"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jq8TeIAJ"},"Name":"A J DIANA SONS COMPANY","Id":"001W000000Jq8TeIAJ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhZLTIA3"},"Name":"name","Id":"001W000000JhZLTIA3"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jq8LVIAZ"},"Name":"A PLAZA DRIVING SCHOOL","Id":"001W000000Jq8LVIAZ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhZBTIA3"},"Name":"A J DIANA SONS INC","Id":"001W000000JhZBTIA3"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JgxwjIAB"},"Name":"[First]Name [Last]Name","Id":"001W000000JgxwjIAB"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhUlVIAV"},"Name":"Central Texas Christian School","Id":"001W000000JhUlVIAV"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhKbtIAF"},"Name":"Sylvan Learning Center - 112","Id":"001W000000JhKbtIAF"}]}
%d bloggers like this: