Selaa lähdekoodia

#877 REST/JSON More flexibility on case log updates (in particular, it is now possible to write the entire case log), remains compatible with the previous API

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3078 a333f486-631f-4898-b8df-5754b55c2be0
romainq 11 vuotta sitten
vanhempi
commit
372f60bf32

+ 16 - 2
application/applicationextension.inc.php

@@ -944,7 +944,14 @@ class RestUtils
 		foreach ($aFields as $sAttCode => $value)
 		foreach ($aFields as $sAttCode => $value)
 		{
 		{
 			$realValue = self::MakeValue($sClass, $sAttCode, $value);
 			$realValue = self::MakeValue($sClass, $sAttCode, $value);
-			$oObject->Set($sAttCode, $realValue);
+			try
+			{
+				$oObject->Set($sAttCode, $realValue);
+			}
+			catch (Exception $e)
+			{
+				throw new Exception("$sAttCode: ".$e->getMessage(), $e->getCode());
+			}
 		}
 		}
 		return $oObject;
 		return $oObject;
 	}
 	}
@@ -964,7 +971,14 @@ class RestUtils
 		foreach ($aFields as $sAttCode => $value)
 		foreach ($aFields as $sAttCode => $value)
 		{
 		{
 			$realValue = self::MakeValue($sClass, $sAttCode, $value);
 			$realValue = self::MakeValue($sClass, $sAttCode, $value);
-			$oObject->Set($sAttCode, $realValue);
+			try
+			{
+				$oObject->Set($sAttCode, $realValue);
+			}
+			catch (Exception $e)
+			{
+				throw new Exception("$sAttCode: ".$e->getMessage(), $e->getCode());
+			}
 		}
 		}
 		return $oObject;
 		return $oObject;
 	}
 	}

+ 40 - 7
core/attributedef.class.inc.php

@@ -1915,7 +1915,12 @@ class AttributeCaseLog extends AttributeLongText
 	// Facilitate things: allow the user to Set the value from a string
 	// Facilitate things: allow the user to Set the value from a string
 	public function MakeRealValue($proposedValue, $oHostObj)
 	public function MakeRealValue($proposedValue, $oHostObj)
 	{
 	{
-		if (!($proposedValue instanceof ormCaseLog))
+		if ($proposedValue instanceof ormCaseLog)
+		{
+			// Passthrough
+			$ret = $proposedValue;
+		}
+		else
 		{
 		{
 			// Append the new value if an instance of the object is supplied
 			// Append the new value if an instance of the object is supplied
 			//
 			//
@@ -1937,13 +1942,21 @@ class AttributeCaseLog extends AttributeLongText
 			{
 			{
 				$oCaseLog = new ormCaseLog();
 				$oCaseLog = new ormCaseLog();
 			}
 			}
-			if (strlen($proposedValue) > 0)
+
+			if ($proposedValue instanceof stdClass)
+			{
+				$oCaseLog->AddLogEntryFromJSON($proposedValue);
+			}
+			else
 			{
 			{
-				$oCaseLog->AddLogEntry(parent::MakeRealValue($proposedValue, $oHostObj));
+				if (strlen($proposedValue) > 0)
+				{
+					$oCaseLog->AddLogEntry(parent::MakeRealValue($proposedValue, $oHostObj));
+				}
 			}
 			}
-			return $oCaseLog;
+			$ret = $oCaseLog;
 		}
 		}
-		return $proposedValue;
+		return $ret;
 	}
 	}
 
 
 	public function GetSQLExpressions($sPrefix = '')
 	public function GetSQLExpressions($sPrefix = '')
