public static function ParseSingleFeatureXml($app, $classDef, $xml, $featureNodeName = "Feature", $propertyNodeName = "Property") { $doc = new DOMDocument($xml); $doc->loadXML($xml); return MgUtils::ParseSingleFeatureDocument($app, $classDef, $doc, $featureNodeName, $propertyNodeName); }
public function UpdateFeatures($resId, $schemaName, $className, $format) { //Check for unsupported representations $fmt = $this->ValidateRepresentation($format, array("xml", "json")); $mimeType = $this->GetMimeTypeForFormat($fmt); $trans = null; try { $sessionId = ""; if ($resId->GetRepositoryType() == MgRepositoryType::Session) { $sessionId = $resId->GetRepositoryName(); } $this->EnsureAuthenticationForSite($sessionId); $siteConn = new MgSiteConnection(); $siteConn->Open($this->userInfo); $site = $siteConn->GetSite(); $this->VerifyWhitelist($resId->ToString(), $mimeType, "UPDATEFEATURES", $fmt, $site, $this->userName); $body = $this->app->request->getBody(); if ($fmt == "json") { $json = json_decode($body); if ($json == NULL) { throw new Exception($this->app->localizer->getText("E_MALFORMED_JSON_BODY")); } $body = MgUtils::Json2Xml($json); } $resSvc = $siteConn->CreateService(MgServiceType::ResourceService); $perms = self::CheckPermissions($resSvc, $resId); //Not a session-based resource, must check for appropriate flag in header before we continue if ($sessionId === "") { if ($perms->AllowUpdate === false) { $e = new Exception(); $this->OutputException("Forbidden", $this->app->localizer->getText("E_OPERATION_NOT_ALLOWED"), $this->app->localizer->getText("E_FEATURE_SOURCE_NOT_CONFIGURED_TO_ALLOW_UPDATES", $resId->ToString()), $e->getTraceAsString(), 403, $mimeType); } } $featSvc = $siteConn->CreateService(MgServiceType::FeatureService); $doc = new DOMDocument(); $doc->loadXML($body); $commands = new MgFeatureCommandCollection(); $filter = ""; $filterNode = $doc->getElementsByTagName("Filter"); if ($filterNode->length == 1) { $filter = $filterNode->item(0)->nodeValue; } $classDef = $featSvc->GetClassDefinition($resId, $schemaName, $className); $props = MgUtils::ParseSingleFeatureDocument($this->app, $classDef, $doc, "UpdateProperties"); $updateCmd = new MgUpdateFeatures("{$schemaName}:{$className}", $props, $filter); $commands->Add($updateCmd); if ($perms->UseTransaction === true) { $trans = $featSvc->BeginTransaction($resId); } //HACK: Due to #2252, we can't call UpdateFeatures() with NULL MgTransaction, so to workaround //that we call the original UpdateFeatures() overload with useTransaction = false if we find a //NULL MgTransaction if ($trans == null) { $result = $featSvc->UpdateFeatures($resId, $commands, false); } else { $result = $featSvc->UpdateFeatures($resId, $commands, $trans); } if ($trans != null) { $trans->Commit(); } $this->OutputUpdateFeaturesResult($commands, $result, $classDef, $fmt == "json"); } catch (MgException $ex) { if ($trans != null) { $trans->Rollback(); } $this->OnException($ex, $mimeType); } }
/** * Handles PUT requests for this adapter. Overridable. Does nothing if not overridden. */ public function HandlePut($single) { $trans = null; try { $tokens = explode(":", $this->className); $schemaName = $tokens[0]; $className = $tokens[1]; if ($this->app->REQUEST_BODY_DOCUMENT == null) { $doc = new DOMDocument(); $doc->loadXML($this->app->request->getBody()); } else { $doc = $this->app->REQUEST_BODY_DOCUMENT; } $commands = new MgFeatureCommandCollection(); $classDef = $this->featSvc->GetClassDefinition($this->featureSourceId, $schemaName, $className); $filter = ""; //If single-record, infer filter from URI if ($single === true) { $idProps = $classDef->GetIdentityProperties(); if ($idProps->GetCount() != 1) { $app->halt(400, $this->app->localizer->getText("E_CANNOT_APPLY_UPDATE_CANNOT_UNIQUELY_IDENTIFY", $this->featureId, $idProps->GetCount())); } else { $idProp = $idProps->GetItem(0); if ($idProp->GetDataType() == MgPropertyType::String) { $filter = $idProp->GetName() . " = '" . $this->featureId . "'"; } else { $filter = $idProp->GetName() . " = " . $this->featureId; } } } else { //Otherwise, use the filter from the request envelope (if specified) $filterNode = $doc->getElementsByTagName("Filter"); if ($filterNode->length == 1) { $filter = $filterNode->item(0)->nodeValue; } } $classDef = $this->featSvc->GetClassDefinition($this->featureSourceId, $schemaName, $className); $props = MgUtils::ParseSingleFeatureDocument($this->app, $classDef, $doc, "UpdateProperties"); $updateCmd = new MgUpdateFeatures("{$schemaName}:{$className}", $props, $filter); $commands->Add($updateCmd); if ($this->useTransaction) { $trans = $this->featSvc->BeginTransaction($this->featureSourceId); } //HACK: Due to #2252, we can't call UpdateFeatures() with NULL MgTransaction, so to workaround //that we call the original UpdateFeatures() overload with useTransaction = false if we find a //NULL MgTransaction if ($trans == null) { $result = $this->featSvc->UpdateFeatures($this->featureSourceId, $commands, false); } else { $result = $this->featSvc->UpdateFeatures($this->featureSourceId, $commands, $trans); } if ($trans != null) { $trans->Commit(); } $this->OutputUpdateFeaturesResult($commands, $result, $classDef); } catch (MgException $ex) { if ($trans != null) { $trans->Rollback(); } $this->OnException($ex); } }