Location: PHPKode > projects > Entier Studio > entierstudio/studio/classes/procs/proc.ElementRepositoryToTemporaryImport.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.ElementRepositoryToTemporaryImport.php 81 2008-01-17 23:08:21Z yannromefort $
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefElementRepositoryToTemporaryImport")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefElementRepositoryToTemporaryImport", "1");
    //-------------------------------------------------------------------------
    //	Include
    //
    @include_once (COMPONENTS_DATA . "data.ObjectoryElement.php");
    @include_once (COMPONENTS_DATA . "data.ObjectoryElementType.php");
    @include_once (COMPONENTS_DATA . "data.TemporaryModelElement.php");
    @include_once (COMPONENTS_DATA . "data.TemporaryModelElementSet.php");
    //
    @include_once (COMPONENTS_DATA . "data.TemporaryElement.php");
    @include_once (COMPONENTS_DATA . "data.TemporaryElementSet.php");
    @include_once (COMPONENTS_DATA . "data.TemporaryElementPropertyValue.php");
    //
    @include_once (COMPONENTS_DATA . "data.RepositoryModelElementType.php");
    @include_once (COMPONENTS_DATA . "data.RepositoryCoreElementPropertyValueSet.php");
    @include_once (COMPONENTS_DATA . "data.RepositoryModelElementPropertyValueSet.php");
    //
    //-------------------------------------------------------------------------
    //	Class
    class ElementRepositoryToTemporaryImport {
        //---------------------------------------------------------------------
        //	Attributes
        
        /**
         *
         * @var	array
         * @see
         */
        var $m_optionSet = array();
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         *
         */
        function ElementRepositoryToTemporaryImport() {
        }
        //---------------------------------------------------------------------
        //	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 RepositoryModelElementPropertyValueSet($repository["REPMELEPKID"]);
            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("REPMEPVDATA"));
                    $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) {
                        print $temporaryValue->getInsertQuery();
                        exit();
                        return (false);
                    }
                    //
                    unset($temporaryValue);
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	object		name="temporary"
         *
         */
        function ImportTreeItem(&$datasource, &$repository, &$objectory, $temporary) {
            //
            $element = new TemporaryElement();
            //
            $element->set_field_value("OBJECTOPKID", $temporary["OBJECTOPKID"]);
            $element->set_field_value("OBJELEMPKID", $temporary["OBJELEMPKID"]);
            $element->set_field_value("TMPELEMLINK", $temporary["TMPELEMPKID"]);
            //
            $element->set_field_value("OBJETYPPKID", $repository["OBJETYPPKID"]);
            $element->set_field_value("REPMELEPKID", $repository["REPMELEPKID"]);
            $element->set_field_value("REPCELEPKID", $repository["REPCELEPKID"]);
            $element->set_field_value("TMPELEMRANK", $repository["REPMELERANK"]);
            $element->set_field_value("TMPELEMTEXT", $repository["REPMELETEXT"]);
            //
            if ($this->get_option_value("USEPREFNAME")) $element->set_field_value("TMPELEMNAME", $objectory["OBJELEMNAME"] . $repository["REPMELENAME"]);
            else $element->set_field_value("TMPELEMNAME", $repository["REPMELENAME"]);
            if ($this->get_option_value("USEPREFCODE")) $element->set_field_value("TMPELEMCODE", $objectory["OBJELEMCODE"] . $repository["REPMELECODE"]);
            else $element->set_field_value("TMPELEMCODE", $repository["REPMELECODE"]);
            if ($this->get_option_value("USEPREFINAM")) $element->set_field_value("TMPELEMINAM", $objectory["OBJELEMINAM"] . $repository["REPMELEINAM"]);
            else $element->set_field_value("TMPELEMINAM", $repository["REPMELEINAM"]);
            //
            if ($element->insertRow($datasource, REFLECTKEY) == true) {
                //
                if ($this->ImportCorePropertyValueSet($datasource, $repository, $element->fieldSet()) == false) return (false);
                //
                if ($this->ImportModelPropertyValueSet($datasource, $repository, $element->fieldSet()) == false) return (false);
                //	insert base row
                if ($this->ImportBase($datasource, $repository, $objectory, $element->fieldSet()) == false) return (false);
                //	select dependent rows
                if ($this->ImportTree($datasource, $repository, $objectory, $element->fieldSet()) == false) return (false);
            }
            //
            return (true);
        }
        /**
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	array		name="temporary"
         *
         */
        function ImportTree(&$datasource, &$repository, &$objectory, $temporary) {
            //
            $elementSet = new TemporaryModelElementSet($repository["REPMELEPKID"]);
            if ($elementSet->selectSet($datasource, REFLECTKEY, NATURALKEY) == true) {
                //
                for ($i = 0;$i < $elementSet->rowCount();$i++) {
                    //	select next row
                    if ($elementSet->fetchCursorRow($datasource) == false) return (false);
                    //
                    if ($this->ImportTreeItem($datasource, $elementSet->fieldSet() , $objectory, $temporary) == false) return (false);
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	array		name="temporary"
         *
         */
        function ImportBase(&$datasource, &$repository, &$objectory, $temporary) {
            //
            $elementType = new RepositoryModelElementType($repository["REPMELTPKID"]);
            $elementType->selectRow($datasource);
            if ($elementType->get_field_value("REPMELTTYPE") == MODEL_ENABLED) {
                $element = new RepositoryModelElement();
                $element->set_field_value("REPCELEPKID", $repository["REPCELEPKID"]);
                if ($element->selectRow($datasource, FOREIGNKEY) == true) {
                    //
                    if ($this->ImportTree($datasource, $element->fieldSet() , $objectory, $temporary) == false) return (false);
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	protected
         *
         * @param	object		name="datasource"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	object		name="temporary"
         *
         */
        function ImportItem(&$datasource, &$repository, &$objectory, $temporary) {
            //
            $element = new TemporaryElement();
            //
            $element->set_field_value("OBJECTOPKID", $temporary["OBJECTOPKID"]);
            $element->set_field_value("OBJELEMPKID", $temporary["OBJELEMPKID"]);
            //
            $element->set_field_value("OBJETYPPKID", $repository["OBJETYPPKID"]);
            $element->set_field_value("REPMELEPKID", $repository["REPMELEPKID"]);
            $element->set_field_value("REPCELEPKID", $repository["REPCELEPKID"]);
            $element->set_field_value("TMPELEMRANK", $repository["REPMELERANK"]);
            $element->set_field_value("TMPELEMTEXT", $repository["REPMELETEXT"]);
            //
            $element->set_field_value("TMPELEMNAME", $objectory["OBJELEMNAME"]);
            $element->set_field_value("TMPELEMCODE", $objectory["OBJELEMCODE"]);
            $element->set_field_value("TMPELEMINAM", $objectory["OBJELEMINAM"]);
            //
            if ($element->insertRow($datasource) == true) {
                //
                if ($this->ImportCorePropertyValueSet($datasource, $repository, $element->fieldSet()) == false) return (false);
                //
                if ($this->ImportModelPropertyValueSet($datasource, $repository, $element->fieldSet()) == false) return (false);
                //	insert base row
                if ($this->ImportBase($datasource, $repository, $objectory, $element->fieldSet()) == false) return (false);
                //	select dependent rows
                if ($this->ImportTree($datasource, $repository, $objectory, $element->fieldSet()) == false) return (false);
            }
            //
            return (true);
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="repository"
         * @param	array		name="objectory"
         * @param	boolean		name="deletion"
         *
         */
        function Import(&$datasource, &$repository, &$objectory, $deletion = false) {
            //
            if (!is_object($datasource)) return (false);
            //
            if (!is_array($repository)) return (false);
            //
            if (!is_array($objectory)) return (false);
            //
            /*
            * 1 - Delete TemporaryElementSet
            */
            //
            if ($deletion == true) {
                $temporarySet = new TemporaryElementSet($objectory["OBJELEMPKID"]);
                $temporarySet->deleteSet($datasource);
            }
            //
            /*
            * 2 Select Repository model
            */
            //
            $modelElement = new TemporaryModelElement($repository["REPMELEPKID"]);
            if ($modelElement->selectRow($datasource) == true) {
                //
                /*
                * 3 Initialize a TemporaryElement + check if already in temporary model set
                */
                //
                $temporary = new TemporaryElement();
                //
                $temporary->set_field_value("OBJECTOPKID", $objectory["OBJECTOPKID"]);
                $temporary->set_field_value("OBJELEMPKID", $objectory["OBJELEMPKID"]);
                //
                $temporary->set_field_value("TMPELEMCODE", $objectory["OBJELEMCODE"]);
                $temporary->set_field_value("OBJETYPPKID", $modelElement->get_field_value("OBJETYPPKID"));
                if ($temporary->selectRow($datasource, NATURALKEY) == true) {
                    // NATURALKEY( TMPELEMCODE OBJETYPPKID OBJELEMPKID ) exists
                    return (true);
                }
                //
                /*
                * 4 Insert relevant row
                */
                //
                return ($this->ImportItem($datasource, $modelElement->fieldSet() , $objectory, $temporary->fieldSet()));
            }
            //
            return (false);
        }
    };
    //	Class
    //-------------------------------------------------------------------------
    
}
//	namespace
//-----------------------------------------------------------------------------
?>
Return current item: Entier Studio