Procházet zdrojové kódy

added Delete to wfs to clear the_geom field

Piotr Labudda před 10 roky
rodič
revize
5cebf19c3e
1 změnil soubory, kde provedl 117 přidání a 20 odebrání
  1. 117 20
      SE/se-lib/Api/WfsServerBase.php

+ 117 - 20
SE/se-lib/Api/WfsServerBase.php

@@ -608,6 +608,7 @@ class Api_WfsServerBase {
 		//}
 		{// get used typeNames
 			$usedTypeNames = array();
+
 			// <Update xmlns="http://www.opengis.net/wfs" typeName="p5_default_db:TEST_PERMS">
 			foreach ($requestXmlTags as $tag) {
 				if ('Update' == $tag['tag'] && 'open' == $tag['type']) {
@@ -634,13 +635,25 @@ class Api_WfsServerBase {
 				}
 				$lastTagName = $tag['tag'];
 			}
+
+			// <Delete xmlns="http://www.opengis.net/wfs" typeName="p5_default_db:TEST_GEOM_POINT">
+			foreach ($requestXmlTags as $tag) {
+				if ('Delete' == $tag['tag'] && 'open' == $tag['type']) {
+					$typeName = $tag['attributes']['typeName'];
+					foreach ($sourceNsList as $nsInd => $ns) {
+						if ("p5_{$ns[0]}:{$ns[1]}" == $typeName) {
+							$usedSourceNsList[$nsInd] = $ns;
+						}
+					}
+				}
+			}
 		}
 		if (empty($usedSourceNsList)) {
 			throw new Exception("Parse Request xml error #" . __LINE__ . ": not implemented");
 		}
 
 		$convertedTransaction = $this->_convertTransactionXml($requestXml, $usedSourceNsList);
-if($DBG){echo '$convertedTransaction:';print_r($convertedTransaction);echo "\n";}
+if($DBG){echo 'L.' . __LINE__ . ' $convertedTransaction:';print_r($convertedTransaction);echo "\n";}
 		if (empty($convertedTransaction)) {
 			throw new Exception("Parse Request xml error #" . __LINE__ . ": convert Transaction");
 		}
@@ -680,7 +693,9 @@ if($DBG){echo '$convertedTransaction:';print_r($convertedTransaction);echo "\n";
 				case 'Insert':
 				case 'Delete':
 				case 'Native':
-					if ('open' == $tag['type']) {
+					if ('open' == $tag['type'] // Insert, Update
+						|| 'complete' == $tag['type'] // Delete
+						) {
 						$actionTag = array();
 						$actionTag['tag'] = substr($tag['tag'], 0, 6);
 						$actionTag['typeName'] = $tag['attributes']['typeName'];
@@ -692,7 +707,16 @@ if($DBG){echo '$convertedTransaction:';print_r($convertedTransaction);echo "\n";
 						if ('Update' == substr($tag['tag'], 0, 6) && empty($actionTag['featureId'])) {
 							throw new Api_WfsException("Syntax error - could not read feature id!");
 						}
-						$actionTag['itemPatch'] = array();
+						if ('Delete' == substr($tag['tag'], 0, 6) && empty($actionTag['featureId'])) {
+							throw new Api_WfsException("Syntax error - could not read feature id!");
+						}
+
+						if ('complete' == $tag['type']) {
+							$itemPatchs[] = $actionTag;
+							$actionTag = null;
+						} else {
+							$actionTag['itemPatch'] = array();
+						}
 					} else {
 						$itemPatchs[] = $actionTag;
 						$actionTag = null;
@@ -718,6 +742,8 @@ if($DBG){echo '$convertedTransaction:';print_r($convertedTransaction);echo "\n";
 								$actionTag['itemPatch'][$tag['tag']] = $tag['value'];
 							}
 						}
+						else if ('Delete' == $actionTag['tag']) {
+						}
 					}
 			}
 
@@ -725,26 +751,32 @@ if($DBG){echo '$convertedTransaction:';print_r($convertedTransaction);echo "\n";
 		}
 
 		if($DBG){echo "\nTODO: itemPatchs L." . __LINE__ . ":\n"; print_r($itemPatchs);echo "\n";}
-		if($DBG){echo "\nTODO: _user_id L." . __LINE__ . ":\n"; print_r($this->_usrAcl->_user_id);echo "\n";}
+		if($DBG){echo "\nTODO: _user_id L." . __LINE__ . " '{$this->_usrAcl->_user_id}'" . "\n";}
 		$changesList = array();
 		if (!empty($itemPatchs)) {
 			foreach ($itemPatchs as $itemPatchInfo) {
 				if($DBG){echo "itemPatchInfo['itemPatch']:\n";print_r($itemPatchInfo['itemPatch']);}
-				if (empty($itemPatchInfo['itemPatch'])) continue;
-
 				$acl = $this->getAclFromTypeName($itemPatchInfo['typeName']);
-				$itemPatch = $itemPatchInfo['itemPatch'];
-				if ('Update' == $itemPatchInfo['tag']) {
+				if ('Delete' == $itemPatchInfo['tag']) {
+					$itemPatch = array();
 					$itemPatch[$acl->getPrimaryKeyField()] = $itemPatchInfo['featureId'];
-				}
-				else if ('Insert' == $itemPatchInfo['tag']) {
-					if (!empty($itemPatch[$acl->getPrimaryKeyField()])) {
-						$itemPatchInfo['featureId'] = $itemPatch[$acl->getPrimaryKeyField()];
+					$itemPatch['the_geom'] = 'NULL';
+				} else {
+					if (empty($itemPatchInfo['itemPatch'])) continue;
+					$itemPatch = $itemPatchInfo['itemPatch'];
+					if ('Update' == $itemPatchInfo['tag']) {
+						$itemPatch[$acl->getPrimaryKeyField()] = $itemPatchInfo['featureId'];
 					}
-					else {
-						//throw new Exception("TODO: Insert #" . __LINE__ . ": Create new record");
+					else if ('Insert' == $itemPatchInfo['tag']) {
+						if (!empty($itemPatch[$acl->getPrimaryKeyField()])) {
+							$itemPatchInfo['featureId'] = $itemPatch[$acl->getPrimaryKeyField()];
+						}
+						else {
+							//throw new Exception("TODO: Insert #" . __LINE__ . ": Create new record");
+						}
 					}
 				}
+
 				if($DBG){echo "TODO '" . ($itemPatchInfo['tag'])? 'Insert' : 'Update' . "' itemPatch:\n";print_r($itemPatch);}
 				if ('Insert' == $itemPatchInfo['tag'] && empty($itemPatch[$acl->getPrimaryKeyField()])) {
 					$newId = $acl->addItem($itemPatch);
@@ -833,9 +865,10 @@ EOF;
 
 	public function _convertTransactionXml($requestXmlString, $sourceNsList) {
 		$DBG = (V::get('DBG_XSL', '', $_GET) > 0);// TODO: Profiler
-if($DBG){echo 'sourceNsList:';print_r($sourceNsList);echo "\n";}
+if($DBG){echo 'L.' . __LINE__ . ' sourceNsList:';print_r($sourceNsList);echo "\n";}
 		$updateActionsXsd = array();
 		$insertActionsXsd = array();
+		$deleteActionsXsd = array();
 		//<!-- TODO: create tag Update{X} where X is namespace index -->
 		foreach ($sourceNsList as $nsInd => $sourceNs) {
 			// <Update>
@@ -898,6 +931,21 @@ EOF;
 					</xsl:when>
 EOF;
 			$insertActionsXsd[] = $actionXsd;
+
+			$deleteElementName = "DeleteNs{$nsInd}";
+			$typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
+			$actionXsd = <<<EOF
+					<xsl:when test="@typeName = '{$typeName}'">
+						<xsl:variable name="typeName" select="@typeName" />
+						<xsl:for-each select="ogc:Filter/ogc:FeatureId">
+							<xsl:element name="{$deleteElementName}">
+								<xsl:attribute name="typeName"><xsl:value-of select="\$typeName" /></xsl:attribute>
+								<xsl:attribute name="featureId"><xsl:value-of select="@fid" /></xsl:attribute>
+							</xsl:element>
+						</xsl:for-each>
+					</xsl:when>
+EOF;
+			$deleteActionsXsd[] = $actionXsd;
 		}
 		if (!empty($updateActionsXsd)) {
 			$updateActionsXsd = implode("\n", $updateActionsXsd);
@@ -919,6 +967,16 @@ EOF;
 		} else {
 			$insertActionsXsd = '';
 		}
+		if (!empty($deleteActionsXsd)) {
+			$deleteActionsXsd = implode("\n", $deleteActionsXsd);
+			$deleteActionsXsd = <<<EOF
+				<xsl:choose>
+					{$deleteActionsXsd}
+				</xsl:choose>
+EOF;
+		} else {
+			$deleteActionsXsd = '';
+		}
 
 		$convertTransactionXslString = <<<EOF
 <?xml version="1.0"?>
@@ -936,11 +994,12 @@ EOF;
 				<xsl:for-each select="wfs:Update">
 					{$updateActionsXsd}
 				</xsl:for-each>
-				<!-- TODO: Insert -->
 				<xsl:for-each select="wfs:Insert/*">
 					{$insertActionsXsd}
 				</xsl:for-each>
-				<!-- TODO: Delete -->
+				<xsl:for-each select="wfs:Delete">
+					{$deleteActionsXsd}
+				</xsl:for-each>
 				<!-- TODO: Native -->
 			</Transaction>
 		</xsl:for-each>
@@ -948,7 +1007,7 @@ EOF;
 
 </xsl:transform>
 EOF;
-if($DBG){echo '$convertTransactionXslString:' . $convertTransactionXslString . "\n";}
+if($DBG){echo 'L.' . __LINE__ . ' $convertTransactionXslString:' . $convertTransactionXslString . "\n";}
 		$requestXml = new DOMDocument();
 		$requestXml->loadXml($requestXmlString);
 
@@ -961,7 +1020,7 @@ if($DBG){echo '$convertTransactionXslString:' . $convertTransactionXslString . "
 
 	public function _validateConvertedTransactionXml($convertedTransaction, $sourceNsList) {
 		$DBG = (V::get('DBG_XSD', '', $_GET) > 0);// TODO: Profiler
-if($DBG){echo 'sourceNsList:';print_r($sourceNsList);echo "\n";}
+if($DBG){echo 'L.' . __LINE__ . ' sourceNsList:';print_r($sourceNsList);echo "\n";}
 
 		$dom = new DOMDocument('1.0', 'utf-8');
 		$dom->formatOutput = true;
@@ -1011,6 +1070,15 @@ if($DBG){echo 'sourceNsList:';print_r($sourceNsList);echo "\n";}
 						$insertElemNode->setAttribute('type', $insertElementType);
 					}
 					// <!-- <xsd:element ref="Delete"/>	-->
+					foreach ($sourceNsList as $nsInd => $sourceNs) {
+						$typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
+						$deleteElementName = "DeleteNs{$nsInd}";
+						$deleteElementType = "DeleteNs{$nsInd}ElementType";
+						$deleteElemNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
+						$choiceNode->appendChild($deleteElemNode);
+						$deleteElemNode->setAttribute('name', $deleteElementName);
+						$deleteElemNode->setAttribute('type', $deleteElementType);
+					}
 					// <!-- <xsd:element ref="Native"/>	-->
 
 				$attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
@@ -1184,10 +1252,39 @@ if($DBG){echo 'sourceNsList:';print_r($sourceNsList);echo "\n";}
 								$patternNode->setAttribute('value', '[a-zA-Z_][a-zA-Z0-9_]*\.[0-9]*');
 				}
 			}
+			{// 'Delete'
+				$typeName = "p5_{$sourceNs[0]}:{$sourceNs[1]}";
+				$acl = $this->getAclFromTypeName($typeName);
+				$deleteElementType = "DeleteNs{$nsInd}ElementType";
+				$deleteTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
+				$rootNode->appendChild($deleteTypeNode);
+				$deleteTypeNode->setAttribute('name', $deleteElementType);
+
+				/* <xsd:attribute name="typeName" type="xsd:token" use="required"/> */
+				$attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
+				$deleteTypeNode->appendChild($attrNode);
+				$attrNode->setAttribute('name', 'typeName');
+				$attrNode->setAttribute('type', 'xsd:token');
+				$attrNode->setAttribute('use', 'required');
+				/* <xsd:attribute name="featureId" use="required"> */
+				$attrNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:attribute');
+				$deleteTypeNode->appendChild($attrNode);
+				$attrNode->setAttribute('name', 'featureId');
+				$attrNode->setAttribute('use', 'required');
+				$sTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
+				$attrNode->appendChild($sTypeNode);
+					$resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
+					$sTypeNode->appendChild($resNode);
+					$resNode->setAttribute('base', 'xsd:string');
+						$patternNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:pattern');
+						$resNode->appendChild($patternNode);
+						$patternNode->setAttribute('value', '[a-zA-Z_][a-zA-Z0-9_]*\.[0-9]*');
+			}
 		}
 
 		$validateConvertedTransactionXsdString = $dom->saveXml();
-if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvertedTransactionXsdString);echo "\n";}
+
+if($DBG){echo 'L.' . __LINE__ . ' $validateConvertedTransactionXsdString:';print_r($validateConvertedTransactionXsdString);echo "\n";}
 		$reqXml = new DOMDocument();
 		$reqXml->loadXml($convertedTransaction);
 		// TODO: fetch PHP Warning: DOMDocument::schemaValidateSource(): Element 'PARENT_ID': 'abc' is not a valid value of the atomic type 'xs:integer'.