public function InsertFeatures($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, false, $mimeType); $siteConn = new MgSiteConnection(); $siteConn->Open($this->userInfo); $site = $siteConn->GetSite(); $this->VerifyWhitelist($resId->ToString(), $mimeType, "INSERTFEATURES", $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->AllowInsert === 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); $commands = new MgFeatureCommandCollection(); $classDef = $featSvc->GetClassDefinition($resId, $schemaName, $className); $batchProps = MgUtils::ParseMultiFeatureXml($this->app, $classDef, $body); $insertCmd = new MgInsertFeatures("{$schemaName}:{$className}", $batchProps); $commands->Add($insertCmd); 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 POST requests for this adapter. Overridable. Does nothing if not overridden. */ public function HandlePost($single) { $trans = null; try { $tokens = explode(":", $this->className); $schemaName = $tokens[0]; $className = $tokens[1]; $commands = new MgFeatureCommandCollection(); $classDef = $this->featSvc->GetClassDefinition($this->featureSourceId, $schemaName, $className); if ($this->app->REQUEST_BODY_DOCUMENT != null) { $batchProps = MgUtils::ParseMultiFeatureDocument($this->app, $classDef, $this->app->REQUEST_BODY_DOCUMENT); } else { $batchProps = MgUtils::ParseMultiFeatureXml($this->app, $classDef, $this->app->request->getBody()); } $insertCmd = new MgInsertFeatures("{$schemaName}:{$className}", $batchProps); $commands->Add($insertCmd); 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); } }