synchrodatasource.class.inc.php 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Data Exchange - synchronization with external applications (incoming data)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. class SynchroExceptionNotStarted extends CoreException
  25. {
  26. }
  27. class SynchroDataSource extends cmdbAbstractObject
  28. {
  29. public static function Init()
  30. {
  31. $aParams = array
  32. (
  33. "category" => "core/cmdb,view_in_gui",
  34. "key_type" => "autoincrement",
  35. "name_attcode" => array('name'),
  36. "state_attcode" => "",
  37. "reconc_keys" => array(),
  38. "db_table" => "priv_sync_datasource",
  39. "db_key_field" => "id",
  40. "db_finalclass_field" => "realclass",
  41. "display_template" => "",
  42. "icon" => "../images/synchro.png",
  43. );
  44. MetaModel::Init_Params($aParams);
  45. //MetaModel::Init_InheritAttributes();
  46. MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  47. MetaModel::Init_AddAttribute(new AttributeString("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  48. MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('implementation,production,obsolete'), "sql"=>"status", "default_value"=>"implementation", "is_null_allowed"=>false, "depends_on"=>array())));
  49. MetaModel::Init_AddAttribute(new AttributeExternalKey("user_id", array("targetclass"=>"User", "jointype"=>null, "allowed_values"=>null, "sql"=>"user_id", "is_null_allowed"=>true, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  50. MetaModel::Init_AddAttribute(new AttributeClass("scope_class", array("class_category"=>"bizmodel", "more_values"=>"", "sql"=>"scope_class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  51. // Declared here for a future usage, but ignored so far
  52. MetaModel::Init_AddAttribute(new AttributeString("scope_restriction", array("allowed_values"=>null, "sql"=>"scope_restriction", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  53. //MetaModel::Init_AddAttribute(new AttributeDateTime("last_synchro_date", array("allowed_values"=>null, "sql"=>"last_synchro_date", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  54. // Format: seconds (int)
  55. MetaModel::Init_AddAttribute(new AttributeDuration("full_load_periodicity", array("allowed_values"=>null, "sql"=>"full_load_periodicity", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  56. // MetaModel::Init_AddAttribute(new AttributeString("reconciliation_list", array("allowed_values"=>null, "sql"=>"reconciliation_list", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  57. MetaModel::Init_AddAttribute(new AttributeEnum("reconciliation_policy", array("allowed_values"=>new ValueSetEnum('use_primary_key,use_attributes'), "sql"=>"reconciliation_policy", "default_value"=>"use_attributes", "is_null_allowed"=>false, "depends_on"=>array())));
  58. MetaModel::Init_AddAttribute(new AttributeEnum("action_on_zero", array("allowed_values"=>new ValueSetEnum('create,error'), "sql"=>"action_on_zero", "default_value"=>"create", "is_null_allowed"=>false, "depends_on"=>array())));
  59. MetaModel::Init_AddAttribute(new AttributeEnum("action_on_one", array("allowed_values"=>new ValueSetEnum('update,error'), "sql"=>"action_on_one", "default_value"=>"update", "is_null_allowed"=>false, "depends_on"=>array())));
  60. MetaModel::Init_AddAttribute(new AttributeEnum("action_on_multiple", array("allowed_values"=>new ValueSetEnum('take_first,create,error'), "sql"=>"action_on_multiple", "default_value"=>"error", "is_null_allowed"=>false, "depends_on"=>array())));
  61. MetaModel::Init_AddAttribute(new AttributeEnum("delete_policy", array("allowed_values"=>new ValueSetEnum('ignore,delete,update,update_then_delete'), "sql"=>"delete_policy", "default_value"=>"ignore", "is_null_allowed"=>false, "depends_on"=>array())));
  62. MetaModel::Init_AddAttribute(new AttributeString("delete_policy_update", array("allowed_values"=>null, "sql"=>"delete_policy_update", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  63. // Format: seconds (unsigned int)
  64. MetaModel::Init_AddAttribute(new AttributeDuration("delete_policy_retention", array("allowed_values"=>null, "sql"=>"delete_policy_retention", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  65. MetaModel::Init_AddAttribute(new AttributeLinkedSet("attribute_list", array("linked_class"=>"SynchroAttribute", "ext_key_to_me"=>"sync_source_id", "allowed_values"=>null, "count_min"=>0, "count_max"=>0, "depends_on"=>array())));
  66. // Display lists
  67. MetaModel::Init_SetZListItems('details', array('name', 'description', 'scope_class', /*'scope_restriction', */'status', 'user_id', 'full_load_periodicity', 'reconciliation_policy', 'action_on_zero', 'action_on_one', 'action_on_multiple', 'delete_policy', 'delete_policy_update', 'delete_policy_retention' /*'attribute_list'*/)); // Attributes to be displayed for the complete details
  68. MetaModel::Init_SetZListItems('list', array('scope_class', 'status', 'user_id', 'full_load_periodicity')); // Attributes to be displayed for a list
  69. // Search criteria
  70. MetaModel::Init_SetZListItems('standard_search', array('name', 'status', 'scope_class', 'user_id')); // Criteria of the std search form
  71. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  72. }
  73. public function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  74. {
  75. if (!$this->IsNew())
  76. {
  77. $oPage->SetCurrentTab(Dict::S('Core:SynchroAttributes'));
  78. $oAttributeSet = $this->Get('attribute_list');
  79. $aAttributes = array();
  80. while($oAttribute = $oAttributeSet->Fetch())
  81. {
  82. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  83. }
  84. $aAttribs = array(
  85. 'attcode' => array('label'=>'Attribute', 'description' => 'Field of the object'),
  86. 'reconciliation' => array('label'=>'Reconciliation ?', 'description' => 'Used for searching'),
  87. 'update' => array('label'=>'Update ?', 'description' => 'Used to update the object'),
  88. 'update_policy' => array('label'=>'Update Policy', 'description' => 'Behavior of the updated field'),
  89. );
  90. $aValues = array();
  91. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  92. {
  93. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  94. {
  95. if (isset($aAttributes[$sAttCode]))
  96. {
  97. $oAttribute = $aAttributes[$sAttCode];
  98. }
  99. else
  100. {
  101. if ($oAttDef->IsExternalKey())
  102. {
  103. $oAttribute = new SynchroAttExtKey();
  104. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  105. }
  106. else
  107. {
  108. $oAttribute = new SynchroAttribute();
  109. }
  110. $oAttribute->Set('sync_source_id', $this->GetKey());
  111. $oAttribute->Set('attcode', $sAttCode);
  112. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  113. $oAttribute->Set('update', 1);
  114. $oAttribute->Set('update_policy', 'master_locked');
  115. }
  116. if (!$bEditMode)
  117. {
  118. // Read-only mode
  119. $aRow['reconciliation'] = $oAttribute->Get('reconcile') == 1 ? Dict::S('Core:SynchroReconcile:Yes') : Dict::S('Core:SynchroReconcile:No');
  120. $aRow['update'] = $oAttribute->Get('update') == 1 ? Dict::S('Core:SynchroUpdate:Yes') : Dict::S('Core:SynchroUpdate:No');
  121. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  122. $aRow['update_policy'] = $oAttribute->GetAsHTML('update_policy');
  123. }
  124. else
  125. {
  126. // Read-only mode
  127. $sAttCode = $oAttribute->Get('attcode');
  128. $sChecked = $oAttribute->Get('reconcile') == 1 ? 'checked' : '';
  129. $aRow['reconciliation'] = "<input type=\"checkbox\" name=\"reconciliation[$sAttCode]\" $sChecked/>";
  130. $sChecked = $oAttribute->Get('update') == 1 ? 'checked' : '';
  131. $aRow['update'] = "<input type=\"checkbox\" name=\"update[$sAttCode]\" $sChecked/>";
  132. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  133. $oAttDef = MetaModel::GetAttributeDef(get_class($oAttribute), 'update_policy');
  134. $aRow['update_policy'] = cmdbAbstractObject::GetFormElementForField($oPage, get_class($oAttribute), 'update_policy', $oAttDef, $oAttribute->Get('update_policy'), '', 'update_policy_'.$sAttCode, "[$sAttCode]");
  135. }
  136. $aValues[] = $aRow;
  137. }
  138. }
  139. $oPage->Table($aAttribs, $aValues);
  140. $this->DisplayStatusTab($oPage);
  141. }
  142. parent::DisplayBareRelations($oPage, $bEditMode);
  143. }
  144. /**
  145. * Displays the status (SynchroLog) of the datasource in a graphical manner
  146. * @param $oPage WebPage
  147. * @return void
  148. */
  149. protected function DisplayStatusTab(WebPage $oPage)
  150. {
  151. $oPage->SetCurrentTab(Dict::S('Core:SynchroStatus'));
  152. $sSelectSynchroLog = 'SELECT SynchroLog WHERE sync_source_id = :source_id';
  153. $oSetSynchroLog = new CMDBObjectSet(DBObjectSearch::FromOQL($sSelectSynchroLog), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()));
  154. if ($oSetSynchroLog->Count() > 0)
  155. {
  156. $oLastLog = $oSetSynchroLog->Fetch();
  157. $sStartDate = $oLastLog->Get('start_date');
  158. $oLastLog->Get('stats_nb_replica_seen');
  159. if ($oLastLog->Get('status') == 'running')
  160. {
  161. // Still running !
  162. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroRunningStartedOn_Date', $sStartDate).'</h2>');
  163. }
  164. else
  165. {
  166. $sEndDate = $oLastLog->Get('end_date');
  167. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroEndedOn_Date', $sEndDate).'</h2>');
  168. }
  169. $oPage->add('<table class="synoptics"><tr><td style="color:#333;vertical-align:top">');
  170. // List all the log entries for the user to select
  171. $oPage->add('<h2>'.Dict::S('Core:Synchro:History').'</h2>');
  172. $oSetSynchroLog->Rewind();
  173. $oPage->add('<select size="25" onChange="UpdateSynoptics(this.value);">');
  174. $sSelected = ' selected'; // First log is selected by default
  175. $sScript = "var aSynchroLog = {\n";
  176. while($oLog = $oSetSynchroLog->Fetch())
  177. {
  178. $sLogTitle = Dict::Format('Core:SynchroLogTitle', $oLog->Get('status'), $oLog->Get('start_date'));
  179. $oPage->add('<option value="'.$oLog->GetKey().'"'.$sSelected.'>'.$sLogTitle.'</option>');
  180. $sSelected = ''; // only the first log is selected by default
  181. $aData = $this->ProcessLog($oLog);
  182. $sScript .= '"'.$oLog->GetKey().'": '.json_encode($aData).",\n";
  183. }
  184. $sScript .= "end: 'Done'";
  185. $sScript .= "};\n";
  186. $sScript .= <<<EOF
  187. function UpdateSynoptics(id)
  188. {
  189. var aValues = aSynchroLog[id];
  190. for (var sKey in aValues)
  191. {
  192. $('#c_'+sKey).html(aValues[sKey]);
  193. var fOpacity = (aValues[sKey] == 0) ? 0.3 : 1;
  194. $('#'+sKey).fadeTo("slow", fOpacity);
  195. }
  196. }
  197. EOF
  198. ;
  199. $oPage->add_script($sScript);
  200. $oPage->add('</select>');
  201. $oPage->add('</td><td style="vertical-align:top;">');
  202. // Now build the big "synoptics" view
  203. $aData = $this->ProcessLog($oLastLog);
  204. $sNbReplica = $this->GetIcon()."&nbsp;".Dict::Format('Core:Synchro:Nb_Replica', "<span id=\"c_nb_replica_total\">{$aData['nb_replica_total']}</span>");
  205. $sNbObjects = MetaModel::GetClassIcon($this->GetTargetClass())."&nbsp;".Dict::Format('Core:Synchro:Nb_Class:Objects', $this->GetTargetClass(), "<span id=\"c_nb_obj_total\">{$aData['nb_obj_total']}</span>");
  206. $oPage->add(
  207. <<<EOF
  208. <table class="synoptics">
  209. <tr class="synoptics_header">
  210. <td>$sNbReplica</td><td>&nbsp;</td><td>$sNbObjects</td>
  211. </tr>
  212. <tr>
  213. EOF
  214. );
  215. $oPage->add($this->HtmlBox('repl_ignored', $aData, '#999').'<td colspan="2">&nbsp;</td>');
  216. $oPage->add("</tr>\n<tr>");
  217. $oPage->add($this->HtmlBox('repl_disappeared', $aData, '#630', 'rowspan="4"').'<td rowspan="4" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_disappeared_no_action', $aData, '#333'));
  218. $oPage->add("</tr>\n<tr>");
  219. $oPage->add($this->HtmlBox('obj_deleted', $aData, '#000'));
  220. $oPage->add("</tr>\n<tr>");
  221. $oPage->add($this->HtmlBox('obj_obsoleted', $aData, '#630'));
  222. $oPage->add("</tr>\n<tr>");
  223. $oPage->add($this->HtmlBox('obj_disappeared_errors', $aData, '#C00'));
  224. $oPage->add("</tr>\n<tr>");
  225. $oPage->add($this->HtmlBox('repl_existing', $aData, '#093', 'rowspan="3"').'<td rowspan="3" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_unchanged', $aData, '#393'));
  226. $oPage->add("</tr>\n<tr>");
  227. $oPage->add($this->HtmlBox('obj_updated', $aData, '#3C3'));
  228. $oPage->add("</tr>\n<tr>");
  229. $oPage->add($this->HtmlBox('obj_updated_errors', $aData, '#C00'));
  230. $oPage->add("</tr>\n<tr>");
  231. $oPage->add($this->HtmlBox('repl_new', $aData, '#339', 'rowspan="4"').'<td rowspan="4" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_new_unchanged', $aData, '#393'));
  232. $oPage->add("</tr>\n<tr>");
  233. $oPage->add($this->HtmlBox('obj_new_updated', $aData, '#3C3'));
  234. $oPage->add("</tr>\n<tr>");
  235. $oPage->add($this->HtmlBox('obj_created', $aData, '#339'));
  236. $oPage->add("</tr>\n<tr>");
  237. $oPage->add($this->HtmlBox('obj_new_errors', $aData, '#C00'));
  238. $oPage->add("</tr>\n</table>\n");
  239. $oPage->add('</td></tr></table>');
  240. }
  241. else
  242. {
  243. $oPage->p('<h2>'.Dict::S('Core:Synchro:NeverRun').'</h2>');
  244. }
  245. }
  246. protected function HtmlBox($sId, $aData, $sColor, $sHTMLAttribs = '')
  247. {
  248. $iCount = $aData[$sId];
  249. $sCount = "<span id=\"c_{$sId}\">$iCount</span>";
  250. $sLabel = Dict::Format('Core:Synchro:label_'.$sId, $sCount);
  251. $sOpacity = ($iCount==0) ? "opacity:0.3;" : "";
  252. return "<td id=\"$sId\" style=\"background-color:$sColor;$sOpacity;\" {$sHTMLAttribs}>$sLabel</td>";
  253. }
  254. protected function ProcessLog($oLastLog)
  255. {
  256. $aData = array(
  257. 'obj_deleted' => $oLastLog->Get('stats_nb_obj_deleted'),
  258. 'obj_obsoleted' => $oLastLog->Get('stats_nb_obj_obsoleted'),
  259. 'obj_disappeared_errors' => $oLastLog->Get('stats_nb_obj_obsoleted_errors') + $oLastLog->Get('stats_nb_obj_deleted_errors'),
  260. 'obj_disappeared_no_action' => $oLastLog->Get('stats_nb_replica_disappeared_no_action'),
  261. 'obj_updated' => $oLastLog->Get('stats_nb_obj_updated'),
  262. 'obj_updated_errors' => $oLastLog->Get('stats_nb_obj_updated_errors'),
  263. 'obj_new_updated' => $oLastLog->Get('stats_nb_obj_new_updated'),
  264. 'obj_new_unchanged' => $oLastLog->Get('stats_nb_obj_new_unchanged'),
  265. 'obj_created' => $oLastLog->Get('stats_nb_obj_created'),
  266. 'obj_created_errors' => $oLastLog->Get('stats_nb_obj_created_errors'),
  267. );
  268. $iReconciledErrors = $oLastLog->Get('stats_nb_replica_reconciled_errors');
  269. $iDisappeared = $aData['obj_disappeared_errors'] + $aData['obj_obsoleted'] + $aData['obj_deleted'] + $aData['obj_disappeared_no_action'];
  270. $aData['repl_disappeared'] = $iDisappeared;
  271. $iNewErrors = $aData['obj_created_errors'] + $oLastLog->Get('stats_nb_replica_reconciled_errors');
  272. $aData['obj_new_errors'] = $iNewErrors;
  273. $iNew = $aData['obj_created'] + $iNewErrors + $aData['obj_new_updated'] + $aData['obj_new_unchanged'];
  274. $aData['repl_new'] = $iNew;
  275. $iExisting = $oLastLog->Get('stats_nb_replica_seen') - $iNew;
  276. $aData['repl_existing'] = $iExisting;
  277. $aData['obj_unchanged'] = $iExisting - $aData['obj_updated'] - $aData['obj_updated_errors'];
  278. $iIgnored = $oLastLog->Get('stats_nb_replica_total') - $iNew - $iExisting - $iDisappeared;
  279. $aData['repl_ignored'] = $iIgnored;
  280. $aData['nb_obj_total'] = $iNew + $iExisting + $iDisappeared;
  281. $aData['nb_replica_total'] = $aData['nb_obj_total'] + $iIgnored;
  282. return $aData;
  283. }
  284. public function GetAttributeFlags($sAttCode)
  285. {
  286. if (($sAttCode == 'scope_class') && (!$this->IsNew()))
  287. {
  288. return OPT_ATT_READONLY;
  289. }
  290. return parent::GetAttributeFlags($sAttCode);
  291. }
  292. public function UpdateObject($sFormPrefix = '')
  293. {
  294. parent::UpdateObject($sFormPrefix);
  295. // And now read the other post parameters...
  296. $oAttributeSet = $this->Get('attribute_list');
  297. $aAttributes = array();
  298. while($oAttribute = $oAttributeSet->Fetch())
  299. {
  300. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  301. }
  302. $aReconcile = utils::ReadPostedParam('reconciliation', array());
  303. $aUpdate = utils::ReadPostedParam('update', array());
  304. $aUpdatePolicy = utils::ReadPostedParam('attr_update_policy', array());
  305. // update_policy cannot be empty, so there is one entry per attribute, use this to iterate
  306. // through all the writable attributes
  307. foreach($aUpdatePolicy as $sAttCode => $sValue)
  308. {
  309. if(!isset($aAttributes[$sAttCode]))
  310. {
  311. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  312. if ($oAttDef->IsExternalKey())
  313. {
  314. $oAttribute = new SynchroAttExtKey();
  315. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  316. }
  317. else
  318. {
  319. $oAttribute = new SynchroAttribute();
  320. }
  321. $oAttribute->Set('sync_source_id', $this->GetKey());
  322. $oAttribute->Set('attcode', $sAttCode);
  323. }
  324. else
  325. {
  326. $oAttribute = $aAttributes[$sAttCode];
  327. }
  328. $bReconcile = 0;
  329. if (isset($aReconcile[$sAttCode]))
  330. {
  331. $bReconcile = $aReconcile[$sAttCode] == 'on' ? 1 : 0;
  332. }
  333. $bUpdate = 0 ; // Default / initial value
  334. if (isset($aUpdate[$sAttCode]))
  335. {
  336. $bUpdate = $aUpdate[$sAttCode] == 'on' ? 1 : 0;
  337. }
  338. $oAttribute->Set('reconcile', $bReconcile);
  339. $oAttribute->Set('update', $bUpdate);
  340. $oAttribute->Set('update_policy', $sValue);
  341. $oAttributeSet->AddObject($oAttribute);
  342. }
  343. $this->Set('attribute_list', $oAttributeSet);
  344. }
  345. /*
  346. * Overload the standard behavior
  347. */
  348. public function DoCheckToWrite()
  349. {
  350. parent::DoCheckToWrite();
  351. // Check that there is at least one reconciliation key defined
  352. if ($this->Get('reconciliation_policy') == 'use_attributes')
  353. {
  354. $oSet = $this->Get('attribute_list');
  355. $oSynchroAttributeList = $oSet->ToArray();
  356. $bReconciliationKey = false;
  357. foreach($oSynchroAttributeList as $oSynchroAttribute)
  358. {
  359. if ($oSynchroAttribute->Get('reconcile') == 1)
  360. {
  361. $bReconciliationKey = true; // At least one key is defined
  362. }
  363. }
  364. if (!$bReconciliationKey)
  365. {
  366. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:AtLeastOneReconciliationKeyMustBeSpecified');
  367. }
  368. }
  369. // If 'update_then_delete' is specified there must be a delete_retention_period
  370. if (($this->Get('delete_policy') == 'update_then_delete') && ($this->Get('delete_policy_retention') == 0))
  371. {
  372. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeleteRetentionDurationMustBeSpecified');
  373. }
  374. // If update is specified, then something to update must be defined
  375. if ((($this->Get('delete_policy') == 'update_then_delete') || ($this->Get('delete_policy') == 'update'))
  376. && ($this->Get('delete_policy_update') == ''))
  377. {
  378. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeletePolicyUpdateMustBeSpecified');
  379. }
  380. }
  381. public function GetTargetClass()
  382. {
  383. return $this->Get('scope_class');
  384. }
  385. public function GetDataTable()
  386. {
  387. $sName = strtolower($this->GetTargetClass());
  388. $sName = str_replace('\'"&@|\\/ ', '_', $sName); // Remove forbidden characters from the table name
  389. $sName .= '_'.$this->GetKey(); // Add a suffix for unicity
  390. $sTable = MetaModel::GetConfig()->GetDBSubName()."synchro_data_$sName"; // Add the prefix if any
  391. return $sTable;
  392. }
  393. /**
  394. * When inserting a new datasource object, also create the SynchroAttribute objects
  395. * for each field of the target class
  396. */
  397. protected function OnInsert()
  398. {
  399. // Create all the SynchroAttribute records
  400. $oAttributeSet = $this->Get('attribute_list');
  401. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  402. {
  403. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  404. {
  405. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  406. if ($oAttDef->IsExternalKey())
  407. {
  408. $oAttribute = new SynchroAttExtKey();
  409. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  410. }
  411. else
  412. {
  413. $oAttribute = new SynchroAttribute();
  414. }
  415. $oAttribute->Set('sync_source_id', $this->GetKey());
  416. $oAttribute->Set('attcode', $sAttCode);
  417. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  418. $oAttribute->Set('update', 1);
  419. $oAttribute->Set('update_policy', 'master_locked');
  420. $oAttributeSet->AddObject($oAttribute);
  421. }
  422. }
  423. $this->Set('attribute_list', $oAttributeSet);
  424. }
  425. /**
  426. * When the new datasource has been created, let's create the synchro_data table
  427. * that will hold the data records and the correspoding triggers which will maintain
  428. * both tables in sync
  429. */
  430. protected function AfterInsert()
  431. {
  432. parent::AfterInsert();
  433. $sTable = $this->GetDataTable();
  434. $aColumns = $this->GetSQLColumns();
  435. $aFieldDefs = array();
  436. // Allow '0', otherwise mysql will render an error when the id is not given
  437. // (the trigger is expected to set the value, but it is not executed soon enough)
  438. $aFieldDefs[] = "id INTEGER(11) NOT NULL DEFAULT 0 ";
  439. $aFieldDefs[] = "`primary_key` VARCHAR(255) NULL DEFAULT NULL";
  440. foreach($aColumns as $sColumn => $ColSpec)
  441. {
  442. $aFieldDefs[] = "`$sColumn` $ColSpec NULL DEFAULT NULL";
  443. }
  444. $aFieldDefs[] = "INDEX (id)";
  445. $aFieldDefs[] = "INDEX (primary_key)";
  446. $sFieldDefs = implode(', ', $aFieldDefs);
  447. $sCreateTable = "CREATE TABLE `$sTable` ($sFieldDefs) ENGINE = innodb;";
  448. CMDBSource::Query($sCreateTable);
  449. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_bi` BEFORE INSERT ON $sTable";
  450. $sTriggerInsert .= " FOR EACH ROW";
  451. $sTriggerInsert .= " BEGIN";
  452. $sTriggerInsert .= " INSERT INTO priv_sync_replica (sync_source_id, status_last_seen, `status`) VALUES ({$this->GetKey()}, NOW(), 'new');";
  453. $sTriggerInsert .= " SET NEW.id = LAST_INSERT_ID();";
  454. $sTriggerInsert .= " END;";
  455. CMDBSource::Query($sTriggerInsert);
  456. $aModified = array();
  457. foreach($aColumns as $sColumn => $ColSpec)
  458. {
  459. // <=> is a null-safe 'EQUALS' operator (there is no equivalent for "DIFFERS FROM")
  460. $aModified[] = "NOT(NEW.`$sColumn` <=> OLD.`$sColumn`)";
  461. }
  462. $sIsModified = '('.implode(') OR (', $aModified).')';
  463. // Update the replica
  464. //
  465. // status is forced to "new" if the replica was obsoleted directly from the state "new" (dest_id = null)
  466. // otherwise, if status was either 'obsolete' or 'synchronized' it is turned into 'modified' or 'synchronized' depending on the changes
  467. // otherwise, the status is left as is
  468. $sTriggerUpdate = "CREATE TRIGGER `{$sTable}_bu` BEFORE UPDATE ON $sTable";
  469. $sTriggerUpdate .= " FOR EACH ROW";
  470. $sTriggerUpdate .= " BEGIN";
  471. $sTriggerUpdate .= " IF @itopuser is null THEN";
  472. $sTriggerUpdate .= " UPDATE priv_sync_replica SET status_last_seen = NOW(), `status` = IF(`status` = 'obsolete', IF(`dest_id` IS NULL, 'new', 'modified'), IF(`status` IN ('synchronized') AND ($sIsModified), 'modified', `status`)) WHERE sync_source_id = {$this->GetKey()} AND id = OLD.id;";
  473. $sTriggerUpdate .= " SET NEW.id = OLD.id;"; // make sure this id won't change
  474. $sTriggerUpdate .= " END IF;";
  475. $sTriggerUpdate .= " END;";
  476. CMDBSource::Query($sTriggerUpdate);
  477. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_ad` AFTER DELETE ON $sTable";
  478. $sTriggerInsert .= " FOR EACH ROW";
  479. $sTriggerInsert .= " BEGIN";
  480. $sTriggerInsert .= " DELETE FROM priv_sync_replica WHERE id = OLD.id;";
  481. $sTriggerInsert .= " END;";
  482. CMDBSource::Query($sTriggerInsert);
  483. }
  484. protected function AfterDelete()
  485. {
  486. parent::AfterDelete();
  487. $sTable = $this->GetDataTable();
  488. $sDropTable = "DROP TABLE `$sTable`";
  489. CMDBSource::Query($sDropTable);
  490. // TO DO - check that triggers get dropped with the table
  491. }
  492. /**
  493. * Perform a synchronization between the data stored in the replicas (&synchro_data_xxx_xx table)
  494. * and the iTop objects. If the lastFullLoadStartDate is NOT specified then the full_load_periodicity
  495. * is used to determine which records are obsolete.
  496. * @param Hash $aTraces Debugs/Trace information, one or more entries per replica
  497. * @param DateTime $oLastFullLoadStartDate Date of the last full load (start date/time), if known
  498. * @return void
  499. */
  500. public function Synchronize($oLastFullLoadStartDate = null)
  501. {
  502. // Create a change used for logging all the modifications/creations happening during the synchro
  503. $oMyChange = MetaModel::NewObject("CMDBChange");
  504. $oMyChange->Set("date", time());
  505. $sUserString = CMDBChange::GetCurrentUserName();
  506. $oMyChange->Set("userinfo", $sUserString);
  507. $iChangeId = $oMyChange->DBInsert();
  508. // Start logging this execution (stats + protection against reentrance)
  509. //
  510. $oStatLog = new SynchroLog();
  511. $oStatLog->Set('sync_source_id', $this->GetKey());
  512. $oStatLog->Set('start_date', time());
  513. $oStatLog->Set('status', 'running');
  514. $oStatLog->Set('stats_nb_replica_seen', 0);
  515. $oStatLog->Set('stats_nb_replica_total', 0);
  516. $oStatLog->Set('stats_nb_obj_deleted', 0);
  517. $oStatLog->Set('stats_nb_obj_deleted_errors', 0);
  518. $oStatLog->Set('stats_nb_obj_obsoleted', 0);
  519. $oStatLog->Set('stats_nb_obj_obsoleted_errors', 0);
  520. $oStatLog->Set('stats_nb_obj_created', 0);
  521. $oStatLog->Set('stats_nb_obj_created_errors', 0);
  522. $oStatLog->Set('stats_nb_obj_updated', 0);
  523. $oStatLog->Set('stats_nb_obj_updated_errors', 0);
  524. // $oStatLog->Set('stats_nb_replica_reconciled', 0);
  525. $oStatLog->Set('stats_nb_replica_reconciled_errors', 0);
  526. $oStatLog->Set('stats_nb_replica_disappeared_no_action', 0);
  527. $oStatLog->Set('stats_nb_obj_new_updated', 0);
  528. $oStatLog->Set('stats_nb_obj_new_unchanged',0);
  529. $sSelectTotal = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  530. $oSetTotal = new DBObjectSet(DBObjectSearch::FromOQL($sSelectTotal), array() /* order by*/, array('source_id' => $this->GetKey()));
  531. $oStatLog->Set('stats_nb_replica_total', $oSetTotal->Count());
  532. $oStatLog->DBInsertTracked($oMyChange);
  533. try
  534. {
  535. $this->DoSynchronize($oLastFullLoadStartDate, $oMyChange, $oStatLog);
  536. $oStatLog->Set('end_date', time());
  537. $oStatLog->Set('status', 'completed');
  538. $oStatLog->DBUpdateTracked($oMyChange);
  539. }
  540. catch (SynchroExceptionNotStarted $e)
  541. {
  542. // Set information for reporting... but delete the object in DB
  543. $oStatLog->Set('end_date', time());
  544. $oStatLog->Set('status', 'error');
  545. $oStatLog->Set('last_error', $e->getMessage());
  546. $oStatLog->DBDeleteTracked($oMyChange);
  547. }
  548. catch (Exception $e)
  549. {
  550. $oStatLog->Set('end_date', time());
  551. $oStatLog->Set('status', 'error');
  552. $oStatLog->Set('last_error', $e->getMessage());
  553. $oStatLog->DBUpdateTracked($oMyChange);
  554. }
  555. return $oStatLog;
  556. }
  557. protected function DoSynchronize($oLastFullLoadStartDate, $oMyChange, &$oStatLog)
  558. {
  559. if ($this->Get('status') == 'obsolete')
  560. {
  561. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceObsolete'));
  562. }
  563. if (!UserRights::IsAdministrator() && $this->Get('user_id') != UserRights::GetUserId())
  564. {
  565. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceAccessRestriction'));
  566. }
  567. $sDeletePolicy = $this->Get('delete_policy');
  568. // Count the replicas
  569. $sSelectAll = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  570. $oSetAll = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAll), array() /* order by*/, array('source_id' => $this->GetKey()));
  571. $iCountAllReplicas = $oSetAll->Count();
  572. $oStatLog->Set('stats_nb_replica_total', $iCountAllReplicas);
  573. // Get all the replicas that were not seen in the last import and mark them as obsolete
  574. if ($oLastFullLoadStartDate == null)
  575. {
  576. // No previous import known, use the full_load_periodicity value... and the current date
  577. $oLastFullLoadStartDate = new DateTime(); // Now
  578. $iLoadPeriodicity = $this->Get('full_load_periodicity'); // Duration in seconds
  579. if ($iLoadPeriodicity > 0)
  580. {
  581. $sInterval = "-$iLoadPeriodicity seconds";
  582. $oLastFullLoadStartDate->Modify($sInterval);
  583. }
  584. }
  585. $sLimitDate = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  586. $oStatLog->AddTrace("Limit Date: $sLimitDate");
  587. $sSelectToObsolete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
  588. $oSetToObsolete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
  589. if (($iCountAllReplicas > 10) && ($iCountAllReplicas == $oSetToObsolete->Count()))
  590. {
  591. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncTooManyMissingReplicas'));
  592. }
  593. while($oReplica = $oSetToObsolete->Fetch())
  594. {
  595. switch ($sDeletePolicy)
  596. {
  597. case 'update':
  598. case 'update_then_delete':
  599. $oStatLog->AddTrace("Destination object to be updated", $oReplica);
  600. $aToUpdate = array();
  601. $aToUpdate = explode(';', $this->Get('delete_policy_update')); //ex: 'status:obsolete;description:stopped',
  602. foreach($aToUpdate as $sUpdateSpec)
  603. {
  604. $aUpdateSpec = explode(':', $sUpdateSpec);
  605. if (count($aUpdateSpec) == 2)
  606. {
  607. $sAttCode = $aUpdateSpec[0];
  608. $sValue = $aUpdateSpec[1];
  609. $aToUpdate[$sAttCode] = $sValue;
  610. }
  611. }
  612. $oReplica->UpdateDestObject($aToUpdate, $oMyChange, $oStatLog);
  613. $oReplica->Set('status', 'obsolete');
  614. $oReplica->DBUpdateTracked($oMyChange);
  615. break;
  616. case 'delete':
  617. default:
  618. $oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  619. $oReplica->DeleteDestObject($oMyChange, $oStatLog);
  620. }
  621. }
  622. //Count "seen" objects
  623. $sSelectSeen = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen >= :last_import";
  624. $oSetSeen = new DBObjectSet(DBObjectSearch::FromOQL($sSelectSeen), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
  625. $oStatLog->Set('stats_nb_replica_seen', $oSetSeen->Count());
  626. // Get all the replicas that are 'new' or modified
  627. //
  628. // Get the list of SQL columns
  629. $sClass = $this->GetTargetClass();
  630. $aAttCodesExpected = array();
  631. $aAttCodesToReconcile = array();
  632. $aAttCodesToUpdate = array();
  633. $sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND (update = 1 OR reconcile = 1)";
  634. $oSetAtt = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */);
  635. while ($oSyncAtt = $oSetAtt->Fetch())
  636. {
  637. if ($oSyncAtt->Get('update'))
  638. {
  639. $aAttCodesToUpdate[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  640. }
  641. if ($oSyncAtt->Get('reconcile'))
  642. {
  643. $aAttCodesToReconcile[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  644. }
  645. $aAttCodesExpected[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  646. }
  647. $aColumns = $this->GetSQLColumns(array_keys($aAttCodesExpected));
  648. $aExtDataFields = array_keys($aColumns);
  649. $aExtDataFields[] = 'primary_key';
  650. $aExtDataSpec = array(
  651. 'table' => $this->GetDataTable(),
  652. 'join_key' => 'id',
  653. 'fields' => $aExtDataFields
  654. );
  655. // Get the list of reconciliation keys
  656. if ($this->Get('reconciliation_policy') == 'use_attributes')
  657. {
  658. $aReconciliationKeys = $aAttCodesToReconcile;
  659. }
  660. elseif ($this->Get('reconciliation_policy') == 'use_primary_key')
  661. {
  662. // Override the setings made at the attribute level !
  663. $aReconciliationKeys = array("primary_key" => null);
  664. }
  665. $oStatLog->AddTrace("Update of: {".implode(', ', array_keys($aAttCodesToUpdate))."}");
  666. $oStatLog->AddTrace("Reconciliation on: {".implode(', ', array_keys($aReconciliationKeys))."}");
  667. $aAttributes = array();
  668. foreach($aAttCodesToUpdate as $sAttCode => $oSyncAtt)
  669. {
  670. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  671. if ($oAttDef->IsWritable() && $oAttDef->IsScalar())
  672. {
  673. $aAttributes[$sAttCode] = $oSyncAtt;
  674. }
  675. }
  676. $sSelectToSync = "SELECT SynchroReplica WHERE (status = 'new' OR status = 'modified') AND sync_source_id = :source_id";
  677. $oSetToSync = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, $aExtDataSpec, 0 /* limitCount */, 0 /* limitStart */);
  678. while($oReplica = $oSetToSync->Fetch())
  679. {
  680. $oReplica->Synchro($this, $aReconciliationKeys, $aAttributes, $oMyChange, $oStatLog);
  681. }
  682. // Get all the replicas that are to be deleted
  683. //
  684. if ($sDeletePolicy == 'update_then_delete')
  685. {
  686. $oDeletionDate = $oLastFullLoadStartDate;
  687. $iDeleteRetention = $this->Get('delete_policy_retention'); // Duration in seconds
  688. if ($iDeleteRetention > 0)
  689. {
  690. $sInterval = "-$iDeleteRetention seconds";
  691. $oDeletionDate->Modify($sInterval);
  692. }
  693. $sDeletionDate = $oDeletionDate->Format('Y-m-d H:i:s');
  694. $oStatLog->AddTrace("Deletion date: $sDeletionDate");
  695. $sSelectToDelete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('obsolete') AND status_last_seen < :last_import";
  696. $oSetToDelete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sDeletionDate));
  697. while($oReplica = $oSetToDelete->Fetch())
  698. {
  699. $oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  700. $oReplica->DeleteDestObject($oMyChange, $oStatLog);
  701. }
  702. }
  703. }
  704. /**
  705. * Get the list of SQL columns corresponding to a particular list of attribute codes
  706. * Defaults to the whole list of columns for the current class
  707. */
  708. public function GetSQLColumns($aAttributeCodes = null)
  709. {
  710. $aColumns = array();
  711. $sClass = $this->GetTargetClass();
  712. if (is_null($aAttributeCodes))
  713. {
  714. $aAttributeCodes = array();
  715. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  716. {
  717. if ($sAttCode == 'finalclass') continue;
  718. $aAttributeCodes[] = $sAttCode;
  719. }
  720. }
  721. foreach($aAttributeCodes as $sAttCode)
  722. {
  723. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  724. if ($oAttDef->IsExternalKey())
  725. {
  726. // The pkey might be used as well as any other key column
  727. $aColumns[$sAttCode] = 'VARCHAR (255)';
  728. }
  729. else
  730. {
  731. foreach($oAttDef->GetSQLColumns() as $sField => $sDBFieldType)
  732. {
  733. $aColumns[$sField] = $sDBFieldType;
  734. }
  735. }
  736. }
  737. return $aColumns;
  738. }
  739. public function IsRunning()
  740. {
  741. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='running'";
  742. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  743. if ($oSet->Count() < 1)
  744. {
  745. $bRet = false;
  746. }
  747. else
  748. {
  749. $bRet = true;
  750. }
  751. return $bRet;
  752. }
  753. public function GetLatestLog()
  754. {
  755. $oLog = null;
  756. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id";
  757. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  758. if ($oSet->Count() >= 1)
  759. {
  760. $oLog = $oSet->Fetch();
  761. }
  762. return $oLog;
  763. }
  764. // TO DO: remove if still unused
  765. /**
  766. * Retrieve from the log, the date of the last completed import
  767. * @return DateTime
  768. */
  769. public function GetLastCompletedImportDate()
  770. {
  771. $date = null;
  772. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='completed'";
  773. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('end_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 0 /* limitCount */, 0 /* limitStart */);
  774. if ($oSet->Count() >= 1)
  775. {
  776. $oLog = $oSet->Fetch();
  777. $date = $oLog->Get('end_date');
  778. }
  779. else
  780. {
  781. // TO DO: remove trace
  782. echo "<p>No completed log found</p>\n";
  783. }
  784. return $date;
  785. }
  786. }
  787. class SynchroAttribute extends cmdbAbstractObject
  788. {
  789. public static function Init()
  790. {
  791. $aParams = array
  792. (
  793. "category" => "core/cmdb,view_in_gui",
  794. "key_type" => "autoincrement",
  795. "name_attcode" => "",
  796. "state_attcode" => "",
  797. "reconc_keys" => array(),
  798. "db_table" => "priv_sync_att",
  799. "db_key_field" => "id",
  800. "db_finalclass_field" => "",
  801. "display_template" => "",
  802. );
  803. MetaModel::Init_Params($aParams);
  804. MetaModel::Init_InheritAttributes();
  805. MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  806. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  807. MetaModel::Init_AddAttribute(new AttributeBoolean("update", array("allowed_values"=>null, "sql"=>"update", "default_value"=>true, "is_null_allowed"=>false, "depends_on"=>array())));
  808. MetaModel::Init_AddAttribute(new AttributeBoolean("reconcile", array("allowed_values"=>null, "sql"=>"reconcile", "default_value"=>false, "is_null_allowed"=>false, "depends_on"=>array())));
  809. MetaModel::Init_AddAttribute(new AttributeEnum("update_policy", array("allowed_values"=>new ValueSetEnum('master_locked,master_unlocked,write_if_empty'), "sql"=>"update_policy", "default_value"=>"master_locked", "is_null_allowed"=>false, "depends_on"=>array())));
  810. // Display lists
  811. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for the complete details
  812. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  813. // Search criteria
  814. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  815. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  816. }
  817. }
  818. class SynchroAttExtKey extends SynchroAttribute
  819. {
  820. public static function Init()
  821. {
  822. $aParams = array
  823. (
  824. "category" => "core/cmdb,view_in_gui",
  825. "key_type" => "autoincrement",
  826. "name_attcode" => "",
  827. "state_attcode" => "",
  828. "reconc_keys" => array(),
  829. "db_table" => "priv_sync_att_extkey",
  830. "db_key_field" => "id",
  831. "db_finalclass_field" => "",
  832. "display_template" => "",
  833. );
  834. MetaModel::Init_Params($aParams);
  835. MetaModel::Init_InheritAttributes();
  836. MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  837. // Display lists
  838. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
  839. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  840. // Search criteria
  841. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  842. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  843. }
  844. }
  845. class SynchroAttLinkSet extends SynchroAttribute
  846. {
  847. public static function Init()
  848. {
  849. $aParams = array
  850. (
  851. "category" => "core/cmdb,view_in_gui",
  852. "key_type" => "autoincrement",
  853. "name_attcode" => "",
  854. "state_attcode" => "",
  855. "reconc_keys" => array(),
  856. "db_table" => "priv_sync_att_linkset",
  857. "db_key_field" => "id",
  858. "db_finalclass_field" => "",
  859. "display_template" => "",
  860. );
  861. MetaModel::Init_Params($aParams);
  862. MetaModel::Init_InheritAttributes();
  863. MetaModel::Init_AddAttribute(new AttributeString("row_separator", array("allowed_values"=>null, "sql"=>"row_separator", "default_value"=>'|', "is_null_allowed"=>true, "depends_on"=>array())));
  864. MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
  865. // Display lists
  866. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'row_separator', 'attribute_separator')); // Attributes to be displayed for the complete details
  867. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  868. // Search criteria
  869. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  870. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  871. }
  872. }
  873. //class SynchroLog extends Event
  874. class SynchroLog extends DBObject
  875. {
  876. public static function Init()
  877. {
  878. $aParams = array
  879. (
  880. "category" => "core/cmdb,view_in_gui",
  881. "key_type" => "autoincrement",
  882. "name_attcode" => "",
  883. "state_attcode" => "",
  884. "reconc_keys" => array(),
  885. "db_table" => "priv_sync_log",
  886. "db_key_field" => "id",
  887. "db_finalclass_field" => "",
  888. "display_template" => "",
  889. );
  890. MetaModel::Init_Params($aParams);
  891. MetaModel::Init_InheritAttributes();
  892. // MetaModel::Init_AddAttribute(new AttributeString("userinfo", array("allowed_values"=>null, "sql"=>"userinfo", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  893. MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  894. MetaModel::Init_AddAttribute(new AttributeDateTime("start_date", array("allowed_values"=>null, "sql"=>"start_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  895. MetaModel::Init_AddAttribute(new AttributeDateTime("end_date", array("allowed_values"=>null, "sql"=>"end_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  896. MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('running,completed,error'), "sql"=>"status", "default_value"=>"running", "is_null_allowed"=>false, "depends_on"=>array())));
  897. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_seen", array("allowed_values"=>null, "sql"=>"stats_nb_replica_seen", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  898. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_total", array("allowed_values"=>null, "sql"=>"stats_nb_replica_total", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  899. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_deleted", array("allowed_values"=>null, "sql"=>"stats_nb_obj_deleted", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  900. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_deleted_errors", array("allowed_values"=>null, "sql"=>"stats_deleted_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  901. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_obsoleted", array("allowed_values"=>null, "sql"=>"stats_nb_obj_obsoleted", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  902. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_obsoleted_errors", array("allowed_values"=>null, "sql"=>"stats_nb_obj_obsoleted_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  903. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_created", array("allowed_values"=>null, "sql"=>"stats_nb_obj_created", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  904. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_created_errors", array("allowed_values"=>null, "sql"=>"stats_nb_obj_created_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  905. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_updated", array("allowed_values"=>null, "sql"=>"stats_nb_obj_updated", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  906. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_updated_errors", array("allowed_values"=>null, "sql"=>"stats_nb_obj_updated_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  907. // MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_reconciled", array("allowed_values"=>null, "sql"=>"stats_nb_replica_reconciled", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  908. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_reconciled_errors", array("allowed_values"=>null, "sql"=>"stats_nb_replica_reconciled_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  909. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_disappeared_no_action", array("allowed_values"=>null, "sql"=>"stats_nb_replica_disappeared_no_action", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  910. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_new_updated", array("allowed_values"=>null, "sql"=>"stats_nb_obj_new_updated", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  911. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_new_unchanged", array("allowed_values"=>null, "sql"=>"stats_nb_obj_new_unchanged", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  912. MetaModel::Init_AddAttribute(new AttributeString("last_error", array("allowed_values"=>null, "sql"=>"last_error", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  913. MetaModel::Init_AddAttribute(new AttributeLongText("traces", array("allowed_values"=>null, "sql"=>"traces", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  914. // Display lists
  915. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_replica_total', 'stats_nb_replica_seen', 'stats_nb_obj_created', /*'stats_nb_replica_reconciled',*/ 'stats_nb_obj_updated', 'stats_nb_obj_obsoleted', 'stats_nb_obj_deleted',
  916. 'stats_nb_obj_created_errors', 'stats_nb_replica_reconciled_errors', 'stats_nb_replica_disappeared_no_action', 'stats_nb_obj_updated_errors', 'stats_nb_obj_obsoleted_errors', 'stats_nb_obj_deleted_errors', 'stats_nb_obj_new_unchanged', 'stats_nb_obj_new_updated', 'dump')); // Attributes to be displayed for the complete details
  917. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_replica_seen')); // Attributes to be displayed for a list
  918. // Search criteria
  919. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  920. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  921. }
  922. /**
  923. * Increments a statistics counter
  924. */
  925. function Inc($sCode)
  926. {
  927. $this->Set($sCode, 1+$this->Get($sCode));
  928. }
  929. /**
  930. * Implement traces management
  931. */
  932. protected $m_aTraces = array();
  933. public function AddTrace($sMsg, $oReplica = null)
  934. {
  935. if (MetaModel::GetConfig()->Get('synchro_trace') == 'none')
  936. {
  937. return;
  938. }
  939. if ($oReplica)
  940. {
  941. $sDestClass = $oReplica->Get('dest_class');
  942. if (!empty($sDestClass))
  943. {
  944. $sPrefix = $oReplica->GetKey().','.$sDestClass.'::'.$oReplica->Get('dest_id').',';
  945. }
  946. else
  947. {
  948. $sPrefix = $oReplica->GetKey().',,';
  949. }
  950. }
  951. else
  952. {
  953. $sPrefix = ',,';
  954. }
  955. $this->m_aTraces[] = $sPrefix.$sMsg;
  956. }
  957. public function GetTraces()
  958. {
  959. return $this->m_aTraces;
  960. }
  961. protected function TraceToText()
  962. {
  963. if (MetaModel::GetConfig()->Get('synchro_trace') != 'save')
  964. {
  965. // none, or display only
  966. return;
  967. }
  968. $oAttDef = MetaModel::GetAttributeDef(get_class($this), 'traces');
  969. $iMaxSize = $oAttDef->GetMaxSize();
  970. $sTrace = implode("\n", $this->m_aTraces);
  971. if (strlen($sTrace) >= $iMaxSize)
  972. {
  973. $sTrace = substr($sTrace, 0, $iMaxSize - 255)."...\nTruncated (size: ".strlen($sTrace).')';
  974. }
  975. $this->Set('traces', $sTrace);
  976. }
  977. protected function OnInsert()
  978. {
  979. $this->TraceToText();
  980. parent::OnInsert();
  981. }
  982. protected function OnUpdate()
  983. {
  984. $this->TraceToText();
  985. parent::OnUpdate();
  986. }
  987. }
  988. class SynchroReplica extends DBObject
  989. {
  990. static $aSearches = array(); // Cache of OQL queries used for reconciliation (per data source)
  991. public static function Init()
  992. {
  993. $aParams = array
  994. (
  995. "category" => "core/cmdb,view_in_gui",
  996. "key_type" => "autoincrement",
  997. "name_attcode" => "",
  998. "state_attcode" => "",
  999. "reconc_keys" => array(),
  1000. "db_table" => "priv_sync_replica",
  1001. "db_key_field" => "id",
  1002. "db_finalclass_field" => "",
  1003. "display_template" => "",
  1004. );
  1005. MetaModel::Init_Params($aParams);
  1006. MetaModel::Init_InheritAttributes();
  1007. MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  1008. MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1009. MetaModel::Init_AddAttribute(new AttributeClass("dest_class", array("class_category"=>"bizmodel", "more_values"=>"", "sql"=>"dest_class", "default_value"=>'Organization', "is_null_allowed"=>true, "depends_on"=>array())));
  1010. MetaModel::Init_AddAttribute(new AttributeDateTime("status_last_seen", array("allowed_values"=>null, "sql"=>"status_last_seen", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  1011. MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('new,synchronized,modified,orphan,obsolete'), "sql"=>"status", "default_value"=>"new", "is_null_allowed"=>false, "depends_on"=>array())));
  1012. MetaModel::Init_AddAttribute(new AttributeBoolean("status_dest_creator", array("allowed_values"=>null, "sql"=>"status_dest_creator", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1013. MetaModel::Init_AddAttribute(new AttributeString("status_last_error", array("allowed_values"=>null, "sql"=>"status_last_error", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1014. MetaModel::Init_AddAttribute(new AttributeDateTime("info_creation_date", array("allowed_values"=>null, "sql"=>"info_creation_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1015. MetaModel::Init_AddAttribute(new AttributeDateTime("info_last_modified", array("allowed_values"=>null, "sql"=>"info_last_modified", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1016. MetaModel::Init_AddAttribute(new AttributeDateTime("info_last_synchro", array("allowed_values"=>null, "sql"=>"info_last_synchro", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1017. // Display lists
  1018. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'dest_id', 'dest_class', 'status_last_seen', 'status', 'status_dest_creator', 'status_last_error', 'info_creation_date', 'info_last_modified', 'info_last_synchro')); // Attributes to be displayed for the complete details
  1019. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'dest_id', 'dest_class', 'status_last_seen', 'status', 'status_dest_creator', 'status_last_error')); // Attributes to be displayed for a list
  1020. // Search criteria
  1021. MetaModel::Init_SetZListItems('standard_search', array('sync_source_id', 'status_last_seen', 'status', 'status_dest_creator', 'dest_class', 'dest_id', 'status_last_error')); // Criteria of the std search form
  1022. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1023. }
  1024. public function DBInsert()
  1025. {
  1026. throw new CoreException('A synchronization replica must be created only by the mean of triggers');
  1027. }
  1028. // Overload the deletion -> the replica has been created by the mean of a trigger,
  1029. // it will be deleted by the mean of a trigger too
  1030. public function DBDelete()
  1031. {
  1032. $oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1033. $sTable = $oDataSource->GetDataTable();
  1034. $sSQL = "DELETE FROM `$sTable` WHERE id = '{$this->GetKey()}'";
  1035. CMDBSource::Query($sSQL);
  1036. $this->m_bIsInDB = false;
  1037. $this->m_iKey = null;
  1038. }
  1039. public function SetLastError($sMessage, $oException = null)
  1040. {
  1041. if ($oException)
  1042. {
  1043. $sText = $sMessage.$oException->getMessage();
  1044. }
  1045. else
  1046. {
  1047. $sText = $sMessage;
  1048. }
  1049. if (strlen($sText) > 255)
  1050. {
  1051. $sText = substr($sText, 0, 200).'...('.strlen($sText).' chars)...';
  1052. }
  1053. $this->Set('status_last_error', $sText);
  1054. }
  1055. public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange, &$oStatLog)
  1056. {
  1057. switch($this->Get('status'))
  1058. {
  1059. case 'new':
  1060. $this->Set('status_dest_creator', false);
  1061. // If needed, construct the query used for the reconciliation
  1062. if (!isset(self::$aSearches[$oDataSource->GetKey()]))
  1063. {
  1064. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1065. {
  1066. $aCriterias[] = ($sFilterCode == 'primary_key' ? 'id' : $sFilterCode).' = :'.$sFilterCode;
  1067. }
  1068. $sOQL = "SELECT ".$oDataSource->GetTargetClass()." WHERE ".implode(' AND ', $aCriterias);
  1069. self::$aSearches[$oDataSource->GetKey()] = DBObjectSearch::FromOQL($sOQL);
  1070. }
  1071. // Get the criterias for the search
  1072. $aFilterValues = array();
  1073. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1074. {
  1075. $value = $this->GetValueFromExtData($sFilterCode, $oSyncAtt, $oStatLog);
  1076. if (!is_null($value))
  1077. {
  1078. $aFilterValues[$sFilterCode] = $value;
  1079. }
  1080. else
  1081. {
  1082. // Reconciliation could not be performed - log and EXIT
  1083. $oStatLog->AddTrace("Could not reconcile on null value: ".$sFilterCode, $this);
  1084. $this->SetLastError('Could not reconcile on null value: '.$sFilterCode);
  1085. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1086. return;
  1087. }
  1088. }
  1089. $oDestSet = new DBObjectSet(self::$aSearches[$oDataSource->GetKey()], array(), $aFilterValues);
  1090. $iCount = $oDestSet->Count();
  1091. $aConditions = array();
  1092. foreach($aFilterValues as $sCode => $sValue)
  1093. {
  1094. $aConditions[] = $sCode.'='.$sValue;
  1095. }
  1096. $sConditionDesc = implode(' AND ', $aConditions);
  1097. // How many objects match the reconciliation criterias
  1098. switch($iCount)
  1099. {
  1100. case 0:
  1101. $oStatLog->AddTrace("Nothing found on: $sConditionDesc", $this);
  1102. if ($oDataSource->Get('action_on_zero') == 'create')
  1103. {
  1104. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1105. }
  1106. else // assumed to be 'error'
  1107. {
  1108. $oStatLog->AddTrace("Failed to reconcile (no match)", $this);
  1109. $this->SetLastError('Could not find a match for reconciliation');
  1110. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1111. }
  1112. break;
  1113. case 1:
  1114. $oStatLog->AddTrace("Found 1 object on: $sConditionDesc", $this);
  1115. if ($oDataSource->Get('action_on_one') == 'update')
  1116. {
  1117. $oDestObj = $oDestSet->Fetch();
  1118. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1119. $this->Set('dest_id', $oDestObj->GetKey());
  1120. $this->Set('dest_class', get_class($oDestObj));
  1121. }
  1122. else
  1123. {
  1124. // assumed to be 'error'
  1125. $oStatLog->AddTrace("Failed to reconcile (1 match)", $this);
  1126. $this->SetLastError('Found a match while expecting several');
  1127. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1128. }
  1129. break;
  1130. default:
  1131. $oStatLog->AddTrace("Found $iCount objects on: $sConditionDesc", $this);
  1132. if ($oDataSource->Get('action_on_multiple') == 'error')
  1133. {
  1134. $oStatLog->AddTrace("Failed to reconcile (N>1 matches)", $this);
  1135. $this->SetLastError($iCount.' destination objects match the reconciliation criterias: '.$sConditionDesc);
  1136. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1137. }
  1138. elseif ($oDataSource->Get('action_on_multiple') == 'create')
  1139. {
  1140. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1141. }
  1142. else
  1143. {
  1144. // assumed to be 'take_first'
  1145. $oDestObj = $oDestSet->Fetch();
  1146. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1147. $this->Set('dest_id', $oDestObj->GetKey());
  1148. $this->Set('dest_class', get_class($oDestObj));
  1149. }
  1150. }
  1151. break;
  1152. case 'modified':
  1153. $oDestObj = MetaModel::GetObject($oDataSource->GetTargetClass(), $this->Get('dest_id'));
  1154. if ($oDestObj == null)
  1155. {
  1156. $this->Set('status', 'orphan'); // The destination object has been deleted !
  1157. $this->SetLastError('Destination object deleted unexpectedly');
  1158. $oStatLog->Inc('stats_nb_obj_updated_errors');
  1159. }
  1160. else
  1161. {
  1162. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj', 'stats_nb_obj_updated_errors');
  1163. }
  1164. break;
  1165. default: // Do nothing in all other cases
  1166. }
  1167. $this->DBUpdateTracked($oChange);
  1168. }
  1169. /**
  1170. * Updates the destination object with the Extended data found in the synchro_data_XXXX table
  1171. */
  1172. protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, &$oStatLog, $sStatsCode, $sStatsCodeError)
  1173. {
  1174. $aValueTrace = array();
  1175. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1176. {
  1177. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1178. if (!is_null($value))
  1179. {
  1180. $oDestObj->Set($sAttCode, $value);
  1181. $aValueTrace[] = "$sAttCode: $value";
  1182. }
  1183. }
  1184. try
  1185. {
  1186. // Really modified ?
  1187. if ($oDestObj->IsModified())
  1188. {
  1189. $oDestObj->DBUpdateTracked($oChange);
  1190. $oStatLog->AddTrace('Updated object - Values: {'.implode(', ', $aValueTrace).'}', $this);
  1191. $oStatLog->Inc($sStatsCode.'_updated');
  1192. }
  1193. else
  1194. {
  1195. $oStatLog->AddTrace('Unchanged object', $this);
  1196. $oStatLog->Inc($sStatsCode.'_unchanged');
  1197. }
  1198. $this->Set('status_last_error', '');
  1199. $this->Set('status', 'synchronized');
  1200. }
  1201. catch(Exception $e)
  1202. {
  1203. $oStatLog->AddTrace("Failed to update destination object: {$e->getMessage()}", $this);
  1204. $this->SetLastError('Unable to update destination object: ', $e);
  1205. $oStatLog->Inc($sStatsCodeError);
  1206. }
  1207. }
  1208. /**
  1209. * Creates the destination object populating it with the Extended data found in the synchro_data_XXXX table
  1210. */
  1211. protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange, &$oStatLog)
  1212. {
  1213. $oDestObj = MetaModel::NewObject($sClass);
  1214. try
  1215. {
  1216. $aValueTrace = array();
  1217. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1218. {
  1219. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1220. if (!is_null($value))
  1221. {
  1222. $oDestObj->Set($sAttCode, $value);
  1223. $aValueTrace[] = "$sAttCode: $value";
  1224. }
  1225. }
  1226. $iNew = $oDestObj->DBInsertTracked($oChange);
  1227. $this->Set('dest_id', $oDestObj->GetKey());
  1228. $this->Set('dest_class', get_class($oDestObj));
  1229. $this->Set('status_dest_creator', true);
  1230. $this->Set('status_last_error', '');
  1231. $this->Set('status', 'synchronized');
  1232. $oStatLog->AddTrace("Created (".implode(', ', $aValueTrace).")", $this);
  1233. $oStatLog->Inc('stats_nb_obj_created');
  1234. }
  1235. catch(Exception $e)
  1236. {
  1237. $oStatLog->AddTrace("Failed to create $sClass ({$e->getMessage()})", $this);
  1238. $this->SetLastError('Unable to create destination object: ', $e);
  1239. $oStatLog->Inc('stats_nb_obj_created_errors');
  1240. }
  1241. }
  1242. /**
  1243. * Update the destination object with given values
  1244. */
  1245. public function UpdateDestObject($aValues, $oChange, &$oStatLog)
  1246. {
  1247. try
  1248. {
  1249. if ($this->Get('dest_class') == '')
  1250. {
  1251. $this->SetLastError('No destination object to update');
  1252. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1253. }
  1254. else
  1255. {
  1256. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1257. foreach($aValues as $sAttCode => $value)
  1258. {
  1259. $oDestObj->Set($sAttCode, $value);
  1260. }
  1261. $oDestObj->DBUpdateTracked($oChange);
  1262. $oStatLog->AddTrace("Replica marked as obsolete", $this);
  1263. $oStatLog->Inc('stats_nb_obj_obsoleted');
  1264. }
  1265. }
  1266. catch(Exception $e)
  1267. {
  1268. $this->SetLastError('Unable to update the destination object: ', $e);
  1269. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1270. }
  1271. }
  1272. /**
  1273. * Delete the destination object
  1274. */
  1275. public function DeleteDestObject($oChange, &$oStatLog)
  1276. {
  1277. if($this->Get('status_dest_creator'))
  1278. {
  1279. try
  1280. {
  1281. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1282. $oDestObj->DBDeleteTracked($oChange);
  1283. $this->DBDeleteTracked($oChange);
  1284. $oStatLog->Inc('stats_nb_obj_deleted');
  1285. }
  1286. catch(Exception $e)
  1287. {
  1288. $this->SetLastError('Unable to delete the destination object: ', $e);
  1289. $this->Set('status', 'obsolete');
  1290. $this->DBUpdateTracked($oChange);
  1291. $oStatLog->Inc('stats_nb_obj_deleted_errors');
  1292. }
  1293. }
  1294. else
  1295. {
  1296. $this->DBDeleteTracked($oChange);
  1297. $oStatLog->Inc('stats_nb_replica_disappeared_no_action');
  1298. }
  1299. }
  1300. /**
  1301. * Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
  1302. */
  1303. protected function GetValueFromExtData($sColumnName, $oSyncAtt, &$oStatLog)
  1304. {
  1305. // $aData should contain attributes defined either for reconciliation or create/update
  1306. $aData = $this->GetExtendedData();
  1307. // In any case, a null column means "ignore this column"
  1308. //
  1309. if (is_null($aData[$sColumnName]))
  1310. {
  1311. return null;
  1312. }
  1313. if (!is_null($oSyncAtt) && ($oSyncAtt instanceof SynchroAttExtKey))
  1314. {
  1315. $sReconcAttCode = $oSyncAtt->Get('reconciliation_attcode');
  1316. if (!empty($sReconcAttCode))
  1317. {
  1318. $oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1319. $sClass = $oDataSource->GetTargetClass();
  1320. $oAttDef = MetaModel::GetAttributeDef($sClass, $sColumnName);
  1321. $sRemoteClass = $oAttDef->GetTargetClass();
  1322. $oObj = MetaModel::GetObjectByColumn($sRemoteClass, $sReconcAttCode, $aData[$sColumnName], false);
  1323. if ($oObj)
  1324. {
  1325. return $oObj->GetKey();
  1326. }
  1327. else
  1328. {
  1329. // Note: differs from null (in which case the value would be left unchanged)
  1330. $oStatLog->AddTrace("Could not find [unique] object for '$sColumnName': searched on $sReconcAttCode = '$aData[$sColumnName]'", $this);
  1331. return 0;
  1332. }
  1333. }
  1334. }
  1335. return $aData[$sColumnName];
  1336. }
  1337. }
  1338. // TO DO: finalize.... admins only ? which options ? troubleshoot WebPageMenuNode::__construct(.... sEnableClass...) ?
  1339. //if (UserRights::IsAdministrator())
  1340. {
  1341. $oAdminMenu = new MenuGroup('AdminTools', 80 /* fRank */);
  1342. new OQLMenuNode('DataSources', 'SELECT SynchroDataSource', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  1343. new WebPageMenuNode('Test:RunSynchro', '../synchro/synchro_exec.php', $oAdminMenu->GetIndex(), 13 /* fRank */, 'SynchroDataSource');
  1344. }
  1345. ?>