fmsystem-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Fmsystem-commits] [6736] Added xpath query and test


From: Petur Bjorn Thorsteinsson
Subject: [Fmsystem-commits] [6736] Added xpath query and test
Date: Fri, 07 Jan 2011 15:32:15 +0000

Revision: 6736
          http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=6736
Author:   peturbjorn
Date:     2011-01-07 15:32:14 +0000 (Fri, 07 Jan 2011)
Log Message:
-----------
Added xpath query and test

Modified Paths:
--------------
    branches/dev-bim2/property/inc/class.sobim.inc.php
    branches/dev-bim2/property/tests/BIM/TestSObim.php

Modified: branches/dev-bim2/property/inc/class.sobim.inc.php
===================================================================
--- branches/dev-bim2/property/inc/class.sobim.inc.php  2011-01-07 14:21:07 UTC 
(rev 6735)
+++ branches/dev-bim2/property/inc/class.sobim.inc.php  2011-01-07 15:32:14 UTC 
(rev 6736)
@@ -1,8 +1,8 @@
 <?php
 
+//phpgw::import_class('phpgwapi.datetime');  // just a test import
 
 
-
 interface sobim {
         
        const bimItemTable = 'fm_bim_data';
@@ -21,6 +21,7 @@
        public function deleteBimItem($guid);
        public function checkIfBimItemExists($guid);
        public function updateBimItem($bimItem);
+       public function getBimItemAttributeValue($bimItemGuid, $attribute);
 }
 class sobim_impl implements sobim
 {
@@ -55,7 +56,7 @@
                                        'FROM public.fm_bim_data,  
public.fm_bim_type '.
                                        'WHERE  fm_bim_data.type = 
fm_bim_type.id ' .
                                'AND fm_bim_data.guid =\''.$bimObjectGuid.'\'';
-               $this->db->query($sql);
+               $this->db->query($sql,__LINE__,__FILE__);
                if($this->db->num_rows() == 0) {
                        throw new Exception('Item not found!');
                } else {
@@ -76,7 +77,11 @@
                        return $this->db->num_rows();
                }
        }
-       
+       /*
+        * Checks if the bim item exists
+        * @param string GUID
+        * @return boolean
+        */
        public function checkIfBimItemExists($guid) {
                $resultAlias = 'test_item_count';
                $sql = "SELECT count(id) as $resultAlias from 
public.".self::bimItemTable." where guid = '$guid'";
@@ -105,13 +110,45 @@
                if(!$this->checkIfBimItemExists($bimItem->getGuid())) {
                        throw new Exception("Item does not exist!");
                }
-               $sql = "Update ".self::bimItemTable." set 
xml_representation='$bimItem->getXml()' where guid='".$bimItem->getGuid()."'";
+               $sql = "Update ".self::bimItemTable." set 
xml_representation='".$bimItem->getXml()."' where 
guid='".$bimItem->getGuid()."'";
+               
         if(is_null($this->db->query($sql,__LINE__,__FILE__) )){
                        throw new Exception("Error updating xml of bim item!");
                } else {
                        return (bool)$this->db->num_rows();
                }
        }
+       /*
+        * Searches the xml representation and returns the values of any 
attributes that have the specified name
+        * If there are multiple elements with the same name, all of their 
value's will be returned
+        * Note: the name can be written in xpath format in relation to it's 
parent, so instead of 'name',
+        * you could write 'attributes/name'
+        * @access public
+        * @param string $bimItemGuid the guid of the item
+        * @param string $attribute the name of the attribute
+        * @throws Exception if nothing is found
+        * $return array results
+        */
+       public function getBimItemAttributeValue($bimItemGuid, $attribute) {
+               $columnAlias = "attribute_values";
+               //$sql = "select 
xpath('descendant-or-self::*[$attribute]/$attribute/text()', (select 
xml_representation from fm_bim_data where guid='$bimItemGuid'))";
+               $sql = "select 
array_to_string(xpath('descendant-or-self::*[$attribute]/$attribute/text()', 
(select xml_representation from fm_bim_data where guid='$bimItemGuid')), ',') 
as $columnAlias";
+               $this->db->query($sql,__LINE__,__FILE__);
+               if($this->db->num_rows() == 0) {
+                       throw new Exception('Error!');
+               } else {
+                       $this->db->next_record();
+                       $result = $this->db->f($columnAlias);
+                       return preg_split('/,/', $result);
+                       //$match; // xpath result from database will look like: 
'{data1, data2, data3}', or '{}' for no results
+                       //preg_match('/^\{(.*)\}$/', $result, $match);
+                       /*if(!$match[1]) {
+                               throw new Exception('Attribute not found!');
+                       } else {
+                               return preg_split('/,/', $match[1]);
+                       }*/
+               }
+       }
 
 
 

