synchrodatasource.class.inc.php 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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,addon/authentication", "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"=>86400, "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. // Not used yet !
  67. MetaModel::Init_AddAttribute(new AttributeEnum("user_delete_policy", array("allowed_values"=>new ValueSetEnum('everybody,administrators,nobody'), "sql"=>"user_delete_policy", "default_value"=>"nobody", "is_null_allowed"=>true, "depends_on"=>array())));
  68. MetaModel::Init_AddAttribute(new AttributeURL("url_icon", array("allowed_values"=>null, "sql"=>"url_icon", "default_value"=>null, "is_null_allowed"=>true, "target"=> '_top', "depends_on"=>array())));
  69. // The field below is not a real URL since it can contain placeholders like $replica->primary_key$ which are not syntactically allowed in a real URL
  70. MetaModel::Init_AddAttribute(new AttributeString("url_application", array("allowed_values"=>null, "sql"=>"url_application", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  71. // Display lists
  72. MetaModel::Init_SetZListItems('details', array(
  73. 'col:0'=> array(
  74. 'fieldset:SynchroDataSource:Description' => array('name','description','status','scope_class','user_id','url_icon','url_application')),
  75. 'col:1'=> array(
  76. 'fieldset:SynchroDataSource:Reconciliation' => array('reconciliation_policy','action_on_zero','action_on_one','action_on_multiple'),
  77. 'fieldset:SynchroDataSource:Deletion' => array('user_delete_policy','full_load_periodicity','delete_policy','delete_policy_update','delete_policy_retention'))
  78. )
  79. );
  80. MetaModel::Init_SetZListItems('list', array('scope_class', 'status', 'user_id', 'full_load_periodicity')); // Attributes to be displayed for a list
  81. // Search criteria
  82. MetaModel::Init_SetZListItems('standard_search', array('name', 'status', 'scope_class', 'user_id')); // Criteria of the std search form
  83. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  84. }
  85. public static $m_oCurrentTask = null;
  86. public static function GetCurrentTaskId()
  87. {
  88. if (is_object(self::$m_oCurrentTask))
  89. {
  90. return self::$m_oCurrentTask->GetKey();
  91. }
  92. else
  93. {
  94. return null;
  95. }
  96. }
  97. public function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  98. {
  99. if (!$this->IsNew())
  100. {
  101. $oPage->SetCurrentTab(Dict::S('Core:SynchroAttributes'));
  102. $oAttributeSet = $this->Get('attribute_list');
  103. $aAttributes = array();
  104. while($oAttribute = $oAttributeSet->Fetch())
  105. {
  106. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  107. }
  108. // Columns of the form
  109. $aAttribs = array();
  110. foreach(array('attcode', 'reconciliation', 'update', 'update_policy', 'reconciliation_attcode') as $s )
  111. {
  112. $aAttribs[$s] = array( 'label' => Dict::S("Core:SynchroAtt:$s"), "description" => Dict::S("Core:SynchroAtt:$s+"));
  113. }
  114. // Rows of the form
  115. $aValues = array();
  116. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  117. {
  118. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  119. {
  120. if (isset($aAttributes[$sAttCode]))
  121. {
  122. $oAttribute = $aAttributes[$sAttCode];
  123. }
  124. else
  125. {
  126. if ($oAttDef->IsExternalKey())
  127. {
  128. $oAttribute = new SynchroAttExtKey();
  129. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  130. }
  131. else
  132. {
  133. $oAttribute = new SynchroAttribute();
  134. }
  135. $oAttribute->Set('sync_source_id', $this->GetKey());
  136. $oAttribute->Set('attcode', $sAttCode);
  137. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  138. $oAttribute->Set('update', 1);
  139. $oAttribute->Set('update_policy', 'master_locked');
  140. }
  141. if (!$bEditMode)
  142. {
  143. // Read-only mode
  144. $aRow['reconciliation'] = $oAttribute->Get('reconcile') == 1 ? Dict::S('Core:SynchroReconcile:Yes') : Dict::S('Core:SynchroReconcile:No');
  145. $aRow['update'] = $oAttribute->Get('update') == 1 ? Dict::S('Core:SynchroUpdate:Yes') : Dict::S('Core:SynchroUpdate:No');
  146. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  147. $aRow['update_policy'] = $oAttribute->GetAsHTML('update_policy');
  148. if ($oAttDef->IsExternalKey())
  149. {
  150. $aRow['reconciliation_attcode'] = $oAttribute->GetAsHTML('reconciliation_attcode');
  151. }
  152. else
  153. {
  154. $aRow['reconciliation_attcode'] = '&nbsp;';
  155. }
  156. }
  157. else
  158. {
  159. // Edit mode
  160. $sAttCode = $oAttribute->Get('attcode');
  161. $sChecked = $oAttribute->Get('reconcile') == 1 ? 'checked' : '';
  162. $aRow['reconciliation'] = "<input type=\"checkbox\" name=\"reconciliation[$sAttCode]\" $sChecked/>";
  163. $sChecked = $oAttribute->Get('update') == 1 ? 'checked' : '';
  164. $aRow['update'] = "<input type=\"checkbox\" name=\"update[$sAttCode]\" $sChecked/>";
  165. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  166. $oUpdateAttDef = MetaModel::GetAttributeDef(get_class($oAttribute), 'update_policy');
  167. $aRow['update_policy'] = cmdbAbstractObject::GetFormElementForField($oPage, get_class($oAttribute), 'update_policy', $oUpdateAttDef, $oAttribute->Get('update_policy'), '', 'update_policy_'.$sAttCode, "[$sAttCode]");
  168. if ($oAttDef->IsExternalKey())
  169. {
  170. $aRow['reconciliation_attcode'] = $oAttribute->GetReconciliationFormElement($oAttDef->GetTargetClass(), "attr_reconciliation_attcode[$sAttCode]");
  171. }
  172. else
  173. {
  174. $aRow['reconciliation_attcode'] = '&nbsp;';
  175. }
  176. }
  177. $aValues[] = $aRow;
  178. }
  179. }
  180. $oPage->p(Dict::Format('Class:SynchroDataSource:DataTable', $this->GetDataTable()));
  181. $oPage->Table($aAttribs, $aValues);
  182. $this->DisplayStatusTab($oPage);
  183. }
  184. parent::DisplayBareRelations($oPage, $bEditMode);
  185. }
  186. /**
  187. * Displays the status (SynchroLog) of the datasource in a graphical manner
  188. * @param $oPage WebPage
  189. * @return void
  190. */
  191. protected function DisplayStatusTab(WebPage $oPage)
  192. {
  193. $oPage->SetCurrentTab(Dict::S('Core:SynchroStatus'));
  194. $sSelectSynchroLog = 'SELECT SynchroLog WHERE sync_source_id = :source_id';
  195. $oSetSynchroLog = new CMDBObjectSet(DBObjectSearch::FromOQL($sSelectSynchroLog), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()));
  196. if ($oSetSynchroLog->Count() > 0)
  197. {
  198. $oLastLog = $oSetSynchroLog->Fetch();
  199. $sStartDate = $oLastLog->Get('start_date');
  200. $oLastLog->Get('stats_nb_replica_seen');
  201. $iLastLog = 0;
  202. $iDSid = $this->GetKey();
  203. if ($oLastLog->Get('status') == 'running')
  204. {
  205. // Still running !
  206. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroRunningStartedOn_Date', $sStartDate).'</h2>');
  207. }
  208. else
  209. {
  210. $sEndDate = $oLastLog->Get('end_date');
  211. $iLastLog = $oLastLog->GetKey();
  212. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroEndedOn_Date', $sEndDate).'</h2>');
  213. }
  214. $oPage->add('<table class="synoptics"><tr><td style="color:#333;vertical-align:top">');
  215. // List all the log entries for the user to select
  216. $oPage->add('<h2 style="line-height:55px;">'.Dict::S('Core:Synchro:History').'</h2>');
  217. $oSetSynchroLog->Rewind();
  218. $oPage->add('<select size="25" onChange="UpdateSynoptics(this.value);">');
  219. $sSelected = ' selected'; // First log is selected by default
  220. $sScript = "var aSynchroLog = {\n";
  221. while($oLog = $oSetSynchroLog->Fetch())
  222. {
  223. $sLogTitle = Dict::Format('Core:SynchroLogTitle', $oLog->Get('status'), $oLog->Get('start_date'));
  224. $oPage->add('<option value="'.$oLog->GetKey().'"'.$sSelected.'>'.$sLogTitle.'</option>');
  225. $sSelected = ''; // only the first log is selected by default
  226. $aData = $this->ProcessLog($oLog);
  227. $sScript .= '"'.$oLog->GetKey().'": '.json_encode($aData).",\n";
  228. }
  229. $sScript .= "end: 'Done'";
  230. $sScript .= "};\n";
  231. $sScript .= <<<EOF
  232. var sLastLog = '$iLastLog';
  233. function UpdateSynoptics(id)
  234. {
  235. var aValues = aSynchroLog[id];
  236. for (var sKey in aValues)
  237. {
  238. $('#c_'+sKey).html(aValues[sKey]);
  239. var fOpacity = (aValues[sKey] == 0) ? 0.3 : 1;
  240. $('#'+sKey).fadeTo("slow", fOpacity);
  241. }
  242. //alert('id = '+id+', lastLog='+sLastLog+', id==sLastLog: '+(id==sLastLog)+' obj_updated_errors: '+aValues['obj_updated_errors']);
  243. if ( (id == sLastLog) && (aValues['obj_new_errors'] > 0) )
  244. {
  245. $('#new_errors_link').show();
  246. }
  247. else
  248. {
  249. $('#new_errors_link').hide();
  250. }
  251. if ( (id == sLastLog) && (aValues['obj_updated_errors'] > 0) )
  252. {
  253. $('#updated_errors_link').show();
  254. }
  255. else
  256. {
  257. $('#updated_errors_link').hide();
  258. }
  259. if ( (id == sLastLog) && (aValues['obj_disappeared_errors'] > 0) )
  260. {
  261. $('#disappeared_errors_link').show();
  262. }
  263. else
  264. {
  265. $('#disappeared_errors_link').hide();
  266. }
  267. }
  268. EOF
  269. ;
  270. $oPage->add_script($sScript);
  271. $oPage->add('</select>');
  272. $oPage->add('</td><td style="vertical-align:top;">');
  273. // Now build the big "synoptics" view
  274. $aData = $this->ProcessLog($oLastLog);
  275. $sNbReplica = $this->GetIcon()."&nbsp;".Dict::Format('Core:Synchro:Nb_Replica', "<span id=\"c_nb_replica_total\">{$aData['nb_replica_total']}</span>");
  276. $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>");
  277. $oPage->add(
  278. <<<EOF
  279. <table class="synoptics">
  280. <tr class="synoptics_header">
  281. <td>$sNbReplica</td><td>&nbsp;</td><td>$sNbObjects</td>
  282. </tr>
  283. <tr>
  284. EOF
  285. );
  286. $sBaseOQL = "SELECT SynchroReplica WHERE sync_source_id=".$this->GetKey()." AND status_last_error!=''";
  287. $oPage->add($this->HtmlBox('repl_ignored', $aData, '#999').'<td colspan="2">&nbsp;</td>');
  288. $oPage->add("</tr>\n<tr>");
  289. $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'));
  290. $oPage->add("</tr>\n<tr>");
  291. $oPage->add($this->HtmlBox('obj_deleted', $aData, '#000'));
  292. $oPage->add("</tr>\n<tr>");
  293. $oPage->add($this->HtmlBox('obj_obsoleted', $aData, '#630'));
  294. $oPage->add("</tr>\n<tr>");
  295. $sOQL = urlencode($sBaseOQL." AND status='obsolete'");
  296. $oPage->add($this->HtmlBox('obj_disappeared_errors', $aData, '#C00', '', " <a style=\"color:#fff\" href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\" id=\"disappeared_errors_link\">Show</a>"));
  297. $oPage->add("</tr>\n<tr>");
  298. $oPage->add($this->HtmlBox('repl_existing', $aData, '#093', 'rowspan="3"').'<td rowspan="3" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_unchanged', $aData, '#393'));
  299. $oPage->add("</tr>\n<tr>");
  300. $oPage->add($this->HtmlBox('obj_updated', $aData, '#3C3'));
  301. $oPage->add("</tr>\n<tr>");
  302. $sOQL = urlencode($sBaseOQL." AND status='modified'");
  303. $oPage->add($this->HtmlBox('obj_updated_errors', $aData, '#C00', '', " <a style=\"color:#fff\" href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\" id=\"updated_errors_link\">Show</a>"));
  304. $oPage->add("</tr>\n<tr>");
  305. $oPage->add($this->HtmlBox('repl_new', $aData, '#339', 'rowspan="4"').'<td rowspan="4" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_new_unchanged', $aData, '#393'));
  306. $oPage->add("</tr>\n<tr>");
  307. $oPage->add($this->HtmlBox('obj_new_updated', $aData, '#3C3'));
  308. $oPage->add("</tr>\n<tr>");
  309. $oPage->add($this->HtmlBox('obj_created', $aData, '#339'));
  310. $oPage->add("</tr>\n<tr>");
  311. $sOQL = urlencode($sBaseOQL." AND status='new'");
  312. $oPage->add($this->HtmlBox('obj_new_errors', $aData, '#C00', '', " <a style=\"color:#fff\" href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\" id=\"new_errors_link\">Show</a>"));
  313. $oPage->add("</tr>\n</table>\n");
  314. $oPage->add('</td></tr></table>');
  315. $oPage->add_ready_script("UpdateSynoptics('$iLastLog')");
  316. }
  317. else
  318. {
  319. $oPage->p('<h2>'.Dict::S('Core:Synchro:NeverRun').'</h2>');
  320. }
  321. }
  322. protected function HtmlBox($sId, $aData, $sColor, $sHTMLAttribs = '', $sErrorLink = '')
  323. {
  324. $iCount = $aData[$sId];
  325. $sCount = "<span id=\"c_{$sId}\">$iCount</span>";
  326. $sLabel = Dict::Format('Core:Synchro:label_'.$sId, $sCount);
  327. $sOpacity = ($iCount==0) ? "opacity:0.3;" : "";
  328. return "<td id=\"$sId\" style=\"background-color:$sColor;$sOpacity;\" {$sHTMLAttribs}>{$sLabel}{$sErrorLink}</td>";
  329. }
  330. protected function ProcessLog($oLastLog)
  331. {
  332. $aData = array(
  333. 'obj_deleted' => $oLastLog->Get('stats_nb_obj_deleted'),
  334. 'obj_obsoleted' => $oLastLog->Get('stats_nb_obj_obsoleted'),
  335. 'obj_disappeared_errors' => $oLastLog->Get('stats_nb_obj_obsoleted_errors') + $oLastLog->Get('stats_nb_obj_deleted_errors'),
  336. 'obj_disappeared_no_action' => $oLastLog->Get('stats_nb_replica_disappeared_no_action'),
  337. 'obj_updated' => $oLastLog->Get('stats_nb_obj_updated'),
  338. 'obj_updated_errors' => $oLastLog->Get('stats_nb_obj_updated_errors'),
  339. 'obj_new_updated' => $oLastLog->Get('stats_nb_obj_new_updated'),
  340. 'obj_new_unchanged' => $oLastLog->Get('stats_nb_obj_new_unchanged'),
  341. 'obj_created' => $oLastLog->Get('stats_nb_obj_created'),
  342. 'obj_created_errors' => $oLastLog->Get('stats_nb_obj_created_errors'),
  343. );
  344. $iReconciledErrors = $oLastLog->Get('stats_nb_replica_reconciled_errors');
  345. $iDisappeared = $aData['obj_disappeared_errors'] + $aData['obj_obsoleted'] + $aData['obj_deleted'] + $aData['obj_disappeared_no_action'];
  346. $aData['repl_disappeared'] = $iDisappeared;
  347. $iNewErrors = $aData['obj_created_errors'] + $oLastLog->Get('stats_nb_replica_reconciled_errors');
  348. $aData['obj_new_errors'] = $iNewErrors;
  349. $iNew = $aData['obj_created'] + $iNewErrors + $aData['obj_new_updated'] + $aData['obj_new_unchanged'];
  350. $aData['repl_new'] = $iNew;
  351. $iExisting = $oLastLog->Get('stats_nb_replica_seen') - $iNew;
  352. $aData['repl_existing'] = $iExisting;
  353. $aData['obj_unchanged'] = $iExisting - $aData['obj_updated'] - $aData['obj_updated_errors'];
  354. $iIgnored = $oLastLog->Get('stats_nb_replica_total') - $iNew - $iExisting - $iDisappeared;
  355. $aData['repl_ignored'] = $iIgnored;
  356. $aData['nb_obj_total'] = $iNew + $iExisting + $iDisappeared;
  357. $aData['nb_replica_total'] = $aData['nb_obj_total'] + $iIgnored;
  358. return $aData;
  359. }
  360. public function GetIcon($bImgTag = true, $sMoreStyles = '')
  361. {
  362. if ($this->Get('url_icon') == '') return MetaModel::GetClassIcon(get_class($this), $bImgTag);
  363. if ($bImgTag)
  364. {
  365. return "<img src=\"".$this->Get('url_icon')."\" style=\"vertical-align:middle;$sMoreStyles\"/>";
  366. }
  367. return $this->Get('url_icon');
  368. }
  369. /**
  370. * Get the actual hyperlink to the remote application for the given replica and dest object
  371. */
  372. public function GetApplicationUrl(DBObject $oDestObj, SynchroReplica $oReplica)
  373. {
  374. if ($this->Get('url_application') == '') return '';
  375. $aSearches = array();
  376. $aReplacements = array();
  377. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  378. {
  379. if ($oAttDef->IsScalar())
  380. {
  381. $aSearches[] = '$this->'.$sAttCode.'$';
  382. $aReplacements[] = $oDestObj->Get($sAttCode);
  383. }
  384. }
  385. $aData = $oReplica->LoadExtendedDataFromTable($this->GetDataTable());
  386. foreach($aData as $sColumn => $value)
  387. {
  388. $aSearches[] = '$replica->'.$sColumn.'$';
  389. $aReplacements[] = $value;
  390. }
  391. return str_replace($aSearches, $aReplacements, $this->Get('url_application'));
  392. }
  393. public function GetAttributeFlags($sAttCode, &$aReasons = array())
  394. {
  395. if (($sAttCode == 'scope_class') && (!$this->IsNew()))
  396. {
  397. return OPT_ATT_READONLY;
  398. }
  399. return parent::GetAttributeFlags($sAttCode, $aReasons);
  400. }
  401. public function UpdateObject($sFormPrefix = '')
  402. {
  403. parent::UpdateObject($sFormPrefix);
  404. // And now read the other post parameters...
  405. $oAttributeSet = $this->Get('attribute_list');
  406. $aAttributes = array();
  407. while($oAttribute = $oAttributeSet->Fetch())
  408. {
  409. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  410. }
  411. $aReconcile = utils::ReadPostedParam('reconciliation', array());
  412. $aUpdate = utils::ReadPostedParam('update', array());
  413. $aUpdatePolicy = utils::ReadPostedParam('attr_update_policy', array());
  414. $aReconciliation = utils::ReadPostedParam('attr_reconciliation_attcode', array());
  415. // update_policy cannot be empty, so there is one entry per attribute, use this to iterate
  416. // through all the writable attributes
  417. foreach($aUpdatePolicy as $sAttCode => $sValue)
  418. {
  419. if(!isset($aAttributes[$sAttCode]))
  420. {
  421. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  422. if ($oAttDef->IsExternalKey())
  423. {
  424. $oAttribute = new SynchroAttExtKey();
  425. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  426. }
  427. else
  428. {
  429. $oAttribute = new SynchroAttribute();
  430. }
  431. $oAttribute->Set('sync_source_id', $this->GetKey());
  432. $oAttribute->Set('attcode', $sAttCode);
  433. }
  434. else
  435. {
  436. $oAttribute = $aAttributes[$sAttCode];
  437. }
  438. $bReconcile = 0;
  439. if (isset($aReconcile[$sAttCode]))
  440. {
  441. $bReconcile = $aReconcile[$sAttCode] == 'on' ? 1 : 0;
  442. }
  443. $bUpdate = 0 ; // Default / initial value
  444. if (isset($aUpdate[$sAttCode]))
  445. {
  446. $bUpdate = $aUpdate[$sAttCode] == 'on' ? 1 : 0;
  447. }
  448. $oAttribute->Set('reconcile', $bReconcile);
  449. $oAttribute->Set('update', $bUpdate);
  450. $oAttribute->Set('update_policy', $sValue);
  451. if ($oAttribute instanceof SynchroAttExtKey)
  452. {
  453. $oAttribute->Set('reconciliation_attcode', $aReconciliation[$sAttCode]);
  454. }
  455. $oAttributeSet->AddObject($oAttribute);
  456. }
  457. $this->Set('attribute_list', $oAttributeSet);
  458. }
  459. /*
  460. * Overload the standard behavior
  461. */
  462. public function ComputeValues()
  463. {
  464. parent::ComputeValues();
  465. if ($this->IsNew())
  466. {
  467. // When inserting a new datasource object, also create the SynchroAttribute objects
  468. // for each field of the target class
  469. // Create all the SynchroAttribute records
  470. $oAttributeSet = $this->Get('attribute_list');
  471. if ($oAttributeSet->Count() == 0)
  472. {
  473. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  474. {
  475. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  476. {
  477. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  478. if ($oAttDef->IsExternalKey())
  479. {
  480. $oAttribute = new SynchroAttExtKey();
  481. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  482. }
  483. else
  484. {
  485. $oAttribute = new SynchroAttribute();
  486. }
  487. $oAttribute->Set('sync_source_id', $this->GetKey());
  488. $oAttribute->Set('attcode', $sAttCode);
  489. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  490. $oAttribute->Set('update', 1);
  491. $oAttribute->Set('update_policy', 'master_locked');
  492. $oAttributeSet->AddObject($oAttribute);
  493. }
  494. }
  495. $this->Set('attribute_list', $oAttributeSet);
  496. }
  497. }
  498. }
  499. public function DoCheckToWrite()
  500. {
  501. parent::DoCheckToWrite();
  502. // Check that there is at least one reconciliation key defined
  503. if ($this->Get('reconciliation_policy') == 'use_attributes')
  504. {
  505. $oSet = $this->Get('attribute_list');
  506. $oSynchroAttributeList = $oSet->ToArray();
  507. $bReconciliationKey = false;
  508. foreach($oSynchroAttributeList as $oSynchroAttribute)
  509. {
  510. if ($oSynchroAttribute->Get('reconcile') == 1)
  511. {
  512. $bReconciliationKey = true; // At least one key is defined
  513. }
  514. }
  515. if (!$bReconciliationKey)
  516. {
  517. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:AtLeastOneReconciliationKeyMustBeSpecified');
  518. }
  519. }
  520. // If 'update_then_delete' is specified there must be a delete_retention_period
  521. if (($this->Get('delete_policy') == 'update_then_delete') && ($this->Get('delete_policy_retention') == 0))
  522. {
  523. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeleteRetentionDurationMustBeSpecified');
  524. }
  525. // If update is specified, then something to update must be defined
  526. if ((($this->Get('delete_policy') == 'update_then_delete') || ($this->Get('delete_policy') == 'update'))
  527. && ($this->Get('delete_policy_update') == ''))
  528. {
  529. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeletePolicyUpdateMustBeSpecified');
  530. }
  531. }
  532. public function GetTargetClass()
  533. {
  534. return $this->Get('scope_class');
  535. }
  536. public function GetDataTable()
  537. {
  538. $sName = strtolower($this->GetTargetClass());
  539. $sName = str_replace('\'"&@|\\/ ', '_', $sName); // Remove forbidden characters from the table name
  540. $sName .= '_'.$this->GetKey(); // Add a suffix for unicity
  541. $sTable = MetaModel::GetConfig()->GetDBSubName()."synchro_data_$sName"; // Add the prefix if any
  542. return $sTable;
  543. }
  544. /**
  545. * When the new datasource has been created, let's create the synchro_data table
  546. * that will hold the data records and the correspoding triggers which will maintain
  547. * both tables in sync
  548. */
  549. protected function AfterInsert()
  550. {
  551. parent::AfterInsert();
  552. $sTable = $this->GetDataTable();
  553. $aColumns = $this->GetSQLColumns();
  554. $aFieldDefs = array();
  555. // Allow '0', otherwise mysql will render an error when the id is not given
  556. // (the trigger is expected to set the value, but it is not executed soon enough)
  557. $aFieldDefs[] = "id INTEGER(11) NOT NULL DEFAULT 0 ";
  558. $aFieldDefs[] = "`primary_key` VARCHAR(255) NULL DEFAULT NULL";
  559. foreach($aColumns as $sColumn => $ColSpec)
  560. {
  561. $aFieldDefs[] = "`$sColumn` $ColSpec NULL DEFAULT NULL";
  562. }
  563. $aFieldDefs[] = "INDEX (id)";
  564. $aFieldDefs[] = "INDEX (primary_key)";
  565. $sFieldDefs = implode(', ', $aFieldDefs);
  566. $sCreateTable = "CREATE TABLE `$sTable` ($sFieldDefs) ENGINE = innodb;";
  567. CMDBSource::Query($sCreateTable);
  568. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_bi` BEFORE INSERT ON $sTable";
  569. $sTriggerInsert .= " FOR EACH ROW";
  570. $sTriggerInsert .= " BEGIN";
  571. $sTriggerInsert .= " INSERT INTO priv_sync_replica (sync_source_id, status_last_seen, `status`) VALUES ({$this->GetKey()}, NOW(), 'new');";
  572. $sTriggerInsert .= " SET NEW.id = LAST_INSERT_ID();";
  573. $sTriggerInsert .= " END;";
  574. CMDBSource::Query($sTriggerInsert);
  575. $aModified = array();
  576. foreach($aColumns as $sColumn => $ColSpec)
  577. {
  578. // <=> is a null-safe 'EQUALS' operator (there is no equivalent for "DIFFERS FROM")
  579. $aModified[] = "NOT(NEW.`$sColumn` <=> OLD.`$sColumn`)";
  580. }
  581. $sIsModified = '('.implode(') OR (', $aModified).')';
  582. // Update the replica
  583. //
  584. // status is forced to "new" if the replica was obsoleted directly from the state "new" (dest_id = null)
  585. // otherwise, if status was either 'obsolete' or 'synchronized' it is turned into 'modified' or 'synchronized' depending on the changes
  586. // otherwise, the status is left as is
  587. $sTriggerUpdate = "CREATE TRIGGER `{$sTable}_bu` BEFORE UPDATE ON $sTable";
  588. $sTriggerUpdate .= " FOR EACH ROW";
  589. $sTriggerUpdate .= " BEGIN";
  590. $sTriggerUpdate .= " IF @itopuser is null THEN";
  591. $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;";
  592. $sTriggerUpdate .= " SET NEW.id = OLD.id;"; // make sure this id won't change
  593. $sTriggerUpdate .= " END IF;";
  594. $sTriggerUpdate .= " END;";
  595. CMDBSource::Query($sTriggerUpdate);
  596. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_ad` AFTER DELETE ON $sTable";
  597. $sTriggerInsert .= " FOR EACH ROW";
  598. $sTriggerInsert .= " BEGIN";
  599. $sTriggerInsert .= " DELETE FROM priv_sync_replica WHERE id = OLD.id;";
  600. $sTriggerInsert .= " END;";
  601. CMDBSource::Query($sTriggerInsert);
  602. }
  603. protected function AfterDelete()
  604. {
  605. parent::AfterDelete();
  606. $sTable = $this->GetDataTable();
  607. $sDropTable = "DROP TABLE `$sTable`";
  608. CMDBSource::Query($sDropTable);
  609. // TO DO - check that triggers get dropped with the table
  610. }
  611. /**
  612. * Perform a synchronization between the data stored in the replicas (&synchro_data_xxx_xx table)
  613. * and the iTop objects. If the lastFullLoadStartDate is NOT specified then the full_load_periodicity
  614. * is used to determine which records are obsolete.
  615. * @param Hash $aTraces Debugs/Trace information, one or more entries per replica
  616. * @param DateTime $oLastFullLoadStartDate Date of the last full load (start date/time), if known
  617. * @return void
  618. */
  619. public function Synchronize($oLastFullLoadStartDate = null)
  620. {
  621. // Create a change used for logging all the modifications/creations happening during the synchro
  622. $oMyChange = MetaModel::NewObject("CMDBChange");
  623. $oMyChange->Set("date", time());
  624. $sUserString = CMDBChange::GetCurrentUserName();
  625. $oMyChange->Set("userinfo", $sUserString.' '.Dict::S('Core:SyncDataExchangeComment'));
  626. $iChangeId = $oMyChange->DBInsert();
  627. // Start logging this execution (stats + protection against reentrance)
  628. //
  629. $oStatLog = new SynchroLog();
  630. $oStatLog->Set('sync_source_id', $this->GetKey());
  631. $oStatLog->Set('start_date', time());
  632. $oStatLog->Set('status', 'running');
  633. $oStatLog->Set('stats_nb_replica_seen', 0);
  634. $oStatLog->Set('stats_nb_replica_total', 0);
  635. $oStatLog->Set('stats_nb_obj_deleted', 0);
  636. $oStatLog->Set('stats_nb_obj_deleted_errors', 0);
  637. $oStatLog->Set('stats_nb_obj_obsoleted', 0);
  638. $oStatLog->Set('stats_nb_obj_obsoleted_errors', 0);
  639. $oStatLog->Set('stats_nb_obj_created', 0);
  640. $oStatLog->Set('stats_nb_obj_created_errors', 0);
  641. $oStatLog->Set('stats_nb_obj_updated', 0);
  642. $oStatLog->Set('stats_nb_obj_updated_errors', 0);
  643. // $oStatLog->Set('stats_nb_replica_reconciled', 0);
  644. $oStatLog->Set('stats_nb_replica_reconciled_errors', 0);
  645. $oStatLog->Set('stats_nb_replica_disappeared_no_action', 0);
  646. $oStatLog->Set('stats_nb_obj_new_updated', 0);
  647. $oStatLog->Set('stats_nb_obj_new_unchanged',0);
  648. $sSelectTotal = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  649. $oSetTotal = new DBObjectSet(DBObjectSearch::FromOQL($sSelectTotal), array() /* order by*/, array('source_id' => $this->GetKey()));
  650. $oStatLog->Set('stats_nb_replica_total', $oSetTotal->Count());
  651. $oStatLog->DBInsertTracked($oMyChange);
  652. self::$m_oCurrentTask = $this;
  653. try
  654. {
  655. $this->DoSynchronize($oLastFullLoadStartDate, $oMyChange, $oStatLog);
  656. $oStatLog->Set('end_date', time());
  657. $oStatLog->Set('status', 'completed');
  658. $oStatLog->DBUpdateTracked($oMyChange);
  659. }
  660. catch (SynchroExceptionNotStarted $e)
  661. {
  662. // Set information for reporting... but delete the object in DB
  663. $oStatLog->Set('end_date', time());
  664. $oStatLog->Set('status', 'error');
  665. $oStatLog->Set('last_error', $e->getMessage());
  666. $oStatLog->DBDeleteTracked($oMyChange);
  667. }
  668. catch (Exception $e)
  669. {
  670. $oStatLog->Set('end_date', time());
  671. $oStatLog->Set('status', 'error');
  672. $oStatLog->Set('last_error', $e->getMessage());
  673. $oStatLog->DBUpdateTracked($oMyChange);
  674. }
  675. self::$m_oCurrentTask = null;
  676. return $oStatLog;
  677. }
  678. protected function DoSynchronize($oLastFullLoadStartDate, $oMyChange, &$oStatLog)
  679. {
  680. if ($this->Get('status') == 'obsolete')
  681. {
  682. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceObsolete'));
  683. }
  684. if (!UserRights::IsAdministrator() && $this->Get('user_id') != UserRights::GetUserId())
  685. {
  686. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceAccessRestriction'));
  687. }
  688. // Get the list of SQL columns
  689. $sClass = $this->GetTargetClass();
  690. $aAttCodesExpected = array();
  691. $aAttCodesToReconcile = array();
  692. $aAttCodesToUpdate = array();
  693. $sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND (update = 1 OR reconcile = 1)";
  694. $oSetAtt = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */);
  695. while ($oSyncAtt = $oSetAtt->Fetch())
  696. {
  697. if ($oSyncAtt->Get('update'))
  698. {
  699. $aAttCodesToUpdate[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  700. }
  701. if ($oSyncAtt->Get('reconcile'))
  702. {
  703. $aAttCodesToReconcile[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  704. }
  705. $aAttCodesExpected[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  706. }
  707. $aColumns = $this->GetSQLColumns(array_keys($aAttCodesExpected));
  708. $aExtDataFields = array_keys($aColumns);
  709. $aExtDataFields[] = 'primary_key';
  710. $aExtDataSpec = array(
  711. 'table' => $this->GetDataTable(),
  712. 'join_key' => 'id',
  713. 'fields' => $aExtDataFields
  714. );
  715. // Get the list of attributes, determine reconciliation keys and update targets
  716. //
  717. if ($this->Get('reconciliation_policy') == 'use_attributes')
  718. {
  719. $aReconciliationKeys = $aAttCodesToReconcile;
  720. }
  721. elseif ($this->Get('reconciliation_policy') == 'use_primary_key')
  722. {
  723. // Override the settings made at the attribute level !
  724. $aReconciliationKeys = array("primary_key" => null);
  725. }
  726. $oStatLog->AddTrace("Update of: {".implode(', ', array_keys($aAttCodesToUpdate))."}");
  727. $oStatLog->AddTrace("Reconciliation on: {".implode(', ', array_keys($aReconciliationKeys))."}");
  728. if (count($aAttCodesToUpdate) == 0)
  729. {
  730. $oStatLog->AddTrace("No attribute to update");
  731. throw new SynchroExceptionNotStarted('There is no attribute to update');
  732. }
  733. if (count($aReconciliationKeys) == 0)
  734. {
  735. $oStatLog->AddTrace("No attribute for reconciliation");
  736. throw new SynchroExceptionNotStarted('No attribute for reconciliation');
  737. }
  738. $aAttributes = array();
  739. foreach($aAttCodesToUpdate as $sAttCode => $oSyncAtt)
  740. {
  741. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  742. if ($oAttDef->IsWritable() && $oAttDef->IsScalar())
  743. {
  744. $aAttributes[$sAttCode] = $oSyncAtt;
  745. }
  746. }
  747. $sDeletePolicy = $this->Get('delete_policy');
  748. // Count the replicas
  749. $sSelectAll = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  750. $oSetAll = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAll), array() /* order by*/, array('source_id' => $this->GetKey()));
  751. $iCountAllReplicas = $oSetAll->Count();
  752. $oStatLog->Set('stats_nb_replica_total', $iCountAllReplicas);
  753. // Get all the replicas that were not seen in the last import and mark them as obsolete
  754. if ($oLastFullLoadStartDate == null)
  755. {
  756. // No previous import known, use the full_load_periodicity value... and the current date
  757. $oLastFullLoadStartDate = new DateTime(); // Now
  758. $iLoadPeriodicity = $this->Get('full_load_periodicity'); // Duration in seconds
  759. if ($iLoadPeriodicity > 0)
  760. {
  761. $sInterval = "-$iLoadPeriodicity seconds";
  762. $oLastFullLoadStartDate->Modify($sInterval);
  763. }
  764. }
  765. $sLimitDate = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  766. $oStatLog->AddTrace("Limit Date: $sLimitDate");
  767. $sSelectToObsolete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
  768. $oSetToObsolete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
  769. if (($iCountAllReplicas > 10) && ($iCountAllReplicas == $oSetToObsolete->Count()))
  770. {
  771. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncTooManyMissingReplicas'));
  772. }
  773. while($oReplica = $oSetToObsolete->Fetch())
  774. {
  775. switch ($sDeletePolicy)
  776. {
  777. case 'update':
  778. case 'update_then_delete':
  779. $oStatLog->AddTrace("Destination object to be updated", $oReplica);
  780. $aToUpdate = array();
  781. $aToUpdate = explode(';', $this->Get('delete_policy_update')); //ex: 'status:obsolete;description:stopped',
  782. foreach($aToUpdate as $sUpdateSpec)
  783. {
  784. $aUpdateSpec = explode(':', $sUpdateSpec);
  785. if (count($aUpdateSpec) == 2)
  786. {
  787. $sAttCode = $aUpdateSpec[0];
  788. $sValue = $aUpdateSpec[1];
  789. $aToUpdate[$sAttCode] = $sValue;
  790. }
  791. }
  792. $oReplica->UpdateDestObject($aToUpdate, $oMyChange, $oStatLog);
  793. $oReplica->Set('status', 'obsolete');
  794. $oReplica->DBUpdateTracked($oMyChange);
  795. break;
  796. case 'delete':
  797. default:
  798. $oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  799. $oReplica->DeleteDestObject($oMyChange, $oStatLog);
  800. }
  801. }
  802. //Count "seen" objects
  803. $sSelectSeen = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen >= :last_import";
  804. $oSetSeen = new DBObjectSet(DBObjectSearch::FromOQL($sSelectSeen), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
  805. $oStatLog->Set('stats_nb_replica_seen', $oSetSeen->Count());
  806. // Get all the replicas that are 'new' or modified
  807. //
  808. $sSelectToSync = "SELECT SynchroReplica WHERE (status = 'new' OR status = 'modified') AND sync_source_id = :source_id";
  809. $oSetToSync = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, $aExtDataSpec, 0 /* limitCount */, 0 /* limitStart */);
  810. while($oReplica = $oSetToSync->Fetch())
  811. {
  812. $oReplica->Synchro($this, $aReconciliationKeys, $aAttributes, $oMyChange, $oStatLog);
  813. }
  814. // Get all the replicas that are to be deleted
  815. //
  816. if ($sDeletePolicy == 'update_then_delete')
  817. {
  818. $oDeletionDate = $oLastFullLoadStartDate;
  819. $iDeleteRetention = $this->Get('delete_policy_retention'); // Duration in seconds
  820. if ($iDeleteRetention > 0)
  821. {
  822. $sInterval = "-$iDeleteRetention seconds";
  823. $oDeletionDate->Modify($sInterval);
  824. }
  825. $sDeletionDate = $oDeletionDate->Format('Y-m-d H:i:s');
  826. $oStatLog->AddTrace("Deletion date: $sDeletionDate");
  827. $sSelectToDelete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('obsolete') AND status_last_seen < :last_import";
  828. $oSetToDelete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sDeletionDate));
  829. while($oReplica = $oSetToDelete->Fetch())
  830. {
  831. $oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  832. $oReplica->DeleteDestObject($oMyChange, $oStatLog);
  833. }
  834. }
  835. }
  836. /**
  837. * Get the list of SQL columns corresponding to a particular list of attribute codes
  838. * Defaults to the whole list of columns for the current class
  839. */
  840. public function GetSQLColumns($aAttributeCodes = null)
  841. {
  842. $aColumns = array();
  843. $sClass = $this->GetTargetClass();
  844. if (is_null($aAttributeCodes))
  845. {
  846. $aAttributeCodes = array();
  847. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  848. {
  849. if ($sAttCode == 'finalclass') continue;
  850. $aAttributeCodes[] = $sAttCode;
  851. }
  852. }
  853. foreach($aAttributeCodes as $sAttCode)
  854. {
  855. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  856. if ($oAttDef->IsExternalKey())
  857. {
  858. // The pkey might be used as well as any other key column
  859. $aColumns[$sAttCode] = 'VARCHAR (255)';
  860. }
  861. else
  862. {
  863. foreach($oAttDef->GetImportColumns() as $sField => $sDBFieldType)
  864. {
  865. $aColumns[$sField] = $sDBFieldType;
  866. }
  867. }
  868. }
  869. return $aColumns;
  870. }
  871. public function IsRunning()
  872. {
  873. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='running'";
  874. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  875. if ($oSet->Count() < 1)
  876. {
  877. $bRet = false;
  878. }
  879. else
  880. {
  881. $bRet = true;
  882. }
  883. return $bRet;
  884. }
  885. public function GetLatestLog()
  886. {
  887. $oLog = null;
  888. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id";
  889. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  890. if ($oSet->Count() >= 1)
  891. {
  892. $oLog = $oSet->Fetch();
  893. }
  894. return $oLog;
  895. }
  896. // TO DO: remove if still unused
  897. /**
  898. * Retrieve from the log, the date of the last completed import
  899. * @return DateTime
  900. */
  901. public function GetLastCompletedImportDate()
  902. {
  903. $date = null;
  904. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='completed'";
  905. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('end_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 0 /* limitCount */, 0 /* limitStart */);
  906. if ($oSet->Count() >= 1)
  907. {
  908. $oLog = $oSet->Fetch();
  909. $date = $oLog->Get('end_date');
  910. }
  911. else
  912. {
  913. // TO DO: remove trace
  914. echo "<p>No completed log found</p>\n";
  915. }
  916. return $date;
  917. }
  918. }
  919. class SynchroAttribute extends cmdbAbstractObject
  920. {
  921. public static function Init()
  922. {
  923. $aParams = array
  924. (
  925. "category" => "core/cmdb,view_in_gui",
  926. "key_type" => "autoincrement",
  927. "name_attcode" => "",
  928. "state_attcode" => "",
  929. "reconc_keys" => array(),
  930. "db_table" => "priv_sync_att",
  931. "db_key_field" => "id",
  932. "db_finalclass_field" => "",
  933. "display_template" => "",
  934. );
  935. MetaModel::Init_Params($aParams);
  936. MetaModel::Init_InheritAttributes();
  937. 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_SILENT, "depends_on"=>array())));
  938. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  939. MetaModel::Init_AddAttribute(new AttributeBoolean("update", array("allowed_values"=>null, "sql"=>"update", "default_value"=>true, "is_null_allowed"=>false, "depends_on"=>array())));
  940. MetaModel::Init_AddAttribute(new AttributeBoolean("reconcile", array("allowed_values"=>null, "sql"=>"reconcile", "default_value"=>false, "is_null_allowed"=>false, "depends_on"=>array())));
  941. 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())));
  942. // Display lists
  943. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for the complete details
  944. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  945. // Search criteria
  946. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  947. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  948. }
  949. }
  950. class SynchroAttExtKey extends SynchroAttribute
  951. {
  952. public static function Init()
  953. {
  954. $aParams = array
  955. (
  956. "category" => "core/cmdb,view_in_gui",
  957. "key_type" => "autoincrement",
  958. "name_attcode" => "",
  959. "state_attcode" => "",
  960. "reconc_keys" => array(),
  961. "db_table" => "priv_sync_att_extkey",
  962. "db_key_field" => "id",
  963. "db_finalclass_field" => "",
  964. "display_template" => "",
  965. );
  966. MetaModel::Init_Params($aParams);
  967. MetaModel::Init_InheritAttributes();
  968. MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  969. // Display lists
  970. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
  971. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  972. // Search criteria
  973. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  974. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  975. }
  976. public function GetReconciliationFormElement($sTargetClass, $sFieldName)
  977. {
  978. $sHtml = "<select name=\"$sFieldName\">\n";
  979. $sSelected = (''== $this->Get('reconciliation_attcode')) ? ' selected' : '';
  980. $sHtml .= "<option value=\"\" $sSelected>".Dict::S('Core:SynchroAttExtKey:ReconciliationById')."</option>\n";
  981. foreach(MetaModel::ListAttributeDefs($sTargetClass) as $sAttCode => $oAttDef)
  982. {
  983. if ($oAttDef->IsScalar())
  984. {
  985. $sSelected = ($sAttCode == $this->Get('reconciliation_attcode')) ? ' selected' : '';
  986. $sHtml .= "<option value=\"$sAttCode\" $sSelected>".MetaModel::GetLabel($sTargetClass, $sAttCode)."</option>\n";
  987. }
  988. }
  989. $sHtml .= "</select>\n";
  990. return $sHtml;
  991. }
  992. }
  993. class SynchroAttLinkSet extends SynchroAttribute
  994. {
  995. public static function Init()
  996. {
  997. $aParams = array
  998. (
  999. "category" => "core/cmdb,view_in_gui",
  1000. "key_type" => "autoincrement",
  1001. "name_attcode" => "",
  1002. "state_attcode" => "",
  1003. "reconc_keys" => array(),
  1004. "db_table" => "priv_sync_att_linkset",
  1005. "db_key_field" => "id",
  1006. "db_finalclass_field" => "",
  1007. "display_template" => "",
  1008. );
  1009. MetaModel::Init_Params($aParams);
  1010. MetaModel::Init_InheritAttributes();
  1011. MetaModel::Init_AddAttribute(new AttributeString("row_separator", array("allowed_values"=>null, "sql"=>"row_separator", "default_value"=>'|', "is_null_allowed"=>true, "depends_on"=>array())));
  1012. MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
  1013. // Display lists
  1014. 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
  1015. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1016. // Search criteria
  1017. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1018. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1019. }
  1020. }
  1021. //class SynchroLog extends Event
  1022. class SynchroLog extends DBObject
  1023. {
  1024. public static function Init()
  1025. {
  1026. $aParams = array
  1027. (
  1028. "category" => "core/cmdb,view_in_gui",
  1029. "key_type" => "autoincrement",
  1030. "name_attcode" => "",
  1031. "state_attcode" => "",
  1032. "reconc_keys" => array(),
  1033. "db_table" => "priv_sync_log",
  1034. "db_key_field" => "id",
  1035. "db_finalclass_field" => "",
  1036. "display_template" => "",
  1037. );
  1038. MetaModel::Init_Params($aParams);
  1039. MetaModel::Init_InheritAttributes();
  1040. // MetaModel::Init_AddAttribute(new AttributeString("userinfo", array("allowed_values"=>null, "sql"=>"userinfo", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  1041. 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_SILENT, "depends_on"=>array())));
  1042. MetaModel::Init_AddAttribute(new AttributeDateTime("start_date", array("allowed_values"=>null, "sql"=>"start_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1043. MetaModel::Init_AddAttribute(new AttributeDateTime("end_date", array("allowed_values"=>null, "sql"=>"end_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1044. 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())));
  1045. 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())));
  1046. 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())));
  1047. 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())));
  1048. 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())));
  1049. 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())));
  1050. 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())));
  1051. 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())));
  1052. 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())));
  1053. 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())));
  1054. 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())));
  1055. // 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())));
  1056. 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())));
  1057. 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())));
  1058. 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())));
  1059. 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())));
  1060. MetaModel::Init_AddAttribute(new AttributeString("last_error", array("allowed_values"=>null, "sql"=>"last_error", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1061. MetaModel::Init_AddAttribute(new AttributeLongText("traces", array("allowed_values"=>null, "sql"=>"traces", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1062. // Display lists
  1063. 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',
  1064. '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', 'traces')); // Attributes to be displayed for the complete details
  1065. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_replica_seen')); // Attributes to be displayed for a list
  1066. // Search criteria
  1067. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1068. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1069. }
  1070. /**
  1071. * Increments a statistics counter
  1072. */
  1073. function Inc($sCode)
  1074. {
  1075. $this->Set($sCode, 1+$this->Get($sCode));
  1076. }
  1077. /**
  1078. * Implement traces management
  1079. */
  1080. protected $m_aTraces = array();
  1081. public function AddTrace($sMsg, $oReplica = null)
  1082. {
  1083. if (MetaModel::GetConfig()->Get('synchro_trace') == 'none')
  1084. {
  1085. return;
  1086. }
  1087. if ($oReplica)
  1088. {
  1089. $sDestClass = $oReplica->Get('dest_class');
  1090. if (!empty($sDestClass))
  1091. {
  1092. $sPrefix = $oReplica->GetKey().','.$sDestClass.'::'.$oReplica->Get('dest_id').',';
  1093. }
  1094. else
  1095. {
  1096. $sPrefix = $oReplica->GetKey().',,';
  1097. }
  1098. }
  1099. else
  1100. {
  1101. $sPrefix = ',,';
  1102. }
  1103. $this->m_aTraces[] = $sPrefix.$sMsg;
  1104. }
  1105. public function GetTraces()
  1106. {
  1107. return $this->m_aTraces;
  1108. }
  1109. protected function TraceToText()
  1110. {
  1111. if (MetaModel::GetConfig()->Get('synchro_trace') != 'save')
  1112. {
  1113. // none, or display only
  1114. return;
  1115. }
  1116. $oAttDef = MetaModel::GetAttributeDef(get_class($this), 'traces');
  1117. $iMaxSize = $oAttDef->GetMaxSize();
  1118. $sTrace = implode("\n", $this->m_aTraces);
  1119. if (strlen($sTrace) >= $iMaxSize)
  1120. {
  1121. $sTrace = substr($sTrace, 0, $iMaxSize - 255)."...\nTruncated (size: ".strlen($sTrace).')';
  1122. }
  1123. $this->Set('traces', $sTrace);
  1124. }
  1125. protected function OnInsert()
  1126. {
  1127. $this->TraceToText();
  1128. parent::OnInsert();
  1129. }
  1130. protected function OnUpdate()
  1131. {
  1132. $this->TraceToText();
  1133. parent::OnUpdate();
  1134. }
  1135. }
  1136. class SynchroReplica extends DBObject implements iDisplay
  1137. {
  1138. static $aSearches = array(); // Cache of OQL queries used for reconciliation (per data source)
  1139. public static function Init()
  1140. {
  1141. $aParams = array
  1142. (
  1143. "category" => "core/cmdb,view_in_gui",
  1144. "key_type" => "autoincrement",
  1145. "name_attcode" => "",
  1146. "state_attcode" => "",
  1147. "reconc_keys" => array(),
  1148. "db_table" => "priv_sync_replica",
  1149. "db_key_field" => "id",
  1150. "db_finalclass_field" => "",
  1151. "display_template" => "",
  1152. );
  1153. MetaModel::Init_Params($aParams);
  1154. MetaModel::Init_InheritAttributes();
  1155. 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_SILENT, "depends_on"=>array())));
  1156. MetaModel::Init_AddAttribute(new AttributeExternalField("base_class", array("allowed_values"=>null, "extkey_attcode"=> 'sync_source_id', "target_attcode"=>"scope_class")));
  1157. MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1158. MetaModel::Init_AddAttribute(new AttributeClass("dest_class", array("class_category"=>"", "more_values"=>"", "sql"=>"dest_class", "default_value"=>'Organization', "is_null_allowed"=>true, "depends_on"=>array())));
  1159. 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())));
  1160. 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())));
  1161. 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())));
  1162. 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())));
  1163. 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())));
  1164. 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())));
  1165. // Display lists
  1166. MetaModel::Init_SetZListItems('details', array('' .
  1167. 'col:0'=> array(
  1168. 'fieldset:SynchroDataSource:Definition' => array('sync_source_id','dest_id','dest_class'),
  1169. 'fieldset:SynchroDataSource:Status' => array('status','status_last_seen','status_dest_creator','status_last_error'),
  1170. 'fieldset:SynchroDataSource:Information' => array('info_creation_date','info_last_modified'))
  1171. )
  1172. );
  1173. 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
  1174. // Search criteria
  1175. 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
  1176. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1177. }
  1178. public function DBInsert()
  1179. {
  1180. throw new CoreException('A synchronization replica must be created only by the mean of triggers');
  1181. }
  1182. // Overload the deletion -> the replica has been created by the mean of a trigger,
  1183. // it will be deleted by the mean of a trigger too
  1184. public function DBDelete(&$oDeletionPlan = null)
  1185. {
  1186. $oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1187. $sTable = $oDataSource->GetDataTable();
  1188. $sSQL = "DELETE FROM `$sTable` WHERE id = '{$this->GetKey()}'";
  1189. CMDBSource::Query($sSQL);
  1190. $this->m_bIsInDB = false;
  1191. $this->m_iKey = null;
  1192. }
  1193. public function SetLastError($sMessage, $oException = null)
  1194. {
  1195. if ($oException)
  1196. {
  1197. $sText = $sMessage.$oException->getMessage();
  1198. }
  1199. else
  1200. {
  1201. $sText = $sMessage;
  1202. }
  1203. if (strlen($sText) > 255)
  1204. {
  1205. $sText = substr($sText, 0, 200).'...('.strlen($sText).' chars)...';
  1206. }
  1207. $this->Set('status_last_error', $sText);
  1208. }
  1209. public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange, &$oStatLog)
  1210. {
  1211. switch($this->Get('status'))
  1212. {
  1213. case 'new':
  1214. $this->Set('status_dest_creator', false);
  1215. // If needed, construct the query used for the reconciliation
  1216. if (!isset(self::$aSearches[$oDataSource->GetKey()]))
  1217. {
  1218. $aCriterias = array();
  1219. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1220. {
  1221. $aCriterias[] = ($sFilterCode == 'primary_key' ? 'id' : $sFilterCode).' = :'.$sFilterCode;
  1222. }
  1223. $sOQL = "SELECT ".$oDataSource->GetTargetClass()." WHERE ".implode(' AND ', $aCriterias);
  1224. self::$aSearches[$oDataSource->GetKey()] = DBObjectSearch::FromOQL($sOQL);
  1225. }
  1226. // Get the criterias for the search
  1227. $aFilterValues = array();
  1228. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1229. {
  1230. $value = $this->GetValueFromExtData($sFilterCode, $oSyncAtt, $oStatLog);
  1231. if (!is_null($value))
  1232. {
  1233. $aFilterValues[$sFilterCode] = $value;
  1234. }
  1235. else
  1236. {
  1237. // Reconciliation could not be performed - log and EXIT
  1238. $oStatLog->AddTrace("Could not reconcile on null value: ".$sFilterCode, $this);
  1239. $this->SetLastError('Could not reconcile on null value: '.$sFilterCode);
  1240. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1241. return;
  1242. }
  1243. }
  1244. $oDestSet = new DBObjectSet(self::$aSearches[$oDataSource->GetKey()], array(), $aFilterValues);
  1245. $iCount = $oDestSet->Count();
  1246. $aConditions = array();
  1247. foreach($aFilterValues as $sCode => $sValue)
  1248. {
  1249. $aConditions[] = $sCode.'='.$sValue;
  1250. }
  1251. $sConditionDesc = implode(' AND ', $aConditions);
  1252. // How many objects match the reconciliation criterias
  1253. switch($iCount)
  1254. {
  1255. case 0:
  1256. $oStatLog->AddTrace("Nothing found on: $sConditionDesc", $this);
  1257. if ($oDataSource->Get('action_on_zero') == 'create')
  1258. {
  1259. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1260. }
  1261. else // assumed to be 'error'
  1262. {
  1263. $oStatLog->AddTrace("Failed to reconcile (no match)", $this);
  1264. $this->SetLastError('Could not find a match for reconciliation');
  1265. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1266. }
  1267. break;
  1268. case 1:
  1269. $oStatLog->AddTrace("Found 1 object on: $sConditionDesc", $this);
  1270. if ($oDataSource->Get('action_on_one') == 'update')
  1271. {
  1272. $oDestObj = $oDestSet->Fetch();
  1273. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1274. $this->Set('dest_id', $oDestObj->GetKey());
  1275. $this->Set('dest_class', get_class($oDestObj));
  1276. }
  1277. else
  1278. {
  1279. // assumed to be 'error'
  1280. $oStatLog->AddTrace("Failed to reconcile (1 match)", $this);
  1281. $this->SetLastError('Found a match while expecting several');
  1282. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1283. }
  1284. break;
  1285. default:
  1286. $oStatLog->AddTrace("Found $iCount objects on: $sConditionDesc", $this);
  1287. if ($oDataSource->Get('action_on_multiple') == 'error')
  1288. {
  1289. $oStatLog->AddTrace("Failed to reconcile (N>1 matches)", $this);
  1290. $this->SetLastError($iCount.' destination objects match the reconciliation criterias: '.$sConditionDesc);
  1291. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1292. }
  1293. elseif ($oDataSource->Get('action_on_multiple') == 'create')
  1294. {
  1295. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1296. }
  1297. else
  1298. {
  1299. // assumed to be 'take_first'
  1300. $oDestObj = $oDestSet->Fetch();
  1301. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1302. $this->Set('dest_id', $oDestObj->GetKey());
  1303. $this->Set('dest_class', get_class($oDestObj));
  1304. }
  1305. }
  1306. break;
  1307. case 'modified':
  1308. $oDestObj = MetaModel::GetObject($oDataSource->GetTargetClass(), $this->Get('dest_id'));
  1309. if ($oDestObj == null)
  1310. {
  1311. $this->Set('status', 'orphan'); // The destination object has been deleted !
  1312. $this->SetLastError('Destination object deleted unexpectedly');
  1313. $oStatLog->Inc('stats_nb_obj_updated_errors');
  1314. }
  1315. else
  1316. {
  1317. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj', 'stats_nb_obj_updated_errors');
  1318. }
  1319. break;
  1320. default: // Do nothing in all other cases
  1321. }
  1322. $this->DBUpdateTracked($oChange);
  1323. }
  1324. /**
  1325. * Updates the destination object with the Extended data found in the synchro_data_XXXX table
  1326. */
  1327. protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, &$oStatLog, $sStatsCode, $sStatsCodeError)
  1328. {
  1329. $aValueTrace = array();
  1330. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1331. {
  1332. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1333. if (!is_null($value))
  1334. {
  1335. $oDestObj->Set($sAttCode, $value);
  1336. $aValueTrace[] = "$sAttCode: $value";
  1337. }
  1338. }
  1339. try
  1340. {
  1341. // Really modified ?
  1342. if ($oDestObj->IsModified())
  1343. {
  1344. $oDestObj->DBUpdateTracked($oChange);
  1345. $oStatLog->AddTrace('Updated object - Values: {'.implode(', ', $aValueTrace).'}', $this);
  1346. $oStatLog->Inc($sStatsCode.'_updated');
  1347. $this->Set('info_last_modified', date('Y-m-d H:i:s'));
  1348. }
  1349. else
  1350. {
  1351. $oStatLog->AddTrace('Unchanged object', $this);
  1352. $oStatLog->Inc($sStatsCode.'_unchanged');
  1353. }
  1354. $this->Set('status_last_error', '');
  1355. $this->Set('status', 'synchronized');
  1356. }
  1357. catch(Exception $e)
  1358. {
  1359. $oStatLog->AddTrace("Failed to update destination object: {$e->getMessage()}", $this);
  1360. $this->SetLastError('Unable to update destination object: ', $e);
  1361. $oStatLog->Inc($sStatsCodeError);
  1362. }
  1363. }
  1364. /**
  1365. * Creates the destination object populating it with the Extended data found in the synchro_data_XXXX table
  1366. */
  1367. protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange, &$oStatLog)
  1368. {
  1369. $oDestObj = MetaModel::NewObject($sClass);
  1370. try
  1371. {
  1372. $aValueTrace = array();
  1373. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1374. {
  1375. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1376. if (!is_null($value))
  1377. {
  1378. $oDestObj->Set($sAttCode, $value);
  1379. $aValueTrace[] = "$sAttCode: $value";
  1380. }
  1381. }
  1382. $iNew = $oDestObj->DBInsertTracked($oChange);
  1383. $this->Set('dest_id', $oDestObj->GetKey());
  1384. $this->Set('dest_class', get_class($oDestObj));
  1385. $this->Set('status_dest_creator', true);
  1386. $this->Set('status_last_error', '');
  1387. $this->Set('status', 'synchronized');
  1388. $this->Set('info_creation_date', date('Y-m-d H:i:s'));
  1389. $oStatLog->AddTrace("Created (".implode(', ', $aValueTrace).")", $this);
  1390. $oStatLog->Inc('stats_nb_obj_created');
  1391. }
  1392. catch(Exception $e)
  1393. {
  1394. $oStatLog->AddTrace("Failed to create $sClass ({$e->getMessage()})", $this);
  1395. $this->SetLastError('Unable to create destination object: ', $e);
  1396. $oStatLog->Inc('stats_nb_obj_created_errors');
  1397. }
  1398. }
  1399. /**
  1400. * Update the destination object with given values
  1401. */
  1402. public function UpdateDestObject($aValues, $oChange, &$oStatLog)
  1403. {
  1404. try
  1405. {
  1406. if ($this->Get('dest_class') == '')
  1407. {
  1408. $this->SetLastError('No destination object to update');
  1409. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1410. }
  1411. else
  1412. {
  1413. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1414. foreach($aValues as $sAttCode => $value)
  1415. {
  1416. $oDestObj->Set($sAttCode, $value);
  1417. }
  1418. $this->Set('info_last_modified', date('Y-m-d H:i:s'));
  1419. $oDestObj->DBUpdateTracked($oChange);
  1420. $oStatLog->AddTrace("Replica marked as obsolete", $this);
  1421. $oStatLog->Inc('stats_nb_obj_obsoleted');
  1422. }
  1423. }
  1424. catch(Exception $e)
  1425. {
  1426. $this->SetLastError('Unable to update the destination object: ', $e);
  1427. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1428. }
  1429. }
  1430. /**
  1431. * Delete the destination object
  1432. */
  1433. public function DeleteDestObject($oChange, &$oStatLog)
  1434. {
  1435. if($this->Get('status_dest_creator'))
  1436. {
  1437. try
  1438. {
  1439. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1440. $oDestObj->DBDeleteTracked($oChange);
  1441. $this->DBDeleteTracked($oChange);
  1442. $oStatLog->Inc('stats_nb_obj_deleted');
  1443. }
  1444. catch(Exception $e)
  1445. {
  1446. $this->SetLastError('Unable to delete the destination object: ', $e);
  1447. $this->Set('status', 'obsolete');
  1448. $this->DBUpdateTracked($oChange);
  1449. $oStatLog->Inc('stats_nb_obj_deleted_errors');
  1450. }
  1451. }
  1452. else
  1453. {
  1454. $this->DBDeleteTracked($oChange);
  1455. $oStatLog->Inc('stats_nb_replica_disappeared_no_action');
  1456. }
  1457. }
  1458. /**
  1459. * Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
  1460. */
  1461. protected function GetValueFromExtData($sAttCode, $oSyncAtt, &$oStatLog)
  1462. {
  1463. // $aData should contain attributes defined either for reconciliation or create/update
  1464. $aData = $this->GetExtendedData();
  1465. $sClass = $this->Get('base_class');
  1466. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1467. if (!is_null($oSyncAtt) && ($oSyncAtt instanceof SynchroAttExtKey))
  1468. {
  1469. $rawValue = $aData[$sAttCode];
  1470. if (is_null($rawValue))
  1471. {
  1472. // Null means "ignore" this attribute
  1473. return null;
  1474. }
  1475. $sReconcAttCode = $oSyncAtt->Get('reconciliation_attcode');
  1476. if (!empty($sReconcAttCode))
  1477. {
  1478. $sRemoteClass = $oAttDef->GetTargetClass();
  1479. $oObj = MetaModel::GetObjectByColumn($sRemoteClass, $sReconcAttCode, $rawValue, false);
  1480. if ($oObj)
  1481. {
  1482. $retValue = $oObj->GetKey();
  1483. }
  1484. else
  1485. {
  1486. // Note: differs from null (in which case the value would be left unchanged)
  1487. $oStatLog->AddTrace("Could not find [unique] object for '$sAttCode': searched on $sReconcAttCode = '$rawValue'", $this);
  1488. $retValue = 0;
  1489. }
  1490. }
  1491. else
  1492. {
  1493. $retValue = $rawValue;
  1494. }
  1495. }
  1496. else
  1497. {
  1498. $aColumns = $oAttDef->GetImportColumns();
  1499. foreach($aColumns as $sColumn => $sFormat)
  1500. {
  1501. // In any case, a null column means "ignore this attribute"
  1502. //
  1503. if (is_null($aData[$sColumn]))
  1504. {
  1505. return null;
  1506. }
  1507. }
  1508. $retValue = $oAttDef->FromImportToValue($aData, $sAttCode);
  1509. }
  1510. return $retValue;
  1511. }
  1512. /**
  1513. * Maps the given context parameter name to the appropriate filter/search code for this class
  1514. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  1515. * @return string Filter code, i.e. 'customer_id'
  1516. */
  1517. public static function MapContextParam($sContextParam)
  1518. {
  1519. if ($sContextParam == 'menu')
  1520. {
  1521. return null;
  1522. }
  1523. else
  1524. {
  1525. return $sContextParam;
  1526. }
  1527. }
  1528. /**
  1529. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  1530. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  1531. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1532. * To Be overridden by derived classes
  1533. * @param void
  1534. * @return String The desired higlight class for the object/row
  1535. */
  1536. public function GetHilightClass()
  1537. {
  1538. // Possible return values are:
  1539. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1540. return HILIGHT_CLASS_NONE; // Not hilighted by default
  1541. }
  1542. public static function GetUIPage()
  1543. {
  1544. return '../synchro/replica.php';
  1545. }
  1546. function DisplayDetails(WebPage $oPage, $bEditMode = false)
  1547. {
  1548. // Object's details
  1549. //$this->DisplayBareHeader($oPage, $bEditMode);
  1550. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  1551. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  1552. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  1553. $this->DisplayBareProperties($oPage, $bEditMode);
  1554. }
  1555. function DisplayBareProperties(WebPage $oPage, $bEditMode = false)
  1556. {
  1557. if ($bEditMode) return; // Not editable
  1558. $oPage->add('<table style="vertical-align:top"><tr style="vertical-align:top"><td>');
  1559. $aDetails = array();
  1560. $sClass = get_class($this);
  1561. $oPage->add('<fieldset>');
  1562. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PrivateDetails').'</legend>');
  1563. $aZList = MetaModel::FlattenZlist(MetaModel::GetZListItems($sClass, 'details'));
  1564. foreach( $aZList as $sAttCode)
  1565. {
  1566. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1567. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1568. }
  1569. $oPage->Details($aDetails);
  1570. $oPage->add('</fieldset>');
  1571. $oPage->add('</td><td>');
  1572. $oPage->add('<fieldset>');
  1573. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PublicData').'</legend>');
  1574. $oSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1575. $sSQLTable = $oSource->GetDataTable();
  1576. $aData = $this->LoadExtendedDataFromTable($sSQLTable);
  1577. $aHeaders = array('attcode' => array('label' => 'Attribute Code', 'description' => ''),
  1578. 'data' => array('label' => 'Value', 'description' => ''));
  1579. $aRows = array();
  1580. foreach($aData as $sKey => $value)
  1581. {
  1582. $aRows[] = array('attcode' => $sKey, 'data' => $value);
  1583. }
  1584. $oPage->Table($aHeaders, $aRows);
  1585. $oPage->add('</fieldset>');
  1586. $oPage->add('</td></tr></table>');
  1587. }
  1588. public function LoadExtendedDataFromTable($sSQLTable)
  1589. {
  1590. $sSQL = "SELECT * FROM $sSQLTable WHERE id=".$this->GetKey();
  1591. $rQuery = CMDBSource::Query($sSQL);
  1592. return CMDBSource::FetchArray($rQuery);
  1593. }
  1594. }
  1595. $oAdminMenu = new MenuGroup('AdminTools', 80 /* fRank */, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  1596. new OQLMenuNode('DataSources', 'SELECT SynchroDataSource', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  1597. // new OQLMenuNode('Replicas', 'SELECT SynchroReplica', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroReplica', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  1598. // new WebPageMenuNode('Test:RunSynchro', '../synchro/synchro_exec.php', $oAdminMenu->GetIndex(), 13 /* fRank */, 'SynchroDataSource');
  1599. ?>