[Mapbender-commits] r8371 - trunk/mapbender/http/classes

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Wed Jun 6 11:43:36 PDT 2012


Author: armin11
Date: 2012-06-06 11:43:34 -0700 (Wed, 06 Jun 2012)
New Revision: 8371

Modified:
   trunk/mapbender/http/classes/class_iso19139.php
   trunk/mapbender/http/classes/class_wms.php
Log:
Some further work to outsource the metadata management functions into another class. This is needed for metadataurl handling in case of wfs featuretypes.

Modified: trunk/mapbender/http/classes/class_iso19139.php
===================================================================
--- trunk/mapbender/http/classes/class_iso19139.php	2012-06-06 15:19:06 UTC (rev 8370)
+++ trunk/mapbender/http/classes/class_iso19139.php	2012-06-06 18:43:34 UTC (rev 8371)
@@ -57,13 +57,15 @@
 			$iso19139Xml = simplexml_load_string($this->metadata);
 			if ($iso19139Xml === false) {
 				foreach(libxml_get_errors() as $error) {
-        				$e = new mb_exception($error->message);
+        				$err = new mb_exception($error->message);
     				}
 				throw new Exception('Cannot parse Metadata XML!');
+				return false;
 			}
 		}
 		catch (Exception $e) {
-    			$e = new mb_exception($e->getMessage());
+    			$err = new mb_exception($e->getMessage());
+			return false;
 		}
 		//if parsing was successful
 		if ($iso19139Xml != false) {
@@ -124,10 +126,82 @@
 	public function createFromDB($fileIdentifier){
 		return $this;
 	}
+
+	public function insertMetadataUrlToDB($resourceType, $resourceId){
+		$sql = "INSERT INTO mb_metadata (link, origin, md_format, linktype, createdate, changedate, export2csw, randomid) ";
+		$sql .= "VALUES($1, $2, $3, $4, now(), now(), $5, $6)";
+		
+		$v = array(
+			$this->href,
+			$this->origin,
+			$this->format,
+			$this->type,
+			'f',
+			$this->randomId
+		);
+		$t = array('s','s','s','s','b','s');
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			db_rollback();
+			$e = new mb_exception(_mb("Cannot save metadataUrl top database!"));
+			return false;
+		} else {
+			//insert relation into db
+			//get inserted metadata_id
+			$sql = <<<SQL
+SELECT metadata_id FROM mb_metadata WHERE randomid = $1
+SQL;
+			//maybe there are more than one results - which should be used??? case of creating new linkage with old metadata TODO TODO
+			$v = array($this->randomId);
+			$t = array('s');
+			try {
+				$res = db_prep_query($sql,$v,$t);
+			}
+			catch (Exception $e){
+				$e = new mb_exception(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
+			}
+			if (!$res) {
+				//do nothing
+				$e = new mb_exception(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
+				return false;
+			} else {
+				//insert relation
+				$row = db_fetch_assoc($res);
+				$metadataId = $row['metadata_id'];
+				//insert relation to layer/featuretype
+				if ($resourceType == 'layer') {
+					$sql = "INSERT INTO ows_relation_metadata (fkey_layer_id, fkey_metadata_id) values ($1, $2);";
+				}
+				if ($resourceType == 'featuretype') {
+					$sql = "INSERT INTO ows_relation_metadata (fkey_featuretype_id, fkey_metadata_id) values ($1, $2);";
+				}
+				$v = array($resourceId, $metadataId);
+				$t = array('i','i');
+				$res = db_prep_query($sql,$v,$t);
+				if(!$res){
+					db_rollback();
+					$e = new mb_exception(_mb("Cannot insert metadata relation!"));
+					return false;
+				} else {
+					$sql = "UPDATE mb_metadata SET harvestresult = 0 where metadata_id = $1";
+					$v = array($metadataId);
+					$t = array('i');
+					$res = db_prep_query($sql,$v,$t);
+					if(!$res){
+						db_rollback();
+						$e = new mb_exception(_mb("Cannot update mb_metadata table!"));
+						return false;
+					}
+				}		
+			}
+			return true;
+		}
+		return true;
+	}
 	
-	public function insertToDB(){
+	public function insertToDB($resourceType, $resourceId){
 		$sql = "INSERT INTO mb_metadata (link, origin, md_format, data, linktype, uuid, title, createdate, changedate, abstract, searchtext, type, tmp_reference_1, tmp_reference_2, export2csw, datasetid, datasetid_codespace, randomid) ";
-		$sql .= "VALUES($1, 'capabilities', $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)";
+		$sql .= "VALUES($1, $18, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)";
 		$v = array($this->href,
 			$this->format,
 			$this->metadata,
@@ -144,14 +218,64 @@
 			't',
 			$this->datasetId,
 			$this->datasetIdCodeSpace,
-			$this->randomId
+			$this->randomId,
+			$this->origin
 		);
-		$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','b','s','s','s');
+		$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','b','s','s','s','s');
 		$res = db_prep_query($sql,$v,$t);
 		if(!$res){
 			db_rollback();
+			$e = new mb_exception(_mb("Cannot insert metadata record into mapbenders mb_metadata table!");
 			return false;
 		} else {
+			//insert relation into db
+			//get inserted metadata_id
+			$sql = <<<SQL
+SELECT metadata_id FROM mb_metadata WHERE randomid = $1
+SQL;
+			//maybe there are more than one results - which should be used??? case of creating new linkage with old metadata TODO TODO
+			$v = array($this->randomId);
+			$t = array('s');
+			try {
+				$res = db_prep_query($sql,$v,$t);
+			}
+			catch (Exception $e){
+				$e = new mb_exception(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
+			}
+			if (!$res) {
+				//do nothing
+				$e = new mb_exception(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
+				return false;
+			} else {
+				//insert relation
+				$row = db_fetch_assoc($res);
+				$metadataId = $row['metadata_id'];
+				//insert relation to layer/featuretype
+				if ($resourceType == 'layer') {
+					$sql = "INSERT INTO ows_relation_metadata (fkey_layer_id, fkey_metadata_id) values ($1, $2);";
+				}
+				if ($resourceType == 'featuretype') {
+					$sql = "INSERT INTO ows_relation_metadata (fkey_featuretype_id, fkey_metadata_id) values ($1, $2);";
+				}
+				$v = array($resourceId, $metadataId);
+				$t = array('i','i');
+				$res = db_prep_query($sql,$v,$t);
+				if(!$res){
+					db_rollback();
+					$e = new mb_exception(_mb("Cannot insert metadata relation!"));
+					return false;
+				} else {
+					$sql = "UPDATE mb_metadata SET harvestresult = 1 where metadata_id = $1";
+					$v = array($metadataId);
+					$t = array('i');
+					$res = db_prep_query($sql,$v,$t);
+					if(!$res){
+						db_rollback();
+						$e = new mb_exception(_mb("Cannot update mb_metadata table to fill in harvest result!"));
+						return false;
+					}
+				} 	
+			}
 			return true;
 		}
 		

Modified: trunk/mapbender/http/classes/class_wms.php
===================================================================
--- trunk/mapbender/http/classes/class_wms.php	2012-06-06 15:19:06 UTC (rev 8370)
+++ trunk/mapbender/http/classes/class_wms.php	2012-06-06 18:43:34 UTC (rev 8371)
@@ -2377,104 +2377,38 @@
 				$mbMetadata = new Iso19139();
 				$randomid = new Uuid();
 				$mbMetadata = $mbMetadata->createFromUrl($this->objLayer[$i]->layer_metadataurl[$j]->href);
-				if ($mbMetadata != false) {
+				if ($mbMetadata != false) { //metadata has been parsed
 					//create randomId for metadata
 					$mbMetadata->randomId = $randomid;
 					$mbMetadata->href = $this->objLayer[$i]->layer_metadataurl[$j]->href;
 					$mbMetadata->format = $this->objLayer[$i]->layer_metadataurl[$j]->format;
 					$mbMetadata->type = $this->objLayer[$i]->layer_metadataurl[$j]->type;
 					$mbMetadata->origin = "capabilities";
-					if ($mbMetadata->insertToDB()){
-						//get inserted metadata_id
-						$sql = <<<SQL
-SELECT metadata_id FROM mb_metadata WHERE randomid = $1
-SQL;
-						//maybe there are more than one results - which should be used??? case of creating new linkage with old metadata TODO TODO
-						$v = array($randomid);
-						$t = array('s');
-						try {
-							$res = db_prep_query($sql,$v,$t);
-						}
-						catch (Exception $e){
-							$e = new mb_exception(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
-						}
-						if (!$res) {
-							//do nothing
-						} else {
-							//insert relation
-							$row = db_fetch_assoc($res);
-							$metadata_id = $row['metadata_id'];
-							//insert relation to layer
-							$sql = "INSERT INTO ows_relation_metadata (fkey_layer_id, fkey_metadata_id) values ($1, $2);";
-							$v = array($this->objLayer[$i]->db_id, $metadata_id);
-							$t = array('i','i');
-							$res = db_prep_query($sql,$v,$t);
-							if(!$res){
-								db_rollback();	
-							} else {
-								$sql = "UPDATE mb_metadata SET harvestresult = 1 where metadata_id = $1";
-								$v = array($metadata_id);
-								$t = array('i');
-								$res = db_prep_query($sql,$v,$t);
-							}		
-						}
+					//push metadata object into database
+					if (!$mbMetadata->insertToDB("layer",$this->objLayer[$i]->db_id)) {
+						$e = new mb_exception("Problem while storing metadata to mb_metadata table!");
+					} else {
+						$e = new mb_notice("Metadata with id ".$randomid." stored to db!");
 					}
 				} else {
-					//parsing made problems - fill only the urls into database and give a reasonable error message
-					$sql = "INSERT INTO mb_metadata (link, origin, md_format, linktype, randomid) ";
-					$sql .= "VALUES($1, 'capabilities', $2, $3, $4)";
-					$v = array($this->objLayer[$i]->layer_metadataurl[$j]->href,
-						$this->objLayer[$i]->layer_metadataurl[$j]->format,
-						$this->objLayer[$i]->layer_metadataurl[$j]->type,
-						$randomid	
-					);
-					$t = array('s','s','s','s');
-					$res = db_prep_query($sql,$v,$t);
-					if(!$res){
-						db_rollback();	
+					//parsing had problems - fill only the urls into database and give a reasonable error message	
+					$mbMetadata = new Iso19139();
+					$mbMetadata->randomId = $randomid;
+					$mbMetadata->href = $this->objLayer[$i]->layer_metadataurl[$j]->href;
+					$mbMetadata->format = $this->objLayer[$i]->layer_metadataurl[$j]->format;
+					$mbMetadata->type = $this->objLayer[$i]->layer_metadataurl[$j]->type;
+					$mbMetadata->origin = "capabilities";
+					if (!$mbMetadata->insertMetadataUrlToDB("layer",$this->objLayer[$i]->db_id)) {
+						$e = new mb_exception("Problem while storing MetadataURL entry from wms capabilities to mb_metadata table!");
 					} else {
-						//get last inserted metadata_id
-						$sql = <<<SQL
-SELECT metadata_id FROM mb_metadata WHERE randomid = $1
-SQL;
-						//maybe there are more than one results - which should be used??? case of creating new linkage with old metadata TODO TODO
-						$v = array($randomid);
-						$t = array('s');
-						try {
-							$res = db_prep_query($sql,$v,$t);
-						}
-						catch (Exception $e){
-							$e = new mb_exception(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
-						}
-						if (!$res) {
-							//do nothing
-						} else {
-							//insert relation
-							$row = db_fetch_assoc($res);
-							$metadata_id = $row['metadata_id'];
-							//insert relation to layer
-							$sql = "INSERT INTO ows_relation_metadata (fkey_layer_id, fkey_metadata_id) values ($1, $2);";
-							$v = array($this->objLayer[$i]->db_id, $metadata_id);
-							$t = array('i','i');
-							$res = db_prep_query($sql,$v,$t);
-							if(!$res){
-								db_rollback();
-								//TODO delete mb_metadata from table, cause the relation cannot be inserted
-							} else {
-								$sql = "UPDATE mb_metadata SET harvestresult = 0, harvestexception = 'Problem occured while parsing ISO19139 metadata!' where metadata_id = $1";
-								$v = array($metadata_id);
-								$t = array('i');
-								$res = db_prep_query($sql,$v,$t);
-							}		
-						}
+						$e = new mb_notice("Stored MetadataURL ".$mbMetadata->href." from wms capabilities to mb database cause the target could not be accessed or parsed!");
 					}
-				} //either links or metadata stored to database
+				}
 			}
 		}
 	}
 
 
-
 	function updateObjInDB($myWMS,$updateMetadataOnly=false){ //TODO give return true or false to allow feedback to user!!!
 		if (!isset($this->wms_network_access) || ($this->wms_network_access == '')) {
 			$this->wms_network_access = intval('0');



More information about the Mapbender_commits mailing list