From 8a39e816a54d090051da52c4c71f41207f312d2a Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Tue, 14 Apr 2009 13:33:22 +0000 Subject: [PATCH] Created the web services folder, and draft for the import service SVN:code[32] --- webservices/import.php | 157 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 webservices/import.php diff --git a/webservices/import.php b/webservices/import.php new file mode 100644 index 0000000000..1d0caeb0a5 --- /dev/null +++ b/webservices/import.php @@ -0,0 +1,157 @@ +SetSeparator($sSep); + $oCSVParser->SetSkipLines(1); + + // Limitation: as the attribute list is in the first line, we can not match external key by an third-party attribute + $sRawFieldList = $oCSVParser->ListFields(); + $aAttList = array(); + foreach($sRawFieldList as $iField => $sFieldName) + { + $aAttList[$sFieldName] = $iField; + } + $aExtKeys = array(); + + // Limitation: the reconciliation key is the first attribute + $aReconcilKeys = array($sRawFieldList[0]); + +// print_r($oCSVParser->ListFields()); +// print_r($oCSVParser->ToArray($oCSVParser->ListFields())); + + $aData = $oCSVParser->ToArray(); + $oBulk = new BulkChange( + $sClass, + $aData, + $aAttList, + $aReconcilKeys, + $aExtKeys + ); + + $oMyChange = MetaModel::NewObject("CMDBChange"); + $oMyChange->Set("date", time()); + $oMyChange->Set("userinfo", "CSV Import Web Service"); + $iChangeId = $oMyChange->DBInsert(); + + $aRes = $oBulk->Process($oMyChange); + + // Setup result presentation + // + $aDisplayConfig = array(); + $aDisplayConfig["__RECONCILIATION__"] = array("label"=>"Reconciliation", "description"=>""); + $aDisplayConfig["__STATUS__"] = array("label"=>"Status", "description"=>""); + if (isset($iPKeyId)) + { + $aDisplayConfig["col$iPKeyId"] = array("label"=>"pkey", "description"=>""); + } + foreach($aReconcilKeys as $iCol => $sAttCode) + { + $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel(); + $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>""); + } + foreach ($aAttList as $sAttCode => $iCol) + { + $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel(); + $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>""); + } + + + $aResultDisp = array(); // to be displayed + foreach($aRes as $iRow => $aRowData) + { + $aRowDisp = array(); + $aRowDisp["__RECONCILIATION__"] = $aRowData["__RECONCILIATION__"]; + $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription(); + foreach($aRowData as $sKey => $value) + { + if ($sKey == '__RECONCILIATION__') continue; + if ($sKey == '__STATUS__') continue; + + switch (get_class($value)) + { + case 'CellChangeSpec_Void': + $sClass = ''; + break; + case 'CellChangeSpec_Unchanged': + $sClass = ''; + break; + case 'CellChangeSpec_Modify': + $sClass = 'csvimport_ok'; + break; + case 'CellChangeSpec_Issue': + $sClass = 'csvimport_error'; + break; + } + if (empty($sClass)) + { + $aRowDisp[$sKey] = $value->GetDescription(); + } + else + { + $aRowDisp[$sKey] = "
".$value->GetDescription()."
"; + } + } + $aResultDisp[$iRow] = $aRowDisp; + } + $oP->table($aDisplayConfig, $aResultDisp); + +} +catch(Exception $e) +{ + $oP->add(''.((string)$e).''); +} + +$oP->output(); +?>