From 944d1d206df0c16b1d3ef9cee3f8f088e8dbd9de Mon Sep 17 00:00:00 2001 From: Lieven Janssen Date: Sun, 18 Sep 2011 21:08:58 +0200 Subject: [PATCH 1/2] Added generic HTML Table resource, solved bug in generic XLS resource. --- model/DBQueries.class.php | 42 +++++ .../resources/strategies/HTMLTable.class.php | 143 ++++++++++++++++++ model/resources/strategies/XLS.class.php | 14 +- unittests/temp/person.html | 8 + .../tests/APITestGenericHTMLTable.class.php | 71 +++++++++ unittests/tests/APITestSuite.class.php | 1 + 6 files changed, 272 insertions(+), 7 deletions(-) create mode 100644 model/resources/strategies/HTMLTable.class.php create mode 100644 unittests/temp/person.html create mode 100644 unittests/tests/APITestGenericHTMLTable.class.php diff --git a/model/DBQueries.class.php b/model/DBQueries.class.php index fe93d46a..665260ea 100644 --- a/model/DBQueries.class.php +++ b/model/DBQueries.class.php @@ -572,6 +572,48 @@ static function deleteXLSResource($package, $resource) { and package.id=package_id)", array(":package" => $package, ":resource" => $resource) ); + } + + /** + * Retrieve a specific HTML Table resource + */ + static function getHTMLTableResource($package, $resource) { + return R::getRow( + "SELECT generic_resource.id as gen_res_id,generic_resource_htmltable.uri as uri,generic_resource_htmltable.xpath as xpath + FROM package,resource, generic_resource, generic_resource_htmltable + WHERE package.package_name=:package and resource.resource_name=:resource + and package.id=resource.package_id + and resource.id = generic_resource.resource_id + and generic_resource.id=generic_resource_htmltable.gen_resource_id", + array(':package' => $package, ':resource' => $resource) + ); + } + + /** + * Store a HTML Table resource + */ + static function storeHTMLTableResource($resource_id, $uri, $xpath) { + $xlsresource = R::dispense("generic_resource_htmltable"); + $xlsresource->gen_resource_id = $resource_id; + $xlsresource->uri = $uri; + $xlsresource->xpath = $xpath; + return R::store($xlsresource); + } + + /** + * Delete a specific HTML Table resource + */ + static function deleteHTMLTableResource($package, $resource) { + return R::exec( + "DELETE FROM generic_resource_htmltable + WHERE gen_resource_id IN + (SELECT generic_resource.id FROM generic_resource,package,resource + WHERE resource.resource_name=:resource + and package.package_name=:package + and resource_id = resource.id + and package.id=package_id)", + array(":package" => $package, ":resource" => $resource) + ); } /** diff --git a/model/resources/strategies/HTMLTable.class.php b/model/resources/strategies/HTMLTable.class.php new file mode 100644 index 00000000..5260eb33 --- /dev/null +++ b/model/resources/strategies/HTMLTable.class.php @@ -0,0 +1,143 @@ +loadHtmlFile($uri); + + $domxpath = new DOMXPath( $html ); + $tablerows = $domxpath->query($xpath . "/tr" ); + + $rowIndex = 1; + foreach ($tablerows as $tr) { + $newDom = new DOMDocument; + $newDom->appendChild($newDom->importNode($tr,true)); + + $domxpath = new DOMXPath( $newDom ); + if ($rowIndex == 1) { + $tablecols = $domxpath->query("td"); + $columnIndex = 1; + foreach($tablecols as $td) { + $fieldhash[ $td->nodeValue ] = $columnIndex; + $columnIndex++; + } + } else { + $tablecols = $domxpath->query("td"); + $columnIndex = 1; + $rowobject = new stdClass(); + $keys = array_keys($fieldhash); + foreach($tablecols as $td) { + $c = $keys[$columnIndex - 1]; + if(sizeof($columns) == 0 || in_array($c,$columns)){ + $rowobject->$c = $td->nodeValue; + } + $columnIndex++; + } + if($PK == "") { + array_push($arrayOfRowObjects,$rowobject); + } else { + if(!isset($arrayOfRowObjects[$rowobject->$PK])){ + $arrayOfRowObjects[$rowobject->$PK] = $rowobject; + } + } + } + $rowIndex++; + } + + $resultobject->object = $arrayOfRowObjects; + return $resultobject; + } catch( Exception $ex) { + throw new CouldNotGetDataTDTException( $uri ); + } + } + + public function onDelete($package,$resource){ + DBQueries::deleteHTMLTableResource($package, $resource); + } + + public function onAdd($package_id,$resource_id,$content){ + $this->evaluateHTMLTableResource($resource_id,$content); + parent::evaluateColumns($content["columns"],$content["PK"],$resource_id); + } + + public function onUpdate($package,$resource,$content){ + // At the moment there's no request for foreign relationships between XLS files + // Yet this could be perfectly possible! + } + + + private function evaluateHTMLTableResource($resource_id,$content){ + DBQueries::storeHTMLTableResource($resource_id, $content["uri"], $content["xpath"]); + } +} +?> diff --git a/model/resources/strategies/XLS.class.php b/model/resources/strategies/XLS.class.php index 4dd92186..649826ee 100644 --- a/model/resources/strategies/XLS.class.php +++ b/model/resources/strategies/XLS.class.php @@ -91,13 +91,13 @@ public function onCall($package,$resource){ if(sizeof($columns) == 0 || in_array($c,$columns)){ $rowobject->$c = $cell->getCalculatedValue(); } - if($PK == "") { - array_push($arrayOfRowObjects,$rowobject); - } else { - if(!isset($arrayOfRowObjects[$rowobject->$PK])){ - $arrayOfRowObjects[$rowobject->$PK] = $rowobject; - } - } + } + } + if($PK == "") { + array_push($arrayOfRowObjects,$rowobject); + } else { + if(!isset($arrayOfRowObjects[$rowobject->$PK])){ + $arrayOfRowObjects[$rowobject->$PK] = $rowobject; } } } diff --git a/unittests/temp/person.html b/unittests/temp/person.html new file mode 100644 index 00000000..4d1cf376 --- /dev/null +++ b/unittests/temp/person.html @@ -0,0 +1,8 @@ + + + + + + +
nameagecity
Jan22Veridian City
Pieter22Pallet Town
Ash21Pallet Town
+ \ No newline at end of file diff --git a/unittests/tests/APITestGenericHTMLTable.class.php b/unittests/tests/APITestGenericHTMLTable.class.php new file mode 100644 index 00000000..0528fbfb --- /dev/null +++ b/unittests/tests/APITestGenericHTMLTable.class.php @@ -0,0 +1,71 @@ +install_as; + $data = array( "resource_type" => "generic", + "printmethods" => $this->printmethods, + "generic_type" => $this->generic_type, + "documentation" => "this is some documentation.", + "uri" => Config::$HOSTNAME . Config::$SUBDIR . $this->location, + "xpath" => $this->xpath, + "columns" => $this->columns, + "PK" => $this->PK + ); + + $request = new REST($url, $data, "PUT"); + $request->execute(); + + $this->assertEqual($request->http_code, 200); + if($request->result) + $this->debug($request->result); + } + + function testGetHTMLTable() { + $url = Config::$HOSTNAME . Config::$SUBDIR . $this->install_as; + $request = new REST($url, array(), "GET"); + $request->execute(); + + $this->assertEqual($request->http_code, 200); + if($request->http_code != 200 && $request->result) + $this->debug($request->result); + } + + function testDeleteHTMLTable() { + $url = Config::$HOSTNAME . Config::$SUBDIR . $this->install_as; + $request = new REST($url, array(), "DELETE"); + $request->execute(); + + $this->assertEqual($request->http_code, 200); + if($request->result) + $this->debug($request->result); + } +} + +?> \ No newline at end of file diff --git a/unittests/tests/APITestSuite.class.php b/unittests/tests/APITestSuite.class.php index b0fb015f..e818436a 100644 --- a/unittests/tests/APITestSuite.class.php +++ b/unittests/tests/APITestSuite.class.php @@ -19,6 +19,7 @@ function TestAPISuite() { $this->addFile(dirname(__FILE__) . "/APITestInstalled.class.php"); $this->addFile(dirname(__FILE__) . "/APITestGenericCSV.class.php"); $this->addFile(dirname(__FILE__) . "/APITestGenericXLS.class.php"); + $this->addFile(dirname(__FILE__) . "/APITestGenericHTMLTable.class.php"); $this->addFile(dirname(__FILE__) . "/APITestGenericDB.class.php"); $this->addFile(dirname(__FILE__) . "/APITestRemote.class.php"); } From dac46f43918163163e945a6e592b2e4beabe0953 Mon Sep 17 00:00:00 2001 From: Lieven Janssen Date: Sun, 18 Sep 2011 21:33:58 +0200 Subject: [PATCH 2/2] Generic HTML Table also working with thead, tbody structure. --- model/resources/strategies/HTMLTable.class.php | 8 ++++++++ unittests/temp/person.html | 12 ++++++++---- unittests/tests/APITestGenericHTMLTable.class.php | 3 +-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/model/resources/strategies/HTMLTable.class.php b/model/resources/strategies/HTMLTable.class.php index 5260eb33..505a57df 100644 --- a/model/resources/strategies/HTMLTable.class.php +++ b/model/resources/strategies/HTMLTable.class.php @@ -77,6 +77,10 @@ public function onCall($package,$resource){ $domxpath = new DOMXPath( $html ); $tablerows = $domxpath->query($xpath . "/tr" ); + if ($tablerows->length == 0) { + //table has thead and tbody + $tablerows = $domxpath->query($xpath . "/*/tr" ); + } $rowIndex = 1; foreach ($tablerows as $tr) { @@ -86,6 +90,10 @@ public function onCall($package,$resource){ $domxpath = new DOMXPath( $newDom ); if ($rowIndex == 1) { $tablecols = $domxpath->query("td"); + if ($tablecols->length == 0) { + //thead row has th instead of td + $tablecols = $domxpath->query("th" ); + } $columnIndex = 1; foreach($tablecols as $td) { $fieldhash[ $td->nodeValue ] = $columnIndex; diff --git a/unittests/temp/person.html b/unittests/temp/person.html index 4d1cf376..20e50c7b 100644 --- a/unittests/temp/person.html +++ b/unittests/temp/person.html @@ -1,8 +1,12 @@ - - - - + + + + + + + +
nameagecity
Jan22Veridian City
Pieter22Pallet Town
Ash21Pallet Town
nameagecity
Jan22Veridian City
Pieter22Pallet Town
Ash21Pallet Town
\ No newline at end of file diff --git a/unittests/tests/APITestGenericHTMLTable.class.php b/unittests/tests/APITestGenericHTMLTable.class.php index 0528fbfb..9ad56898 100644 --- a/unittests/tests/APITestGenericHTMLTable.class.php +++ b/unittests/tests/APITestGenericHTMLTable.class.php @@ -16,7 +16,7 @@ class APITestGenericHTMLTable extends TDTUnitTest{ //private $location = "http://www.nieuws.be/"; //private $xpath = "//table[@id='htmlGrid_a119d7e2-e979-436a-8cad-765d37fc0fdd']"; - private $location = "/unittests/temp/person.html"; + private $location = "unittests/temp/person.html"; private $xpath = "//table"; private $install_as = "htmltablepackage/person/"; private $generic_type = "HTMLTable"; @@ -56,7 +56,6 @@ function testGetHTMLTable() { if($request->http_code != 200 && $request->result) $this->debug($request->result); } - function testDeleteHTMLTable() { $url = Config::$HOSTNAME . Config::$SUBDIR . $this->install_as; $request = new REST($url, array(), "DELETE");