Modified: branches/dev-bim2/property/tests/BIM/TestSObim.php
===================================================================
--- branches/dev-bim2/property/tests/BIM/TestSObim.php  2011-01-07 14:21:07 UTC 
(rev 6735)
+++ branches/dev-bim2/property/tests/BIM/TestSObim.php  2011-01-07 15:32:14 UTC 
(rev 6736)
@@ -29,6 +29,7 @@
        private $bimItemTableName = 'fm_bim_data';
        private $projectGuid;
        private $projectType= 'ifcprojecttest';
+       private $newProjectName = 'New_project name';
        private $projectXml;
        private $buildingStorey1Guid;
        private $buildingStorey2Guid;
@@ -97,7 +98,9 @@
        public function testDb(){
                $this->assertNotNull($this->db);
        }
-       
+       /*
+        * @depends testDb
+        */
        public function testGetAll() {
                $sobim = new sobim_impl($this->db);
                $bimItems = $sobim->getAll();
@@ -111,42 +114,71 @@
                        
                }
        }
-       
+       /*
+        * @depends testGetAll
+        */
        public function testGetBimItem() {
                $sobim = new sobim_impl($this->db);
-               /* @var $bimItem BimItem */
+               /* @var $bimItem BimItem */  
                $bimItem = $sobim->getBimItem($this->projectGuid);
-               var_dump($bimItem);
+               
                $this->assertNotNull($bimItem);
                $bimItem->setDatabaseId(0);
                $localBimItem = new BimItem(0, $this->projectGuid, 
$this->projectType, $this->projectXml->asXML());
                $this->assertEquals($localBimItem, $bimItem);
        }
-       
+       /*
+        * @depends testGetBimItem
+        */
        public function testIfBimItemExists() {
                $sobim = new sobim_impl($this->db);
                
$this->assertTrue($sobim->checkIfBimItemExists($this->projectGuid));
        }
-       
+       /*
+        * @depends testIfBimItemExists
+        */
        public function testDeleteBimItem() {
                $sobim = new sobim_impl($this->db);
                $this->assertEquals(1, 
$sobim->deleteBimItem($this->projectGuid));
        }
-       
+       /*
+        * @depends testDeleteBimItem
+        */
        public function testAddBimItem() {
                $sobim = new sobim_impl($this->db);
                $itemToBeAdded = new BimItem(null, $this->projectGuid, 
$this->projectType, $this->projectXml->asXML());
                $this->assertEquals(1, $sobim->addBimItem($itemToBeAdded));
        }
-       
+       /*
+        * @depends testAddBimItem
+        */
        public function testUpdateBimItem() {
                $sobim = new sobim_impl($this->db);
                $bimItem = $sobim->getBimItem($this->projectGuid);
                $xml = new SimpleXMLElement($bimItem->getXml());
-               $xml->attributes->name = "new name";
+               $xml->attributes->name = $this->newProjectName;
+               
                $bimItem->setXml($xml->asXML());
                
                $this->assertTrue($sobim->updateBimItem($bimItem));
        }
-       
+       public function testExistingAttributeValue() {
+               $sobim = new sobim_impl($this->db);
+               try {
+                       $result = 
$sobim->getBimItemAttributeValue($this->projectGuid, "name");
+                       $this->assertTrue(in_array($this->newProjectName, 
$result));
+               } catch (Exception $e) {
+                       $this->fail($e);
+               }
+       }
+       public function testNonExistingAttributeValue() {
+               $sobim = new sobim_impl($this->db);
+               try {
+                       $result = 
$sobim->getBimItemAttributeValue($this->projectGuid, "nonExisting");
+                       var_dump($result);
+                       $this->assertFalse(count($result) > 0);
+               } catch (Exception $e) {
+                       $this->assertTrue(true);
+               }
+       }
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]