Location: PHPKode > projects > Entier Studio > entierstudio/studio/classes/procs/proc.OnlineDataResourceToTemporaryImport.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.OnlineDataResourceToTemporaryImport.php 94 2008-02-03 23:35:48Z yannromefort $
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefOnlineDataResourceToTemporaryImport")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefOnlineDataResourceToTemporaryImport", "1");
    //-------------------------------------------------------------------------
    //	Include
    @include_once (COMPONENTS_DATA . "data.TemporaryElement.php");
    @include_once (COMPONENTS_DATA . "data.TemporaryElementSet.php");
    @include_once (COMPONENTS_DATA . "data.TemporaryModelResource.php");
    @include_once (COMPONENTS_DATA . "data.TemporaryElementPropertyValue.php");
    //
    @include_once (COMPONENTS_DATA . "data.RepositoryModelResourceIndexer.php");
    @include_once (COMPONENTS_DATA . "data.RepositoryCoreElementPropertyValueSet.php");
    @include_once (COMPONENTS_DATA . "data.RepositoryModelResourcePropertyValueSet.php");
    //-------------------------------------------------------------------------
    //	Class
    class OnlineDataResourceToTemporaryImport {
        //---------------------------------------------------------------------
        //	Attributes
        
        /**
         *
         * @var	array
         * @see
         */
        var $m_optionSet = array();
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         *
         */
        function OnlineDataResourceToTemporaryImport() {
        }
        //---------------------------------------------------------------------
        //	Properties
        //
        //	_OptionSet.field_value
        /*
        * @param	string
        * @param	variant
        * @return	boolean
        */
        function set_option_value($option, $value) {
            $this->m_optionSet["$option"] = $value;
            return (true);
        }
        //
        /*
        * @param	string
        * @return	variant
        */
        function get_option_value($option) {
            if (isset($this->m_optionSet["$option"])) return ($this->m_optionSet["$option"]);
            return (false);
        }
        //---------------------------------------------------------------------
        //	Methods
        
        /**
         *
         * @access	private
         *
         * @param	object		name="datasource"
         * @param	array		name="repository"
         * @param	array		name="temporary"
         *
         */
        function importModelPropertyValueSet(&$datasource, &$repository, &$temporary) {
            //
            $temporaryValue = null;
            $propertyValueSet = new RepositoryModelResourcePropertyValueSet($repository["REPMRESPKID"]);
            if ($propertyValueSet->selectSet($datasource) == true) {
                //
                for ($i = 0;$i < $propertyValueSet->rowCount();$i++) {
                    //	select next row
                    if ($propertyValueSet->fetchCursorRow($datasource) == false) return (false);
                    //	insert relevant item
                    $temporaryValue = new TemporaryElementPropertyValue();
                    $temporaryValue->set_field_value(TMPELEMPKID, $temporary["TMPELEMPKID"]);
                    $temporaryValue->set_field_value(DICPROPPKID, $propertyValueSet->get_field_value("DICPROPPKID"));
                    $temporaryValue->set_field_value(DICPVALPKID, $propertyValueSet->get_field_value("DICPVALPKID"));
                    $temporaryValue->set_field_value(TMPEPRODATA, $propertyValueSet->get_field_value("REPMRPVDATA"));
                    $temporaryValue->set_field_value(TMPEPROTYPE, 2); // constraint type
                    //
                    if ($temporaryValue->insertRow($datasource) == false) return (false);
                    //
                    unset($temporaryValue);
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	private
         *
         * @param	object		name="datasource"
         * @param	array		name="repository"
         * @param	array		name="temporary"
         *
         */
        function importCorePropertyValueSet(&$datasource, &$repository, &$temporary) {
            //
            $temporaryValue = null;
            $propertyValueSet = new RepositoryCoreElementPropertyValueSet($repository["REPCELEPKID"]);
            if ($propertyValueSet->selectSet($datasource) == true) {
                //
                for ($i = 0;$i < $propertyValueSet->rowCount();$i++) {
                    //	select next row
                    if ($propertyValueSet->fetchCursorRow($datasource) == false) return (false);
                    //	insert relevant item
                    $temporaryValue = new TemporaryElementPropertyValue();
                    $temporaryValue->set_field_value(TMPELEMPKID, $temporary["TMPELEMPKID"]);
                    $temporaryValue->set_field_value(DICPROPPKID, $propertyValueSet->get_field_value("DICPROPPKID"));
                    $temporaryValue->set_field_value(DICPVALPKID, $propertyValueSet->get_field_value("DICPVALPKID"));
                    $temporaryValue->set_field_value(TMPEPRODATA, $propertyValueSet->get_field_value("REPCEPVDATA"));
                    $temporaryValue->set_field_value(TMPEPROTYPE, 1); // property type
                    //
                    if ($temporaryValue->insertRow($datasource) == false) return (false);
                    //
                    unset($temporaryValue);
                }
            }
            //
            return (true);
        }
        /**
         /
         * Import the field table element
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="directory"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	object		name="temporary"
         *
         */
        function importListTableNode(&$datasource, &$directory, &$repository, &$objectory, &$temporary) {
            //
            /*
            * 1 Initialize a TemporaryElement + check if already in temporary model set
            */
            //
            $element = new TemporaryElement();
            //
            $element->set_field_value("OBJELEMPKID", $temporary["OBJELEMPKID"]);
            $element->set_field_value("OBJECTOPKID", $temporary["OBJECTOPKID"]);
            $element->set_field_value("TMPELEMLINK", $temporary["TMPELEMPKID"]);
            //
            $element->set_field_value("OBJETYPPKID", $repository["OBJETYPPKID"]);
            $element->set_field_value("TMPELEMRANK", $repository["REPMRESRANK"]);
            $element->set_field_value("TMPELEMTEXT", $repository["REPMRESTEXT"]);
            //
            if ($this->get_option_value("USEPREFNAME")) // use prefix
            $element->set_field_value("TMPELEMNAME", $objectory["OBJELEMNAME"] . $repository["REPMRESNAME"]);
            else $element->set_field_value("TMPELEMNAME", $repository["REPMRESNAME"]);
            //
            if ($this->get_option_value("USEPREFCODE")) // use prefix
            $element->set_field_value("TMPELEMCODE", $objectory["OBJELEMCODE"] . $repository["REPMRESCODE"]);
            else $element->set_field_value("TMPELEMCODE", $repository["REPMRESCODE"]);
            //
            if ($this->get_option_value("USEPREFINAM")) // use prefix
            $element->set_field_value("TMPELEMINAM", $objectory["OBJELEMINAM"] . $repository["REPMRESINAM"]);
            else $element->set_field_value("TMPELEMINAM", $repository["REPMRESINAM"]);
            //
            if ($element->selectRow($datasource, NATURALKEY) == false) return ($element->insertRow($datasource, REFLECTKEY));
            //
            return (true);
        }
        /**
         * Import the root table element
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="directory"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	object		name="temporary"
         *
         */
        function importTableNode(&$datasource, &$directory, &$repository, &$objectory, &$temporary) {
            //
            /*
            * 1 Initialize a TemporaryElement + check if already in temporary model set
            */
            //
            $element = new TemporaryElement();
            //
            $element->set_field_value("OBJELEMPKID", $temporary["OBJELEMPKID"]);
            $element->set_field_value("OBJECTOPKID", $temporary["OBJECTOPKID"]);
            //
            $element->set_field_value("OBJETYPPKID", $repository["OBJETYPPKID"]);
            $element->set_field_value("TMPELEMRANK", $repository["REPMRESRANK"]);
            $element->set_field_value("TMPELEMTEXT", $repository["REPMRESTEXT"]);
            //
            $element->set_field_value("TMPELEMNAME", $objectory["OBJELEMNAME"]);
            $element->set_field_value("TMPELEMCODE", $objectory["OBJELEMCODE"]);
            $element->set_field_value("TMPELEMINAM", $objectory["OBJELEMINAM"]);
            //
            if ($element->selectRow($datasource, NATURALKEY) == false) {
                //
                if ($element->insertRow($datasource) == false) return (false);
                //
                /*
                * 2 Import node list
                */
                //
                switch ($directory->m_dataType) {
                    case TABLE_NODE:
                        //
                        return ($this->importNodeList($datasource, $directory, $objectory, $element->fieldSet()));
                        //
                        
                    break;
                }
            }
            //
            return (true);
        }
        /**
         *
         * Import the model resource leaf mapped to a field element
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="directory"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	object		name="temporary"
         *
         */
        function importListModelNode(&$datasource, &$directory, &$repository, &$objectory, &$temporary) {
            //
            /*
            * 1 Initialize a TemporaryElement + check if already in temporary model set
            */
            //
            $element = new TemporaryElement();
            //
            $element->set_field_value("OBJELEMPKID", $temporary["OBJELEMPKID"]);
            $element->set_field_value("OBJECTOPKID", $temporary["OBJECTOPKID"]);
            $element->set_field_value("TMPELEMLINK", $temporary["TMPELEMPKID"]);
            //
            $element->set_field_value("OBJETYPPKID", $repository["OBJETYPPKID"]);
            $element->set_field_value("TMPELEMRANK", $repository["REPMRESRANK"]);
            $element->set_field_value("TMPELEMTEXT", $repository["REPMRESTEXT"]);
            //
            if ($this->get_option_value("USEPREFNAME")) // use prefix
            $element->set_field_value("TMPELEMNAME", $objectory["OBJELEMNAME"] . $repository["REPMRESNAME"]);
            else $element->set_field_value("TMPELEMNAME", $repository["REPMRESNAME"]);
            //
            if ($this->get_option_value("USEPREFCODE")) // use prefix
            $element->set_field_value("TMPELEMCODE", $objectory["OBJELEMCODE"] . $repository["REPMRESCODE"]);
            else $element->set_field_value("TMPELEMCODE", $repository["REPMRESCODE"]);
            //
            if ($this->get_option_value("USEPREFINAM")) // use prefix
            $element->set_field_value("TMPELEMINAM", $objectory["OBJELEMINAM"] . $repository["REPMRESINAM"]);
            else $element->set_field_value("TMPELEMINAM", $repository["REPMRESINAM"]);
            //
            if ($element->selectRow($datasource, NATURALKEY) == true) {
                // Duplicate element => use data element identifier
                $element->set_field_value("TMPELEMCODE", $objectory["OBJELEMNAME"] . $directory->m_nodeName);
                $element->set_field_value("TMPELEMCODE", $objectory["OBJELEMCODE"] . $directory->m_nodeName);
            }
            //
            if ($element->selectRow($datasource, NATURALKEY) == false) {
                //
                if ($element->insertRow($datasource, REFLECTKEY) == false) return (false);
                //
                if ($this->importCorePropertyValueSet($datasource, $repository, $element->fieldSet()) == false) return (false);
                //
                if ($this->importModelPropertyValueSet($datasource, $repository, $element->fieldSet()) == false) return (false);
                //
                
            }
            //
            return (true);
        }
        /**
         /
         * Parse the data element tree
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="directory"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	object		name="temporary"
         *
         */
        function importNodeList(&$datasource, &$directory, &$repository, &$objectory, &$temporary) {
            //
            if (!empty($directory)) {
                //
                while ($node = $directory->fetchElements($directory->m_dataPath, "table")) {
                    //
                    /*
                    * 3 Lookup in RepositoryModelResource to a matching [Type + Flags] node
                    */
                    //
                    $resource = new RepositoryModelResourceIndexer();
                    $resource->set_field_value("REPMRESLINK", $repository["REPMRESPKID"]);
                    $resource->set_field_value("REPMRESFLAG", $node->m_dataProp);
                    if ($resource->selectRow($datasource, REFLECTKEY) == true) {
                        //
                        /*
                        * 3-1 Import model resource
                        */
                        //
                        if ($this->importListModelNode($datasource, $node, $resource->fieldSet() , $objectory, $temporary) == false) return (false);
                    } else {
                        //
                        /*
                        * 3-2 Import table resource
                        */
                        //
                        $model = new TemporaryModelResource($node->m_typeName);
                        if ($model->selectRow($datasource) == true) {
                            //
                            /*
                            * 3-3 Import table resource
                            */
                            //
                            $model->set_field_value("REPMRESNAME", $node->m_nodeName);
                            $model->set_field_value("REPMRESCODE", $node->m_nodeName);
                            $model->set_field_value("REPMRESINAM", $node->m_nodeName);
                            $model->set_field_value("REPMRESTEXT", $node->m_dataProp);
                            $model->set_field_value("REPMRESRANK", 1);
                            //
                            if ($this->importListTableNode($datasource, $node, $model->fieldSet() , $objectory, $temporary) == false) return (false);
                        }
                    }
                }
                //
                return (true);
            }
            //
            return (false);
        }
        /**
         *
         * Import the model resource root mapped to a table element
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="directory"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	object		name="temporary"
         *
         */
        function importModelNode(&$datasource, &$directory, &$repository, &$objectory, &$temporary) {
            //
            /*
            * 1 Initialize a TemporaryElement + check if already in temporary model set
            */
            //
            $element = new TemporaryElement();
            //
            $element->set_field_value("OBJELEMPKID", $temporary["OBJELEMPKID"]);
            $element->set_field_value("OBJECTOPKID", $temporary["OBJECTOPKID"]);
            //
            $element->set_field_value("OBJETYPPKID", $repository["OBJETYPPKID"]);
            $element->set_field_value("TMPELEMRANK", $repository["REPMRESRANK"]);
            $element->set_field_value("TMPELEMTEXT", $repository["REPMRESTEXT"]);
            //
            $element->set_field_value("TMPELEMNAME", $objectory["OBJELEMNAME"]);
            $element->set_field_value("TMPELEMCODE", $objectory["OBJELEMCODE"]);
            $element->set_field_value("TMPELEMINAM", $objectory["OBJELEMINAM"]);
            //
            if ($element->selectRow($datasource, NATURALKEY) == false) {
                //
                if ($element->insertRow($datasource) == false) return (false);
                //
                if ($this->importCorePropertyValueSet($datasource, $repository, $element->fieldSet()) == false)
                    return (false);
                //
                if ($this->importModelPropertyValueSet($datasource, $repository, $element->fieldSet()) == false)
                    return (false);
                //
                /*
                * 2 Import node list
                */
                //
                switch ($directory->m_nodeType) {
                    case TABLE_NODE:
                        //
                        return ($this->importNodeList($datasource, $directory, &$repository, $objectory, $element->fieldSet()));
                        //
                    break;
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="directory"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	boolean		name="deletion"
         * @return	boolean
         */
        function import(&$datasource, &$directory, &$repository, &$objectory, $deletion = false) {
            //
            if (!is_object($datasource))
                return (false);
            //
            if (!is_object($directory))
                return (false);
            //
            if (!is_array($objectory))
                return (false);
            //
            /*
            * 1 - Delete TemporaryElementSet
            */
            //
            if ($deletion == true) {
                $elementSet = new TemporaryElementSet($objectory["OBJELEMPKID"]);
                $elementSet->deleteSet($datasource);
            }
            //
            /*
            * 2 Initialize a TemporaryElement
            */
            //
            $temporary = new TemporaryElement();
            $temporary->set_field_value("OBJELEMPKID", $objectory["OBJELEMPKID"]);
            $temporary->set_field_value("OBJECTOPKID", $objectory["OBJECTOPKID"]);
            //
            /*
            * 3 Lookup in RepositoryModelResourceSet to a matching [Type + Flags] node
            */
            //
            $resource = new RepositoryModelResourceIndexer();
            $resource->set_field_value("DIRRESOPKID", $repository["DIRRESOPKID"]);
            $resource->set_field_value("REPMRESFLAG", (@is_null($directory->m_dataProp)) ? $directory->m_dataType : $directory->m_dataProp);
            // Search a relevant model resource
            if ($resource->selectRow($datasource, FOREIGNKEY) == true) {
                //
                /*
                * 3-1 Import model resource
                */
                //
                return ($this->importModelNode(
								$datasource, $directory, $resource->fieldSet() , $objectory, $temporary->fieldSet())
								);
            } else {
                //
                /*
                * 3-2 Import table resource
                */
                //
                $model = new TemporaryModelResource($directory->m_typeName);
                if ($model->selectRow($datasource) == true) {
                    //
                    /*
                    * 3-3 Import table resource
                    */
                    //
                    $model->set_field_value("REPMRESNAME", $directory->m_nodeName);
                    $model->set_field_value("REPMRESCODE", $directory->m_nodeName);
                    $model->set_field_value("REPMRESINAM", $directory->m_nodeName);
                    $model->set_field_value("REPMRESTEXT", $directory->m_dataProp);
                    $model->set_field_value("REPMRESRANK", 1);
                    //
                    return ($this->importTableNode(
								$datasource, $directory, $model->fieldSet() , $objectory, $temporary->fieldSet())
								);
                }
            }
            //
            return (true);
        }
    };
    //	Class
    //-------------------------------------------------------------------------    
}
//	namespace
//-----------------------------------------------------------------------------
?>
Return current item: Entier Studio