#925 REST/JSON: Added an option to output all the fields of the object found (not only the fields of the queried class)

SVN:trunk[3163]
This commit is contained in:
Romain Quetiez
2014-05-20 08:15:53 +00:00
parent b0a84f96f1
commit 3e79dad435
2 changed files with 40 additions and 11 deletions

View File

@@ -715,7 +715,7 @@ class RestUtils
* @param string $sClass Name of the class
* @param StdClass $oData Structured input data.
* @param string $sParamName Name of the parameter to fetch from the input data
* @return void
* @return An array of class => list of attributes (see RestResultWithObjects::AddObject that uses it)
* @throws Exception
* @api
*/
@@ -727,7 +727,17 @@ class RestUtils
{
foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
{
$aShowFields[] = $sAttCode;
$aShowFields[$sClass][] = $sAttCode;
}
}
elseif ($sFields == '*+')
{
foreach (MetaModel::EnumChildClasses($sClass, ENUM_CHILD_CLASSES_ALL) as $sRefClass)
{
foreach (MetaModel::ListAttributeDefs($sRefClass) as $sAttCode => $oAttDef)
{
$aShowFields[$sRefClass][] = $sAttCode;
}
}
}
else
@@ -739,7 +749,7 @@ class RestUtils
{
throw new Exception("$sParamName: invalid attribute code '$sAttCode'");
}
$aShowFields[] = $sAttCode;
$aShowFields[$sClass][] = $sAttCode;
}
}
return $aShowFields;

View File

@@ -143,15 +143,35 @@ class RestResultWithObjects extends RestResult
* @param int An error code (RestResult::OK is no issue has been found)
* @param string $sMessage Description of the error if any, an empty string otherwise
* @param DBObject $oObject The object being reported
* @param array $aFields An array of attribute codes. List of the attributes to be reported.
* @param array $aFieldSpec An array of class => attribute codes (Cf. RestUtils::GetFieldList). List of the attributes to be reported.
* @return void
*/
public function AddObject($iCode, $sMessage, $oObject, $aFields)
public function AddObject($iCode, $sMessage, $oObject, $aFieldSpec = null)
{
$oObjRes = new ObjectResult(get_class($oObject), $oObject->GetKey());
$sClass = get_class($oObject);
$oObjRes = new ObjectResult($sClass, $oObject->GetKey());
$oObjRes->code = $iCode;
$oObjRes->message = $sMessage;
$aFields = null;
if (!is_null($aFieldSpec))
{
// Enum all classes in the hierarchy, starting with the current one
foreach (MetaModel::EnumParentClasses($sClass, ENUM_PARENT_CLASSES_ALL, false) as $sRefClass)
{
if (array_key_exists($sRefClass, $aFieldSpec))
{
$aFields = $aFieldSpec[$sRefClass];
break;
}
}
}
if (is_null($aFields))
{
// No fieldspec given, or not found...
$aFields = array('id', 'friendlyname');
}
foreach ($aFields as $sAttCode)
{
$oObjRes->AddField($oObject, $sAttCode);
@@ -388,7 +408,6 @@ class CoreServices implements iRestServiceProvider
$key = RestUtils::GetMandatoryParam($aParams, 'key');
$sRelation = RestUtils::GetMandatoryParam($aParams, 'relation');
$iMaxRecursionDepth = RestUtils::GetOptionalParam($aParams, 'depth', 20 /* = MAX_RECURSION_DEPTH */);
$aShowFields = array('id', 'friendlyname');
$oObjectSet = RestUtils::GetObjectSetFromKey($sClass, $key);
$aIndexByClass = array();
@@ -397,7 +416,7 @@ class CoreServices implements iRestServiceProvider
$aRelated = array();
$aGraph = array();
$aIndexByClass[get_class($oObject)][$oObject->GetKey()] = null;
$oResult->AddObject(0, '', $oObject, $aShowFields);
$oResult->AddObject(0, '', $oObject);
$this->GetRelatedObjects($oObject, $sRelation, $iMaxRecursionDepth, $aRelated, $aGraph);
foreach($aRelated as $sClass => $aObjects)
@@ -405,7 +424,7 @@ class CoreServices implements iRestServiceProvider
foreach($aObjects as $oRelatedObj)
{
$aIndexByClass[get_class($oRelatedObj)][$oRelatedObj->GetKey()] = null;
$oResult->AddObject(0, '', $oRelatedObj, $aShowFields);
$oResult->AddObject(0, '', $oRelatedObj);
}
}
foreach($aGraph as $sSrcKey => $aDestinations)
@@ -503,7 +522,7 @@ class CoreServices implements iRestServiceProvider
$sPlanned = 'Must be deleted explicitely';
}
}
$oResult->AddObject($iCode, $sPlanned, $oToDelete, array('id', 'friendlyname'));
$oResult->AddObject($iCode, $sPlanned, $oToDelete);
}
}
foreach ($oDeletionPlan->ListUpdates() as $sRemoteClass => $aToUpdate)
@@ -521,7 +540,7 @@ class CoreServices implements iRestServiceProvider
$iCode = RestDelete::AUTO_UPDATE;
$sPlanned = 'Reset external keys: '.$aData['attributes_list'];
}
$oResult->AddObject($iCode, $sPlanned, $oToUpdate, array('id', 'friendlyname'));
$oResult->AddObject($iCode, $sPlanned, $oToUpdate);
}
}