MixDEM Programmer's Reference Guide

Component building

Introduction

Components (or nodes in graph terminology) are independent units which perform simple (or sometimes complicated) transformations on data. Each component has 0 to N inputs and 0 to N outputs through which flows data. These inputs and outputs are represented by ports - Input Ports for reading data into componet nad Output Ports for writing data out of component.

1 - A simple component based on Google Map

In this section, we introduce simple component wich creating a google map and how to grab the latitude and longitude from the http://www.mapnut.com/calstatepark.xml feed, from the geo:lat and geo:long elements..


Example of an application using MAP_GOOGLE component
<?xml version="1.0" encoding="UTF-8"?>
<Graph>

    <!-- Define metadata -->
    <Metadata>
        <DataRecord name="geo.rss">
            <DataField name="title"    type="string" />
            <DataField name="link"     type="string" />
            <DataField name="geo:lat"  rename="lat" type="string" />
            <DataField name="geo:long" rename="long" type="string" />
        </DataRecord>
    </Metadata>

    <Element>
        <Node id="Read xml feed" type="XML_READER" path="rss.channel.item" 
        url="http://www.mapnut.com/calstatepark.xml" skiprows="1" maxrows="5" 
    />

        <Node id="Simple Mashup with a GMap" type="GOOGLE_MAP" skiprows="1" maxrows="2" 
              GMAPIKey="ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA"
              GMCenter="41.1, -121.4" GMZoom="5" GMCWidth="680px" GMCHeight="270px" GMMevent="click"
              GMContainer="map" GMMdataLat="@{lat}" GMMdataLong="@{long}" GMMdataHtml="@{title}"  
        />
        
        <Edge from="Read xml feed" to="Simple Mashup with a GMap" metadata="geo.rss" />
    </Element>
</Graph>
This sample application will generate an HTML page like this :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example: Map Markers</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=YOUR_KEY_HERE" type="text/javascript"></script>
    <script type="text/javascript">
        var GMapCenter    = new GLatLng( 41.1, -121.4 );
        var GMapZoom      = 5;
        var GMapContainer = "map";

        var GMapCGSM = true;
        var GMapCGMT = true;
        var GMapCGOM = true;

        var GMevent       = "click";
        var GMdataLat     = new Array( 41.12505189 , 37.8866 );
        var GMdataLong    = new Array( -121.4332765, -122.32 );
        var GMdataHtml    = new Array( "Ahjumawi Lava Springs SP" , "Albany State Marine Reserve" );

        function createGMarker( point, index, event, html, icon ) {
            var markerOpts;

            if( icon ) {
                var iIcon   = new GIcon(baseIcon);
                iIcon.image = icon;
                markerOpts  = { icon:iIcon };
            }

            var marker = new GMarker( point, markerOpts );

            GEvent.addListener( marker, event, function() {
                marker.openInfoWindowHtml( html );
            });

            return marker;
        }

        function initialize() {
            if ( GBrowserIsCompatible() ) {
                var map = new GMap2(document.getElementById( GMapContainer ));

                map.setCenter( GMapCenter, GMapZoom );
                
                if( GMapCGSM ) map.addControl(new GSmallMapControl());
                if( GMapCGMT ) map.addControl(new GMapTypeControl());
                if( GMapCGOM ) map.addControl(new GOverviewMapControl());

                for (var i = 0; i < GMdataLong.length; i++) {
                    var point = new GLatLng( GMdataLat[i], GMdataLong[i] );
                    map.addOverlay( createGMarker( point, i, GMevent, GMdataHtml[i] ));
                }
            }
        }
    </script>
    </head>
    <body onload="initialize()" onunload="GUnload()">
        <div id="map" style="width: 600px; height: 350px"></div>
    </body>
</html>
Code example with comments
<?php
    /*
        +--------------------------------------------------------------------------+
        | MixDEM ETL tools for Data Integration, Data Migration and Mashup edition |
        +--------------------------------------------------------------------------+
        | Copyright ( c ) 2008 MixDEM http://sourceforge.net/projects/mixdem/      |
        +--------------------------------------------------------------------------+
        |   This program is free software; you can redistribute it and/or modify   |
        |   it under the terms of the GNU General Public License as published by   |
        |   the Free Software Foundation; either version 2 of the License, or      |
        |   ( at your option ) any later version.                                  |
        |                                                                          |
        |   This program is distributed in the hope that it will be useful,        |
        |   but WITHOUT ANY WARRANTY; without even the implied warranty of         |
        |   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
        |   GNU General Public License for more details.                           |
        |                                                                          |
        |   You should have received a copy of the GNU General Public License      |
        |   along with this program; if not, write to the Free Software            |
        |   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307   |
        |   USA                                                                    |
        +--------------------------------------------------------------------------+
        | Authors: MMMed <momed@users.sourceforge.net>                             |
        +--------------------------------------------------------------------------+
    */

    /*
        +----------------------------------------------------------------------------------------------+
        |                                $ File identity card $                                        |
        +----------------------------------------------------------------------------------------------+
        | $IDFile            :                                                                         |
        | $Author            :                                                                         |
        | $DateCreation      : jj-mm-aaaa --:--:-- +hhmm                                               |
        | $LastModification  : jj-mm-aaaa --:--:-- +hhmm                                               |
        | $Contributor       :                                                                         |
        | $FileName          :                                                                         |
        | $FilePath          :                                                                         |
        +----------------------------------------------------------------------------------------------+
        | $FileDescription   :                                                                         |
        +----------------------------------------------------------------------------------------------+
    */
