integrate social networks into bpm, part 1: collect...

28
Integrate social networks into BPM, Part 1: Collect Twitter data Use Bonita's process engine and connector framework to integrate external applications Skill Level: Intermediate Bilal Siddiqui Freelance consultant 10 May 2011 Updated 16 May 2011 Thanks to the flexibility of business process management (BPM) tools, you can integrate social networks into a company's business processes with minimal coding. This two-part article shows how an application built with Bonita Open Solution — an open source BPM tool — collects, displays, and uses data from social-networking sites. Part 1 shows how to configure the application to connect to the Twitter web service and enable users to do a keyword search for relevant data. In Part 2, you'll see how the application interfaces with a customer relationship management (CRM) system. IBM and BPM IBM has long been active in the area of business process, offering a robust set of commercial tooling to model, simulate, execute, adapt, monitor, and optimize your business processes. In April, IBM announced Business Process Manager V7.5, a substantial update to its comprehensive BPM platform. Expected availability is June 2011. In the meantime, keep up with IBM's BPM activities on developerWorks. Compelled by the increasing business value of social-networking websites such as Twitter and Facebook, enterprises want quick and easy ways to integrate social Collect Twitter data Trademarks © Copyright IBM Corporation 2011 Page 1 of 28

Upload: others

Post on 20-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Integrate social networks into BPM, Part 1: CollectTwitter dataUse Bonita's process engine and connector framework tointegrate external applications

Skill Level: Intermediate

Bilal SiddiquiFreelance consultant

10 May 2011

Updated 16 May 2011

Thanks to the flexibility of business process management (BPM) tools, you canintegrate social networks into a company's business processes with minimal coding.This two-part article shows how an application built with Bonita Open Solution — anopen source BPM tool — collects, displays, and uses data from social-networkingsites. Part 1 shows how to configure the application to connect to the Twitter webservice and enable users to do a keyword search for relevant data. In Part 2, you'llsee how the application interfaces with a customer relationship management (CRM)system.

IBM and BPMIBM has long been active in the area of business process, offering arobust set of commercial tooling to model, simulate, execute, adapt,monitor, and optimize your business processes. In April, IBMannounced Business Process Manager V7.5, a substantial updateto its comprehensive BPM platform. Expected availability is June2011. In the meantime, keep up with IBM's BPM activities ondeveloperWorks.

Compelled by the increasing business value of social-networking websites such asTwitter and Facebook, enterprises want quick and easy ways to integrate social

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 1 of 28

Page 2: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

networks into their business processes. One efficient approach is to use BPM tools,which are highly capable of integrating external applications and data into businessworkflows.

This two-part article shows how to use Bonita, an open source BPM tool, to build asocial-media search application for a hypothetical company. The application consistsof a business process that integrates seamlessly with external applications. It letsusers do a keyword search to collect business-related data from a social-networkingsite and then add some of that data to a CRM system with a single button click. Part1 describes the application's user interface and architecture, then walks through thesteps for configuring Bonita to fetch and display Twitter data in a business process.Part 2 completes the application by integrating it with the company's CRM system.

Exercise: IBM BPM in actionIBM BPM provides the end-to-end process visibility and control thatis critical for organizations to survive and excel in today's constantlychanging business environment. Use the IBM BPM in actionexercise to model and run business processes in a visual, code-freeenvironment.

The source code for this article's application is available for download. To follow thearticle's steps hands-on, download and install Bonita and Twitter4J beforeproceeding (see Resources for download links). I used Bonita version 5.4 andTwitter4J version 2.1.12 to develop the application.

Application overview

The entire application is developed as a business process through Bonita processconfiguration. A simple UI lets users search for data of business value from Twitterand displays messages (tweets) that meet the search criteria. The user selects atweet of interest from the results and fetches the profile of the Twitter user whoposted it. The search application also enables the application user to add thatTwitter user as a potential contact in SugarCRM, an open source CRM system.

User interface

