From ddff9180acf5f709e20b0c7e194a601ce21bc9a2 Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Wed, 27 Feb 2013 15:58:16 +0000 Subject: [PATCH] Added 'core/apply_stimulus' as a possible operation for the REST web services. SVN:trunk[2605] --- core/restservices.class.inc.php | 59 ++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/core/restservices.class.inc.php b/core/restservices.class.inc.php index 20f858315..7a7a49a0a 100644 --- a/core/restservices.class.inc.php +++ b/core/restservices.class.inc.php @@ -192,6 +192,10 @@ class CoreServices implements iRestServiceProvider 'verb' => 'core/update', 'description' => 'Update an object' ); + $aOps[] = array( + 'verb' => 'core/apply_stimulus', + 'description' => 'Apply a stimulus to change the state of an object' + ); $aOps[] = array( 'verb' => 'core/get', 'description' => 'Search for objects' @@ -237,7 +241,60 @@ class CoreServices implements iRestServiceProvider $oResult->AddObject(0, 'updated', $oObject, $aShowFields); break; - case 'core/get': + case 'core/apply_stimulus': + RestUtils::InitTrackingComment($aParams); + $sClass = RestUtils::GetClass($aParams, 'class'); + $key = RestUtils::GetMandatoryParam($aParams, 'key'); + $aFields = RestUtils::GetMandatoryParam($aParams, 'fields'); + $aShowFields = RestUtils::GetFieldList($sClass, $aParams, 'output_fields'); + $sStimulus = RestUtils::GetMandatoryParam($aParams, 'stimulus'); + + $oObject = RestUtils::FindObjectFromKey($sClass, $key); + RestUtils::UpdateObjectFromFields($oObject, $aFields); + + $aTransitions = $oObject->EnumTransitions(); + $aStimuli = MetaModel::EnumStimuli(get_class($oObject)); + if (!isset($aTransitions[$sStimulus])) + { + // Invalid stimulus + $oResult->code = RestResult::INTERNAL_ERROR; + $oResult->message = "Invalid stimulus: '$sStimulus' on the object ".$oObject->GetName()." in state '".$oObject->GetState()."'"; + } + else + { + $aTransition = $aTransitions[$sStimulus]; + $sTargetState = $aTransition['target_state']; + $aStates = MetaModel::EnumStates($sClass); + $aTargetStateDef = $aStates[$sTargetState]; + $aExpectedAttributes = $aTargetStateDef['attribute_list']; + + $aMissingMandatory = array(); + foreach($aExpectedAttributes as $sAttCode => $iExpectCode) + { + if ( ($iExpectCode & OPT_ATT_MANDATORY) && ($oObject->Get($sAttCode) == '')) + { + $aMissingMandatory[] = $sAttCode; + } + } + if (count($aMissingMandatory) == 0) + { + // If all the mandatory fields are already present, just apply the transition silently... + if ($oObject->ApplyStimulus($sStimulus)) + { + $oObject->DBUpdate(); + $oResult->AddObject(0, 'updated', $oObject, $aShowFields); + } + } + else + { + // Missing mandatory attributes for the transition + $oResult->code = RestResult::INTERNAL_ERROR; + $oResult->message = 'Missing mandatory attribute(s) for applying the stimulus: '.implode(', ', $aMissingMandatory).'.'; + } + } + break; + + case 'core/get': $sClass = RestUtils::GetClass($aParams, 'class'); $key = RestUtils::GetMandatoryParam($aParams, 'key'); $aShowFields = RestUtils::GetFieldList($sClass, $aParams, 'output_fields');