浏览代码

Trac #51 - Implemented external key feed by the mean of reconciliation on external fields

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@206 a333f486-631f-4898-b8df-5754b55c2be0
romainq 15 年之前
父节点
当前提交
4c40dba4f7
共有 2 个文件被更改,包括 31 次插入6 次删除
  1. 2 2
      core/bulkchange.class.inc.php
  2. 29 4
      webservices/import.php

+ 2 - 2
core/bulkchange.class.inc.php

@@ -229,7 +229,8 @@ class BulkChange
 	protected $m_sClass; 
 	protected $m_aData; // Note: hereafter, iCol maybe actually be any acceptable key (string)
 	// #@# todo: rename the variables to sColIndex
-	protected $m_aAttList; // attcode => iCol protected $m_aReconcilKeys;// iCol => attcode (attcode = 'id' for the pkey) 
+	protected $m_aAttList; // attcode => iCol
+	protected $m_aReconcilKeys;// iCol => attcode (attcode = 'id' for the pkey) 
 	protected $m_aExtKeys; // aExtKeys[sExtKeyAttCode][sExtReconcKeyAttCode] = iCol;
 
 	public function __construct($sClass, $aData, $aAttList, $aReconcilKeys, $aExtKeys)
@@ -268,7 +269,6 @@ class BulkChange
 			foreach ($aKeyConfig as $sForeignAttCode => $iCol)
 			{
 				// The foreign attribute is one of our reconciliation key
-				$sFieldId = MakeExtFieldSelectValue($sAttCode, $sForeignAttCode);
 				$oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
 				$aResults["col$iCol"] = new CellChangeSpec_Void($aRowData[$iCol]);
 			}

+ 29 - 4
webservices/import.php

@@ -15,7 +15,7 @@
 //
 // Known limitations
 // - output still in html, because the errors are not displayed in xml
-// - could not set the external keys by the mean of a reconciliation (the exact key must be given)
+// - only external fields attributes could be used as reconciliation keys for external keys
 // - reconciliation is made on the first column
 // - no option to force 'always create' or 'never create'
 //
@@ -35,6 +35,11 @@ require_once('../application/xmlpage.class.inc.php');
 require_once('../application/startup.inc.php');
 
 require_once('../application/loginwebpage.class.inc.php');
+
+class WebServiceException extends Exception
+{
+}
+
 login_web_page::DoLogin(); // Check user rights and prompt if needed
 
 $oContext = new UserContext();
@@ -46,6 +51,7 @@ $oAppContext = new ApplicationContext();
 
 //$oP = new XMLPage("iTop - Bulk import");
 $oP = new web_page("iTop - Bulk import");
+$oP->add('<warning>This is a prototype, I repeat: PRO-TO-TYPE, therefore it suffers bugs and limitations, documented in the code. Next step: specify...</warning>');		
 try
 {
 	$sClass = utils::ReadParam('class', '');
@@ -59,11 +65,30 @@ try
 	// 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)
+	$aExtKeys = array();
+	foreach($sRawFieldList as $iFieldId => $sFieldName)
 	{
-		$aAttList[$sFieldName] = $iField;
+		if (!MetaModel::IsValidAttCode($sClass, $sFieldName))
+		{
+			throw new WebServiceException("Unknown attribute '$sFieldName' (class: '$sClass')");
+		}
+		$oAtt = MetaModel::GetAttributeDef($sClass, $sFieldName);
+		if ($oAtt->IsExternalKey())
+		{
+			$aExtKeys[$sFieldName]['id'] = $iFieldId;
+			$aAttList[$sFieldName] = $iFieldId;
+		}
+		elseif ($oAtt->IsExternalField())
+		{
+			$sExtKeyAttCode = $oAtt->GetKeyAttCode();
+			$sRemoteAttCode = $oAtt->GetExtAttCode();
+			$aExtKeys[$sExtKeyAttCode][$sRemoteAttCode] = $iFieldId;
+		}
+		else
+		{
+			$aAttList[$sFieldName] = $iFieldId;
+		}
 	}
-	$aExtKeys = array();
 
 	// Limitation: the reconciliation key is the first attribute
 	$aReconcilKeys = array($sRawFieldList[0]);