The search application's UI consists of three simple forms that I've implementedusing Bonita's form designer. The first form presents two UI components: a SearchKeyword text box and a Search button, as shown in Figure 1:

Figure 1. The Search Tweets form

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 2 of 28

Page 3: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Bonita's form designer is quite powerful, providing graphical components for formdesign and allowing you to link user actions with other forms or back-end activitiesthrough configuration screens. For example, Bonita allows you to configure theSearch button in Figure 1 to be linked to the back-end search activity that looks forrelevant tweets.

After the user enters a search keyword and clicks the Search button, control istransferred to Bonita's business process engine. The engine works in thebackground to fetch tweets containing the search keyword. Bonita displays themessages it finds on a second UI form, as shown in Figure 2:

Figure 2. Form showing Twitter messages

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 3 of 28

Page 4: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

The user can select any message and click the View Profile button. Control is againtransferred to Bonita's process engine, which works in the background to fetchprofile data for the Twitter user who sent that tweet. The Twitter user's profile isdisplayed in a third UI form, as shown in Figure 3:

Figure 3. User Profile form

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 4 of 28

Page 5: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Users can click the Add to CRM button on the third UI form to add the Twitter userto their business contacts in SugarCRM. Bonita's process engine interfacesseamlessly with SugarCRM to make this possible.

Application architecture

BPM tools like Bonita have the flexibility to integrate with a variety of applications.Bonita's flexibility comes from its process engine and its connector framework. I'lldescribe the application's architecture to show how the process engine andconnector framework work with other components. Figure 4 diagrams thearchitecture:

Figure 4. Architecture of the search application

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 5 of 28

Page 6: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

You can see in Figure 4 that the search application consists of several majorcomponents:

• User interface

• Process variables

• Bonita's process engine

• Bonita's connector framework

• Twitter4J

• SugarCRM API

I've already covered the UI, so I'll discuss the other boxes shown in Figure 4:

• Process variables: Bonita provides process variables to hold applicationdata. Both application-level and local temporary variables are available.Bonita provides a GUI for configuring variables according to yourapplication's business logic and evaluating mathematical and logicalexpressions based on process variables.

• Bonita's process engine: Bonita's process engine coordinates all theactivities occurring among different application components. At the start,the process engine identifies the UI form to be displayed to the applicationuser. After the user enters search keywords, the process engine handlesthe application's flow and allows process variables to capture user input.You can say that the process engine manages the process's life cycleaccording to the business logic throughout the execution of theapplication.

• Bonita's connector framework: The connector framework is Bonita'sextensibility mechanism for integrating with outside applications. You usethe framework to build custom connectors. You can see that theconnector framework box in Figure 4 contains three connectors —

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 6 of 28

Page 7: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

TwitterSearchConnector, TwitterUserProfileConnector, andSugarCRMAddContactConnector — all built using the connectorframework. Each connector does a particular task. For example,TwitterSearchConnector acts as a search engine to search formessages of interest on Twitter.

• Twitter4J: The Twitter4J box is connected through the Internet to theTwitter web service, which lets client applications (such as the searchapplication) interact with the Twitter web site. Twitter4J is an open sourceJava API that makes it easy to work with the Twitter web service (seeResources). The TwitterSearchConnector wraps Twitter4J insideBonita's connector framework to enable Bonita to start working with theTwitter web service. (You will learn how to do this in this article'sImplementing the TwitterSearchConnector class section.)

• SugarCRM API: I'll discuss this component in Part 2.

Next I'll describe how the components of the search application work together.

Component-interaction sequence

Figure 5 shows the sequence of events that happens when a business user uses thesearch application to interact with the Twitter web service:

Figure 5. Sequence of events in the search application

1. The user enters a search keyword in the first UI form (Figure 1) and clicksthe Search button.

2. Bonita's process engine receives the search keyword and stores it in aprocess variable.

3. Bonita's process engine invokes TwitterSearchConnector.

