[Mapbender-commits] r8374 - in trunk/mapbender: http/classes http/php http/plugins resources/db/pgsql/UTF-8/update

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Mon Jun 11 13:56:50 PDT 2012


Author: armin11
Date: 2012-06-11 13:56:49 -0700 (Mon, 11 Jun 2012)
New Revision: 8374

Modified:
   trunk/mapbender/http/classes/class_iso19139.php
   trunk/mapbender/http/classes/class_wfsToDb.php
   trunk/mapbender/http/classes/class_wms.php
   trunk/mapbender/http/php/mod_deleteWMS.php
   trunk/mapbender/http/plugins/mb_metadata_server.php
   trunk/mapbender/http/plugins/mb_metadata_wfs_server.php
   trunk/mapbender/resources/db/pgsql/UTF-8/update/update_2.7.2_to_2.7.3_pgsql_UTF-8.sql
Log:
Change of mb_metadata handling for INSPIRE and SDI conformity. With the new handling, no doubled entries should be generated. Mapbender checks if a MetadataURL does already exists. If so, the metadata will be updated and only a new relation will be inserted into database. Independent updates of wms and wfs resources will result  in independent updates of coupled metadata resources. TODOs: 1. check handling for metadata upload and linkage (mapbender ows metadata editors), 2. push origin from mb_metadata to ows_relation_metadata for existing dbs (update sql), 3.  give back messages from class_iso19139 to metadata_server components. 4. control internal link function - make a concept for this (a posteriori service-metadata coupling), 5. less debug messages

Modified: trunk/mapbender/http/classes/class_iso19139.php
===================================================================
--- trunk/mapbender/http/classes/class_iso19139.php	2012-06-07 09:03:25 UTC (rev 8373)
+++ trunk/mapbender/http/classes/class_iso19139.php	2012-06-11 20:56:49 UTC (rev 8374)
@@ -1,5 +1,4 @@
 <?php
-# $Id: class_iso19139.php 3094 2008-10-01 13:52:35Z christoph $
 # http://www.mapbender.org/index.php/class_iso19139.php
 # Copyright (C) 2002 CCGIS 
 #
@@ -39,6 +38,18 @@
 	var $format;
 	var $type;
 	var $origin;
+	var $owner;
+	var $harvestResult;
+	var $harvestException;
+	//new
+	var $lineage;
+	var $inspireTopConsistence; //bool
+	var $spatialResType;
+	var $spatialResValue;
+	var $export2Csw; //bool
+	var $updateFrequency;
+	var $dataFormat;
+	var $inspireCharset;
 
 	function __construct() {
 		//initialize empty iso19139 object
@@ -55,12 +66,25 @@
 		$this->tmpExtentBegin = "1900-01-01";
 		$this->tmpExtentEnd = "1900-01-01";
 		$this->randomId = "";
+		$this->owner = 0; //dummy entry for metadata owner - in case of metadataURL entries the owner of the corresponding service
 		$this->href = "";
 		$this->format = "";
 		$this->type = "";
 		$this->origin = "";
 		$this->refSystem = "";
+		$this->harvestResult = 0;
+		$this->harvestException = "";
+
+		$this->lineage = "";
+		$this->inspireTopConsistence = "f";
+		$this->spatialResType = "";
+		$this->spatialResValue = "";
+		$this->export2Csw = "t";
+		$this->updateFrequency = "";
+		$this->dataFormat = "";
+		$this->inspireCharset = "";
 	}
+
 	public function removeGetRecordTag ($xml) {
 		$regex = "#<csw:GetRecordByIdResponse .*?>#";
 		$xml = preg_replace($regex,"",$xml);
@@ -146,6 +170,7 @@
 	
 	public function createFromUrl($url){
 		$metadataConnector = new connector($url);
+		$metadataConnector->set("timeOut", "5");
 		$xml = $metadataConnector->file;
 		$mbMetadata = $this->createMapbenderMetadataFromXML($xml);
 		return $mbMetadata;
@@ -155,23 +180,104 @@
 		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)";
-		
+	public function deleteMetadataRelation($resourceType, $resourceId, $relationType){
+		//delete all relations which are defined from capabilities - this don't delete the metadata entries themself!
+		//all other relations stay alive
+		$sql = "DELETE FROM ows_relation_metadata WHERE fkey_".$resourceType."_id = $1 AND relation_type = '".$relationType."'";
+		$v = array($resourceId);
+		$t = array('i');
+		$res = db_prep_query($sql,$v,$t);
+		if(!$res){
+			$e = new mb_exception("Cannot delete metadata relations for resource ".$resourceType." with id: ".$resourceId);
+			return false;
+		} else {
+			return true;
+		}
+	}
+
+public function checkMetadataRelation($resourceType, $resourceId, $metadataId){
+	//check if one relation already exists - if so no new one should be generated!!!
+	}
+
+	public function isLinkAlreadyInDB(){
+		$sql = <<<SQL
+SELECT metadata_id FROM mb_metadata WHERE link = $1 ORDER BY lastchanged DESC
+SQL;
 		$v = array(
-			$this->href,
-			$this->origin,
-			$this->format,
-			$this->type,
-			'f',
-			$this->randomId
+			$this->href
 		);
-		$t = array('s','s','s','s','b','s');
+		$t = array('s');
 		$res = db_prep_query($sql,$v,$t);
+		while ($row = db_fetch_array($res)){
+			$metadataId[] = $row['metadata_id'];	
+		}
+		if (count($metadataId) > 0) {
+			return $metadataId;
+		} else {	
+			return false;
+		}
+	}
+	
+	public function isFileIdentifierAlreadyInDB(){
+		$sql = <<<SQL
+SELECT metadata_id FROM mb_metadata WHERE uuid = $1 ORDER BY lastchanged DESC
+SQL;
+		$v = array(
+			$this->fileIdentifier
+		);
+		$t = array('s');
+		$res = db_prep_query($sql,$v,$t);
+		$metadataId = array();
+		while ($row = db_fetch_array($res)){
+			$metadataId[] = $row['metadata_id'];	
+		}
+		if (count($metadataId) > 0) {
+			return $metadataId;
+		} else {	
+			return false;
+		}
+	}
+
+	public function insertMetadataUrlToDB($resourceType, $resourceId){
+		//check if metadata record already exists, if not create a new one else insert relation only!
+		$metadataId = $this->isLinkAlreadyInDB();
+		if ($metadataId != false) {
+			//update the metadataURL entry
+			$e = new mb_exception("existing metadata link(s) found: ".$metadataId[0]." - update will be performed");
+			$sql = "UPDATE mb_metadata SET link = $1, origin = $2, md_format = $3, linktype = $4, changedate = now(), export2csw = $5, randomid = $6, harvestresult = $8, harvestexception = $9 WHERE metadata_id = $7";
+			$v = array(
+				$this->href,
+				$this->origin,
+				$this->format,
+				$this->type,
+				'f',
+				$this->randomId,
+				$metadataId[0],
+				$this->harvestResult,
+				$this->harvestException
+			);
+			$t = array('s','s','s','s','b','s','i','i','s');
+		} else {
+			$sql = "INSERT INTO mb_metadata (link, origin, md_format, linktype, createdate, changedate, export2csw, randomid, fkey_mb_user_id, harvestresult, harvestexception) ";
+			$sql .= "VALUES($1, $2, $3, $4, now(), now(), $5, $6, $7, $8, $9)";
+			$v = array(
+				$this->href,
+				$this->origin,
+				$this->format,
+				$this->type,
+				'f',
+				$this->randomId,
+				$this->owner,
+				$this->harvestresult,
+				$this->harvestexception
+			);
+			$t = array('s','s','s','s','b','s','i','i','s');
+		}	
+
+		$res = db_prep_query($sql,$v,$t);
 		if(!$res){
 			db_rollback();
-			$e = new mb_exception(_mb("Cannot save metadataUrl top database!"));
+			$e = new mb_exception(_mb("Cannot insert or update metadataUrl in database!"));
 			return false;
 		} else {
 			//insert relation into db
@@ -198,13 +304,13 @@
 				$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);";
+					$sql = "INSERT INTO ows_relation_metadata (fkey_layer_id, fkey_metadata_id, relation_type) values ($1, $2, $3);";
 				}
 				if ($resourceType == 'featuretype') {
-					$sql = "INSERT INTO ows_relation_metadata (fkey_featuretype_id, fkey_metadata_id) values ($1, $2);";
+					$sql = "INSERT INTO ows_relation_metadata (fkey_featuretype_id, fkey_metadata_id, relation_type) values ($1, $2, $3);";
 				}
-				$v = array($resourceId, $metadataId);
-				$t = array('i','i');
+				$v = array($resourceId, $metadataId, $this->origin);
+				$t = array('i','i', 's');
 				$res = db_prep_query($sql,$v,$t);
 				if(!$res){
 					db_rollback();
@@ -228,32 +334,99 @@
 	}
 	
 	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, $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,
-			$this->type,
-			$this->fileIdentifier[0],
-			$this->title[0],
-			$this->createDate,
-			$this->changeDate,
-			$this->abstract[0],
-			$this->keywords[0],
-			$this->hierachyLevel,
-			$this->tmpExtentBegin,
-			$this->tmpExtentEnd,
-			't',
-			$this->datasetId,
-			$this->datasetIdCodeSpace,
-			$this->randomId,
-			$this->origin
-		);
-		$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);
+		//check if metadata record already exists, if not create a new one, else insert relation only!
+		$metadataId = $this->isFileIdentifierAlreadyInDB();
+
+		if ($metadataId != false) {
+			//update the metadata - new random id set therefor there is no problem when setting the relation afterwards
+			$e = new mb_exception("existing metadata fileIdentifier found at metadata with id: ".$metadataId[0]." - update will be performed");
+			//problem: <<<SQL have a limited number of chars!
+			$sql = "UPDATE mb_metadata SET link = $1, origin = $18, md_format = $2, data = $3, ";
+			$sql .= "linktype = $4, uuid = $5, title = $6, createdate = $7, changedate = $8, lastchanged = now(), ";
+			$sql .= "abstract = $9, searchtext = $10, type = $11, tmp_reference_1 = $12, tmp_reference_2 = $13, export2csw = $14, datasetid = $15, ";
+			$sql .= "datasetid_codespace = $16, randomid = $17, harvestresult = $20, harvestexception = $21, lineage = $22, inspire_top_consistence = $23, ";
+			$sql .= "spatial_res_type = $24, spatial_res_value = $25, update_frequency = $26, format = $27, inspire_charset = $28 WHERE metadata_id = $19";
+
+			$v = array(
+				$this->href,
+				$this->format,
+				$this->metadata,
+				$this->type,
+				$this->fileIdentifier,
+				$this->title,
+				$this->createDate,
+				$this->changeDate,
+				$this->abstract,
+				$this->keywords[0],
+				$this->hierachyLevel,
+				$this->tmpExtentBegin,
+				$this->tmpExtentEnd,
+				$this->export2Csw,
+				$this->datasetId,
+				$this->datasetIdCodeSpace,
+				$this->randomId,
+				$this->origin,
+				//$this->owner, //owner is the old one - maybe here we have something TODO!
+				$metadataId[0], //The first metadataId which was found will be selected!
+				$this->harvestResult,
+				$this->harvestException,
+				$this->lineage,
+				$this->inspireTopConsistence,
+				$this->spatialResType,
+				$this->spatialResValue,
+				$this->updateFrequency,
+				$this->dataFormat,
+				$this->inspireCharset
+			);
+			$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','b','s','s','s','s','i','i','s','s','b','s','s','s','s','s');
+			$res = db_prep_query($sql,$v,$t);
+
+		} else {
+			//insert new record
+			$e = new mb_exception("existing metadata fileIdentifier found at metadata with id: ".$metadataId[0]);
+
+			$sql = <<<SQL
+INSERT INTO mb_metadata (lastchanged, link, origin, md_format, data, linktype, uuid, title, createdate, changedate, abstract, searchtext, type, tmp_reference_1, tmp_reference_2, export2csw, datasetid, datasetid_codespace, randomid, fkey_mb_user_id, harvestresult, harvestexception, lineage, inspire_top_consistence, spatial_res_type, spatial_res_value, update_frequency, format, inspire_charset)  VALUES(now(), $1, $18, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28)
+SQL;
+			$v = array(
+				$this->href,
+				$this->format,
+				$this->metadata,
+				$this->type,
+				$this->fileIdentifier,
+				$this->title,
+				$this->createDate,
+				$this->changeDate,
+				$this->abstract,
+				$this->keywords[0],
+				$this->hierachyLevel,
+				$this->tmpExtentBegin,
+				$this->tmpExtentEnd,
+				$this->export2Csw,
+				$this->datasetId,
+				$this->datasetIdCodeSpace,
+				$this->randomId,
+				$this->origin,
+				$this->owner,				
+				$this->harvestResult,
+				$this->harvestException,
+				$this->lineage,
+				$this->inspireTopConsistence,
+				$this->spatialResType,
+				$this->spatialResValue,
+				$this->updateFrequency,
+				$this->dataFormat,
+				$this->inspireCharset
+				
+			);
+			$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','b','s','s','s','s','i','i','s','s','b','s','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!"));
+			$e = new mb_exception(_mb("Cannot insert or update metadata record into mapbenders mb_metadata table!"));
 			return false;
 		} else {
 			//insert relation into db
@@ -280,13 +453,13 @@
 				$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);";
