From f454ebf4b5965835b69142194d6710e844391363 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Fri, 12 Mar 2010 13:48:39 +0000 Subject: [PATCH] Fixed bugs on CSV import (was not working fine when exporting/importing incident tickets) SVN:trunk[316] --- core/attributedef.class.inc.php | 32 ++++++------------------ core/bulkchange.class.inc.php | 43 +++++++++++++++++++++------------ core/csvparser.class.inc.php | 10 ++++++-- core/dbobject.class.php | 24 +++++++++++++++++- 4 files changed, 65 insertions(+), 44 deletions(-) diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index 328516a2b..94d4abfa7 100644 --- a/core/attributedef.class.inc.php +++ b/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 diff --git a/core/bulkchange.class.inc.php b/core/bulkchange.class.inc.php index f2641afeb..8bafa10c2 100644 --- a/core/bulkchange.class.inc.php +++ b/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 { + $oObj = MetaModel::GetObject($sClass, $iId); + } + catch(CoreException $e) + { + // in case an ext key is 0 (which is currently acceptable) return $iId; } - else - { - return $oObj; - } + 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"); } } diff --git a/core/csvparser.class.inc.php b/core/csvparser.class.inc.php index 20b952427..fe0662b61 100644 --- a/core/csvparser.class.inc.php +++ b/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 { diff --git a/core/dbobject.class.php b/core/dbobject.class.php index 07c498b44..7dbc94de9 100644 --- a/core/dbobject.class.php +++ b/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:
\n";
+					//var_dump($this->m_aOrigValues[$sAtt]);
+					//var_dump($proposedValue);
+					//echo "
\n"; + $aDelta[$sAtt] = $proposedValue; + } + } } return $aDelta; }