4. TwitterSearchConnector uses Twitter4J to authenticate itself withthe Twitter web service and send it a search query containing the searchkeyword.

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 7 of 28

Page 8: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

5. The Twitter web service receives the search query, performs the actualsearch operation over Twitter, and returns the relevant messages toTwitterSearchConnector.

6. TwitterSearchConnector receives the search results and handsthem over to Bonita's process engine.

7. Bonita's process engine populates a process variable with the searchresults. Then it identifies the next task in the sequence and hands overcontrol to the search-results form (Figure 2).

8. The search-results UI form fetches the response from the processvariable and displays the messages to the user.

The search sequence can continue in a similar way from the second UI form (Figure2) to the user-profile form (Figure 3). If a user selects one of the tweets displayed inthe second form and clicks the View Profile button, the process engine callsTwitterUserProfileConnector, which fetches the profile of the user whoposted that tweet. Similarly, if the user decides to add that Twitter user's contactinformation into SugarCRM (by clicking the Add to CRM button), the process enginecalls the SugarCRM connector, which in turn adds the contact information to theCRM system.

You can see from this sequence of events that Bonita's process engine has a centralrole in coordinating all activities of the search application. Therefore, developing thesearch application in Bonita consists of telling Bonita's process engine what it mustdo at every step. This is called configuring a business process in Bonita. The rest ofthis article describes how to start configuring the search application.

Configuring the search application

The application-configuration tasks that you'll complete are:

1. Create a Twitter account.

2. Register the application with the Twitter web service and get securitycredentials for authentication.

3. Design and configure TwitterSearchConnector.

4. Configure the application's remaining components in Bonita (finishing inPart 2).

The first step, creating a Twitter account, is even simpler than creating an email

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 8 of 28

Page 9: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

account. The search application will act as a client of the Twitter web service. Forthe purpose of this article, I created a Twitter account named searchMessages. Ifyou want to perform the configuration steps yourself, go to https://twitter.com/signupand create your own account now.

Registering the application with Twitter and getting security credentials

By registering an application with the Twitter web service, you get two pairs of keys:

• A consumer key and a consumer secret that represent the clientapplication: The search application will use this pair of keys to accessthe Twitter web service using the account you've created.

• An access token and an access-token secret: The access token inconjunction with the secret will be used to sign the requests the searchapplication sends to the Twitter web service.

The security mechanism behind the two pairs of keys is called the OpenAuthentication (OAuth) protocol (see Resources). You don't need to worry abouthow OAuth works, because Twitter4J handles it behind the scenes. TheTwitterSearchConnector uses Twitter4J classes.

