synchrodatasource.class.inc.php 89 KB

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