Accessing Google Search Web Service and displaying the search
result
Java Studio Creator is bundled with a very powerful data handling layer between the components and the underlying data. Data Provider allows the users to access data in a consistent way, even though the data may come from different sources such as Database, EJB or a Web Service. In this blog I want to show how to use Data Provider to connect to Google Search Web Service to fetch data and display it in a table with proper link to web sites. Every thing is done graphically except for 2 line of code user needs to write in the Java file. Yes, just 2 line - it is not a typo!
This is what you need to do after creating a creating a project in Creator 2.
Lets call the project GoogleSearch.
Adding components
- Add a Text Field
- Add a button (text - Google Search)
- Add a Table component
Adding Web Service
- Drag and Drop
Servers -> Web Services -> samples -> GoogleSearch -> doGoogleSearch
(This adds W/S client googleSearchClient1 &
MethodResultTableDataProvider
googleSearchDoGoogleSearch1)
- Select googleSearchDoGoogleSearch1 in the out line and modify in the
property sheet
key - <your Google search key> (Note: You need to register and get a
key at http://www.google.com/apis)
maxResults = 10
q - Sun Java Studio Creator (Initial search, you can change it
later in the text field)
Adding Data Provider
- Drag and Drop an Object Array Data Provider. (id -
resultArrayDataProvider)
- Add an indexed property called "resultArray" of type
webservice.googlesearchservice.ResultElement[]
- Select resultArrayDataProvider and set its array property to "resultArray"
Setting Data Provider to Table
- Right click on table and select Table layout and then select Data
Provider resultArrayDataProvider
- Remove all columns except URL, snippet, summary and title
- Select HyperLink for component type for the column URL
- In the Outline Select tableColumn1 -> hyperlink and click [...] for the
"url" property in the property sheet
- Deselect "escapt" property of tableColumn2 -> staticText1, tableColumn3
-> staticText2, tableColumn3 -> staticText2
Setting Text Field Value to googleSearchDoGoogleSearch1
- Right click on TextField and select property binding and set the
binding to googleSearchDoGoogleSearch1
(The text should be - Sun Java Studio Creator)
Setting the Search Result to resultArrayDataProvider
- Double click on "Google Search" button and add code
resultArray = (ResultElement[]) ((GoogleSearchResult) getGoogleSearchDoGoogleSearch1()
.getResultObject()).getResultElements();
resultArrayDataProvider.setArray(resultArray);
Deploy and see the result after clicking on the "Google Search" button.