Log in to the Twitter web site (http://www.twitter.com) using the account you createdearlier. Type https://dev.twitter.com/apps/new in your browser's addressbar. You will see the Register an Application page, as shown in Figure 6:

Figure 6. Register an Application page on Twitter

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 9 of 28

Page 10: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Fill in the page as follows:

1. Enter Bonita search client in the text box labeled ApplicationName.

2. Type a short description (such as Client that searches for dataon Twitter in the Description field.

3. Enter the URL for your application in the Application Website field.

4. Select the Client radio button for Application Type. This means that your

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 10 of 28

Page 11: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

search application will act as a web service client.

5. Select the Read-only radio button for Default Access Type (because theapplication just needs to search for Twitter data, not send out any Twittermessages).

Leave the Organization and Callback URL fields empty and submit the completedform. You will get the Bonita search client Settings page giving you your securitycredentials, as shown in Figure 7:

Figure 7. The Bonita search client Settings page on Twitter

Write down the consumer key and consumer secret, because you will need themlater.

To get the second key pair (access token and access token secret), click the MyAccess Token button that's circled in red in Figure 7. The access token and itssecret are then displayed on another Twitter page, named Bonita search clientAccess Token Requisition, as shown in Figure 8:

Figure 8. The Bonita search client Access Token Requisition page on Twitter

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 11 of 28

Page 12: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Write down the access token and access token secret.

Now that you have the two key pairs, the next step is to design and configureTwitterSearchConnector.

Designing and configuring TwitterSearchConnector

Implementing a connector in Bonita is mostly a configuration task, which involves afew steps:

1. Provide descriptive information.

2. Configure input and output parameters for the connector.

3. Generate a Java code template for your connector. Bonita uses theEclipse Modeling Framework (EMF) and Graphical Modeling Framework(GMF) (see Resources) to generate a connector template. The templatecontains all the Java code that Bonita's connector framework requires,letting you focus on your connector's business logic. The only time you'llneed to write Java code for the application is when you use Twitter4J.

Bonita provides graphical wizards for almost every configuration step.

Run Bonita. Figure 9 shows the welcome page:

Figure 9. Bonita welcome page

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 12 of 28

Page 13: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Select Connector > New connector from the welcome page's menu. (I've circled inthe Connector menu item in red in Figure 9.) A new Connector creation wizardappears, as shown in Figure 10:

Figure 10. Connector creation wizard

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 13 of 28

Page 14: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

You can see from Figure 10 that the Connector creation wizard is divided into upperand lower portions. The upper portion collects descriptive information for theconnector (Connector Id, Description, Category, and Name of the Java class that willrepresent the connector). The lower portion consists of two table-like graphicalwidgets (Pages and Outputs), which allow you to define input and outputparameters.

Enter TwitterSearchConnector in the Connector Id text field and Searchtweets in the Description text field. Then select Social from the Category list. Thecategory field helps Bonita to categorize your connectors. You can see that Bonitaautomatically uses the Connector Id field to fill the Class Name field, which is the

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 14 of 28

Page 15: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

name of the Java class for this connector.

Click the Browse button next to the Package field to select the package of yourchoice for your TwitterSearchConnector class. You can also specify an icon foryour connector, which I am not doing in this article. This finishes the descriptioninformation.

After you have provided the description information, the wizard of Figure 10 lookslike Figure 11:

Figure 11. Connector creation wizard showing descriptive information aboutTwitterSearchConnector

Now you will define input parameters for TwitterSearchConnector.

Configuring input parameters for TwitterSearchConnector

Use the Pages table in the lower portion of Connector creation wizard to configureinput parameters for TwitterSearchConnector. Note that input parameters areto be passed by Bonita to TwitterSearchConnector. The Connector creationwizard allows you to group input parameters as pages. This means that whileconfiguring a business process, you can pass a number of input parameters fromone page. This page will be displayed to you when you add this connector to thesearch business process in Part 2.

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 15 of 28

Page 16: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Your TwitterSearchConnector needs the two pairs of keys (consumer key,consumer secret, access token, and access token secret) and the search keywordthat's entered in the first UI form (Figure 1). So you must define five inputparameters for your TwitterSearchConnector: the four security credentials,which are fixed when you add TwitterSearchConnector to the search businessprocess; and the search keyword, which is dynamic — that is, the user supplies itevery time he or she performs a search.

Click the Create button (circled in red in Figure 11) that's to the right of the Pagestable. This opens a new page containing fields for configuring input parameters forTwitterSearchConnector, as shown in Figure 12:

Figure 12. Page for configuring input parameters

You can see from Figure 12 that the input-parameter page contains three fields:

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 16 of 28

Page 17: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Page Id, Page Title, and Description. For your input-parameter page, simply enterTwitterSearchConnectorInputParametersPage in the Page Id field and thentype Twitter Search Connector Input Parameters Page in the Page Titleand Description fields.

Below the three fields, you find a Widgets table, which asks you to provide four fieldsfor each of your input parameters. Field name is the name of the parameter,Mandatory specifies whether the parameter is mandatory or optional, Widgetspecifies which visual component is associated with this input parameter, and theData type field specifies the data type of the variable associated with this parameter.

Now you will configure the four fields for each of the five input parameters. Click theCreate button, circled in red in Figure 12. A row in the Widgets table will appear inwhich you can enter consumerKey as the field name, Mandatory in the Mandatorycolumn, Password in the Widget column, and Text as value of the Data typecolumn. Use these same values for all five input parameters, as shown in Figure 13:

Figure 13. The completed page for configuring input parameters

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 17 of 28

Page 18: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Click the OK button at the bottom of the page. Bonita will save the input parametersconfigurations and will take you back to the earlier Connector creation wizard ofFigure 10.

Configuring output parameters

You need to define only one output parameter, named searchResults, for yourTwitterSearchConnector. Click the Create button (circled in blue in Figure 11)that's next to the output table . A new row appears in the output table. You can seeyou must provide data in just two columns for the output parameters. EntersearchResults in the Field name column. In the Data type column, select Listfrom the drop-down. The Connector creation wizard, which now contains all theconfiguration data that you have provided for input and output parameters, shouldlook like Figure 14:

Figure 14. Connector creation wizard containing configuration data for inputand output parameters

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 18 of 28

Page 19: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Click the Finish button on the Connector creation wizard. Bonita immediatelygenerates all the code for input and output parameters of theTwitterSearchConnector class and displays the code in an editor, as shown inFigure 15:

Figure 15. Template code of the TwitterSearchConnector class

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 19 of 28

Page 20: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Bonita saves the automatically generated code for the TwitterSearchConnectorclass in the X:\BOS-5.4\studio\workspace\local_default_MyExtensions\src-connectors\org\bonitasoft\connectors\twitter folder of your Bonitainstallation. (This article's source-code download includesTwitterSearchConnector.java for your convenience.) Next, you'll learn whatyou need to do with this code.

Implementing the TwitterSearchConnector class

Listing 1 shows the TwitterSearchConnector class:

Listing 1. The TwitterSearchConnector class generated by Bonita

public class TwitterSearchConnector extends ProcessConnector {

private String consumerKey;private String accessToken;private String accessTokenSecret;private String consumerSecret;private String searchKeyword;

@Overrideprotected void executeConnector() throws Exception {

// TODO Auto-generated method stub}

@Overrideprotected List<ConnectorError> validateValues() {

// TODO Auto-generated method stubreturn null;

}

public void setConsumerKey(String consumerKey) {

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 20 of 28

Page 21: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

this.consumerKey = consumerKey;}

public void setAccessToken(String accessToken) {this.accessToken = accessToken;

}

public void setSearchKeyword(String searchKeyword) {this.searchKeyword = searchKeyword;

}

public void setAccessTokenSecret(String accessTokenSecret) {this.accessTokenSecret = accessTokenSecret;

}

public void setConsumerSecret(String consumerSecret) {this.consumerSecret = consumerSecret;

}

public List getSearchResults() {// TODO Add return value for the output herereturn null;

}}

As you can see in Listing 1, TwitterSearchConnector extends theProcessConnector class, which is part of the connector framework.ProcessConnector provides functionality to manage multiple instances of aconnector running in separate threads of execution within Bonita's process engine.

The ProcessConnector class in turn extends Connector, which is the base classof the connector framework. It provides functionality to handle common tasks suchas input validation and exception handling.

If you look at the class-level variables in Listing 1, you'll notice that Bonita hasdeclared five variables corresponding to the five input parameters that youconfigured in the Configuring input parameters of TwitterSearchConnector section.

You also configured an output variable in the Configuring output parameters section,but Bonita has not declared an output variable in the TwitterSearchConnectorclass. So, you must manually add a sixth variable, named searchResults, to storeconnector output, as shown in bold in Listing 2:

Listing 2. Class-level variables for the TwitterSearchConnector class

//Class level variablesprivate String consumerKey;private String accessToken;private String accessTokenSecret;private String consumerSecret;private String searchKeyword;

//Manually added output variableprivate List<Tweet> searchResults;

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 21 of 28

Page 22: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Now look at setter and getter methods in Listing 1. You can see a setter method foreach of the five input parameters (setConsumerKey(), setConsumerToken(),setAccessToken(), setAccessTokenSecret(), and setSearchString()).Notice also that the class has only one getter method, namedgetSearchResults(). It corresponds to the searchResults output variable.However, the getSearchResults() method returns null in Listing 1, so youmust edit it manually, as shown in Listing 3, to return the searchResults outputvariable instead of null:

Listing 3. Manually edited getSearchResults() method

//Manually edited form of the getSearchResults()public List<Tweet> getSearchResults() {

return searchResults;}

Now look at the executeConnector() method in Listing 1. You need to fill thisempty method with the business logic of TwitterSearchConnector. The nextsection describes how to implement the logic using Twitter4J.

Implementing the executeConnector() method

The executeConnector() method of Bonita's connector framework allows you toimplement your connector's business logic. Bonita's process engine will call theexecuteConnector() method each time TwitterSearchConnector isinvoked. Listing 4 shows the executeConnector() method:

Listing 4. The executeConnector() method

protected void executeConnector() throws Exception {//Step1:Twitter twitter = new TwitterFactory().getInstance();

//Step2:twitter.setOAuthConsumer(consumerKey, consumerSecret);twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));

//Step3:twitter4j.Query query = new twitter4j.Query(searchKeyword);twitter4j.QueryResult queryResult = null;

//Step4:try{

queryResult = twitter.search(query);searchResults = queryResult.getTweets();

}catch (TwitterException e) {

System.out.println ("Twitter follow a user exception");e.printStackTrace();

}}

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 22 of 28

Page 23: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

The executeConnector() method uses Twitter4J to implement your businesslogic. The executeConnector() method is simple, consisting of four steps:

1. Create an instance of the Twitter class with the help of a factory classnamed TwitterFactory. The Twitter class allows you to use manyuseful methods of Twitter4J.

2. Call the setOAuthConsumer() and setOAuthAccessToken()methods of the Twitter class, passing authentication data(consumerKey, consumerSecret, accessToken, andaccessTokenSecret) along with the method call. Twitter4J handlesauthentication internally.

3. Instantiate the Query and QueryResult objects. The Query objectwraps the search keyword, and the QueryResult object wraps thesearch results.

4. Call the Twitter class's search() method, which takes the Queryobject as a parameter and returns search results wrapped inside theQueryResult object. Then extract messages from the QueryResultobject by calling its getTweets() method, and store the messages inthe searchResults variable.

Trying the TwitterSearchConnector class stand-alone

I've included a main() method in the TwitterSearchConnector class so youcan test the class's functionality in stand-alone mode without needing to run Bonita.Listing 5 shows the main() method:

Listing 5. The main() method of the TwitterSearchConnector class

public static void main(String args[]) {//Step1: InstantiateTwitterSearchConnector twitterSearchConnector =

new TwitterSearchConnector();

//Step2: Provide security informationtwitterSearchConnector.setConsumerKey(args[0]);twitterSearchConnector.setConsumerSecret(args[1]);twitterSearchConnector.setAccessToken(args[2]);twitterSearchConnector.setAccessTokenSecret(args[3]);twitterSearchConnector.setSearchKeyword(args[4]);

//Step3: Search Twittertry{

twitterSearchConnector.executeConnector();}catch (java.lang.Exception e){

e.printStackTrace();}List <Tweet> tweets = twitterSearchConnector.getSearchResults();

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 23 of 28

Page 24: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

//Step4: Print Twitter messagesfor (int i=0; i<tweets.size(); i++){

System.out.println ("User Name: ["+tweets.get(i).getFromUser()+"]");System.out.println ("Message ["+tweets.get(i).getText() +"/r/n");

}}//main

