瀏覽代碼

Fixed bugs on CSV import (was not working fine when exporting/importing incident tickets)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@316 a333f486-631f-4898-b8df-5754b55c2be0
romainq 15 年之前
父節點
當前提交
2dca0bdb84
共有 4 個文件被更改,包括 64 次插入43 次删除
  1. 7 25
      core/attributedef.class.inc.php
  2. 26 15
      core/bulkchange.class.inc.php
  3. 8 2
      core/csvparser.class.inc.php
  4. 23 1
      core/dbobject.class.php

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

@@ -130,6 +130,7 @@ abstract class AttributeDefinition
 	public function IsExternalField() {return false;} 
 	public function IsWritable() {return false;} 
 	public function IsNullAllowed() {return true;} 
+	public function GetNullValue() {return null;} 
 	public function GetCode() {return $this->m_sCode;} 
 	public function GetLabel() {return $this->Get("label");} 
 	public function GetDescription() {return $this->Get("description");} 
@@ -328,7 +329,6 @@ class AttributeDBFieldVoid extends AttributeDefinition
 	public function IsNullAllowed() {return false;}
 
 	protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
-	protected function SQLToScalar($value) {return $value;} // take the result of a fetch... and make it a PHP variable
 
 	public function GetSQLExpressions()
 	{
@@ -483,12 +483,6 @@ class AttributeInteger extends AttributeDBField
 		assert(is_numeric($value));
 		return $value; // supposed to be an int
 	}
