[Mapbender-commits] r3755 - in trunk/mapbender/http: classes php

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Mon Mar 16 13:45:34 EDT 2009


Author: christoph
Date: 2009-03-16 13:45:34 -0400 (Mon, 16 Mar 2009)
New Revision: 3755

Modified:
   trunk/mapbender/http/classes/class_wfsToDb.php
   trunk/mapbender/http/php/mod_wfs_server.php
Log:
http://trac.osgeo.org/mapbender/ticket/396

Modified: trunk/mapbender/http/classes/class_wfsToDb.php
===================================================================
--- trunk/mapbender/http/classes/class_wfsToDb.php	2009-03-16 17:30:32 UTC (rev 3754)
+++ trunk/mapbender/http/classes/class_wfsToDb.php	2009-03-16 17:45:34 UTC (rev 3755)
@@ -128,7 +128,7 @@
 		);
 			
 		$t = array('s', 's', 's', 's', 's', 's', 's', 's' ,'s' ,'s' ,'s' ,'s', 'i' ,'i');
-		$e = new mb_exception($sql . " " . print_r($v, true));
+		$e = new mb_exception("UPDATING WFS " . $aWfs->id);
 		$res = db_prep_query($sql, $v, $t);
 		if (!$res) {
 			$e = new mb_exception("Error while updating WFS in database.");
@@ -136,15 +136,27 @@
 			return false;
 		}
 		
-		// update existing WFS feature types
+		
 		$featureTypeNameArray = array();
 		for ($i = 0; $i < count($aWfs->featureTypeArray); $i++) {
 			$currentFeatureType = $aWfs->featureTypeArray[$i];
 			array_push($featureTypeNameArray, $currentFeatureType);
-			if (!WfsToDb::updateFeatureType($currentFeatureType)) {
-				db_rollback();
-				return false;
+			if (WfsToDb::featureTypeExists($currentFeatureType)) {
+				// update existing WFS feature types
+				$e = new mb_exception("FT exists");
+				if (!WfsToDb::updateFeatureType($currentFeatureType)) {
+					db_rollback();
+					return false;
+				}
 			}
+			else {
+				$e = new mb_exception("FT ne pas exists");
+				// insert new feature types
+				if (!WfsToDb::insertFeatureType($currentFeatureType)) {
+					db_rollback();
+					return false;
+				}
+			}
 		}		
 		
 		// delete obsolete WFS feature types
@@ -164,7 +176,6 @@
 			$sql .=  " AND featuretype_name NOT IN (" . $sql_in . ")";
 		}
 		
-		$e = new mb_exception($sql . " " . print_r($v, true));
 		$res = db_prep_query($sql,$v,$t);
 		if (!$res) {
 			$e = new mb_exception("Error while deleting obsolete WFS feature types in database.");
@@ -237,6 +248,7 @@
 			$aWfsFeatureTypeNamespace->value
 		);
 		$t = array("s", "s", "s", "s");
+		$e = new mb_exception("INSERTING FT NS $aWfsId, FT: $aWfsFeatureTypeId, NS: $aWfsFeatureTypeNamespace->name");
 		$res = db_prep_query($sql, $v, $t);
 
 		if (!$res) {
@@ -264,6 +276,7 @@
 		);
 		$t = array("i", "s", "s");
 		
+		$e = new mb_exception("INSERTING FT EL (FT: $aWfsFeatureTypeId, NS: $aWfsFeatureTypeElement->name");
 		$res = db_prep_query($sql, $v, $t);
 		
 		if (!$res) {
@@ -273,6 +286,48 @@
 		
 		// set the WFS feature type element ID
 		$aWfsFeatureTypeElement->id = db_insert_id("", "wfs_element", "element_id");
+
+		//
+		//
+		//ADD THIS FEATURETYPE TO WFS CONFIGURATIONS THAT USE THIS FEATURETYPE
+		//
+		//
+		$sql = "SELECT wfs_conf_id FROM wfs_conf WHERE fkey_featuretype_id = $1";
+		$v = array($aWfsFeatureTypeId);
+		$t = array("i");
+		$res = db_prep_query($sql, $v, $t);
+		if (!$res) {
+			// no configuration exists for this featuretype, 
+			// which is fine
+			return true;
+		}
+		while ($row = db_fetch_array($res)) {
+			$wfsConfId = $row["wfs_conf_id"];
+			
+			// check if wfs conf element exists for this
+			// featuretype element
+			$sqlConfElement = "SELECT COUNT(wfs_conf_element_id) AS cnt FROM " . 
+				"wfs_conf_element AS a, wfs_element AS b " . 
+				"WHERE a.f_id = b.element_id AND " .
+				"b.element_id = $1 AND a.fkey_wfs_conf_id = $2";
+			$v = array($aWfsFeatureTypeElement->id, $wfsConfId);
+			$t = array("i", "i");
+			$resConfElement = db_prep_query($sqlConfElement, $v, $t);
+			$rowConfElement = db_fetch_array($resConfElement);
+			$count = $rowConfElement["cnt"];
+			if ($count === 0) {
+				// Insert featuretype element in wfs_conf_element
+				$sqlInsertConfElement = "INSERT INTO wfs_conf_element " . 
+					"(fkey_wfs_conf_id, f_id) VALUES ($1, $2)";
+				$v = array($wfsConfId, $aWfsFeatureTypeElement->id);
+				$t = array("i", "i");
+				$resInsertConfElement = db_prep_query($sqlInsertConfElement, $v, $t);
+				if (!$res) {
+					$e = new mb_exception("Couldn't insert new feature type element in wfs_conf_element!");
+					return false;
+				}
+			}
+		}
 		
 		return true;
 	}
@@ -295,7 +350,7 @@
 		);
 		$t = array("s", "i", "i");
 