The main() method:

1. Instantiates the TwitterSearchConnector class.

2. Provides all the security and search-keyword data to theTwitterSearchConnector class.

3. Calls the executeConnector() method.

4. Prints the search results on the output console.

You can use the SearchTweetsCompile.bat and SearchTweetsRun.bat filesprovided in this article's source code download to compile and run theTwitterSearchConnector class as a stand-alone application. The two pairs ofkeys are hard-coded in the SearchTweetsRun.bat file.

The TwitterSearchConnector class uses Bonita libraries as well as Twitter4J,so Bonita-client-5.4.jar from the Bonita's download and twitter-core-2.1.12.jar fromTwitter4J need to be in your classpath.

Figure 16 shows my output console after I run the SearchTweetsRun.bat file:

Figure 16. Contents of the output console in stand-alone mode

The console displays the user name and message content for each tweet located bya search for the keyword CRM.

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 24 of 28

Page 25: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Part 1 wrap-up

I have discussed the architecture of the search application, showing how Bonita'sprocess engine and its connector framework can cooperate with each other tointegrate social-networking data into a business process. You have seen how toconfigure TwitterSearchConnector and also test theTwitterSearchConnector class in stand-alone mode. In Part 2, I'll show youhow to complete the application by configuring the remaining connectors to captureTwitter user profiles and integrate them into SugarCRM. The application will thencomprehensively demonstrate how to integrate a business process running insideBonita with a variety of external applications.

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 25 of 28