-	public function SQLToScalar($value)
-	{
-		// Use cast (int) or intval() ?
-		return (int)$value;
-		
-	}
 }
 
 /**
@@ -525,11 +519,6 @@ class AttributeBoolean extends AttributeInteger
 		if ($value) return 1;
 		return 0;
 	}
-	public function SQLToScalar($value)
-	{
-		// Use cast (int) or intval() ?
-		return (int)$value;
-	}
 }
 
 /**
@@ -610,10 +599,6 @@ class AttributeString extends AttributeDBField
 		}
 		return $value;
 	}
-	public function SQLToScalar($value)
-	{
-		return $value;
-	}
 
 	public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
 	{
@@ -986,10 +971,6 @@ class AttributeDate extends AttributeDBField
 		}
 		return $value;
 	}
-	public function SQLToScalar($value)
-	{
-		return $value;
-	}
 
 	public function GetAsHTML($value)
 	{
@@ -1047,6 +1028,7 @@ class AttributeExternalKey extends AttributeDBFieldVoid
 
 	public function GetDefaultValue() {return 0;}
 	public function IsNullAllowed() {return $this->Get("is_null_allowed");}
+	public function GetNullValue() {return 0;} 
 
 	public function GetBasicFilterOperators()
 	{
@@ -1093,6 +1075,11 @@ class AttributeExternalKey extends AttributeDBFieldVoid
 	{
 		return $this->Get("on_target_delete");
 	}
+
+	public function MakeRealValue($proposedValue)
+	{
+		return (int)$proposedValue;
+	}
 }
 
 /**
@@ -1226,11 +1213,6 @@ class AttributeExternalField extends AttributeDefinition
 		$oExtAttDef = $this->GetExtAttDef();
 		return $oExtAttDef->ScalarToSQL($value);
 	}
-	public function SQLToScalar($value)
-	{
-		$oExtAttDef = $this->GetExtAttDef();
-		return $oExtAttDef->SQLToScalar($value);
-	}
 
 
 	// Do not overload GetSQLExpression here because this is handled in the joins

+ 26 - 15
core/bulkchange.class.inc.php

@@ -244,15 +244,16 @@ class BulkChange
 
 	static protected function MakeSpecObject($sClass, $iId)
 	{
-		$oObj = MetaModel::GetObject($sClass, $iId);
-		if (is_null($oObj))
+		try
 		{
-			return $iId;
+			$oObj = MetaModel::GetObject($sClass, $iId);
 		}
-		else
+		catch(CoreException $e)
 		{
-			return $oObj;
+			// in case an ext key is 0 (which is currently acceptable)
+			return $iId;
 		}
+		return $oObj;
 	}
 
 	protected function PrepareObject(&$oTargetObj, $aRowData, &$aErrors)
@@ -276,18 +277,33 @@ class BulkChange
 			switch($oExtObjects->Count())
 			{
 			case 0:
-				$aErrors[$sAttCode] = "Object not found";
-				$aResults[$sAttCode]= new CellChangeSpec_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found - check the spelling (no space before/after)');
+				if ($oExtKey->IsNullAllowed())
+				{
+					$oTargetObj->Set($sAttCode, $oExtKey->GetNullValue());
+				}
+				else
+				{
+					$aErrors[$sAttCode] = "Object not found";
+					$aResults[$sAttCode]= new CellChangeSpec_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found - check the spelling (no space before/after)');
+				}
 				break;
 			case 1:
 				// Do change the external key attribute
 				$oForeignObj = $oExtObjects->Fetch();
 				$oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
-	
-				// Report it
+				break;
+			default:
+				$aErrors[$sAttCode] = "Found ".$oExtObjects->Count()." matches";
+				$previousValue = self::MakeSpecObject($oExtKey->GetTargetClass(), $oTargetObj->Get($sAttCode));
+				$aResults[$sAttCode]= new CellChangeSpec_Issue(null, $previousValue, "Found ".$oExtObjects->Count()." matches");
+			}
+
+			// Report
+			if (!array_key_exists($sAttCode, $aResults))
+			{
+				$oForeignObj = $oTargetObj->Get($sAttCode);
 				if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
 				{
-
 					if ($oTargetObj->IsNew())
 					{
 						$aResults[$sAttCode]= new CellChangeSpec_Init($oForeignObj);
@@ -302,11 +318,6 @@ class BulkChange
 				{
 					$aResults[$sAttCode]= new CellChangeSpec_Unchanged($oForeignObj);
 				}
-				break;
-			default:
-				$aErrors[$sAttCode] = "Found ".$oExtObjects->Count()." matches";
-				$previousValue = self::MakeSpecObject($oExtKey->GetTargetClass(), $oTargetObj->Get($sAttCode));
-				$aResults[$sAttCode]= new CellChangeSpec_Issue(null, $previousValue, "Found ".$oExtObjects->Count()." matches");
 			}
 		}	
 	

+ 8 - 2
core/csvparser.class.inc.php

@@ -91,9 +91,15 @@ class CSVParser
 		{
 			$this->m_aDataSet[] = $this->m_aCurrRow;
 		}
-		elseif ((count($this->m_aCurrRow) == 1) && (strlen($this->m_aCurrRow[0]) > 0))
+		elseif (count($this->m_aCurrRow) == 1)
 		{
-			$this->m_aDataSet[] = $this->m_aCurrRow;
+			// Get the unique value
+			$aValues = array_values($this->m_aCurrRow);
+			$sValue = $aValues[0]; 
+			if (strlen($sValue) > 0)
+			{
+				$this->m_aDataSet[] = $this->m_aCurrRow;
+			}
 		}
 		else
 		{

+ 23 - 1
core/dbobject.class.php

@@ -568,10 +568,32 @@ abstract class DBObject
 		$aDelta = array();
 		foreach ($aProposal as $sAtt => $proposedValue)
 		{
-			if (!array_key_exists($sAtt, $this->m_aOrigValues) || ($this->m_aOrigValues[$sAtt] !== $proposedValue))
+			if (!array_key_exists($sAtt, $this->m_aOrigValues))
 			{
+				// The value was not set
 				$aDelta[$sAtt] = $proposedValue;
 			}
+			elseif(is_object($proposedValue))
+			{
+				// The value is an object, the comparison is not strict
+				// #@# todo - should be even less strict => add verb on AttributeDefinition: Compare($a, $b)
+				if ($this->m_aOrigValues[$sAtt] != $proposedValue)
+				{
+					$aDelta[$sAtt] = $proposedValue;
+				}
+			}
+			else
+			{
+				// The value is a scalar, the comparison must be 100% strict
+				if($this->m_aOrigValues[$sAtt] !== $proposedValue)
+				{	
+					//echo "$sAtt:<pre>\n";
+					//var_dump($this->m_aOrigValues[$sAtt]);
+					//var_dump($proposedValue);
+					//echo "</pre>\n";
+					$aDelta[$sAtt] = $proposedValue;
+				}
+			}
 		}
 		return $aDelta;
 	}