?>
<?php /************************************** HEADER GLOBAL *******************************************/
?>
<?php /********************************** LOCALS VARS & include ***************************************/
?>
<?php /***************************************** BODY *************************************************/

   /**
    * 
    *
    * @module     
    * @category   
    * @package    
    * @author     
    * @copyright  
    * @license    
    * @version    
    * @link       
    * @see        
    * @since      
    */

    class ETL_Component_Service_Google_Google_Map extends ETL_Component {

        /**
        * string   Component type
        */
        public $type       = "GOOGLE_MAP";

        var $GMAPIKey      = NULL;
        var $GMCenter      = NULL;
        var $GMZoom        = "5" ;
        var $GMContainer   = "map";

        var $GMCtrlGSM     = "TRUE";
        var $GMCtrlGMT     = "TRUE";
        var $GMCtrlGOM     = "TRUE";

        var $GMMevent      = "click";
        var $GMMIcon       = NULL;
        var $GMMdataLat    = NULL;
        var $GMMdataLong   = NULL;
        var $GMMdataHtml   = NULL;
        var $GMCWidth      = "500px";
        var $GMCHeight     = "500px";

        private $_HTML_TPL = NULL;

        var $skiprows   = 0    ;
        var $maxrows    = 0    ; 
        
        /**
        * This method is called prior to starting component.
        * Any allocation and checking should be done here. If anything goes wrong, it
        * should throw throwException. 
        *
        * @access public
        * @return boolean
        *      
        * @see    
        * @link   
        */
        function init( $options = NULL ) {
            GLOBAL $__L;
            GLOBAL $__E;

            $__L->info( "ETL::ETL_Component_Service_Google_Google_Map::init(" . print_r( $options, TRUE ) . ")" ); 

            $this->skiprows  = @( int ) $options["skiprows"]; 
            $this->maxrows   = @( int ) $options["maxrows"]; 

            # check skiprows & maxrows
            if ( $this->skiprows < 0 ) :
                $this->getSuper( )->setStatus( "ERROR" ); 

                $__E->throwException( $__E->ERROR_SKIPROWS_NEGATIVE, ", you given " . $this->skiprows  . ' in [' . $this->getSuper( )->getId() . ']' );
            endif;

            if ( $this->maxrows < 0 ) :
                $this->getSuper( )->setStatus( "ERROR" ); 

                $__E->throwException( $__E->ERROR_MAXROWS_NEGATIVE, ", you given " . $this->maxrows  . ' in [' . $this->getSuper( )->getId() . ']' );
            endif;

            if( isset ( $options["GMAPIKey"]    ) ) $this->GMAPIKey    = @ $options["GMAPIKey"];  

            if( isset ( $options["GMCenter"]    ) ) $this->GMCenter    = @ $options["GMCenter"];
            if( isset ( $options["GMZoom"]      ) ) $this->GMZoom      = @ $options["GMZoom"];
            if( isset ( $options["GMContainer"] ) ) $this->GMContainer = @ $options["GMContainer"];

            if( isset ( $options["GMCtrlGSM"]   ) ) $this->GMCtrlGSM   = @ $options["GMCtrlGSM"];
            if( isset ( $options["GMCtrlGMT"]   ) ) $this->GMCtrlGMT   = @ $options["GMCtrlGMT"];
            if( isset ( $options["GMCtrlGOM"]   ) ) $this->GMCtrlGOM   = @ $options["GMCtrlGOM"];

            if( isset ( $options["GMMevent"]    ) ) $this->GMMevent    = @ $options["GMMevent"];
            if( isset ( $options["GMMIcon"]     ) ) $this->GMMIcon     = @ $options["GMMIcon"];
            if( isset ( $options["GMMdataLat"]  ) ) $this->GMMdataLat  = @ $options["GMMdataLat"];
            if( isset ( $options["GMMdataLong"] ) ) $this->GMMdataLong = @ $options["GMMdataLong"];
            if( isset ( $options["GMMdataHtml"] ) ) $this->GMMdataHtml = @ $options["GMMdataHtml"];
            if( isset ( $options["GMCWidth"]    ) ) $this->GMCWidth    = @ $options["GMCWidth"];
            if( isset ( $options["GMCHeight"]   ) ) $this->GMCHeight   = @ $options["GMCHeight"];  

            if ( empty( $this->GMAPIKey ) ) :
                $this->getSuper()->setStatus( "ERROR" ); 

                $__E->throwException( $__E->ETL_COMPONENT_SERVICE_GOOGLE_GOOGLE_MAP_EMPTY_APIKEY, ' in [' . $this->getSuper( )->getId() . ']' );
            endif;

            $this->_HTML_TPL = 
                  " <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"                              \n"
                . "     \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">                              \n"
                . " <html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:v=\"urn:schemas-microsoft-com:vml\"> \n"
                . "   <head>                                                                                \n"
                . "     <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>            \n"
                . "     <title>Google Maps</title>                                                          \n"
                . "     <script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=" . $this->GMAPIKey . "\""
                .             " type=\"text/javascript\"></script>                                          \n"
                . "     <script type=\"text/javascript\">                                                   \n"
                . "         // Google Map Center                                                            \n"
                . "         var GMCenter    = new GLatLng( " . $this->GMCenter . " );                       \n"
                . "                                                                                         \n"
                . "         // Google Map Zoom                                                              \n"
                . "         var GMZoom      = " . $this->GMZoom . ";                                        \n"
                . "                                                                                         \n"
                . "         // Google Map Container HTML Element                                            \n"
                . "         var GMContainer = \"" . $this->GMContainer . "\";                               \n"
                . "                                                                                         \n"
                . "         // Google Map Control GSmallMapControl                                          \n"
                . "         var GMCtrlGSM   = true;                                                         \n"
                . "                                                                                         \n"
                . "         // Google Map Control GMapTypeControl                                           \n"
                . "         var GMCtrlGMT   = true;                                                         \n"
                . "                                                                                         \n"
                . "         // Google Map Control GOverviewMapControl                                       \n"
                . "         var GMCtrlGOM   = true;                                                         \n"
                . "                                                                                         \n"
                . "         // Google Map Marker event handler                                              \n"
                . "         var GMMevent    = \"" . $this->GMMevent . "\";                                  \n"
                . "                                                                                         \n"
                . "         // Google Map Marker icon URL                                                   \n"
                . "         var GMMIcon     = " . $this->GMMIcon . ";                                       \n"
                . "                                                                                         \n"
                . "         // Google Map Marker Lat                                                        \n"
                . "         var GMMdataLat  = new Array( {GMMdataLat}  );                                   \n"
                . "                                                                                         \n"
                . "         // Google Map Marker Long                                                       \n"
                . "         var GMMdataLong = new Array( {GMMdataLong} );                                   \n"
                . "                                                                                         \n"
                . "         // Google Map Marker InfoWindowHtml content                                     \n"
                . "         var GMMdataHtml = new Array( {GMMdataHtml} );                                   \n"
                . "                                                                                         \n"
                . "         function createGMarker( point, index, event, html, icon ) {                     \n"
                . "             var markerOpts;                                                             \n"
                . "                                                                                         \n"
                . "             if( icon ) {                                                                \n"
                . "                 var iIcon   = new GIcon(baseIcon);                                      \n"
                . "                 iIcon.image = icon;                                                     \n"
                . "                 markerOpts  = { icon:iIcon };                                           \n"
                . "             }                                                                           \n"
                . "                                                                                         \n"
                . "             var marker = new GMarker( point, markerOpts );                              \n"
                . "                                                                                         \n"
                . "             GEvent.addListener( marker, event, function() {                             \n"
                . "                 marker.openInfoWindowHtml( html );                                      \n"
                . "             });                                                                         \n"
                . "                                                                                         \n"
                . "             return marker;                                                              \n"
                . "         }                                                                               \n"
                . "                                                                                         \n"
                . "         function initialize() {                                                         \n"
                . "             if ( GBrowserIsCompatible() ) {                                             \n"
                . "                 var map = new GMap2(document.getElementById( GMContainer ));            \n"
                . "                                                                                         \n"
                . "                 map.setCenter( GMCenter, GMZoom );                                      \n"
                . "                                                                                         \n"
                . "                 if( GMCtrlGSM ) map.addControl(new GSmallMapControl());                 \n"
                . "                 if( GMCtrlGMT ) map.addControl(new GMapTypeControl());                  \n"
                . "                 if( GMCtrlGOM ) map.addControl(new GOverviewMapControl());              \n"
                . "                                                                                         \n"
                . "                 for (var i = 0; i < GMMdataLat.length; i++) {                           \n"
                . "                     var point = new GLatLng( GMMdataLat[i], GMMdataLong[i] );           \n"
                . "                     map.addOverlay( createGMarker(point, i, GMMevent, GMMdataHtml[i])); \n"
                . "                 }                                                                       \n"
                . "             }                                                                           \n"
                . "         }                                                                               \n"
                . "     </script>                                                                           \n"
                . "     </head>                                                                             \n"
                . "     <body onload=\"initialize()\" onunload=\"GUnload()\">                               \n"
                . "         <div id=\"map\" style="
                .              "\"width: " . $this->GMCWidth . "; height: " . $this->GMCHeight . "\"></div> \n"
                . "     </body>                                                                             \n"
                . " </html>                                                                                 \n";

            return TRUE;
        }

        /**
        * This is a main processing method of component.
        * By implementing run() method, we define what is the thread going to do.
        * After the graph is initialized (by colling init() methods of all registered
        * components), for every component in graph, there is a thread started and it
        * executes run() method. 
        *
        * @access public
        * @return boolean
        * 
        * @see    
        * @link
        */
        function run( ) {
            GLOBAL $__L; 
            GLOBAL $__E; 
            GLOBAL $__A; 

            $__L->info( "ETL::ETL_Component_Service_Google_Google_Map::run( )" );  

            $pattern = "/\@{(.*)}/";
            preg_match( $pattern, $this->GMMdataLat , $matchesGMMdataLat );
            if( count( $matchesGMMdataLat ) ):
                $matchesGMMdataLat = $matchesGMMdataLat[1];
            else:
                $matchesGMMdataLat = NULL;
            endif;

            preg_match( $pattern, $this->GMMdataLong, $matchesGMMdataLong );
            if( count( $matchesGMMdataLong ) ):
                $matchesGMMdataLong = $matchesGMMdataLong[1];
            else:
                $matchesGMMdataLong = NULL;
            endif;

            preg_match( $pattern, $this->GMMdataHtml, $matchesGMMdataHtml );
            if( count( $matchesGMMdataHtml ) ):
                $matchesGMMdataHtml = $matchesGMMdataHtml[1];
            else:
                $matchesGMMdataHtml = NULL;
            endif;

            $_resultLat  = 
            $_resultLong = 
            $_resultHtml = ARRAY( );
            for ( $i = 0; $i < count( $this->getSuper( )->inEdges ) ; $i++ ) :
                $idEdge = $this->getSuper( )->inEdges[$i];
                $edge   = $this->getSuper( )->getSuper( )->getEdge( $idEdge );

                for ( $index = 0 ; $index < $edge->getRecivedRecordCounter ( ) ; $index++ ) :
                    if( $this->skiprows && $index < $this->skiprows ) :
                        continue;
                    endif;

                    if( $this->maxrows && $index > $this->maxrows ) :
                        break;
                    endif;

                    $_rs = $edge->getRecord( $index ); 

                    if( $matchesGMMdataLat ) :
                        $_resultLat[] = $_rs[$matchesGMMdataLat];
                    else:
                        $_resultLat[] = $this->GMMdataLat;
                    endif;

                    if( $matchesGMMdataLong ) :
                        $_resultLong[] = htmlentities( $_rs[$matchesGMMdataLong] );
                    else:
                        $_resultLong[] = htmlentities( $this->GMMdataLong );
                    endif;

                    if( $matchesGMMdataHtml ) :
                        $_resultHtml[] = '"' . $_rs[$matchesGMMdataHtml] . '"';
                    else:
                        $_resultHtml[] = '"' . $this->GMMdataHtml . '"';
                    endif;
                endfor;
            endfor;

            $_result = str_replace( "{GMMdataLat}" , implode( ", ", $_resultLat  ) , $this->_HTML_TPL );
            $_result = str_replace( "{GMMdataLong}", implode( ", ", $_resultLong ) , $_result );
            $_result = str_replace( "{GMMdataHtml}", implode( ", ", $_resultHtml ) , $_result );

            $this->_HTML_TPL = $_result ;

            $this->getSuper( )->writeRecordBroadcast( ARRAY( "content" => $_result ) );

            return TRUE;
        }

        /**
        * Transform $this->super->result to default node (super) output format  
        *
        * @access public
        * @return void
        *      
        * @see    
        * @link   
        */
        function renderDEFAULT( ) {
            GLOBAL $__L;  

            $__L->info( "ETL::ETL_Component_Service_Google_Google_Map::renderDEFAULT( )" );

            echo $this->_HTML_TPL;

            return TRUE;
        }
    }
?>