Page 26: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Downloads

Description Name Size Downloadmethod

Source code for this article's example j-sb1.zip 7KB HTTP

Information about download methods

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 26 of 28

Page 27: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

Resources

Learn

• developerWorks BPM zone: IBM business process management provides thetools you need to model, simulate, execute, adapt, monitor, and optimize yourbusiness processes.

• Business Process Management from IBM: BPM software and services from IBMhelp organizations optimize business performance by discovering,documenting, automating, and continuously improving business processes toincrease efficiency and reduce costs.

• IBM Business Process Management Journal: Learn more about IBM'scomprehensive BPM platform.

• Bonita Open Solution: Visit the Bonita website.

• OAuth: Check out the official OAuth website to learn more about the protocol.

• Authenticating Requests with OAuth: Get details on using OAuth to authenticateyour web and desktop applications with Twitter.

• "Bonita for business process management, Part 1: Configure a simple workflow"and "Bonita for business process management, Part 2: Configure forms andvariables" (Bilal Siddiqui, developerWorks, October-November 2010): Readabout configuring business processes and designing forms in Bonita.

• EMF and GMF: Learn about the Eclipse modeling frameworks that Bonita usesto generate connector templates.

Get products and technologies

• IBM BPM in action exercise: Model and run business processes in a visual,code-free environment.