+					$sql = "INSERT INTO ows_relation_metadata (fkey_layer_id, fkey_metadata_id, relation_type) values ($1, $2, $3);";
 				}
 				if ($resourceType == 'featuretype') {
-					$sql = "INSERT INTO ows_relation_metadata (fkey_featuretype_id, fkey_metadata_id) values ($1, $2);";
+					$sql = "INSERT INTO ows_relation_metadata (fkey_featuretype_id, fkey_metadata_id, relation_type) values ($1, $2, $3);";
 				}
-				$v = array($resourceId, $metadataId);
-				$t = array('i','i');
+				$v = array($resourceId, $metadataId, $this->origin);
+				$t = array('i','i','s');
 				$res = db_prep_query($sql,$v,$t);
 				if(!$res){
 					db_rollback();

Modified: trunk/mapbender/http/classes/class_wfsToDb.php
===================================================================
--- trunk/mapbender/http/classes/class_wfsToDb.php	2012-06-07 09:03:25 UTC (rev 8373)
+++ trunk/mapbender/http/classes/class_wfsToDb.php	2012-06-11 20:56:49 UTC (rev 8374)
@@ -22,6 +22,7 @@
 require_once(dirname(__FILE__)."/class_administration.php");
 require_once(dirname(__FILE__)."/class_wfs.php");
 require_once dirname(__FILE__) . "/class_Uuid.php";
+require_once dirname(__FILE__) . "/class_iso19139.php";
 //require_once dirname(__FILE__) . "//class_Uuid.php";
 
 class WfsToDb {
@@ -267,7 +268,7 @@
 	public static function delete ($aWfs) {
 		//first delete coupled metadata, cause there is no contraints in the database to do so
 		$e = new mb_exception("Deleting MetadataURLs for wfs with id :".$aWfs->id);
-		WfsToDb::deleteFeatureTypeMetadataUrls($aWfs->id);
+		//WfsToDb::deleteFeatureTypeMetadataUrls($aWfs->id); //Not needed any more, cause the relations are deleted thru class_iso19139.php
 		//then delete wfs itself
 		$sql = "DELETE FROM wfs WHERE wfs_id = $1";
 		$v = array($aWfs->id);
@@ -366,94 +367,70 @@
 	 * @param $metadataUrl object
 	 */
 	private static function insertFeatureTypeMetadataUrl ($aWfsFeatureTypeId, $metadataUrl, $withParsing = false) {
-		$randomid = new Uuid();
-		if ($withParsing == true) {
-			//check if format can be parsed - possible only for one format (INSPIRE)
-			if (($metadataUrl->type == 'ISO19115:2003' && $metadataUrl->format =='text/xml') || ($metadataUrl->type == 'TC211' && $metadataUrl->format =='text/xml')) {
-				//extract all relevant information - maybe mapbender metadata object! TODO!!!
-				$metaData = "";
-				$uuid = "";
-				$title = "";
-				$createdate = "1900-01-01";
-				$changedate = "1900-01-01";
-				$abstract = "";
-				$keywords = "";
-				$type = "";
-				$tmp_reference_1 = "1900-01-01";
-				$tmp_reference_2 = "1900-01-01";
-				$export2csw = 't';
-				$datasetid = "";
-				//harvest metadata thru web
-				//harvest = 1;
-			}
+		//function as defined in class wms!
+		//origin 2 - set by mapbender metadata editor - new record
+		//origin 3 - set by mapbender metadata editor - new linkage
+		//harvest the record if some readable format is given - should this be adoptable?
+		//parse the content if iso19139 is given
+		//TODO: generate temporal uuid for inserting and getting the serial afterwards
+		//delete old relations for this resource - only those which are from 'capabilities'
+		$mbMetadata_1 = new Iso19139();
+		$mbMetadata_1->deleteMetadataRelation("featuretype", $aWfsFeatureTypeId,"capabilities");
+		//check for returned format?
+		if (($metadataUrl->type == 'ISO19115:2003' && $metadataUrl->format =='text/xml') || ($metadataUrl->type == 'TC211' && $metadataUrl->format =='text/xml')) {
+			//load metadata from link
+			$mbMetadata = new Iso19139();
+			$randomid = new Uuid();
+			$mbMetadata = $mbMetadata->createFromUrl($metadataUrl->href);
+			$mdOwner = Mapbender::session()->get("mb_user_id");
+			$e = new mb_exception("owner: ".$mdOwner);
+				
+			if ($mbMetadata != false) { //metadata has been parsed
+				//set randomId for metadata
+				$mbMetadata->randomId = $randomid;
+				$mbMetadata->href = $metadataUrl->href;
+				$mbMetadata->format = $metadataUrl->format;
+				$mbMetadata->type = $metadataUrl->type;
+				$mbMetadata->origin = "capabilities";
+				$mbMetadata->owner = $mdOwner;
+				//push metadata object into database
+				if (!$mbMetadata->insertToDB("featuretype",$aWfsFeatureTypeId)) {
+					$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 had problems - fill only the urls into database and give a reasonable error message	
+				$mbMetadata = new Iso19139();
+				$mbMetadata->randomId = $randomid;
+				$mbMetadata->href = $metadataUrl->href;
+				$mbMetadata->format = $metadataUrl->format;
+				$mbMetadata->type = $metadataUrl->type;
+				$mbMetadata->origin = "capabilities";
+				$mbMetadata->owner = $mdOwner;
+				if (!$mbMetadata->insertMetadataUrlToDB("featuretype",$aWfsFeatureTypeId)) {
+					$e = new mb_exception("Problem while storing MetadataURL entry from wfs capabilities to mb_metadata table!");
+				} else {
+					$e = new mb_exception("Stored MetadataURL ".$mbMetadata->href." from wfs capabilities to mb database cause the target could not be accessed or parsed!");
+				}
+			}	
 		} else {
-			$metaData = "";
-			$uuid = "";
-			$title = "";
-			$createdate = "1900-01-01";
-			$changedate = "1900-01-01";
-			$abstract = "";
-			$keywords = "";
-			$type = "";
-			$tmp_reference_1 = "1900-01-01";
-			$tmp_reference_2 = "1900-01-01";
-			$export2csw = 't';
-			$datasetid = "";		
+			//insert link into database but do not try to parse the metadata
+			$mbMetadata = new Iso19139();
+			$mbMetadata->randomId = $randomid;
+			$mbMetadata->href = $metadataUrl->href;
+			$mbMetadata->format = $metadataUrl->format;
+			$mbMetadata->type = $metadataUrl->type;
+			$mbMetadata->origin = "capabilities";
+			$mbMetadata->owner = $mdOwner;
+			$mbMetadata->harvestException = "Metadata format is not known to mapbender!";
+			$mbMetadata->harvestResult = 0;
+			if (!$mbMetadata->insertMetadataUrlToDB("featuretype",$aWfsFeatureTypeId)) {
+				$e = new mb_exception("Problem while storing MetadataURL entry from wfs capabilities to mb_metadata table!");
+			} else {
+				$e = new mb_exception("Stored MetadataURL ".$mbMetadata->href." from wfs capabilities to mb database cause the target could not be accessed or parsed!");
+			}		
 		}
-		//insert result into database
-		$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, randomid) ";
-		$sql .= "VALUES($1, 'capabilities', $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)";
-		$v = array($metadataUrl->href,
-			$metadataUrl->format,
-			$metaData,
-			$metadataUrl->type,
-			$uuid,
-			$title,
-			$createdate,
-			$changedate,
-			$abstract,
-			$keywords,
-			$type,
-			$tmp_reference_1,
-			$tmp_reference_2,
-			$export2csw,
-			$datasetid,
-			$randomid
-		);
-		$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','b','s','s');
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();	
-		}
-		//get metadata id with uuid which was generated
-		$sql = <<<SQL
-
-SELECT metadata_id FROM mb_metadata WHERE randomid = $1 ORDER BY lastchanged DESC LIMIT 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){
-			abort(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
-		}
-		if ($res) {
-			$row = db_fetch_assoc($res);
-			$mdId = $row['metadata_id'];
-		}
-		//insert into ows_relation_metadata (fkey_layer_id, fkey_metadata_id) values (20131,3);
-		$sql = "INSERT INTO ows_relation_metadata (fkey_featuretype_id, fkey_metadata_id) values ($1, $2);";
-		$v = array($aWfsFeatureTypeId, $mdId);
-		$t = array('i','i');
-		$res = db_prep_query($sql,$v,$t);
-		if(!$res){
-			db_rollback();
-			$e = new mb_exception("MetadataURL of featuretype with id: ".$aWfsFeatureTypeId." cannot be stored to database!");
-		}
-		$e = new mb_exception("INSERTING FT MetadataURL (FT: ".$aWfsFeatureTypeId.", MetadataURL: ".$metadataUrl->href);
 		return true;
 	}
 
@@ -797,15 +774,15 @@
 				}
 			}
 			//update MetadataURLs
-			//delete old MetadataURLs from caps
-			$sql = <<<SQL
+			//delete old MetadataURLs from caps - TODO delete this, cause the new way is only to delete the relations and to hold the metadata entries
+			/*$sql = <<<SQL
 
 			DELETE FROM mb_metadata WHERE metadata_id IN (SELECT metadata_id FROM mb_metadata INNER JOIN (SELECT * from ows_relation_metadata WHERE fkey_featuretype_id = $1) as relation ON  mb_metadata.metadata_id = relation.fkey_metadata_id AND mb_metadata.origin = 'capabilities')
 
 SQL;
 			$v = array($aWfsFeatureType->id);
 			$t = array('i');
-			$res = db_prep_query($sql,$v,$t);
+			$res = db_prep_query($sql,$v,$t);*/
 			// insert feature type MetadataURL from caps
 			for ($i = 0; $i < count($aWfsFeatureType->metadataUrlArray); $i++) {
 				$metadataUrl = $aWfsFeatureType->metadataUrlArray[$i];
@@ -891,7 +868,7 @@
 	}
 	
 	/**
-	 * Deletes an implicitly coupled metadata of a wfs from the database.
+	 * Deprecated: Deletes an implicitly coupled metadata of a wfs from the database.
 	 * 
 	 * 
 	 * @return Boolean

Modified: trunk/mapbender/http/classes/class_wms.php
===================================================================
--- trunk/mapbender/http/classes/class_wms.php	2012-06-07 09:03:25 UTC (rev 8373)
+++ trunk/mapbender/http/classes/class_wms.php	2012-06-11 20:56:49 UTC (rev 8374)
@@ -12,6 +12,7 @@
 require_once dirname(__FILE__) . "/class_administration.php";
 require_once dirname(__FILE__) . "/class_georss_factory.php";
 require_once dirname(__FILE__) . "/class_mb_exception.php";
+$e = new mb_exception("test");
 require_once dirname(__FILE__) . "/class_iso19139.php";
 require_once dirname(__FILE__) . "/../classes/class_universal_wms_factory.php";
 
@@ -2357,19 +2358,16 @@
 	}
 
 	function insertLayerMetadataUrls($i){
-		//first delete the old ones - but only those who have been harvested thru caps before!
 		global $con;
-		$sql = "DELETE FROM mb_metadata WHERE metadata_id IN (SELECT metadata_id FROM mb_metadata INNER JOIN";
-		$sql .= " (SELECT * from ows_relation_metadata WHERE fkey_layer_id = $1) as relation ON ";
-		$sql .= " mb_metadata.metadata_id = relation.fkey_metadata_id AND mb_metadata.origin = 'capabilities')";
-		$v = array($this->objLayer[$i]->db_id);
-		$t = array('i');
-		$res = db_prep_query($sql,$v,$t);
 		//origin 2 - set by mapbender metadata editor - new record
 		//origin 3 - set by mapbender metadata editor - new linkage
 		//harvest the record if some readable format is given - should this be adoptable?
 		//parse the content if iso19139 is given
 		//TODO: generate temporal uuid for inserting and getting the serial afterwards
+		//delete old relations for this resource - only those which are from 'capabilities'
+		$mbMetadata_1 = new Iso19139();
+		$mbMetadata_1->deleteMetadataRelation("layer", $this->objLayer[$i]->db_id,"capabilities");
+		//delete object?
 		for($j=0; $j<count($this->objLayer[$i]->layer_metadataurl);$j++){
 			//check for returned format?
 			if (($this->objLayer[$i]->layer_metadataurl[$j]->type == 'ISO19115:2003' && $this->objLayer[$i]->layer_metadataurl[$j]->format =='text/xml') || ($this->objLayer[$i]->layer_metadataurl[$j]->type == 'TC211' && $this->objLayer[$i]->layer_metadataurl[$j]->format =='text/xml')) {
@@ -2377,13 +2375,19 @@
 				$mbMetadata = new Iso19139();
 				$randomid = new Uuid();
 				$mbMetadata = $mbMetadata->createFromUrl($this->objLayer[$i]->layer_metadataurl[$j]->href);
+				$mdOwner = Mapbender::session()->get("mb_user_id");
+				$e = new mb_exception("owner: ".$mdOwner);
+				
 				if ($mbMetadata != false) { //metadata has been parsed
-					//create randomId for metadata
+					//set 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";
+					$mbMetadata->owner = $mdOwner;
+
+
 					//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!");
@@ -2398,12 +2402,30 @@
 					$mbMetadata->format = $this->objLayer[$i]->layer_metadataurl[$j]->format;
 					$mbMetadata->type = $this->objLayer[$i]->layer_metadataurl[$j]->type;
 					$mbMetadata->origin = "capabilities";
+					$mbMetadata->owner = $mdOwner;
 					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 {
 						$e = new mb_notice("Stored MetadataURL ".$mbMetadata->href." from wms capabilities to mb database cause the target could not be accessed or parsed!");
 					}
+				}	
+			} else {
+				//insert link into database but do not try to parse the metadata
+				$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";
+				$mbMetadata->owner = $mdOwner;
+				$mbMetadata->harvestException = "Metadata format is not known to mapbender!";
+				$mbMetadata->harvestResult = 0;
+				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 {
+					$e = new mb_notice("Stored MetadataURL ".$mbMetadata->href." from wms capabilities to mb database cause the target could not be accessed or parsed!");
 				}
+				
 			}
 		}
 	}

Modified: trunk/mapbender/http/php/mod_deleteWMS.php
===================================================================
--- trunk/mapbender/http/php/mod_deleteWMS.php	2012-06-07 09:03:25 UTC (rev 8373)
+++ trunk/mapbender/http/php/mod_deleteWMS.php	2012-06-11 20:56:49 UTC (rev 8374)
@@ -204,7 +204,7 @@
 	//Before the wms will be deleted, the metadataUrls and dataUrls from the Layers must be deleted!
 	//The other things will be done by class_wms!
 	//***
-	$sql = "DELETE FROM mb_metadata WHERE metadata_id IN (SELECT metadata_id FROM mb_metadata INNER JOIN";
+	/*$sql = "DELETE FROM mb_metadata WHERE metadata_id IN (SELECT metadata_id FROM mb_metadata INNER JOIN";
 	$sql .= " (SELECT * from ows_relation_metadata WHERE fkey_layer_id IN ";
 	$sql .= " (SELECT layer_id FROM layer WHERE fkey_wms_id = $1) )";
  	$sql .= " as relation ON ";
@@ -212,7 +212,7 @@
 	
 	$v = array($wmsList);
 	$t = array('i');
-	$res = db_prep_query($sql,$v,$t);
+	$res = db_prep_query($sql,$v,$t);*/
 	//***
 	$sql = "DELETE FROM datalink WHERE datalink_id IN (SELECT datalink_id FROM datalink INNER JOIN";
 	$sql .= " (SELECT * from ows_relation_data WHERE fkey_layer_id IN ";

Modified: trunk/mapbender/http/plugins/mb_metadata_server.php
===================================================================
--- trunk/mapbender/http/plugins/mb_metadata_server.php	2012-06-07 09:03:25 UTC (rev 8373)
+++ trunk/mapbender/http/plugins/mb_metadata_server.php	2012-06-11 20:56:49 UTC (rev 8374)
@@ -717,7 +717,7 @@
 		break;
 
 	case "getOwnedMetadata" :
-		$featuretypeId = $ajaxResponse->getParameter("layerId");
+		$layerId = $ajaxResponse->getParameter("layerId");
 		
 		$user = new User(Mapbender::session()->get("mb_user_id"));
 		//$e = new mb_exception("plugins/mb_metadata_wfs_server.php: user_id: ".$user->id);
@@ -741,7 +741,7 @@
 		$ajaxResponse->setResult($resultObj);
 		$ajaxResponse->setSuccess(true);
 		break;
-
+//******************
 	case "insertLayerMetadataAddon" :
 		$layerId = $ajaxResponse->getParameter("layerId");
 		$data = $ajaxResponse->getParameter("data");
@@ -756,7 +756,7 @@
 
 			$sql = <<<SQL
 
-INSERT INTO ows_relation_metadata (fkey_metadata_id, fkey_layer_id, internal) VALUES ( $1, $2, $3)
+INSERT INTO ows_relation_metadata (fkey_metadata_id, fkey_layer_id, internal, relation_type) VALUES ( $1, $2, $3, 'internal')
 
 SQL;
 			$v = array($data->internal_relation,$layerId, 1);
@@ -790,252 +790,102 @@
 		//generate a uuid for the record:
 		$uuid = new Uuid();
 		//initialize database objects
-		$link = '';
-		$title = '';
-		$abstract = '';
-		$format = '';	
-		$ref_system = '';
-		$export2csw = 'f';
-		$inspire_top_consistence = 'f';
-		$tmp_reference_1 = '';
-		$tmp_reference_2 = '';
-		$lineage = '';
-		$spatial_res_type = '';
-		$spatial_res_value = '';
-		$inspire_charset = '';
-		$update_frequency = '';
+		//are initialized from class_iso19139
+		$mbMetadata = new Iso19139();
+		$randomid = new Uuid();
 		//read out json objects 
 		if (isset($data->link)) {
-			$link = $data->link;
+			$mbMetadata->href = $data->link;
 		}
 		if (isset($data->export2csw)) {
-			$export2csw = $data->export2csw;
+			$mbMetadata->export2Csw = $data->export2csw;
 		} else {
-			$export2csw = 'f';
+			$mbMetadata->export2Csw = 'f';
 		}
 		if (isset($data->title)) {
-			$title = $data->title;
+			$mbMetadata->title = $data->title;
 		}
 		if (isset($data->abstract)) {
-			$abstract = $data->abstract;
+			$mbMetadata->abstract = $data->abstract;
 		}
 		if (isset($data->format)) {
-			$format = $data->format;
+			$mbMetadata->dataFormat = $data->format;
 		}
 		if (isset($data->ref_system)) {
-			$ref_system = $data->ref_system;
+			$mbMetadata->refSystem = $data->ref_system;
 		}
 		if (isset($data->inspire_top_consistence)) {
-			$inspire_top_consistence = $data->inspire_top_consistence;
+			$mbMetadata->inspireTopConsistence = $data->inspire_top_consistence;
 		}
 		if (isset($data->tmp_reference_1)) {
-			$tmp_reference_1 = $data->tmp_reference_1;
+			$mbMetadata->tmpExtentBegin = $data->tmp_reference_1;
 		}
-		if ($tmp_reference_1 == "") {
-			$tmp_reference_1 = "2000-01-01";
-		}
 		if (isset($data->tmp_reference_2)) {
-			$tmp_reference_2 = $data->tmp_reference_2;
+			$mbMetadata->tmpExtentEnd = $data->tmp_reference_2;
 		}
-		if ($tmp_reference_2 == "") {
-			$tmp_reference_2 = "2000-01-01";
-		}
 		if (isset($data->lineage)) {
-			$lineage = $data->lineage;
+			$mbMetadata->lineage = $data->lineage;
 		}
 		if (isset($data->spatial_res_type)) {
-			$spatial_res_type = $data->spatial_res_type;
+			$mbMetadata->spatialResType = $data->spatial_res_type;
 		}
 		if (isset($data->spatial_res_value)) {
-			$spatial_res_value = $data->spatial_res_value;
+			$mbMetadata->spatialResValue = $data->spatial_res_value;
 		}
 		if (isset($data->inspire_charset)) {
-			$inspire_charset = $data->inspire_charset;
+			$mbMetadata->inspireCharset = $data->inspire_charset;
 		}
 		if (isset($data->update_frequency)) {
-			$update_frequency = $data->update_frequency;
+			$mbMetadata->updateFrequency = $data->update_frequency;
 		}
-		$randomid = new Uuid();
+
 		//Check if origin is external and export2csw is activated!
 		if ($origin == 'external' ) {
 			//harvest link from location, parse the content for datasetid and push xml into data column
-			//load metadata from link TODO: function from class_wms - generate a class for metadata management and include it here and in class_wms
-			$metadataConnector = new connector();
-			$metadataConnector->set("timeOut", "5");
-			$metaData = $metadataConnector->load($link);
-			//$e = new mb_exception($metaData);
-			if (!$metaData) {
-    				abort(_mb("Could not load metadata from source url!"));
-			}
-			//delete getRecordByIdResponse from xml if there
-			$regex = "#<csw:GetRecordByIdResponse .*?>#";
-			$output = preg_replace($regex,"",$metaData);
-			$regex = "#</csw:GetRecordByIdResponse>#";
-			$output = preg_replace($regex,"",$output);
-			//$e = new mb_exception($output);
-			$metaData = $output;
-			//***
-			//write metadata to temporary file:
-			$randomFileId = new Uuid();
-				
-			$tmpMetadataFile = fopen(TMPDIR.'/link_metadata_file_'.$randomFileId.'.xml', 'w');
-			fwrite($tmpMetadataFile, $metaData);
-			fclose($tmpMetadataFile);
-			$e = new mb_exception("File which has been written: link_metadata_file_".$randomFileId.".xml");
-			//read out objects from xml structure
-			if (file_exists(TMPDIR.'/link_metadata_file_'.$randomFileId.'.xml')) {
-				$iso19139Xml=simplexml_load_file(TMPDIR.'/link_metadata_file_'.$randomFileId.'.xml');
-				//$metaData = file_get_contents(TMPDIR.'/link_metadata_file_'.$randomFileId.'.xml');
+			$mbMetadata = $mbMetadata->createFromUrl($mbMetadata->href);
+			$mdOwner = Mapbender::session()->get("mb_user_id");
+			$e = new mb_exception("owner: ".$mdOwner);	
+			if ($mbMetadata != false) { //metadata has been parsed
+				//set randomId for metadata
+				$mbMetadata->randomId = $randomid;
+				$mbMetadata->format = "text/xml";
+				$mbMetadata->type = "ISO19115:2003";
+				$mbMetadata->origin = "external";
+				$mbMetadata->owner = $mdOwner;
+				//push metadata object into database
+				if (!$mbMetadata->insertToDB("layer",$layerId)) {
+					$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 {
-				abort(_mb("Temporary file could not be parsed!"));
-			}
-			//$metaData = $metadataConnector->file;
-			//parse metadata
-			/*try {
-				$iso19139Xml =  new SimpleXMLElement($metaData);
-			}
-			catch (Exception $e) {
-				abort(_mb("Parsing ISO19139 XML failed!"));
-			}*/
-			if ($iso19139Xml != false) {
-				//get elements for database from xml by using xpath
-				//uuid
-				$uuid = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:fileIdentifier/gco:CharacterString');
-				$e = new mb_exception("plugins/mb_metadata_server.php: File Identifier found: ".$uuid);
-				//createdate
-				$createdate = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:dateStamp/gco:Date');
-				//changedate
-				$changedate = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:dateStamp/gco:Date');
-				//TODO: check if this is set, maybe DateTime must be searched instead?
-				//title
-				$title = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString');
-				//datasetid
-				//next came from class_wms - TODO maybe put it into a special function
-				//dataset identifier - howto model into md_metadata?
-				//check where datasetid is defined - maybe as RS_Identifier or as MD_Identifier see http://inspire.jrc.ec.europa.eu/documents/Metadata/INSPIRE_MD_IR_and_ISO_v1_2_20100616.pdf page 18
-				//First check if MD_Identifier is set, then check if RS_Identifier is used!
-				//Initialize datasetid
-				$datasetid = 'undefined';
-				$code = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString');
-				if (isset($code[0]) && $code[0] != '') {
-					$e = new mb_exception("plugins/mb_metadata_server.php: code given thru MD_Identifier: ".$code[0]);
-					//check if code is defined by codespace and code
-					$codeSplit = explode("#",$code[0]);
-					if (isset($codeSplit[0]) && $codeSplit[0] != '' && isset($codeSplit[1]) && $codeSplit[1] != '') {
-						$e = new mb_exception("plugins/mb_metadata_server.php: code was constructed via codespace#code !");	
-						$datasetid = $code[0];
-						$datasetid_codespace = "";
-					} else {
-						$e = new mb_exception("plugins/mb_metadata_server.php: code was not constructed via codespace#code !");	
-						$datasetid = $code[0];
-						$datasetid_codespace = "";
-					}
-				} else { //try to read code from RS_Identifier 		
-					$code = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:RS_Identifier/gmd:code/gco:CharacterString');
-					$codeSpace = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:RS_Identifier/gmd:codeSpace/gco:CharacterString');
-				#$e = new mb_exception("plugins/mb_metadata_server.php: code: ".$code[0]);
-				#$e = new mb_exception("plugins/mb_metadata_server.php: codeSpace: ".$codeSpace[0]);
-					if (isset($codeSpace[0]) && isset($code[0]) && $codeSpace[0] != '' && $code[0] != '') {
-						$datasetid = $code[0];
-						$datasetid_codespace = $codeSpace[0];
-						$e = new mb_exception("plugins/mb_metadata_server.php: datasetid: ".$datasetid);
-					} else {
-						//neither MD_Identifier nor RS_Identifier are defined in a right way
-						$e = new mb_exception("plugins/mb_metadata_server.php: the service data coupling has problems, cause the metadata doesnt have defined a datasetid");
-					}
+				//parsing had problems - fill only the urls into database and give a reasonable error message	
+				$mbMetadata = new Iso19139();
+				$mbMetadata->randomId = $randomid;
+				$mbMetadata->format = "text/xml";
+				$mbMetadata->type = "ISO19115:2003";
+				$mbMetadata->origin = "external";
+				$mbMetadata->owner = $mdOwner;
+				if (!$mbMetadata->insertMetadataUrlToDB("layer",$layerId)) {
+					$e = new mb_exception("Problem while storing MetadataURL entry from wms capabilities to mb_metadata table!");
+				} else {
+					$e = new mb_exception("Stored MetadataURL ".$mbMetadata->href." from wms capabilities to mb database cause the target could not be accessed or parsed!");
 				}
-				//abstract
-				$abstract = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:abstract/gco:CharacterString');
-				//searchtext -- use keywords!
-				$keywords = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/gco:CharacterString');
-				//type 
-				$type = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:hierarchyLevel/gmd:MD_ScopeCode');
-				//tmp_reference_1
-				$tmp_reference_1 = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition');
-				//tmp_reference_2
-				$tmp_reference_2 = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition');		//spatial_res_type
-				//spatial_res_value
-				//ref_system
-				//format
-				//inspire_charset
-				//inspire_top_consistence
-				//responsible_party
-				//fees
-				//"constraints"	
-				//fill database with elements:
-
-				$sql = <<<SQL
-
-INSERT INTO mb_metadata (link, uuid, origin, title, abstract, format, ref_system, export2csw, inspire_top_consistence, tmp_reference_1, tmp_reference_2, lineage, spatial_res_type, spatial_res_value, inspire_charset, createdate, datasetid, randomid, data, harvestresult, datasetid_codespace) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, now(), $16, $17, $18, 1, $19)
-
-SQL;
-				$v = array($link, $uuid[0], $origin, $title[0], $abstract[0], $format, $ref_system, $export2csw,$inspire_top_consistence,$tmp_reference_1[0],$tmp_reference_2[0],$lineage,$spatial_res_type,$spatial_res_value,$inspire_charset, $datasetid, $randomid, $metaData, $datasetid_codespace);
-				$t = array('s','s','s','s','s','s','s','b','b','s','s','s','s','s','s','s','s','s','s');
-		
-				try {
-					$res = db_prep_query($sql,$v,$t);
-				}
-				catch (Exception $e){
-					abort(_mb("Insert of harvested metadata into database failed!"));
-				}
+			}	
 			
+		} else { //fill thru metador
+			$mbMetadata->origin = "metador";
+			$mbMetadata->fileIdentifier = $uuid;
+			$mbMetadata->randomId = $randomid;
+			if (!$mbMetadata->insertToDB("layer",$layerId)) {
+				$e = new mb_exception("Problem while storing metadata to mb_metadata table!");
+				abort("Problem while storing metadata to mb_metadata table!");
 			} else {
-				abort(_mb("Problem with parsing the XML structure with SimpleXML! Record was not inserted into database! Ask your administrator."));
-				//give back error message - cause parsing has problems
+				$e = new mb_notice("Metadata with id ".$randomid." stored to db!");
+				$ajaxResponse->setMessage("Metadata object inserted into md_metadata!");
+				$ajaxResponse->setSuccess(true);
 			}
-		} else { //fill only links or the edited information into db 
-			$sql = <<<SQL
-
-INSERT INTO mb_metadata (link, uuid, origin, title, abstract, format, ref_system, export2csw, inspire_top_consistence, tmp_reference_1, tmp_reference_2, lineage, spatial_res_type, spatial_res_value, inspire_charset, createdate, randomid, update_frequency) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, now(), $16, $17)
-
-SQL;
-			$v = array($link, $uuid, $origin, $title, $abstract, $format, $ref_system, $export2csw,$inspire_top_consistence,$tmp_reference_1,$tmp_reference_2,$lineage,$spatial_res_type,$spatial_res_value,$inspire_charset, $randomid, $update_frequency);
-			$t = array('s','s','s','s','s','s','s','b','b','s','s','s','s','s','s','s','s');
-		
-			try {
-				$res = db_prep_query($sql,$v,$t);
-			}
-			catch (Exception $e){
-				abort(_mb("Insert of edited metadata into database failed!"));
-			}
-		}
-		//set relation into relation table
-		//get metadata_id of record which have been inserted before
-		$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){
-			abort(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
-		}
-		if ($res) {
-			$row = db_fetch_assoc($res);
-			$metadata_id = $row['metadata_id'];
-		}
-		$sql = <<<SQL
-
-		INSERT INTO ows_relation_metadata (fkey_metadata_id, fkey_layer_id) VALUES ($1, $2)
-
-SQL;
-		$v = array($metadata_id, $layerId);
-		$t = array('i','i');
-		try {
-			$res = db_prep_query($sql,$v,$t);
-		}
-		catch (Exception $e){
-				abort(_mb("Cannot write relation between ows and metadata to database!"));
-		}
-		if ($dbInsertFailed != true) {	
-			$ajaxResponse->setMessage("Metadata object inserted into md_metadata!");
-			$ajaxResponse->setSuccess(true);
-		}				
+		}		
 		break;
 
 	case "deleteInternalMetadataLinkage" :
@@ -1084,182 +934,46 @@
 		$ajaxResponse->setMessage("Metadata deleted!");
 		$ajaxResponse->setSuccess(true);
 		break;
+//***********************
 	case "importLayerXmlAddon" :
-//this is similar to insert the metadata from external link, but came from internal file from tmp folder which has been updated before
+	//this case is similar to insert the metadata from external link, but came from internal file from tmp folder which has been uploaded before
 		$layerId = $ajaxResponse->getParameter("layerId");
 		$filename = $ajaxResponse->getParameter("filename");
 		//normaly the link is only set if a link has been created
 		//if a record has been created, the link element will be empty 
 		//use this to distinguish between the to different inserts!
 		//this insert should also push one entry in the ows_relation_metadata table! - after the insert into md_metadata
-		//origin
-		//generate metador entry
-		$origin = 'upload';
-		//generate a uuid for the record:
 		$uuid = new Uuid();
-		//initialize database objects
-		$link = '';
-		$title = '';
-		$abstract = '';
-		$format = '';	
-		$ref_system = '';
-		$export2csw = 't';
-		$inspire_top_consistence = 'f';
-		$tmp_reference_1 = '';
-		$tmp_reference_2 = '';
-		$lineage = '';
-		$spatial_res_type = '';
-		$spatial_res_value = '';
-		$inspire_charset = '';
 		$randomid = new Uuid();	
-		$e = new mb_exception("File to load: ".$filename);
-		//read out objects from xml structure
-		/*if (file_exists($filename)) {
-			try {	
-				
-				$iso19139Xml = simplexml_load_file($filename);
-			}
-			catch (Exception $e) {
-				abort(_mb("Loading ISO19139 XML failed!"));
-			}
-		}
-		else {
-			abort(_mb("File not found: ".$filename." !"));
-		}*/
+		$e = new mb_notice("File to load: ".$filename);
 		$metaData = file_get_contents($filename);
 		if (!$metaData){
 			abort(_mb("Reading file ".$filename." failed!"));
 		}
-		//delete getRecordByIdResponse from xml if there
-		$regex = "#<csw:GetRecordByIdResponse .*?>#";
-		$output = preg_replace($regex,"",$metaData);
-		$regex = "#</csw:GetRecordByIdResponse>#";
-		$output = preg_replace($regex,"",$output);
-		//$e = new mb_exception($output);
-		$iso19139Xml = simplexml_load_string($output);
-		$e = new mb_exception('');
-		
-		//get elements for database from xml by using xpath
-		//uuid
-		$uuid = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:fileIdentifier/gco:CharacterString');
-		if (!isset($uuid) || $uuid == "") {
-			abort(_mb(" No fileIdentifier found, parsing ISO19139 XML maybe failed. Check if element /gmd:MD_Metadata/gmd:fileIdentifier/gco:CharacterString is given and not empty!"));
-		}
-		//$e = new mb_exception("plugins/mb_metadata_server.php: File Identifier found: ".$uuid);
-		//createdate
-		$createdate = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:dateStamp/gco:Date');
-		//changedate
-		$changedate = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:dateStamp/gco:Date');
-		//TODO: check if this is set, maybe DateTime must be searched instead?
-		//title
-		$title = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString');
-		//next came from class_wms - TODO maybe put it into a special function
-		//dataset identifier - howto model into md_metadata?
-		//check where datasetid is defined - maybe as RS_Identifier or as MD_Identifier see http://inspire.jrc.ec.europa.eu/documents/Metadata/INSPIRE_MD_IR_and_ISO_v1_2_20100616.pdf page 18
-		//First check if MD_Identifier is set, then check if RS_Identifier is used!
-		//Initialize datasetid
-		$datasetid = 'undefined';
-		$code = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString');
-		if (isset($code[0]) && $code[0] != '') {
-			$e = new mb_exception("plugins/mb_metadata_server.php: code given thru MD_Identifier: ".$code[0]);
-			//check if code is defined by codespace and code
-			$codeSplit = explode("#",$code);
-			if (isset($codeSplit[0]) && $codeSplit[0] != '' && isset($codeSplit[1]) && $codeSplit[1] != '') {
-				$e = new mb_exception("plugins/mb_metadata_server.php: code was constructed via codespace#code !");	
-				$datasetid = $codeSplit[0]."#".$codeSplit[1];
+		$mbMetadata = new Iso19139();
+		$mdOwner = Mapbender::session()->get("mb_user_id");
+		$mbMetadata = $mbMetadata->createMapbenderMetadataFromXML($metaData);	
+		$e = new mb_exception("Result of parsing: ".$mbMetadata);
+		if ($mbMetadata != false) { //metadata has been parsed
+			//set randomId for metadata
+			$mbMetadata->randomId = $randomid;
+			$mbMetadata->format = "text/xml";
+			$mbMetadata->type = "ISO19115:2003";
+			$mbMetadata->origin = "upload";
+			$mbMetadata->owner = $mdOwner;
+			//push metadata object into database
+			if (!$mbMetadata->insertToDB("layer",$layerId)) {
+				$e = new mb_exception("Problem while storing uploaded metadata xml to mb_metadata table!");			abort("Problem while storing uploaded metadata xml to mb_metadata table!");
 			} else {
-				$e = new mb_exception("plugins/mb_metadata_server.php: code was not constructed via codespace#code !");	
-				$datasetid = $code;
+				$e = new mb_exception("Metadata with id ".$randomid." stored to db!");
 			}
-		} else { //try to read code from RS_Identifier 		
-			$code = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:RS_Identifier/gmd:code/gco:CharacterString');
-			$codeSpace = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:RS_Identifier/gmd:codeSpace/gco:CharacterString');
-		#$e = new mb_exception("plugins/mb_metadata_server.php: code: ".$code[0]);
-		#$e = new mb_exception("plugins/mb_metadata_server.php: codeSpace: ".$codeSpace[0]);
-			if (isset($codeSpace[0]) && isset($code[0]) && $codeSpace[0] != '' && $code[0] != '') {
-				$datasetid = $codeSpace[0]."#".$code[0];
-				$e = new mb_exception("plugins/mb_metadata_server.php: datasetid: ".$datasetid);
-			} else {
-				//neither MD_Identifier nor RS_Identifier are defined in a right way
-				$e = new mb_exception("plugins/mb_metadata_server.php: the service data coupling has problems, cause the metadata doesnt have defined a datasetid");
-			}
-		}
-
-
-		//abstract
-		$abstract = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:abstract/gco:CharacterString');
-		//searchtext -- use keywords!
-		$keywords = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/gco:CharacterString');
-		//type 
-		$type = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:hierarchyLevel/gmd:MD_ScopeCode');
-		//tmp_reference_1
-		$tmp_reference_1 = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition');
-		//tmp_reference_2
-		$tmp_reference_2 = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition');	//spatial_res_type
-		//spatial_res_value
-		//ref_system
-		//format
-		//inspire_charset
-		//inspire_top_consistence
-		//responsible_party
-		//fees
-		//"constraints"	
-		//fill database with elements:
-		$sql = <<<SQL
-
-INSERT INTO mb_metadata (link, uuid, origin, title, abstract, format, ref_system, export2csw, inspire_top_consistence, tmp_reference_1, tmp_reference_2, lineage, spatial_res_type, spatial_res_value, inspire_charset, createdate, datasetid, randomid, data, harvestresult) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, now(), $16, $17, $18, 1)
-
-			
-SQL;
-			
-		$xml = $iso19139Xml->asXML();
-		$v = array($link, $uuid[0], $origin, $title[0], $abstract[0], $format, $ref_system, $export2csw,$inspire_top_consistence,$tmp_reference_1[0],$tmp_reference_2[0],$lineage,$spatial_res_type,$spatial_res_value,$inspire_charset, $datasetid, $randomid, $xml);
-		$t = array('s','s','s','s','s','s','s','b','b','s','s','s','s','s','s','s','s','s');
-		
-		try {
-				$res = db_prep_query($sql,$v,$t);
-		}
-		catch (Exception $e){
-			abort(_mb("Insert of harvested metadata into database failed!"));
-		}
-	//set relation into relation table
-	//get metadata_id of record which have been inserted before
-	$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){
-		abort(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
-	}
-	if ($res) {
-		$row = db_fetch_assoc($res);
-		$metadata_id = $row['metadata_id'];
-	}
-	$sql = <<<SQL
-
-	INSERT INTO ows_relation_metadata (fkey_metadata_id, fkey_layer_id) VALUES ($1, $2)
-
-SQL;
-	$v = array($metadata_id, $layerId);
-	$t = array('i','i');
-	try {
-		$res = db_prep_query($sql,$v,$t);
-	}
-	catch (Exception $e){
-			abort(_mb("Cannot write relation between ows and metadata to database!"));
-	}
-	if ($dbInsertFailed != true) {	
-		$ajaxResponse->setMessage("Metadata object inserted into md_metadata!");
-		$ajaxResponse->setSuccess(true);
-	}				
+		} else {
+			abort("Could not parse metadata from local file!");
+		}				
+		$ajaxResponse->setMessage("Uploaded metadata object inserted into md_metadata table!");
+		$ajaxResponse->setSuccess(true);		
 	break;		
+//***********************
 
 	default: 
 		$ajaxResponse->setSuccess(false);

Modified: trunk/mapbender/http/plugins/mb_metadata_wfs_server.php
===================================================================
--- trunk/mapbender/http/plugins/mb_metadata_wfs_server.php	2012-06-07 09:03:25 UTC (rev 8373)
+++ trunk/mapbender/http/plugins/mb_metadata_wfs_server.php	2012-06-11 20:56:49 UTC (rev 8374)
@@ -3,6 +3,7 @@
 require_once dirname(__FILE__) . "/../classes/class_user.php";
 require_once dirname(__FILE__) . "/../classes/class_wfs.php";
 require_once(dirname(__FILE__)."/../classes/class_universal_wfs_factory.php");
+require_once dirname(__FILE__) . "/../classes/class_iso19139.php";
 
 $ajaxResponse = new AjaxResponse($_POST);
 
@@ -529,7 +530,7 @@
 
 			$sql = <<<SQL
 
-INSERT INTO ows_relation_metadata (fkey_metadata_id, fkey_featuretype_id, internal) VALUES ( $1, $2, $3)
+INSERT INTO ows_relation_metadata (fkey_metadata_id, fkey_featuretype_id, internal, relation_type) VALUES ( $1, $2, $3, 'internal')
 
 SQL;
 			$v = array($data->internal_relation,$featuretypeId, 1);
@@ -563,250 +564,102 @@
 		//generate a uuid for the record:
 		$uuid = new Uuid();
 		//initialize database objects
-		$link = '';
-		$title = '';
-		$abstract = '';
-		$format = '';	
-		$ref_system = '';
-		$export2csw = 'f';
-		$inspire_top_consistence = 'f';
-		$tmp_reference_1 = '';
-		$tmp_reference_2 = '';
-		$lineage = '';
-		$spatial_res_type = '';
-		$spatial_res_value = '';
-		$inspire_charset = '';
-		$update_frequency = '';
+		//are initialized from class_iso19139
+		$mbMetadata = new Iso19139();
+		$randomid = new Uuid();
 		//read out json objects 
 		if (isset($data->link)) {
-			$link = $data->link;
+			$mbMetadata->href = $data->link;
 		}
 		if (isset($data->export2csw)) {
-			$export2csw = $data->export2csw;
+			$mbMetadata->export2Csw = $data->export2csw;
 		} else {
-			$export2csw = 'f';
+			$mbMetadata->export2Csw = 'f';
 		}
 		if (isset($data->title)) {
-			$title = $data->title;
+			$mbMetadata->title = $data->title;
 		}
 		if (isset($data->abstract)) {
-			$abstract = $data->abstract;
+			$mbMetadata->abstract = $data->abstract;
 		}
 		if (isset($data->format)) {
-			$format = $data->format;
+			$mbMetadata->dataFormat = $data->format;
 		}
 		if (isset($data->ref_system)) {
-			$ref_system = $data->ref_system;
+			$mbMetadata->refSystem = $data->ref_system;
 		}
 		if (isset($data->inspire_top_consistence)) {
-			$inspire_top_consistence = $data->inspire_top_consistence;
+			$mbMetadata->inspireTopConsistence = $data->inspire_top_consistence;
 		}
 		if (isset($data->tmp_reference_1)) {
-			$tmp_reference_1 = $data->tmp_reference_1;
+			$mbMetadata->tmpExtentBegin = $data->tmp_reference_1;
 		}
-		if ($tmp_reference_1 == "") {
-			$tmp_reference_1 = "2000-01-01";
-		}
 		if (isset($data->tmp_reference_2)) {
-			$tmp_reference_2 = $data->tmp_reference_2;
+			$mbMetadata->tmpExtentEnd = $data->tmp_reference_2;
 		}
-		if ($tmp_reference_2 == "") {
-			$tmp_reference_2 = "2000-01-01";
-		}
 		if (isset($data->lineage)) {
-			$lineage = $data->lineage;
+			$mbMetadata->lineage = $data->lineage;
 		}
 		if (isset($data->spatial_res_type)) {
-			$spatial_res_type = $data->spatial_res_type;
+			$mbMetadata->spatialResType = $data->spatial_res_type;
 		}
 		if (isset($data->spatial_res_value)) {
-			$spatial_res_value = $data->spatial_res_value;
+			$mbMetadata->spatialResValue = $data->spatial_res_value;
 		}
 		if (isset($data->inspire_charset)) {
-			$inspire_charset = $data->inspire_charset;
+			$mbMetadata->inspireCharset = $data->inspire_charset;
 		}
 		if (isset($data->update_frequency)) {
-			$update_frequency = $data->update_frequency;
+			$mbMetadata->updateFrequency = $data->update_frequency;
 		}
-		$randomid = new Uuid();
+
 		//Check if origin is external and export2csw is activated!
 		if ($origin == 'external' ) {
 			//harvest link from location, parse the content for datasetid and push xml into data column
-			//load metadata from link TODO: function from class_wms - generate a class for metadata management and include it here and in class_wms
-			$metadataConnector = new connector();
-			$metadataConnector->set("timeOut", "5");
-			$metaData = $metadataConnector->load($link);
-			//$e = new mb_exception($metaData);
-			if (!$metaData) {
-    				abort(_mb("Could not load metadata from source url!"));
-			}
-			//delete getRecordByIdResponse from xml if there
-			$regex = "#<csw:GetRecordByIdResponse .*?>#";
-			$output = preg_replace($regex,"",$metaData);
-			$regex = "#</csw:GetRecordByIdResponse>#";
-			$output = preg_replace($regex,"",$output);
-			//$e = new mb_exception($output);
-			$metaData = $output;
-			//***
-			//write metadata to temporary file:
-			$randomFileId = new Uuid();
-				
-			$tmpMetadataFile = fopen(TMPDIR.'/link_metadata_file_'.$randomFileId.'.xml', 'w');
-			fwrite($tmpMetadataFile, $metaData);
-			fclose($tmpMetadataFile);
-			$e = new mb_exception("File which has been written: link_metadata_file_".$randomFileId.".xml");
-			//read out objects from xml structure
-			if (file_exists(TMPDIR.'/link_metadata_file_'.$randomFileId.'.xml')) {
-				$iso19139Xml=simplexml_load_file(TMPDIR.'/link_metadata_file_'.$randomFileId.'.xml');
-				//$metaData = file_get_contents(TMPDIR.'/link_metadata_file_'.$randomFileId.'.xml');
+			$mbMetadata = $mbMetadata->createFromUrl($mbMetadata->href);
+			$mdOwner = Mapbender::session()->get("mb_user_id");
+			$e = new mb_exception("owner: ".$mdOwner);	
+			if ($mbMetadata != false) { //metadata has been parsed
+				//set randomId for metadata
+				$mbMetadata->randomId = $randomid;
+				$mbMetadata->format = "text/xml";
+				$mbMetadata->type = "ISO19115:2003";
+				$mbMetadata->origin = "external";
+				$mbMetadata->owner = $mdOwner;
+				//push metadata object into database
+				if (!$mbMetadata->insertToDB("featuretype",$featuretypeId)) {
+					$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 {
-				abort(_mb("Temporary file could not be parsed!"));
-			}
-			//$metaData = $metadataConnector->file;
-			//parse metadata
-			/*try {
-				$iso19139Xml =  new SimpleXMLElement($metaData);
-			}
-			catch (Exception $e) {
-				abort(_mb("Parsing ISO19139 XML failed!"));
-			}*/
-			if ($iso19139Xml != false) {
-				//get elements for database from xml by using xpath
-				//uuid
-				$uuid = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:fileIdentifier/gco:CharacterString');
-				$e = new mb_exception("plugins/mb_metadata_server.php: File Identifier found: ".$uuid);
-				//createdate
-				$createdate = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:dateStamp/gco:Date');
-				//changedate
-				$changedate = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:dateStamp/gco:Date');
-				//TODO: check if this is set, maybe DateTime must be searched instead?
-				//title
-				$title = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString');
-				//datasetid
-				//next came from class_wms - TODO maybe put it into a special function
-				//dataset identifier - howto model into md_metadata?
-				//check where datasetid is defined - maybe as RS_Identifier or as MD_Identifier see http://inspire.jrc.ec.europa.eu/documents/Metadata/INSPIRE_MD_IR_and_ISO_v1_2_20100616.pdf page 18
-				//First check if MD_Identifier is set, then check if RS_Identifier is used!
-				//Initialize datasetid
-				$datasetid = 'undefined';
-				$code = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString');
-				if (isset($code[0]) && $code[0] != '') {
-					$e = new mb_exception("plugins/mb_metadata_server.php: code given thru MD_Identifier: ".$code[0]);
-					//check if code is defined by codespace and code
-					$codeSplit = explode("#",$code);
-					if (isset($codeSplit[0]) && $codeSplit[0] != '' && isset($codeSplit[1]) && $codeSplit[1] != '') {
-						$e = new mb_exception("plugins/mb_metadata_server.php: code was constructed via codespace#code !");	
-						$datasetid = $codeSplit[0]."#".$codeSplit[1];
-					} else {
-						$e = new mb_exception("plugins/mb_metadata_server.php: code was not constructed via codespace#code !");	
-						$datasetid = $code[0];
-					}
-				} else { //try to read code from RS_Identifier 		
-					$code = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:RS_Identifier/gmd:code/gco:CharacterString');
-					$codeSpace = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:RS_Identifier/gmd:codeSpace/gco:CharacterString');
-				#$e = new mb_exception("plugins/mb_metadata_server.php: code: ".$code[0]);
-				#$e = new mb_exception("plugins/mb_metadata_server.php: codeSpace: ".$codeSpace[0]);
-					if (isset($codeSpace[0]) && isset($code[0]) && $codeSpace[0] != '' && $code[0] != '') {
-						$datasetid = $codeSpace[0]."#".$code[0];
-						$e = new mb_exception("plugins/mb_metadata_server.php: datasetid: ".$datasetid);
-					} else {
-						//neither MD_Identifier nor RS_Identifier are defined in a right way
-						$e = new mb_exception("plugins/mb_metadata_server.php: the service data coupling has problems, cause the metadata doesnt have defined a datasetid");
-					}
+				//parsing had problems - fill only the urls into database and give a reasonable error message	
+				$mbMetadata = new Iso19139();
+				$mbMetadata->randomId = $randomid;
+				$mbMetadata->format = "text/xml";
+				$mbMetadata->type = "ISO19115:2003";
+				$mbMetadata->origin = "external";
+				$mbMetadata->owner = $mdOwner;
+				if (!$mbMetadata->insertMetadataUrlToDB("featuretype",$featuretypeId)) {
+					$e = new mb_exception("Problem while storing MetadataURL entry from wfs capabilities to mb_metadata table!");
+				} else {
+					$e = new mb_exception("Stored MetadataURL ".$mbMetadata->href." from wfs capabilities to mb database cause the target could not be accessed or parsed!");
 				}
-				//abstract
-				$abstract = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:abstract/gco:CharacterString');
-				//searchtext -- use keywords!
-				$keywords = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/gco:CharacterString');
-				//type 
-				$type = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:hierarchyLevel/gmd:MD_ScopeCode');
-				//tmp_reference_1
-				$tmp_reference_1 = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition');
-				//tmp_reference_2
-				$tmp_reference_2 = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition');		//spatial_res_type
-				//spatial_res_value
-				//ref_system
-				//format
-				//inspire_charset
-				//inspire_top_consistence
-				//responsible_party
-				//fees
-				//"constraints"	
-				//fill database with elements:
-
-				$sql = <<<SQL
-
-INSERT INTO mb_metadata (link, uuid, origin, title, abstract, format, ref_system, export2csw, inspire_top_consistence, tmp_reference_1, tmp_reference_2, lineage, spatial_res_type, spatial_res_value, inspire_charset, createdate, datasetid, randomid, data, harvestresult) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, now(), $16, $17, $18, 1)
-
-SQL;
-				$v = array($link, $uuid[0], $origin, $title[0], $abstract[0], $format, $ref_system, $export2csw,$inspire_top_consistence,$tmp_reference_1[0],$tmp_reference_2[0],$lineage,$spatial_res_type,$spatial_res_value,$inspire_charset, $datasetid, $randomid, $metaData);
-				$t = array('s','s','s','s','s','s','s','b','b','s','s','s','s','s','s','s','s','s');
-		
-				try {
-					$res = db_prep_query($sql,$v,$t);
-				}
-				catch (Exception $e){
-					abort(_mb("Insert of harvested metadata into database failed!"));
-				}
+			}	
 			
+		} else { //fill thru metador
+			$mbMetadata->origin = "metador";
+			$mbMetadata->fileIdentifier = $uuid;
+			$mbMetadata->randomId = $randomid;
+			if (!$mbMetadata->insertToDB("featuretype",$featuretypeId)) {
+				$e = new mb_exception("Problem while storing metadata to mb_metadata table!");
+				abort("Problem while storing metadata to mb_metadata table!");
 			} else {
-
-				abort(_mb("Problem with parsing the XML structure with SimpleXML! Record was not inserted into database! Ask your administrator."));
-				//give back error message - cause parsing has problems
+				$e = new mb_notice("Metadata with id ".$randomid." stored to db!");
+				$ajaxResponse->setMessage("Metadata object inserted into md_metadata!");
+				$ajaxResponse->setSuccess(true);
 			}
-		} else { //fill only links or the edited information into db 
-			$sql = <<<SQL
-
-INSERT INTO mb_metadata (link, uuid, origin, title, abstract, format, ref_system, export2csw, inspire_top_consistence, tmp_reference_1, tmp_reference_2, lineage, spatial_res_type, spatial_res_value, inspire_charset, createdate, randomid, update_frequency) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, now(), $16, $17)
-
-SQL;
-			$v = array($link, $uuid, $origin, $title, $abstract, $format, $ref_system, $export2csw,$inspire_top_consistence,$tmp_reference_1,$tmp_reference_2,$lineage,$spatial_res_type,$spatial_res_value,$inspire_charset, $randomid, $update_frequency);
-			$t = array('s','s','s','s','s','s','s','b','b','s','s','s','s','s','s','s','s');
-		
-			try {
-				$res = db_prep_query($sql,$v,$t);
-			}
-			catch (Exception $e){
-				abort(_mb("Insert of edited metadata into database failed!"));
-			}
-		}
-		//set relation into relation table
-		//get metadata_id of record which have been inserted before
-		$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){
-			abort(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
-		}
-		if ($res) {
-			$row = db_fetch_assoc($res);
-			$metadata_id = $row['metadata_id'];
-		}
-		$sql = <<<SQL
-
-		INSERT INTO ows_relation_metadata (fkey_metadata_id, fkey_featuretype_id) VALUES ($1, $2)
-
-SQL;
-		$v = array($metadata_id, $featuretypeId);
-		$t = array('i','i');
-		try {
-			$res = db_prep_query($sql,$v,$t);
-		}
-		catch (Exception $e){
-				abort(_mb("Cannot write relation between ows and metadata to database!"));
-		}
-		if ($dbInsertFailed != true) {	
-			$ajaxResponse->setMessage("Metadata object inserted into md_metadata!");
-			$ajaxResponse->setSuccess(true);
-		}				
+		}		
 		break;
 
 	case "deleteInternalMetadataLinkage" :
@@ -855,190 +708,44 @@
 		$ajaxResponse->setSuccess(true);
 		break;
 	case "importFeaturetypeXmlAddon" :
-//this is similar to insert the metadata from external link, but came from internal file from tmp folder which has been updated before
+	//this case is similar to insert the metadata from external link, but came from internal file from tmp folder which has been uploaded before
 		$featuretypeId = $ajaxResponse->getParameter("featuretypeId");
 		$filename = $ajaxResponse->getParameter("filename");
 		//normaly the link is only set if a link has been created
 		//if a record has been created, the link element will be empty 
 		//use this to distinguish between the to different inserts!
 		//this insert should also push one entry in the ows_relation_metadata table! - after the insert into md_metadata
-		//origin
-		//generate metador entry
-		$origin = 'upload';
-		//generate a uuid for the record:
 		$uuid = new Uuid();
-		//initialize database objects
-		$link = '';
-		$title = '';
-		$abstract = '';
-		$format = '';	
-		$ref_system = '';
-		$export2csw = 't';
-		$inspire_top_consistence = 'f';
-		$tmp_reference_1 = '';
-		$tmp_reference_2 = '';
-		$lineage = '';
-		$spatial_res_type = '';
-		$spatial_res_value = '';
-		$inspire_charset = '';
 		$randomid = new Uuid();	
 		$e = new mb_notice("File to load: ".$filename);
-		//read out objects from xml structure
-		/*if (file_exists($filename)) {
-			try {	
-				
-				$iso19139Xml = simplexml_load_file($filename);
-			}
-			catch (Exception $e) {
-				abort(_mb("Loading ISO19139 XML failed!"));
-			}
-		}
-		else {
-			abort(_mb("File not found: ".$filename." !"));
-		}*/
 		$metaData = file_get_contents($filename);
 		if (!$metaData){
 			abort(_mb("Reading file ".$filename." failed!"));
 		}
-		//delete getRecordByIdResponse from xml if there
-		$regex = "#<csw:GetRecordByIdResponse .*?>#";
-		$output = preg_replace($regex,"",$metaData);
-		$regex = "#</csw:GetRecordByIdResponse>#";
-		$output = preg_replace($regex,"",$output);
-		//$e = new mb_exception($output);
-		$iso19139Xml = simplexml_load_string($output);
-		//$e = new mb_notice('');
-		
-		//get elements for database from xml by using xpath
-		//uuid
-		$uuid = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:fileIdentifier/gco:CharacterString');
-		if (!isset($uuid) || $uuid == "") {
-			abort(_mb(" No fileIdentifier found, parsing ISO19139 XML maybe failed. Check if element /gmd:MD_Metadata/gmd:fileIdentifier/gco:CharacterString is given and not empty!"));
-		}
-		//$e = new mb_exception("plugins/mb_metadata_server.php: File Identifier found: ".$uuid);
-		//createdate
-		$createdate = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:dateStamp/gco:Date');
-		//changedate
-		$changedate = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:dateStamp/gco:Date');
-		//TODO: check if this is set, maybe DateTime must be searched instead?
-		//title
-		$title = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString');
-		//next came from class_wms - TODO maybe put it into a special function
-		//dataset identifier - howto model into md_metadata?
-		//check where datasetid is defined - maybe as RS_Identifier or as MD_Identifier see http://inspire.jrc.ec.europa.eu/documents/Metadata/INSPIRE_MD_IR_and_ISO_v1_2_20100616.pdf page 18
-		//First check if MD_Identifier is set, then check if RS_Identifier is used!
-		//Initialize datasetid
-		$datasetid = 'undefined';
-		$code = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString');
-		if (isset($code[0]) && $code[0] != '') {
-			$e = new mb_notice("plugins/mb_metadata_server.php: code given thru MD_Identifier: ".$code[0]);
-			//check if code is defined by codespace and code
-			$codeSplit = explode("#",$code);
-			if (isset($codeSplit[0]) && $codeSplit[0] != '' && isset($codeSplit[1]) && $codeSplit[1] != '') {
-				$e = new mb_exception("plugins/mb_metadata_server.php: code was constructed via codespace#code !");	
-				$datasetid = $code[0];
-				$datasetid_codespace = "";
+		$mbMetadata = new Iso19139();
+		$mdOwner = Mapbender::session()->get("mb_user_id");
+		$mbMetadata = $mbMetadata->createMapbenderMetadataFromXML($metaData);	
+		$e = new mb_exception("Result of parsing: ".$mbMetadata);
+		if ($mbMetadata != false) { //metadata has been parsed
+			//set randomId for metadata
+			$mbMetadata->randomId = $randomid;
+			$mbMetadata->format = "text/xml";
+			$mbMetadata->type = "ISO19115:2003";
+			$mbMetadata->origin = "upload";
+			$mbMetadata->owner = $mdOwner;
+			//push metadata object into database
+			if (!$mbMetadata->insertToDB("featuretype",$featuretypeId)) {
+				$e = new mb_exception("Problem while storing uploaded metadata xml to mb_metadata table!");			abort("Problem while storing uploaded metadata xml to mb_metadata table!");
 			} else {
-				$e = new mb_exception("plugins/mb_metadata_server.php: code was not constructed via codespace#code !");	
-				$datasetid = $code[0];
-				$datasetid_codespace = "";
+				$e = new mb_exception("Metadata with id ".$randomid." stored to db!");
 			}
-		} else { //try to read code from RS_Identifier 		
-			$code = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:RS_Identifier/gmd:code/gco:CharacterString');
-			$codeSpace = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:identifier/gmd:RS_Identifier/gmd:codeSpace/gco:CharacterString');
-		#$e = new mb_exception("plugins/mb_metadata_server.php: code: ".$code[0]);
-		#$e = new mb_exception("plugins/mb_metadata_server.php: codeSpace: ".$codeSpace[0]);
-			if (isset($codeSpace[0]) && isset($code[0]) && $codeSpace[0] != '' && $code[0] != '') {
-				$datasetid = $code[0];
-				$datasetid_codespace = $codeSpace[0];
-				$e = new mb_exception("plugins/mb_metadata_server.php: datasetid: ".$datasetid);
-			} else {
-				//neither MD_Identifier nor RS_Identifier are defined in a right way
-				$e = new mb_exception("plugins/mb_metadata_server.php: the service data coupling has problems, cause the metadata doesnt have defined a datasetid");
-			}
-		}
-
-
-		//abstract
-		$abstract = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:abstract/gco:CharacterString');
-		//searchtext -- use keywords!
-		$keywords = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/gco:CharacterString');
-		//type 
-		$type = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:hierarchyLevel/gmd:MD_ScopeCode');
-		//tmp_reference_1
-		$tmp_reference_1 = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition');
-		//tmp_reference_2
-		$tmp_reference_2 = $iso19139Xml->xpath('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition');	//spatial_res_type
-		//spatial_res_value
-		//ref_system
-		//format
-		//inspire_charset
-		//inspire_top_consistence
-		//responsible_party
-		//fees
-		//"constraints"	
-		//fill database with elements:
-		$sql = <<<SQL
-
-INSERT INTO mb_metadata (link, uuid, origin, title, abstract, format, ref_system, export2csw, inspire_top_consistence, tmp_reference_1, tmp_reference_2, lineage, spatial_res_type, spatial_res_value, inspire_charset, createdate, datasetid, randomid, data, harvestresult, datasetid_codespace) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, now(), $16, $17, $18, 1, $19)
-
-			
-SQL;
-			
-		$xml = $iso19139Xml->asXML();
-		$v = array($link, $uuid[0], $origin, $title[0], $abstract[0], $format, $ref_system, $export2csw, $inspire_top_consistence,$tmp_reference_1[0],$tmp_reference_2[0],$lineage,$spatial_res_type,$spatial_res_value,$inspire_charset, $datasetid, $randomid, $xml,$datasetid_codespace);
-		$t = array('s','s','s','s','s','s','s','b','b','s','s','s','s','s','s','s','s','s','s');
-		
-		try {
-				$res = db_prep_query($sql,$v,$t);
-		}
-		catch (Exception $e){
-			abort(_mb("Insert of harvested metadata into database failed!"));
-		}
-	//set relation into relation table
-	//get metadata_id of record which have been inserted before
-	$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){
-		abort(_mb("Cannot get metadata record with following uuid from database: ".$uuid));
-	}
-	if ($res) {
-		$row = db_fetch_assoc($res);
-		$metadata_id = $row['metadata_id'];
-	}
-	$sql = <<<SQL
-
-	INSERT INTO ows_relation_metadata (fkey_metadata_id, fkey_featuretype_id) VALUES ($1, $2)
-
-SQL;
-	$v = array($metadata_id, $featuretypeId);
-	$t = array('i','i');
-	try {
-		$res = db_prep_query($sql,$v,$t);
-	}
-	catch (Exception $e){
-			abort(_mb("Cannot write relation between ows and metadata to database!"));
-	}
-	if ($dbInsertFailed != true) {	
-		$ajaxResponse->setMessage("Metadata object inserted into md_metadata!");
-		$ajaxResponse->setSuccess(true);
-	}				
+		} else {
+			abort("Could not parse metadata from local file!");
+		}				
+		$ajaxResponse->setMessage("Uploaded metadata object inserted into md_metadata table!");
+		$ajaxResponse->setSuccess(true);		
 	break;		
 
-
-
-
-
-
 	case "save":
 		$data = $ajaxResponse->getParameter("data");
 		

Modified: trunk/mapbender/resources/db/pgsql/UTF-8/update/update_2.7.2_to_2.7.3_pgsql_UTF-8.sql
===================================================================
--- trunk/mapbender/resources/db/pgsql/UTF-8/update/update_2.7.2_to_2.7.3_pgsql_UTF-8.sql	2012-06-07 09:03:25 UTC (rev 8373)
+++ trunk/mapbender/resources/db/pgsql/UTF-8/update/update_2.7.2_to_2.7.3_pgsql_UTF-8.sql	2012-06-11 20:56:49 UTC (rev 8374)
@@ -440,3 +440,7 @@
 --Add handling of codespaces as demanded from the INSPIRE regulation
 ALTER TABLE mb_metadata ADD COLUMN datasetid_codespace TEXT;
 
+ALTER TABLE ows_relation_metadata ADD COLUMN relation_type TEXT;
+--update table ows_relation_metadata set type from table mb_metadata -- it is better to have it for relation not for instance
+
+



More information about the Mapbender_commits mailing list