Community MX publishes a web service that displays information about the latest content available from the site. This service can be used by anyone who knows how to consume a web service. Content of Community MX is copyright ©2003 by Community MX and cannot be redistributed, however you may use the web service on your own site to show a list of what is available on CMX. The first part of this article is at http://www.communitymx.com/abstract.cfm?cid=E5EA2A608932F770. To follow along, you will need the following:
The first article showed a simple interface to the Community MX web service built in Flash. To recap, a web service is a service that is created to communicate, using standard XML formats, to any application that knows how to talk to it. The web service will have a public interface written in Web Services Description Language (WSDL). The Community MX web service utilizes a standard .wsdl file that can be consumed by any technology that can consume a web service. The web service is located at http://www.communitymx.com/services/cmxfeed.wsdl. The web service exposes four methods:
*not implemented yet
The searchContent() method allows the consumer of the web service to search the content at Community MX and return the matching content to your program. There are several arguments to this method, as can be seen by examining the WSDL file:
<wsdl:message name ="searchContentRequest" >
<wsdl:part name="search" type ="SOAP-ENC:string" />
<wsdl:part name="anyWords" type ="SOAP-ENC:string" />
<wsdl:part name="exactPhrase" type ="SOAP-ENC:string" />
<wsdl:part name="contentType" type ="SOAP-ENC:string" />
<wsdl:part name="contentDate" type ="SOAP-ENC:string" />
</wsdl:message>
All of the arguments are strings, but they need some explanation:
The Community MX web service was built in ColdFusion, and uses the same mechanism for searching our site content that the site search feature uses. The following step-by-step tutorial will work in Flash MX. The download package contains the example .fla files in Flash MX and Flash MX 2004 format.
// Include the required classes for Flash Remoting
#include "NetServices.as"
// Set up variable for the Flash Remoting URL
var myURL = "http://localhost/flashservices/gateway";
// Set up variable for the Community MX wsdl descriptor file
var servicePath = "http://www.communitymx.com/services/cmxfeed.wsdl";
// Connection hasn't been initialized; create connection and service objects
if (initialized == null) {
initialized = true;
NetServices.setDefaultGatewayUrl(myURL);
var myConnection_conn = NetServices.createGatewayConnection();
var myService = myConnection_conn.getService(servicePath);
}
The URL of the Community MX .wsdl file is http://www.communitymx.com/services/cmxfeed.wsdl. As you can see, this is hard-coded. The path to the .wsdl file becomes our service path in Flash. This will cause the ColdFusion server (or ASP.NET server) to generate a proxy for the web service on the server.
// Define the custom SimpleResult class to display the results
function SimpleResult() { // Class constructor
}
// Set up onResult() and onStatus() handlers as methods of SimpleResult class
SimpleResult.prototype.onResult = function (myResults) {
var temp = "";
var theLength = myResults.length;
if(theLength == 0) {
temp = "No results"; // Display a message if
no results
} else { // Else, display the results
for (var i=0; i < theLength; i++) {
temp += makeLink(myResults[i].TITLE,myResults[i].URL)
+ "<br>";
temp += "Author: " + myResults[i].AUTHOR
+ "<br>";
temp += "Category: " + myResults[i].CATEGORY
+ "<br>";
temp += "<br>";
}
}
content_txt.htmlText = temp;
};
SimpleResult.prototype.onStatus = function (myError) {
content_txt.text = myError.description;
};
_global.System.onStatus = SimpleResult.prototype.onStatus;
The onResult method of the SimpleResult object will handle all results from the call to the web service. It makes use of a utility function, makeLink(), to generate a link from the TITLE and URL fields. We are also using the AUTHOR field and the CATEGORY field in the results. You are free to use the other fields that are not implemented here.
We are also creating an onStatus method to handle any errors from the remote call, as well as creating a handler for the _global.System.onStatus method, which usually indicates a failure to communicate.
// Make a clickable link out of some text, given the text and a location
function makeLink(theText,theLink) {
var temp = '<font color="#00cc00"><a href="';
temp += unescape(theLink);
temp += '" target="_blank">' + theText + '</a></font>';
return temp;
}
// Call the service on submit
submit_pb.setClickHandler("searchContent");
function searchContent(button) {
content_txt.text = "...working";
// the searchContent arguments are in the order:
(search, anyWords, exactPhrase,
contentType, contentDate)
myService.searchContent(
new SimpleResult(), search_txt.text, "false", "false", "0", "0");
}
The search text field provides the first argument so that the content can be searched. The second argument specifies that the search will split the words up and make sure each word matches in the search. If this were set to true, then a search result would allow a match so long as any of the words matched. The third argument is set to false so that the search words are not treated as an exact phrase. The fourth argument specifies a search of all content. The last argument uses a 0 to return all content dates. You could set this to 30 to get the content for the last 30 days.
The results can be seen here.
Quite a few changes have come along in Flash MX 2004 — some of them good, and some of them not so good. The following step can be replaced in Example 1 to make it work with Flash MX 2004:
listenerObject = new Object(); // Create the listener object
// Create the handler for the click event
listenerObject.click = function(searchContent){
content_txt.text = "...working";
myService.searchContent(
new SimpleResult(), search_txt.text, "false", "false", "0", "0");
}
// Add the listener to the click event of the button
submit_pb.addEventListener("click", listenerObject)
Another method of the web service is getContentTypes(), which gives you a list of content types in an array. This can be easily fed into a ComboBox component to further enhance the search. These content types are generated dynamically at Community MX because of the changing nature of the Community — new content and content types are being added regularly. As of this writing, the getContentTypes method returns this result:
0, ALL
1, Article
2, Tutorial
3, FAQ
4, Review
5, Extension
6, Daily Tips
7, Weekly Tips
1000, Knowledge Base
The method returns an array of objects. Each object contains a type_id and a type_desc . These properties can be fed into a drop-down list. To add the drop-down list to the first example, follow these steps for Flash MX:
// New responder object class to handle the comboBox
function ComboResult(theBox) { // pass the box to the responder
this.box = theBox;
}
// The onResult method handles the response from the web service
ComboResult.prototype.onResult = function(result) { // result contains an array
for(var i=0; i < result.length; i++) {
// Loop through and add the the description and ID fields (type_desc and type_id)
this.box.addItemAt(i, result[i].TYPE_DESC, result[i].TYPE_ID);
}
}
ComboResult.prototype.onStatus = SimpleResult.prototype.onStatus
myService.getContentTypes(new ComboResult(contentTypes_cb));
myService.searchContent(new SimpleResult(), search_txt.text, "false", "false", contentTypes_cb.getSelectedItem().data, "0");
Now if you view the movie, you should be able to do searches based on keywords and also search only the content types that you want to search, such as extensions or articles. The results can be seen here.
Component architecture has changed considerably in Flash MX 2004. For this reason, some of the existing methods used in Flash MX will not work the same way in Flash MX 2004, if at all. The ComboBox is one of those components that has been kept relatively the same between versions. This is a good thing. Example 2 should be workable with Flash MX 2004. One thing to keep in mind: Flash MX 2004 is case sensitive, so be careful when typing variables, method names, or any other part of your code. The CMX web service returns the field names as UPPER CASE, so this will not work in Flash MX 2004:
this.box.addItemAt(i, result[i].type_desc, result[i].type_id);
This will work in Flash MX 2004:
this.box.addItemAt(i, result[i].TYPE_DESC, result[i].TYPE_ID);
Flash Remoting remains the easiest, best way to use SOAP-based web services from within Flash. Building a Flash application that utilizes Remoting is simply a matter of learning a few easy API methods.
More information about Flash Remoting can be found at http://www.flash-remoting.com and at Macromedia's site.