• Bonita Open Solution: Download Bonita.

• Twitter4J: Download Twitter4J.

Discuss

• IBM Business Process Manager forum.Join the discussion in thedeveloperWorks community

• Get involved in the developerWorks community. Connect with otherdeveloperWorks users while exploring the developer-driven blogs, forums,groups, and wikis.

ibm.com/developerWorks developerWorks®

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 27 of 28

Page 28: Integrate social networks into BPM, Part 1: Collect ...public.dhe.ibm.com/software/dw/java/j-sb1-pdf.pdf · networks into their business processes. One efficient approach is to use

About the author

Bilal SiddiquiBilal Siddiqui is an electronics engineer, XML consultant, technologyevangelist, and frequently published technical author. He is the founderof XML4Java.com, a company focused on simplifying e-business. Aftergraduating in 1995 from the University of Engineering and Technology,Lahore, Bilal began designing software solutions for industrial controlsystems. Later, he turned to XML and built web- and WAP-based XMLprocessing tools, server-side parsing solutions, and serviceapplications. Since 2006, he has focused exclusively on Java- andXML-based open source tools and solutions. A strong advocate of opensource tools, he not only designs solutions based on them but alsotrains software and IT personnel at Lahore universities in using opensource technologies. Bilal is the author of JasperReports 3.6Development Cookbook (Packt Publishing, 2010).

developerWorks® ibm.com/developerWorks

Collect Twitter data Trademarks© Copyright IBM Corporation 2011 Page 28 of 28