@@ -2077,8 +2090,28 @@ class AttributeCaseLog extends AttributeLongText
 	 */	 	
 	 */	 	
 	public function FromJSONToValue($json)
 	public function FromJSONToValue($json)
 	{
 	{
-		// Passthrough: new text to append to the log
-		return $json;
+		if (is_string($json))
+		{
+			// Will be correctly handled in MakeRealValue
+			$ret = $json;
+		}
+		else
+		{
+			if (isset($json->add_item))
+			{
+				// Will be correctly handled in MakeRealValue
+				$ret = $json->add_item;
+				if (!isset($ret->message))
+				{
+					throw new Exception("Missing mandatory entry: 'message'");
+				}
+			}
+			else
+			{
+				$ret = ormCaseLog::FromJSON($json);
+			}
+		}
+		return $ret;
 	}
 	}
 }
 }
 
 

+ 71 - 1
core/ormcaselog.class.inc.php

@@ -48,6 +48,20 @@ class ormCaseLog {
 		return $this->m_sLog;
 		return $this->m_sLog;
 	}
 	}
 	
 	
+	public static function FromJSON($oJson)
+	{
+		if (!isset($oJson->items))
+		{
+			throw new Exception("Missing 'items' elements");
+		}
+		$oCaseLog = new ormCaseLog();
+		foreach($oJson->items as $oItem)
+		{
+			$oCaseLog->AddLogEntryFromJSON($oItem);
+		}
+		return $oCaseLog;
+	}
+
 	/**
 	/**
 	 * Return a value that will be further JSON encoded	
 	 * Return a value that will be further JSON encoded	
 	 */	
 	 */	
@@ -86,6 +100,7 @@ class ormCaseLog {
 			$aEntries[] = array(
 			$aEntries[] = array(
 				'date' => $sDate,
 				'date' => $sDate,
 				'user_login' => $this->m_aIndex[$index]['user_name'],
 				'user_login' => $this->m_aIndex[$index]['user_name'],
+				'user_id' => $this->m_aIndex[$index]['user_id'],
 				'message' => $sTextEntry
 				'message' => $sTextEntry
 			);
 			);
 		}
 		}
@@ -289,7 +304,62 @@ class ormCaseLog {
 		}
 		}
 		$this->m_bModified = true;
 		$this->m_bModified = true;
 	}
 	}
-	
+
+
+	public function AddLogEntryFromJSON($oJson)
+	{
+		$sText = isset($oJson->message) ? $oJson->message : '';
+
+		if (isset($oJson->user_id))
+		{
+			if (!UserRights::IsAdministrator())
+			{
+				throw new Exception("Only administrators can set the user id", RestResult::UNAUTHORIZED);
+			}
+			try
+			{
+				$oUser = RestUtils::FindObjectFromKey('User', $oJson->user_id);
+			}
+			catch(Exception $e)
+			{
+				throw new Exception('user_id: '.$e->getMessage(), $e->getCode());
+			}
+			$iUserId = $oUser->GetKey();
+			$sOnBehalfOf = $oUser->GetFriendlyName();
+		}
+		else
+		{
+			$iUserId = UserRights::GetUserId();
+			$sOnBehalfOf = UserRights::GetUserFriendlyName();
+		}
+		
+		if (isset($oJson->date))
+		{
+			$oDate = new DateTime($oJson->date);
+			$iDate = (int) $oDate->format('U');
+		}
+		else
+		{
+			$iDate = time();
+		}
+		$sDate = date(Dict::S('UI:CaseLog:DateFormat'), $iDate);
+
+		$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
+		$iSepLength = strlen($sSeparator);
+		$iTextlength = strlen($sText);
+		$this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
+		$this->m_aIndex[] = array(
+			'user_name' => $sOnBehalfOf,	
+			'user_id' => $iUserId,	
+			'date' => $iDate,	
+			'text_length' => $iTextlength,	
+			'separator_length' => $iSepLength,	
+		);
+
+		$this->m_bModified = true;
+	}
+
+
 	public function GetModifiedEntry()
 	public function GetModifiedEntry()
 	{
 	{
 		$sModifiedEntry = '';
 		$sModifiedEntry = '';