Location: PHPKode > projects > Entier Studio > entierstudio/studio/classes/procs/proc.ComponentGeneration.php
<?php
/**
 * Entier Studio
 *
 * LICENSE
 *
 *	Copyright 2006 Entier Studio team.
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *
 *		http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 *
 * @package    entier.studio
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 * @version	   $Id: proc.ComponentGeneration.php 118 2008-03-07 22:38:39Z yannromefort $
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefComponentGeneration")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefComponentGeneration", "1");
    // Errors
    define("RELEASE_ERROR", 1);
    define("GARBAGE_ERROR", 2);
    //-------------------------------------------------------------------------
    //	Include
    @require_once (COMPONENTS_DATA . "data.ObjectoryElement.php");
    @require_once (COMPONENTS_DATA . "data.ObjectoryElementSet.php");
    @require_once (COMPONENTS_DATA . "data.GenerationComponentSet.php");
    @require_once (COMPONENTS_DATA . "data.ObjectoryComponentRelease.php");
    @require_once (COMPONENTS_DATA . "data.ObjectoryComponentTemplate.php");
    @require_once (COMPONENTS_DATA . "data.ObjectoryComponentReleaseSet.php");
    @require_once (COMPONENTS_DATA . "data.ObjectoryComponentReleaseGarbage.php");
    //
    @require_once (INTERCHANGE_PROC. "proc.ElementModelInterchange.php");
    //
    @require_once (INTERCHANGE_PROC. "proc.InterchangeElementNamespace.php");
    //
    //-------------------------------------------------------------------------
    //	Class
    class ComponentGeneration {
        //---------------------------------------------------------------------
        //	Attributes
        
        /**
         * Release unique id
         *
         * @var	integer
         */
        var $m_releaseId = 0;
        /**
         * Released number
         *
         * @var	integer
         */
        var $m_releasedNb = 0;
        /**
         * Garbage number
         *
         * @var	integer
         */
        var $m_discardedNb = 0;
        /**
         *
         * @var	array
         * @see
         */
        var $m_errorSet = array();
        /**
         *
         * @var	object
         * @see
         */
        var $m_modelInterchange = "";
        
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         *
         */
        function ComponentGeneration() {
        }
        //---------------------------------------------------------------------
        //	Properties
        /*
        * releaseId property
        * @return	integer
        */
        function releaseId() {
            //
            return ($this->m_releaseId);
        }
        /*
        * releasedCount property
        * @return	integer
        */
        function releasedCount() {
            //
            return ($this->m_releasedNb);
        }
        /*
        * discardedCount property
        * @return	integer
        */
        function discardedCount() {
            //
            return ($this->m_discardedNb);
        }
        /*
        *	_ErrorSet property
        * @return	array
        */
        function errorSet() {
            return ($this->m_errorSet);
        }
        /*
        *	_InterchangeFile property
        * @return	string
        */
        function InterchangeFile() {
			$modelInterchange = $this->modelInterchange;
			$interchangeFile  = $modelInterchange->InterchangeFile();
            return ("<XMP>" . $interchangeFile . "</XMP>");
        }
        //---------------------------------------------------------------------
        //	Methods
        
        /**
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="element"
         * @param	object		name="interchange"
         *
         * @return	boolean
         *
         */
        function generateComponentTemplate(&$datasource, &$component, &$element, &$interchange) {
            //
            $componentTemplate = new ObjectoryComponentTemplate($component["OBJCOMPPKID"]);
            //
            if ($componentTemplate->selectRow($datasource) == true) {
                //	1.1*	Non empty component
                $target = $componentTemplate->get_field_value("OBJETYPPKID");
                $script = $componentTemplate->get_field_value("OBJFILETEXT");
                if (!empty($script) && ($target == $element["OBJETYPPKID"])) {
                    //	1.2*	Select component transformer
                    $OBJFILELANG = $componentTemplate->get_field_value("OBJFILELANG");
                    $className = $OBJFILELANG . "Transform";
                    $classFile = @strtolower($OBJFILELANG) . ".transform.php";
                    //
                    @include_once (FRAMEWORK_DIR . $classFile);
                    //
                    if (class_exists($className) == true) {
                        //	1.3*	Execute transform
                        $OBJELEMPKID = $element["OBJELEMPKID"];
                        $OBJELEMROOT = $element["OBJELEMROOT"];
                        $OBJELEMLINK = $element["OBJELEMLINK"];
                        $OBJELEMNAME = $element["OBJELEMNAME"];
                        $OBJETYPCODE = $element["OBJETYPCODE"];
                        $transform = new $className($interchange->InterchangeFile() , $script);
                        //
                        if( $transform->output( 
											array(
												'eltId' => "e.$OBJELEMPKID", 
												'eltType' => "$OBJETYPCODE" 
												),
											false 
											) == true ) {                        
                            // Generation done
                            //
                            //	1.4*	Rename component defined pattern
                            $name = $OBJELEMNAME;
                            $model = InterchangeElementNamespace::getNamespaceRoot($datasource, $OBJELEMROOT);
                            $namespace = InterchangeElementNamespace::getNamespacePath($datasource, $OBJELEMLINK);
                            $identifier = $element["OBJELEMCODE"];
                            $instancier = $element["OBJELEMINAM"];
                            //
                            $OBJCRELNAME = $component["OBJCOMPPATH"];
                            //
                            $OBJCRELNAME = @str_replace("\$instancier", $instancier, $OBJCRELNAME);
                            $OBJCRELNAME = @str_replace("\$identifier", $identifier, $OBJCRELNAME);
                            $OBJCRELNAME = @str_replace("\$namespace", $namespace, $OBJCRELNAME);
                            $OBJCRELNAME = @str_replace("\$model", $model, $OBJCRELNAME);
                            $OBJCRELNAME = @str_replace("\$name", $name, $OBJCRELNAME);
                            //
                            //	1.5*	Desactive previous release
                            $releaseSet = new ObjectoryComponentReleaseSet($component["OBJCOMPPKID"], $element["OBJELEMPKID"]);
                            if ($releaseSet->selectSet($datasource) == true) {
                                if ($releaseSet->updateSet($datasource) == false) {
                                    //
                                    $this->m_errorSet["RELEASE_ERROR"] = true;
                                    //
                                    return (false);
                                }
                            }
                            unset($releaseSet);
                            //
                            //	1.5*	Insert new component release
                            $release = new ObjectoryComponentRelease();
                            $release->set_field_value("OBJCOMPPKID", $component["OBJCOMPPKID"]);
                            $release->set_field_value("OBJELEMPKID", $element["OBJELEMPKID"]);
                            $release->set_field_value("OBJCRELMILE", $this->m_releaseId);
                            $release->set_field_value("OBJCRELNAME", $OBJCRELNAME);
                            $release->set_field_value("OBJCRELFILE", @addslashes($transform->getTransformOutput()));
                            $release->set_field_value("OBJCRELMIME", $componentTemplate->get_field_value("OBJFILEMIME"));
                            //
                            if ($release->insertRow($datasource) == false) {
                                //
                                $this->m_errorSet["RELEASE_ERROR"] = true;
                                //
                                return (false);
                            }
                            //
                            unset($release);
                            //
                            $this->m_releasedNb++;
                        } else {
                            // Generation failed
                            //
                            //	1.5*	Insert new component release
                            $garbage = new ObjectoryComponentReleaseGarbage();
                            $garbage->set_field_value("OBJCOMPPKID", $component["OBJCOMPPKID"]);
                            $garbage->set_field_value("OBJELEMPKID", $element["OBJELEMPKID"]);
                            $garbage->set_field_value("OBJCRELCERR", $transform->lastError());
                            $garbage->set_field_value("OBJCRELMILE", $this->m_releaseId);
                            //
                            if ($garbage->insertRow($datasource) == false) {
                                //
                                $this->m_errorSet["GARBAGE_ERROR"] = true;
                                //
                                return (false);
                            }
                            unset($garbage);
                            //
                            $this->m_discardedNb++;
                        }
                        //
                        unset($transform);
                    }
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="element"
         * @param	object		name="template"
         *
         * @return	boolean
         *
         */
        function generateComponentSet(&$datasource, &$component, &$element, &$interchange) {
            //
            if (($component["OBJCTYPTYPE"] == MODEL_FILE_TYPE) && ($component["OBJMAKEMODE"] == DYNAMIC_TYPE)) {
                //
                return ($this->generateComponentTemplate($datasource, $component, $element, $interchange));
            } else {
                //	2*	Generate solutions components
                $componentSet = new GenerationComponentSet($component["OBJCOMPPKID"]);
                if ($componentSet->selectSet($datasource, REFLECTKEY, REFLECTKEY) == true) {
                    //
                    for ($i = 0;$i < $componentSet->rowCount();$i++) {
                        //	select next row
                        if ($componentSet->fetchCursorRow($datasource) == false) return (false);
                        //
                        if ($componentSet->get_field_value("OBJCTYPTYPE") == MODEL_FILE_TYPE) {
                            //
                            if ($componentSet->get_field_value("OBJMAKEMODE") == DYNAMIC_TYPE) {
                                //
                                if ($this->generateComponentTemplate($datasource, $componentSet->fieldSet() , $element, $interchange) == false) return (false);
                            }
                        } else {
                            //
                            if ($this->generateComponentSet($datasource, $componentSet->fieldSet() , $element, $interchange) == false) return (false);
                        }
                    }
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="element"
         * @return	boolean
         */
        function addContainedElementSet(&$datasource, &$component, &$element) {
            //
            $elementSet = new ObjectoryElementSet($element["DIRPROJPKID"], $element["OBJELEMPKID"]);
            if ($elementSet->selectSet($datasource, REFLECTKEY, FOREIGNKEY) == true) {
                //
                $modelInterchange = NULL;
                //
                for ($i = 0;$i < $elementSet->rowCount();$i++) {
                    //	select next row
                    if ($elementSet->fetchCursorRow($datasource) == false)
                        return (false);
                    //
                    /*
                    * 1- ElementSet Generation
                    */
                    //
                    if( PACKAGE_TYPE == $elementSet->get_field_value("OBJETYPPKID"))
                        $this->addContainedElementSet($datasource, $component, $elementSet->fieldSet());                     
                    //
                    /*
                    * 2- ComponentSet Generation
                    */
                    //	1.2- Generate model components
                    if ($this->generateComponentSet($datasource, $component, $elementSet->fieldSet() , $this->modelInterchange) == false) {
                        return (false);
                    }
                    //
                    unset($modelInterchange);
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="element"
         * @param	array		name="flags"
         *
         */
        function generate(&$datasource, &$component, &$element, $flags) {
            //
            if (!is_object($datasource))
                return (false);
            //
            if (!is_array($component))
                return (false);
            //
            if (!is_array($element))
                return (false);
            /*
            * 1- Milestone id
            */
            $this->m_releaseId = @crc32(@md5(@uniqid("")));
            //
            /**
            * 1- Generate interchange file
            */
            //
            $this->modelInterchange = new ElementModelInterchange();
            $modelElement			= NULL;
            if( MODEL_TYPE != $element["OBJETYPPKID"]) {
				// find model element
				$rootElement = new ObjectoryElement( $element["OBJELEMROOT"] );
				if (false == $rootElement->selectRow($datasource)) 
				    return (false);
				$modelElement = $rootElement->fieldSet();
			}
			else
				$modelElement = $element;
			//
            if ($this->modelInterchange->generateDocument($datasource, $modelElement) == false) {
                //
                return (false);
            }
            /**
            * 2- Generate contained elements components
            */
            //
            if ($flags["GENTREEFLAG"] == true) {
                //	1.1.1- Generate contained elements 4 model and package parent
                switch ($element["OBJETYPPKID"]) {
                    case MODEL_TYPE:
                    case PACKAGE_TYPE:
                        //
                        if ($this->addContainedElementSet($datasource, $component, $element) == false)
                            return (false);
                        //
                        break;
                }
            }
            //
            /**
            * 3- Generate main element components
            */
            //	1.2- Generate main model components
            if ($flags["GENMAINFLAG"] == true) {
                //
                if ($this->generateComponentSet($datasource, $component, $element, $this->modelInterchange) == false) {
                    //
                    return (false);
                }
            }
            //
            return (true);
        }
    };
    //	Class
    //-------------------------------------------------------------------------
    
}
//	namespace
//-----------------------------------------------------------------------------
?>
Return current item: Entier Studio