-		$e = new mb_exception($sql . " " . print_r($v, true));
+		$e = new mb_exception("UPDATING FT EL (FT: $aWfsFeatureTypeId, NS: $aWfsFeatureTypeElement->name");
 		$res = db_prep_query($sql, $v, $t);
 
 		if (!$res) {
@@ -326,6 +381,7 @@
 		);
 		$t = array('i','s','s','s','s');
 
+		$e = new mb_exception("INSERTING FT (FT: $aWfsFeatureType->name)");
 		$res = db_prep_query($sql,$v,$t);
 		if (!$res) {
 			$e = new mb_exception("Error while inserting WFS feature type into database.");
@@ -361,6 +417,8 @@
 	 */
 	private static function updateFeatureType ($aWfsFeatureType) {
 
+		$aWfsFeatureType->id = WfsToDb::getFeatureTypeId($aWfsFeatureType);
+
 		$sql = "UPDATE wfs_featuretype SET ";
 		$sql .= "featuretype_title = $1,";
 		$sql .= "featuretype_abstract = $2,";
@@ -374,7 +432,7 @@
 		);
 		$t = array('s','s','s','i');
 
-		$e = new mb_exception($sql . " " . print_r($v, true));
+		$e = new mb_exception("UPDATING FT (FT: $aWfsFeatureType->id)");
 
 		$res = db_prep_query($sql,$v,$t);
 		if (!$res) {
@@ -387,9 +445,16 @@
 		for ($i = 0; $i < count($aWfsFeatureType->elementArray); $i++) {
 			$currentElement = $aWfsFeatureType->elementArray[$i];
 			array_push($featureTypeElementNameArray, $currentElement);
-			if (!WfsToDb::updateFeatureTypeElement($aWfsFeatureType->id, $currentElement)) {
-				return false;
+			if (WfsToDb::featureTypeElementExists($aWfsFeatureType, $currentElement->name)) {
+				if (!WfsToDb::updateFeatureTypeElement($aWfsFeatureType->id, $currentElement)) {
+					return false;
+				}
 			}
+			else {
+				if (!WfsToDb::insertFeatureTypeElement($aWfsFeatureType->id, $currentElement)) {
+					return false;
+				}
+			}
 		}		
 		
 		// delete obsolete WFS feature type elements
@@ -409,7 +474,6 @@
 		if ($sql_in !== "")
 		$sql .= " AND element_name NOT IN (" . $sql_in . ")";
 
-		$e = new mb_exception($sql . " " . print_r($v, true));
 		$res = db_prep_query($sql,$v,$t);
 		if (!$res) {
 			$e = new mb_exception("Error while deleting obsolete WFS feature type element in database.");
@@ -424,7 +488,6 @@
 			$aWfsFeatureType->id
 		);
 		$t = array("i", "i");
-		$e = new mb_exception($sql . " " . print_r($v, true));
 		$res = db_prep_query($sql, $v, $t);
 		if (!$res) {
 			$e = new mb_exception("Error while deleting WFS feature type namespaces from the database.");
@@ -475,9 +538,10 @@
 			"fkey_wfs_id = $1 AND featuretype_name = $2";
 		$v = array(
 			$aWfsFeatureType->wfs->id,
-			$aWfsFeatureType->name,
+			$aWfsFeatureType->name
 		);
 		$t = array("i", "s");
+		$e = new mb_exception($sql . " " . print_r($v, true));
 		$res = db_prep_query($sql, $v, $t);
 		if ($row = db_fetch_array($res)) {
 			return $row["featuretype_id"];
@@ -497,5 +561,41 @@
 		}
 		return false;
 	}
+
+	/**
+	 * Gets the ID of a feature type element 
+	 * 
+	 * @return Integer
+	 * @param $aWfsFeatureType WfsFeatureType
+	 * @param $name WFS feature type element name
+	 */
+	private static function getFeatureTypeElementId ($aWfsFeatureType, $name) {
+		$sql = "SELECT element_id FROM wfs_element WHERE " . 
+			"fkey_featuretype_id = $1 AND element_name = $2";
+		$v = array(
+			$aWfsFeatureType->id,
+			$name,
+		);
+		$t = array("i", "s");
+		$res = db_prep_query($sql, $v, $t);
+		if ($row = db_fetch_array($res)) {
+			return $row["element_id"];
+		}
+		return null;
+	}
+	
+	/**
+	 * Checks if a featuretype element exists in the database. 
+	 * 
+	 * @return Boolean
+	 * @param $aWfsFeatureType WfsFeatureType
+	 * @param $name WFS feature type element name
+	 */
+	private static function featureTypeElementExists ($aWfsFeatureType, $name) {
+		if (WfsToDb::getFeatureTypeElementId($aWfsFeatureType, $name) !== null) {
+			return true;
+		}
+		return false;
+	}
 }
 ?>
\ No newline at end of file

Modified: trunk/mapbender/http/php/mod_wfs_server.php
===================================================================
--- trunk/mapbender/http/php/mod_wfs_server.php	2009-03-16 17:30:32 UTC (rev 3754)
+++ trunk/mapbender/http/php/mod_wfs_server.php	2009-03-16 17:45:34 UTC (rev 3755)
@@ -233,9 +233,11 @@
  */
 function updateWfs($obj){
 	$id = $obj->wfs;
+	$url = $obj->url;
 
 	$wfsFactory = new UniversalWfsFactory();
-	$myWfs = $wfsFactory->createFromDb($id);
+	$myWfs = $wfsFactory->createFromUrl($url);
+	$myWfs->id = $id;
 
 	if(is_null($myWfs) || !$myWfs->update()){
 		$obj->success = false;



More information about the Mapbender_commits mailing list