macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Applying "Digital Hub" Concepts to Enterprise Software Design, Part 6
Pages: 1, 2

Type the following code in a text file named weatherTimeline.pl and give the file permission to execute with the command chmod +x weatherTimeline.pl in Terminal. To run the script, use Terminal to navigate to the directory containing the script and type ./weatherTimeline.pl.



Here is the code:

#!/usr/bin/perl -w
use SOAP::Lite;
use XML::Simple;
use Mac::AppleScript qw(RunAppleScript);

my $service = SOAP::Lite ->
service('http://localhost:55555/cgi-bin/WebObjects/
WeatherHub.woa/ws/XML?wsdl');

my $weatherXml = $service->buildCompleteXml();

$weatherXml =~ s/"/"/g;
$weatherXml =~ s/>/>/g;
$weatherXml =~ s/&lt;/</g;

my $config = XMLin($weatherXml, keyattr => ['id']);

my $outfile = "/tmp/weatherOut.txt";
open(OUT, ">$outfile");

print OUT "Label\tStart Time\tEnd Time\n";

foreach my $itemId (keys %{$config->{data}->{item}}) {
    my $thisItem = $config->{data}->{item}->{$itemId};
    my $typeId = $thisItem->{typeId};
    
print OUT $config->{measureTypes}->{type}->{$typeId}->{name} 
. ": "; 
    print OUT $thisItem->{data} . "\t";
    print OUT $thisItem->{time} . "\t\n";
}

close(OUT);

The above code does the following:

  • Connects to the WeatherHub application and downloads an up-to-date version of the weather data via a web service.

  • The weather data, which was returned as an XML string, is parsed into a data structure.

  • A tab-delimited text file is created, based on the information stored in the data structure.

Now that the tab-delimited file has been created, we can open it with the Bee Docs' Timeline application and do some formatting. We are going to control the Timeline application with AppleScript, but since we are already using Perl, we will call the AppleScript events directly from within the Perl script. Add the following code to the end of your Perl script:


my $script = qq(tell application "Timeline"
	   activate
	   open POSIX file "$outfile"
	   tell first document
		  set font name to "HelveticaNeue-Bold"
		  set font size to "12.0"
		  set background color to {28583, 35517, 42334}
		  
		  set the returned_events to every event
    repeat with i from 1 to the count of the returned_events
			 set thisLabel to label of event i
			 if thisLabel starts with "Temp" then
		   set event color of event i to {65535, 65535, 65535}
				set label of event i to thisLabel & "° F"
			 else
			 set event color of event i to {1758, 8435, 15546}
			 end if
		  end repeat

	   end tell
    end tell);

my $result = RunAppleScript( $script ) || 
die "Couldn't run script: $script";

Hint: When creating this code, it was helpful to test the AppleScript code using the normal Script Editor application. Once the AppleScript code was working, I just pasted it into the Perl script.

If you would prefer to download the code, here is a version with comments added. If WeatherHub is running and you have the modules installed, running the script should download all of the weather measurements from your database, create a timeline with the data, and format the timeline to look similar to the following. Of course, your timeline will be based on your actual data, so it may not exactly like mine.

Take note that we were able to create a cool timeline chart of our data without making any changes to the database, WebObjects code, or data entry tools.

Take note that we were able to create a cool timeline chart of our data without making any changes to the database, WebObjects code, or data entry tools. In fact, our various spoke components now contain code in Objective-C, Java, Perl, and AppleScript, and everything works together happily through the magic of XML and web services.

Extra Credit: Using Your Imagination

The map charts out people in the United States who have recently downloaded the Timeline beta from my web site.

The above map charts out people in the United States who have recently downloaded the Timeline beta from my web site. The map chart was drawn using iMap, which is a pretty cool app (as you can see).

Let us say that you wanted to create a map like this every morning for your sales team. You could start by creating a web service on your web server that returns summarized data from your web logs. Your spoke application could pull the data from the web service, resolve the IP addresses to physical locations, and put the data into a format that iMap can import. How do you get a physical location from an IP address? Another web service, of course. Check out this commercial web service that will do exactly that.

Keep in mind that the web service on the server does not need to be running on any particular platform for this to work. If you are trying to convince your company to buy you a new PowerBook but they are already committed to another server platform, whip up a little demo like this that works with their current server data. I cannot guarantee you will have a new laptop waiting for you at your next review, but you never know.

Here is another example of where this technology can go. I built a web-based document management system using similar technology. Our servers manage and store thousands of scanned documents. The user interface (build with Macromedia Flash) and administration tools (in Java, Perl, and Cocoa) communicate with the servers by passing XML back and forth with web services over secure HTML connections.

Want more ideas? How about some of the following?

  1. Allow unit-testing scripts to automatically log bugs to a centralized bug-tracking tool.

  2. Create a program on your process servers that can vend the latest processing statistics. Create client applications that use the data and cool Macintosh technology to prepare executive reports.

  3. Track the position of your web site in Google. (Click here for more information on Google web services.)

  4. Allow your retail locations to send sales data back to the corporate headquarters.

Web services really are a great technology which fit very nicely into the Hub and Spoke architecture that we have been exploring throughout this entire series. I would love to hear how your are applying these concepts to your area of expertise, so I encourage you to use the comment feature to share what you are up to. Maybe your application will spark ideas for someone else.

That's a Wrap!

I hope that you have enjoyed this series and have learned something you can use. I have enjoyed putting them together and have been encouraged by your emails and comments. Here a summary of what we covered:

  • Part one, Applying "Digital Hub" Concepts to Enterprise Software Design
    Overview of the hub-and-spoke design philosophy and why it works well for enterprise environments.

  • Part two, Designing the Database
    How to design a database hub that is very flexible and expandable.

  • Part three, Designing the XML
    XML is a great technology for transferring information in a platform independent way. How can we represent our data in XML while retaining the flexibility of our database?

  • Part four, XML Data Transfer with WebObjects
    Using WebObjects technology to complete the hub and make it easy to import and export data from our database hub.

  • Part five, Creating a Data Entry Tool With Cocoa
    Our first spoke is a Cocoa application that reads and writes the XML used by the hub.

  • Part six, Web Services and Informative Data Display
    We use our second spoke to demonstrate the power of web services, particularly as they relate to retrieving hub data for applying information display technologies on a client machine.

Resources

Adam Behringer works as the CEO of Bee Documents, which strives to help legal and medical firms efficiently manage their documents.


Return to MacDevCenter.com.