synchrodatasource.class.inc.php 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738
  1. <?php
  2. // Copyright (C) 2012 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 function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  87. {
  88. if (!$this->IsNew())
  89. {
  90. $oPage->SetCurrentTab(Dict::S('Core:SynchroAttributes'));
  91. $oAttributeSet = $this->Get('attribute_list');
  92. $aAttributes = array();
  93. while($oAttribute = $oAttributeSet->Fetch())
  94. {
  95. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  96. }
  97. // Columns of the form
  98. $aAttribs = array();
  99. foreach(array('attcode', 'reconciliation', 'update', 'update_policy', 'reconciliation_attcode') as $s )
  100. {
  101. $aAttribs[$s] = array( 'label' => Dict::S("Core:SynchroAtt:$s"), "description" => Dict::S("Core:SynchroAtt:$s+"));
  102. }
  103. // Rows of the form
  104. $aValues = array();
  105. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  106. {
  107. if ($oAttDef->IsWritable())
  108. {
  109. if (isset($aAttributes[$sAttCode]))
  110. {
  111. $oAttribute = $aAttributes[$sAttCode];
  112. }
  113. else
  114. {
  115. if ($oAttDef->IsExternalKey())
  116. {
  117. $oAttribute = new SynchroAttExtKey();
  118. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  119. }
  120. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  121. {
  122. $oAttribute = new SynchroAttLinkSet();
  123. // Todo - add these settings into the form
  124. $oAttribute->Set('row_separator', MetaModel::GetConfig()->Get('link_set_item_separator'));
  125. $oAttribute->Set('attribute_separator', MetaModel::GetConfig()->Get('link_set_attribute_separator'));
  126. $oAttribute->Set('value_separator', MetaModel::GetConfig()->Get('link_set_value_separator'));
  127. $oAttribute->Set('attribute_qualifier', MetaModel::GetConfig()->Get('link_set_attribute_qualifier'));
  128. }
  129. elseif ($oAttDef->IsScalar())
  130. {
  131. $oAttribute = new SynchroAttribute();
  132. }
  133. else
  134. {
  135. $oAttribute = null;
  136. }
  137. if (!is_null($oAttribute))
  138. {
  139. $oAttribute->Set('sync_source_id', $this->GetKey());
  140. $oAttribute->Set('attcode', $sAttCode);
  141. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  142. $oAttribute->Set('update', 1);
  143. $oAttribute->Set('update_policy', 'master_locked');
  144. }
  145. }
  146. if (!is_null($oAttribute))
  147. {
  148. if (!$bEditMode)
  149. {
  150. // Read-only mode
  151. $aRow['reconciliation'] = $oAttribute->Get('reconcile') == 1 ? Dict::S('Core:SynchroReconcile:Yes') : Dict::S('Core:SynchroReconcile:No');
  152. $aRow['update'] = $oAttribute->Get('update') == 1 ? Dict::S('Core:SynchroUpdate:Yes') : Dict::S('Core:SynchroUpdate:No');
  153. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  154. $aRow['update_policy'] = $oAttribute->GetAsHTML('update_policy');
  155. if ($oAttDef->IsExternalKey())
  156. {
  157. $aRow['reconciliation_attcode'] = $oAttribute->GetAsHTML('reconciliation_attcode');
  158. }
  159. else
  160. {
  161. $aRow['reconciliation_attcode'] = '&nbsp;';
  162. }
  163. }
  164. else
  165. {
  166. // Edit mode
  167. $sAttCode = $oAttribute->Get('attcode');
  168. $sChecked = $oAttribute->Get('reconcile') == 1 ? 'checked' : '';
  169. $aRow['reconciliation'] = "<input type=\"checkbox\" name=\"reconciliation[$sAttCode]\" $sChecked/>";
  170. $sChecked = $oAttribute->Get('update') == 1 ? 'checked' : '';
  171. $aRow['update'] = "<input type=\"checkbox\" name=\"update[$sAttCode]\" $sChecked/>";
  172. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  173. $oUpdateAttDef = MetaModel::GetAttributeDef(get_class($oAttribute), 'update_policy');
  174. $aRow['update_policy'] = cmdbAbstractObject::GetFormElementForField($oPage, get_class($oAttribute), 'update_policy', $oUpdateAttDef, $oAttribute->Get('update_policy'), '', 'update_policy_'.$sAttCode, "[$sAttCode]");
  175. if ($oAttDef->IsExternalKey())
  176. {
  177. $aRow['reconciliation_attcode'] = $oAttribute->GetReconciliationFormElement($oAttDef->GetTargetClass(), "attr_reconciliation_attcode[$sAttCode]");
  178. }
  179. else
  180. {
  181. $aRow['reconciliation_attcode'] = '&nbsp;';
  182. }
  183. }
  184. $aValues[] = $aRow;
  185. }
  186. }
  187. }
  188. $oPage->p(Dict::Format('Class:SynchroDataSource:DataTable', $this->GetDataTable()));
  189. $oPage->Table($aAttribs, $aValues);
  190. $this->DisplayStatusTab($oPage);
  191. }
  192. parent::DisplayBareRelations($oPage, $bEditMode);
  193. }
  194. /**
  195. * Displays the status (SynchroLog) of the datasource in a graphical manner
  196. * @param $oPage WebPage
  197. * @return void
  198. */
  199. protected function DisplayStatusTab(WebPage $oPage)
  200. {
  201. $oPage->SetCurrentTab(Dict::S('Core:SynchroStatus'));
  202. $sSelectSynchroLog = 'SELECT SynchroLog WHERE sync_source_id = :source_id';
  203. $oSetSynchroLog = new CMDBObjectSet(DBObjectSearch::FromOQL($sSelectSynchroLog), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()));
  204. if ($oSetSynchroLog->Count() > 0)
  205. {
  206. $oLastLog = $oSetSynchroLog->Fetch();
  207. $sStartDate = $oLastLog->Get('start_date');
  208. $oLastLog->Get('stats_nb_replica_seen');
  209. $iLastLog = 0;
  210. $iDSid = $this->GetKey();
  211. if ($oLastLog->Get('status') == 'running')
  212. {
  213. // Still running !
  214. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroRunningStartedOn_Date', $sStartDate).'</h2>');
  215. }
  216. else
  217. {
  218. $sEndDate = $oLastLog->Get('end_date');
  219. $iLastLog = $oLastLog->GetKey();
  220. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroEndedOn_Date', $sEndDate).'</h2>');
  221. $sOQL = "SELECT SynchroReplica WHERE sync_source_id=$iDSid";
  222. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL));
  223. $iCountAllReplicas = $oSet->Count();
  224. $sAllReplicas = "<a href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\">$iCountAllReplicas</a>";
  225. $sOQL = "SELECT SynchroReplica WHERE sync_source_id=$iDSid AND status_last_error !=''";
  226. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL));
  227. $iCountAllErrors = $oSet->Count();
  228. $sAllErrors = "<a href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\">$iCountAllErrors</a>";
  229. $sOQL = "SELECT SynchroReplica WHERE sync_source_id=$iDSid AND status_last_warning !=''";
  230. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL));
  231. $iCountAllWarnings = $oSet->Count();
  232. $sAllWarnings = "<a href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\">$iCountAllWarnings</a>";
  233. $oPage->p('<h2>'.Dict::Format('Core:Synchro:ListReplicas_AllReplicas_Errors_Warnings', $sAllReplicas, $sAllErrors, $sAllWarnings).'</h2>');
  234. }
  235. $oPage->add('<table class="synoptics"><tr><td style="color:#333;vertical-align:top">');
  236. // List all the log entries for the user to select
  237. $oPage->add('<h2 style="line-height:55px;">'.Dict::S('Core:Synchro:History').'</h2>');
  238. $oSetSynchroLog->Rewind();
  239. $oPage->add('<select size="25" onChange="UpdateSynoptics(this.value);">');
  240. $sSelected = ' selected'; // First log is selected by default
  241. $sScript = "var aSynchroLog = {\n";
  242. while($oLog = $oSetSynchroLog->Fetch())
  243. {
  244. $sLogTitle = Dict::Format('Core:SynchroLogTitle', $oLog->Get('status'), $oLog->Get('start_date'));
  245. $oPage->add('<option value="'.$oLog->GetKey().'"'.$sSelected.'>'.$sLogTitle.'</option>');
  246. $sSelected = ''; // only the first log is selected by default
  247. $aData = $this->ProcessLog($oLog);
  248. $sScript .= '"'.$oLog->GetKey().'": '.json_encode($aData).",\n";
  249. }
  250. $sScript .= "end: 'Done'";
  251. $sScript .= "};\n";
  252. $sScript .= <<<EOF
  253. var sLastLog = '$iLastLog';
  254. function ToggleSynoptics(sId, bShow)
  255. {
  256. if (bShow)
  257. {
  258. $(sId).show();
  259. }
  260. else
  261. {
  262. $(sId).hide();
  263. }
  264. }
  265. function UpdateSynoptics(id)
  266. {
  267. var aValues = aSynchroLog[id];
  268. if (aValues == undefined) return;
  269. for (var sKey in aValues)
  270. {
  271. $('#c_'+sKey).html(aValues[sKey]);
  272. var fOpacity = (aValues[sKey] == 0) ? 0.3 : 1;
  273. $('#'+sKey).fadeTo("slow", fOpacity);
  274. }
  275. //alert('id = '+id+', lastLog='+sLastLog+', id==sLastLog: '+(id==sLastLog)+' obj_updated_errors: '+aValues['obj_updated_errors']);
  276. if ( (id == sLastLog) && (aValues['obj_new_errors'] > 0) )
  277. {
  278. $('#new_errors_link').show();
  279. }
  280. else
  281. {
  282. $('#new_errors_link').hide();
  283. }
  284. if ( (id == sLastLog) && (aValues['obj_updated_errors'] > 0) )
  285. {
  286. $('#updated_errors_link').show();
  287. }
  288. else
  289. {
  290. $('#updated_errors_link').hide();
  291. }
  292. if ( (id == sLastLog) && (aValues['obj_disappeared_errors'] > 0) )
  293. {
  294. $('#disappeared_errors_link').show();
  295. }
  296. else
  297. {
  298. $('#disappeared_errors_link').hide();
  299. }
  300. ToggleSynoptics('#cw_obj_created_warnings', aValues['obj_created_warnings'] > 0);
  301. ToggleSynoptics('#cw_obj_new_updated_warnings', aValues['obj_new_updated_warnings'] > 0);
  302. ToggleSynoptics('#cw_obj_new_unchanged_warnings', aValues['obj_new_unchanged_warnings'] > 0);
  303. ToggleSynoptics('#cw_obj_updated_warnings', aValues['obj_updated_warnings'] > 0);
  304. ToggleSynoptics('#cw_obj_unchanged_warnings', aValues['obj_unchanged_warnings'] > 0);
  305. }
  306. EOF
  307. ;
  308. $oPage->add_script($sScript);
  309. $oPage->add('</select>');
  310. $oPage->add('</td><td style="vertical-align:top;">');
  311. // Now build the big "synoptics" view
  312. $aData = $this->ProcessLog($oLastLog);
  313. $sNbReplica = $this->GetIcon()."&nbsp;".Dict::Format('Core:Synchro:Nb_Replica', "<span id=\"c_nb_replica_total\">{$aData['nb_replica_total']}</span>");
  314. $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>");
  315. $oPage->add(
  316. <<<EOF
  317. <table class="synoptics">
  318. <tr class="synoptics_header">
  319. <td>$sNbReplica</td><td>&nbsp;</td><td>$sNbObjects</td>
  320. </tr>
  321. <tr>
  322. EOF
  323. );
  324. $sBaseOQL = "SELECT SynchroReplica WHERE sync_source_id=".$this->GetKey()." AND status_last_error!=''";
  325. $oPage->add($this->HtmlBox('repl_ignored', $aData, '#999').'<td colspan="2">&nbsp;</td>');
  326. $oPage->add("</tr>\n<tr>");
  327. $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'));
  328. $oPage->add("</tr>\n<tr>");
  329. $oPage->add($this->HtmlBox('obj_deleted', $aData, '#000'));
  330. $oPage->add("</tr>\n<tr>");
  331. $oPage->add($this->HtmlBox('obj_obsoleted', $aData, '#630'));
  332. $oPage->add("</tr>\n<tr>");
  333. $sOQL = urlencode($sBaseOQL." AND status='obsolete'");
  334. $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>"));
  335. $oPage->add("</tr>\n<tr>");
  336. $oPage->add($this->HtmlBox('repl_existing', $aData, '#093', 'rowspan="3"').'<td rowspan="3" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_unchanged', $aData, '#393'));
  337. $oPage->add("</tr>\n<tr>");
  338. $oPage->add($this->HtmlBox('obj_updated', $aData, '#3C3'));
  339. $oPage->add("</tr>\n<tr>");
  340. $sOQL = urlencode($sBaseOQL." AND status='modified'");
  341. $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>"));
  342. $oPage->add("</tr>\n<tr>");
  343. $oPage->add($this->HtmlBox('repl_new', $aData, '#339', 'rowspan="4"').'<td rowspan="4" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_new_unchanged', $aData, '#393'));
  344. $oPage->add("</tr>\n<tr>");
  345. $oPage->add($this->HtmlBox('obj_new_updated', $aData, '#3C3'));
  346. $oPage->add("</tr>\n<tr>");
  347. $oPage->add($this->HtmlBox('obj_created', $aData, '#339'));
  348. $oPage->add("</tr>\n<tr>");
  349. $sOQL = urlencode($sBaseOQL." AND status='new'");
  350. $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>"));
  351. $oPage->add("</tr>\n</table>\n");
  352. $oPage->add('</td></tr></table>');
  353. $oPage->add_ready_script("UpdateSynoptics('$iLastLog')");
  354. }
  355. else
  356. {
  357. $oPage->p('<h2>'.Dict::S('Core:Synchro:NeverRun').'</h2>');
  358. }
  359. }
  360. protected function HtmlBox($sId, $aData, $sColor, $sHTMLAttribs = '', $sErrorLink = '')
  361. {
  362. $iCount = $aData[$sId];
  363. $sCount = "<span id=\"c_{$sId}\">$iCount</span>";
  364. $sLabel = Dict::Format('Core:Synchro:label_'.$sId, $sCount);
  365. $sOpacity = ($iCount==0) ? "opacity:0.3;" : "";
  366. if (isset($aData[$sId.'_warnings']))
  367. {
  368. $sLabel .= " <span id=\"cw_{$sId}_warnings\"><img src=\"../images/error.png\" style=\"vertical-align:middle\"/> (<span id=\"c_{$sId}_warnings\">".$aData[$sId.'_warnings']."</span>)</span>";
  369. }
  370. return "<td id=\"$sId\" style=\"background-color:$sColor;$sOpacity;\" {$sHTMLAttribs}>{$sLabel}{$sErrorLink}</td>";
  371. }
  372. protected function ProcessLog($oLastLog)
  373. {
  374. $aData = array(
  375. 'obj_deleted' => $oLastLog->Get('stats_nb_obj_deleted'),
  376. 'obj_obsoleted' => $oLastLog->Get('stats_nb_obj_obsoleted'),
  377. 'obj_disappeared_errors' => $oLastLog->Get('stats_nb_obj_obsoleted_errors') + $oLastLog->Get('stats_nb_obj_deleted_errors'),
  378. 'obj_disappeared_no_action' => $oLastLog->Get('stats_nb_replica_disappeared_no_action'),
  379. 'obj_updated' => $oLastLog->Get('stats_nb_obj_updated'),
  380. 'obj_updated_warnings' => $oLastLog->Get('stats_nb_obj_updated_warnings'),
  381. 'obj_updated_errors' => $oLastLog->Get('stats_nb_obj_updated_errors'),
  382. 'obj_new_updated' => $oLastLog->Get('stats_nb_obj_new_updated'),
  383. 'obj_new_updated_warnings' => $oLastLog->Get('stats_nb_obj_new_updated_warnings'),
  384. 'obj_new_unchanged' => $oLastLog->Get('stats_nb_obj_new_unchanged'),
  385. 'obj_created' => $oLastLog->Get('stats_nb_obj_created'),
  386. 'obj_created_warnings' => $oLastLog->Get('stats_nb_obj_created_warnings'),
  387. 'obj_created_errors' => $oLastLog->Get('stats_nb_obj_created_errors'),
  388. 'obj_unchanged_warnings' => $oLastLog->Get('stats_nb_obj_unchanged_warnings'),
  389. );
  390. $iReconciledErrors = $oLastLog->Get('stats_nb_replica_reconciled_errors');
  391. $iDisappeared = $aData['obj_disappeared_errors'] + $aData['obj_obsoleted'] + $aData['obj_deleted'] + $aData['obj_disappeared_no_action'];
  392. $aData['repl_disappeared'] = $iDisappeared;
  393. $iNewErrors = $aData['obj_created_errors'] + $oLastLog->Get('stats_nb_replica_reconciled_errors');
  394. $aData['obj_new_errors'] = $iNewErrors;
  395. $iNew = $aData['obj_created'] + $iNewErrors + $aData['obj_new_updated'] + $aData['obj_new_unchanged'];
  396. $aData['repl_new'] = $iNew;
  397. $iExisting = $oLastLog->Get('stats_nb_replica_seen') - $iNew;
  398. $aData['repl_existing'] = $iExisting;
  399. $aData['obj_unchanged'] = $iExisting - $aData['obj_updated'] - $aData['obj_updated_errors'];
  400. $iIgnored = $oLastLog->Get('stats_nb_replica_total') - $iNew - $iExisting - $iDisappeared;
  401. $aData['repl_ignored'] = $iIgnored;
  402. $aData['nb_obj_total'] = $iNew + $iExisting + $iDisappeared;
  403. $aData['nb_replica_total'] = $aData['nb_obj_total'] + $iIgnored;
  404. return $aData;
  405. }
  406. public function GetIcon($bImgTag = true, $sMoreStyles = '')
  407. {
  408. if ($this->Get('url_icon') == '') return MetaModel::GetClassIcon(get_class($this), $bImgTag);
  409. if ($bImgTag)
  410. {
  411. return "<img src=\"".$this->Get('url_icon')."\" style=\"vertical-align:middle;$sMoreStyles\"/>";
  412. }
  413. return $this->Get('url_icon');
  414. }
  415. /**
  416. * Get the actual hyperlink to the remote application for the given replica and dest object
  417. */
  418. public function GetApplicationUrl(DBObject $oDestObj, SynchroReplica $oReplica)
  419. {
  420. if ($this->Get('url_application') == '') return '';
  421. $aSearches = array();
  422. $aReplacements = array();
  423. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  424. {
  425. if ($oAttDef->IsScalar())
  426. {
  427. $aSearches[] = '$this->'.$sAttCode.'$';
  428. $aReplacements[] = $oDestObj->Get($sAttCode);
  429. }
  430. }
  431. $aData = $oReplica->LoadExtendedDataFromTable($this->GetDataTable());
  432. foreach($aData as $sColumn => $value)
  433. {
  434. $aSearches[] = '$replica->'.$sColumn.'$';
  435. $aReplacements[] = $value;
  436. }
  437. return str_replace($aSearches, $aReplacements, $this->Get('url_application'));
  438. }
  439. public function GetAttributeFlags($sAttCode, &$aReasons = array(), $sTargetState = '')
  440. {
  441. if (($sAttCode == 'scope_class') && (!$this->IsNew()))
  442. {
  443. return OPT_ATT_READONLY;
  444. }
  445. return parent::GetAttributeFlags($sAttCode, $aReasons, $sTargetState);
  446. }
  447. public function UpdateObjectFromPostedForm($sFormPrefix = '', $sAttList = null, $sTargetState = '')
  448. {
  449. parent::UpdateObjectFromPostedForm($sFormPrefix, $sAttList, $sTargetState);
  450. // And now read the other post parameters...
  451. $oAttributeSet = $this->Get('attribute_list');
  452. $aAttributes = array();
  453. while($oAttribute = $oAttributeSet->Fetch())
  454. {
  455. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  456. }
  457. $aReconcile = utils::ReadPostedParam('reconciliation', array());
  458. $aUpdate = utils::ReadPostedParam('update', array());
  459. $aUpdatePolicy = utils::ReadPostedParam('attr_update_policy', array());
  460. $aReconciliation = utils::ReadPostedParam('attr_reconciliation_attcode', array());
  461. // update_policy cannot be empty, so there is one entry per attribute, use this to iterate
  462. // through all the writable attributes
  463. foreach($aUpdatePolicy as $sAttCode => $sValue)
  464. {
  465. if(!isset($aAttributes[$sAttCode]))
  466. {
  467. $oAttribute = $this->CreateSynchroAtt($sAttCode);
  468. }
  469. else
  470. {
  471. $oAttribute = $aAttributes[$sAttCode];
  472. }
  473. $bReconcile = 0;
  474. if (isset($aReconcile[$sAttCode]))
  475. {
  476. $bReconcile = $aReconcile[$sAttCode] == 'on' ? 1 : 0;
  477. }
  478. $bUpdate = 0 ; // Default / initial value
  479. if (isset($aUpdate[$sAttCode]))
  480. {
  481. $bUpdate = $aUpdate[$sAttCode] == 'on' ? 1 : 0;
  482. }
  483. $oAttribute->Set('reconcile', $bReconcile);
  484. $oAttribute->Set('update', $bUpdate);
  485. $oAttribute->Set('update_policy', $sValue);
  486. if ($oAttribute instanceof SynchroAttExtKey)
  487. {
  488. $oAttribute->Set('reconciliation_attcode', $aReconciliation[$sAttCode]);
  489. }
  490. elseif ($oAttribute instanceof SynchroAttLinkSet)
  491. {
  492. }
  493. $oAttributeSet->AddObject($oAttribute);
  494. }
  495. $this->Set('attribute_list', $oAttributeSet);
  496. }
  497. /**
  498. * Creates a new SynchroAttXXX object in memory with the default values
  499. */
  500. protected function CreateSynchroAtt($sAttCode)
  501. {
  502. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  503. if ($oAttDef->IsExternalKey())
  504. {
  505. $oAttribute = new SynchroAttExtKey();
  506. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  507. }
  508. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  509. {
  510. $oAttribute = new SynchroAttLinkSet();
  511. // Todo - set those value from the form
  512. $oAttribute->Set('row_separator', MetaModel::GetConfig()->Get('link_set_item_separator'));
  513. $oAttribute->Set('attribute_separator', MetaModel::GetConfig()->Get('link_set_attribute_separator'));
  514. $oAttribute->Set('value_separator', MetaModel::GetConfig()->Get('link_set_value_separator'));
  515. $oAttribute->Set('attribute_qualifier', MetaModel::GetConfig()->Get('link_set_attribute_qualifier'));
  516. }
  517. else
  518. {
  519. $oAttribute = new SynchroAttribute();
  520. }
  521. $oAttribute->Set('sync_source_id', $this->GetKey());
  522. $oAttribute->Set('attcode', $sAttCode);
  523. $oAttribute->Set('reconcile', 0);
  524. $oAttribute->Set('update', 0);
  525. $oAttribute->Set('update_policy', 'master_locked');
  526. return $oAttribute;
  527. }
  528. /**
  529. * Overload the standard behavior
  530. */
  531. public function ComputeValues()
  532. {
  533. parent::ComputeValues();
  534. if ($this->IsNew())
  535. {
  536. // When inserting a new datasource object, also create the SynchroAttribute objects
  537. // for each field of the target class
  538. // Create all the SynchroAttribute records
  539. $oAttributeSet = $this->Get('attribute_list');
  540. if ($oAttributeSet->Count() == 0)
  541. {
  542. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  543. {
  544. if ($oAttDef->IsWritable())
  545. {
  546. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  547. if ($oAttDef->IsExternalKey())
  548. {
  549. $oAttribute = new SynchroAttExtKey();
  550. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  551. }
  552. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  553. {
  554. $oAttribute = new SynchroAttLinkSet();
  555. // Todo - set those value from the form
  556. $oAttribute->Set('row_separator', MetaModel::GetConfig()->Get('link_set_item_separator'));
  557. $oAttribute->Set('attribute_separator', MetaModel::GetConfig()->Get('link_set_attribute_separator'));
  558. $oAttribute->Set('value_separator', MetaModel::GetConfig()->Get('link_set_value_separator'));
  559. $oAttribute->Set('attribute_qualifier', MetaModel::GetConfig()->Get('link_set_attribute_qualifier'));
  560. }
  561. elseif ($oAttDef->IsScalar())
  562. {
  563. $oAttribute = new SynchroAttribute();
  564. }
  565. else
  566. {
  567. $oAttribute = null;
  568. }
  569. if (!is_null($oAttribute))
  570. {
  571. $oAttribute->Set('sync_source_id', $this->GetKey());
  572. $oAttribute->Set('attcode', $sAttCode);
  573. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  574. $oAttribute->Set('update', 1);
  575. $oAttribute->Set('update_policy', 'master_locked');
  576. $oAttributeSet->AddObject($oAttribute);
  577. }
  578. }
  579. }
  580. $this->Set('attribute_list', $oAttributeSet);
  581. }
  582. }
  583. }
  584. public function DoCheckToWrite()
  585. {
  586. parent::DoCheckToWrite();
  587. // Check that there is at least one reconciliation key defined
  588. if ($this->Get('reconciliation_policy') == 'use_attributes')
  589. {
  590. $oSet = $this->Get('attribute_list');
  591. $oSynchroAttributeList = $oSet->ToArray();
  592. $bReconciliationKey = false;
  593. foreach($oSynchroAttributeList as $oSynchroAttribute)
  594. {
  595. if ($oSynchroAttribute->Get('reconcile') == 1)
  596. {
  597. $bReconciliationKey = true; // At least one key is defined
  598. break;
  599. }
  600. }
  601. if (!$bReconciliationKey)
  602. {
  603. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:AtLeastOneReconciliationKeyMustBeSpecified');
  604. }
  605. }
  606. // If 'update_then_delete' is specified there must be a delete_retention_period
  607. if (($this->Get('delete_policy') == 'update_then_delete') && ($this->Get('delete_policy_retention') == 0))
  608. {
  609. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeleteRetentionDurationMustBeSpecified');
  610. }
  611. // If update is specified, then something to update must be defined
  612. if ((($this->Get('delete_policy') == 'update_then_delete') || ($this->Get('delete_policy') == 'update'))
  613. && ($this->Get('delete_policy_update') == ''))
  614. {
  615. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeletePolicyUpdateMustBeSpecified');
  616. }
  617. }
  618. public function GetTargetClass()
  619. {
  620. return $this->Get('scope_class');
  621. }
  622. public function GetDataTable()
  623. {
  624. $sName = strtolower($this->GetTargetClass());
  625. $sName = str_replace('\'"&@|\\/ ', '_', $sName); // Remove forbidden characters from the table name
  626. $sName .= '_'.$this->GetKey(); // Add a suffix for unicity
  627. $sTable = MetaModel::GetConfig()->GetDBSubName()."synchro_data_$sName"; // Add the prefix if any
  628. return $sTable;
  629. }
  630. /**
  631. * When the new datasource has been created, let's create the synchro_data table
  632. * that will hold the data records and the correspoding triggers which will maintain
  633. * both tables in sync
  634. */
  635. protected function AfterInsert()
  636. {
  637. parent::AfterInsert();
  638. $sTable = $this->GetDataTable();
  639. $sReplicaTable = MetaModel::DBGetTable('SynchroReplica');
  640. $aColumns = $this->GetSQLColumns();
  641. $aFieldDefs = array();
  642. // Allow '0', otherwise mysql will render an error when the id is not given
  643. // (the trigger is expected to set the value, but it is not executed soon enough)
  644. $aFieldDefs[] = "id INTEGER(11) NOT NULL DEFAULT 0 ";
  645. $aFieldDefs[] = "`primary_key` VARCHAR(255) NULL DEFAULT NULL";
  646. foreach($aColumns as $sColumn => $ColSpec)
  647. {
  648. $aFieldDefs[] = "`$sColumn` $ColSpec NULL DEFAULT NULL";
  649. }
  650. $aFieldDefs[] = "INDEX (id)";
  651. $aFieldDefs[] = "INDEX (primary_key)";
  652. $sFieldDefs = implode(', ', $aFieldDefs);
  653. $sCreateTable = "CREATE TABLE `$sTable` ($sFieldDefs) ENGINE = ".MYSQL_ENGINE." CHARACTER SET utf8 COLLATE utf8_unicode_ci;";
  654. CMDBSource::Query($sCreateTable);
  655. $aTriggers = $this->GetTriggersDefinition();
  656. foreach($aTriggers as $key => $sTriggerSQL)
  657. {
  658. CMDBSource::Query($sTriggerSQL);
  659. }
  660. }
  661. /**
  662. * Gets the definitions of the 3 triggers: before insert, before update and after delete
  663. * @return array An array with 3 entries, one for each of the SQL queries
  664. */
  665. protected function GetTriggersDefinition()
  666. {
  667. $sTable = $this->GetDataTable();
  668. $sReplicaTable = MetaModel::DBGetTable('SynchroReplica');
  669. $aColumns = $this->GetSQLColumns();
  670. $aResult = array();
  671. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_bi` BEFORE INSERT ON `$sTable`";
  672. $sTriggerInsert .= " FOR EACH ROW";
  673. $sTriggerInsert .= " BEGIN";
  674. $sTriggerInsert .= " INSERT INTO `{$sReplicaTable}` (`sync_source_id`, `status_last_seen`, `status`) VALUES ({$this->GetKey()}, NOW(), 'new');";
  675. $sTriggerInsert .= " SET NEW.id = LAST_INSERT_ID();";
  676. $sTriggerInsert .= " END;";
  677. $aResult['bi'] = $sTriggerInsert;
  678. $aModified = array();
  679. foreach($aColumns as $sColumn => $ColSpec)
  680. {
  681. // <=> is a null-safe 'EQUALS' operator (there is no equivalent for "DIFFERS FROM")
  682. $aModified[] = "NOT(NEW.`$sColumn` <=> OLD.`$sColumn`)";
  683. }
  684. $sIsModified = '('.implode(') OR (', $aModified).')';
  685. // Update the replica
  686. //
  687. // status is forced to "new" if the replica was obsoleted directly from the state "new" (dest_id = null)
  688. // otherwise, if status was either 'obsolete' or 'synchronized' it is turned into 'modified' or 'synchronized' depending on the changes
  689. // otherwise, the status is left as is
  690. $sTriggerUpdate = "CREATE TRIGGER `{$sTable}_bu` BEFORE UPDATE ON `$sTable`";
  691. $sTriggerUpdate .= " FOR EACH ROW";
  692. $sTriggerUpdate .= " BEGIN";
  693. $sTriggerUpdate .= " IF @itopuser is null THEN";
  694. $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;";
  695. $sTriggerUpdate .= " SET NEW.id = OLD.id;"; // make sure this id won't change
  696. $sTriggerUpdate .= " END IF;";
  697. $sTriggerUpdate .= " END;";
  698. $aResult['bu'] = $sTriggerUpdate;
  699. $sTriggerDelete = "CREATE TRIGGER `{$sTable}_ad` AFTER DELETE ON `$sTable`";
  700. $sTriggerDelete .= " FOR EACH ROW";
  701. $sTriggerDelete .= " BEGIN";
  702. $sTriggerDelete .= " DELETE FROM `{$sReplicaTable}` WHERE id = OLD.id;";
  703. $sTriggerDelete .= " END;";
  704. $aResult['ad'] = $sTriggerDelete;
  705. return $aResult;
  706. }
  707. protected function AfterDelete()
  708. {
  709. parent::AfterDelete();
  710. $sTable = $this->GetDataTable();
  711. $sDropTable = "DROP TABLE `$sTable`";
  712. CMDBSource::Query($sDropTable);
  713. // TO DO - check that triggers get dropped with the table
  714. }
  715. /**
  716. * Checks if the data source definition is consistent with the schema of the target class
  717. * @param $bDiagnostics boolean True to only diagnose the consistency, false to actually apply some changes
  718. * @param $bVerbose boolean True to get some information in the std output (echo)
  719. * @return bool Whether or not the database needs fixing for this data source
  720. */
  721. public function CheckDBConsistency($bDiagnostics, $bVerbose, $oChange = null)
  722. {
  723. $bFixNeeded = false;
  724. $aMissingFields = array();
  725. $oAttributeSet = $this->Get('attribute_list');
  726. $aAttributes = array();
  727. while($oAttribute = $oAttributeSet->Fetch())
  728. {
  729. $sAttCode = $oAttribute->Get('attcode');
  730. if (MetaModel::IsValidAttCode($this->GetTargetClass(), $sAttCode))
  731. {
  732. $aAttributes[$sAttCode] = $oAttribute;
  733. }
  734. else
  735. {
  736. // Old field remaining
  737. if ($bVerbose)
  738. {
  739. echo "Irrelevant field description for the field '$sAttCode', for the data synchro task ".$this->GetName()." (".$this->GetKey()."), will be removed.\n";
  740. }
  741. $bFixNeeded = true;
  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->DBDeleteTracked($oChange);
  754. }
  755. }
  756. }
  757. $sTable = $this->GetDataTable();
  758. foreach($this->ListTargetAttributes() as $sAttCode=>$oAttDef)
  759. {
  760. if (!isset($aAttributes[$sAttCode]))
  761. {
  762. $bFixNeeded = true;
  763. $aMissingFields[] = $sAttCode;
  764. // New field missing...
  765. if ($bVerbose)
  766. {
  767. echo "Missing field description for the field '$sAttCode', for the data synchro task ".$this->GetName()." (".$this->GetKey()."), will be created with default values.\n";
  768. }
  769. if (!$bDiagnostics)
  770. {
  771. if ($oChange == null)
  772. {
  773. $oChange = MetaModel::NewObject("CMDBChange");
  774. $oChange->Set("date", time());
  775. $sUserString = CMDBChange::GetCurrentUserName();
  776. $oChange->Set("userinfo", $sUserString);
  777. $oChange->DBInsert();
  778. }
  779. // Fix the issue
  780. $oAttribute = $this->CreateSynchroAtt($sAttCode);
  781. $oAttribute->DBInsertTracked($oChange);
  782. }
  783. }
  784. else
  785. {
  786. $aColumns = $this->GetSQLColumns(array($sAttCode));
  787. foreach($aColumns as $sColName => $sColumnDef)
  788. {
  789. $bOneColIsMissing = false;
  790. if (!CMDBSource::IsField($sTable, $sColName))
  791. {
  792. $bFixNeeded = true;
  793. $bOneColIsMissing = true;
  794. if ($bVerbose)
  795. {
  796. if (count($aColumns) > 1)
  797. {
  798. echo "Missing column '$sColName', in the table '$sTable' for the data synchro task ".$this->GetName()." (".$this->GetKey()."). The columns '".implode("', '", $aColumns )." will be re-created.'.\n";
  799. }
  800. else
  801. {
  802. echo "Missing column '$sColName', in the table '$sTable' for the data synchro task ".$this->GetName()." (".$this->GetKey()."). The column '$sColName' will be added.\n";
  803. }
  804. }
  805. }
  806. else if (strcasecmp(CMDBSource::GetFieldType($sTable, $sColName), $sColumnDef) != 0)
  807. {
  808. $bFixNeeded = true;
  809. $bOneColIsMissing = true;
  810. if (count($aColumns) > 1)
  811. {
  812. echo "Incorrect column '$sColName' (".CMDBSource::GetFieldType($sTable, $sColName)." instead of ".$sColumnDef."), in the table '$sTable' for the data synchro task ".$this->GetName()." (".$this->GetKey()."). The columns '".implode("', '", $aColumns )." will be re-created.'.\n";
  813. }
  814. else
  815. {
  816. echo "Incorrect column '$sColName' (".CMDBSource::GetFieldType($sTable, $sColName)." instead of ".$sColumnDef."), in the table '$sTable' for the data synchro task ".$this->GetName()." (".$this->GetKey()."). The column '$sColName' will be added.\n";
  817. }
  818. }
  819. if ($bOneColIsMissing)
  820. {
  821. $aMissingFields[] = $sAttCode;
  822. }
  823. }
  824. }
  825. }
  826. if ($bFixNeeded && (count($aMissingFields) > 0))
  827. {
  828. $aRepairQueries = array();
  829. // The structure of the table needs adjusting
  830. $aColumns = $this->GetSQLColumns($aMissingFields);
  831. $aFieldDefs = array();
  832. foreach($aColumns as $sAttCode => $sColumnDef)
  833. {
  834. if (CMDBSource::IsField($sTable, $sAttCode))
  835. {
  836. $aRepairQueries[] = "ALTER TABLE `$sTable` CHANGE `$sAttCode` `$sAttCode` $sColumnDef";
  837. }
  838. else
  839. {
  840. $aFieldDefs[] = "`$sAttCode` $sColumnDef";
  841. }
  842. }
  843. if (count($aFieldDefs) > 0)
  844. {
  845. $aRepairQueries[] = "ALTER TABLE `$sTable` ADD (".implode(',', $aFieldDefs).");";
  846. }
  847. // The triggers as well must be adjusted
  848. $aTriggersDefs = $this->GetTriggersDefinition();
  849. $aRepairQueries[] = "DROP TRIGGER IF EXISTS `{$sTable}_bi`;";
  850. $aRepairQueries[] = $aTriggersDefs['bi'];
  851. $aRepairQueries[] = "DROP TRIGGER IF EXISTS `{$sTable}_bu`;";
  852. $aRepairQueries[] = $aTriggersDefs['bu'];
  853. $aRepairQueries[] = "DROP TRIGGER IF EXISTS `{$sTable}_ad`;";
  854. $aRepairQueries[] = $aTriggersDefs['ad'];
  855. if ($bDiagnostics)
  856. {
  857. if ($bVerbose)
  858. {
  859. // Report the issue
  860. echo "The structure of the table $sTable for the data synchro task ".$this->GetName()." (".$this->GetKey().") must be altered (missing or incorrect fields: ".implode(',', $aMissingFields).").\n";
  861. echo "The trigger {$sTable}_bi, {$sTable}_bu, {$sTable}_ad for the data synchro task ".$this->GetName()." (".$this->GetKey().") must be re-created.\n";
  862. echo implode("\n", $aRepairQueries)."\n";
  863. }
  864. }
  865. else
  866. {
  867. // Fix the issue
  868. foreach($aRepairQueries as $sSQL)
  869. {
  870. CMDBSource::Query($sSQL);
  871. if ($bVerbose)
  872. {
  873. echo "$sSQL\n";
  874. }
  875. }
  876. }
  877. }
  878. return $bFixNeeded;
  879. }
  880. public function SendNotification($sSubject, $sBody)
  881. {
  882. $iContact = $this->Get('notify_contact_id');
  883. if ($iContact == 0)
  884. {
  885. // Leave silently...
  886. return;
  887. }
  888. $oContact = MetaModel::GetObject('Contact', $iContact);
  889. // Determine the email attribute (the first one will be our choice)
  890. $sEmailAttCode = null;
  891. foreach (MetaModel::ListAttributeDefs(get_class($oContact)) as $sAttCode => $oAttDef)
  892. {
  893. if ($oAttDef instanceof AttributeEmailAddress)
  894. {
  895. $sEmailAttCode = $sAttCode;
  896. // we've got one, exit the loop
  897. break;
  898. }
  899. }
  900. if (is_null($sEmailAttCode))
  901. {
  902. // Leave silently...
  903. return;
  904. }
  905. $sTo = $oContact->Get($sEmailAttCode);
  906. $sFrom = $sTo;
  907. $sBody = '<p>Data synchronization: '.$this->GetHyperlink().'</p>'.$sBody;
  908. $sSubject = 'iTop Data Sync - '.$this->GetName().' - '.$sSubject;
  909. $oEmail = new Email();
  910. $oEmail->SetRecipientTO($sTo);
  911. $oEmail->SetRecipientFrom($sFrom);
  912. $oEmail->SetSubject($sSubject);
  913. $oEmail->SetBody($sBody);
  914. if ($oEmail->Send($aIssues) == EMAIL_SEND_ERROR)
  915. {
  916. // mmmm, what can I do?
  917. }
  918. }
  919. /**
  920. * Get the list of attributes eligible to the synchronization
  921. */
  922. public function ListTargetAttributes()
  923. {
  924. $aRet = array();
  925. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode => $oAttDef)
  926. {
  927. if ($sAttCode == 'finalclass') continue;
  928. if (!$oAttDef->IsWritable()) continue;
  929. if ($oAttDef->IsLinkSet() && !$oAttDef->IsIndirect()) continue;
  930. $aRet[$sAttCode] = $oAttDef;
  931. }
  932. return $aRet;
  933. }
  934. /**
  935. * Get the list of SQL columns corresponding to a particular list of attribute codes
  936. * Defaults to the whole list of columns for the current class
  937. */
  938. public function GetSQLColumns($aAttributeCodes = null)
  939. {
  940. $aColumns = array();
  941. $sClass = $this->GetTargetClass();
  942. if (is_null($aAttributeCodes))
  943. {
  944. $aAttributeCodes = array();
  945. foreach($this->ListTargetAttributes() as $sAttCode => $oAttDef)
  946. {
  947. $aAttributeCodes[] = $sAttCode;
  948. }
  949. }
  950. foreach($aAttributeCodes as $sAttCode)
  951. {
  952. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  953. if ($oAttDef->IsExternalKey())
  954. {
  955. // The pkey might be used as well as any other key column
  956. $aColumns[$sAttCode] = 'VARCHAR(255)';
  957. }
  958. else
  959. {
  960. foreach($oAttDef->GetImportColumns() as $sField => $sDBFieldType)
  961. {
  962. $aColumns[$sField] = $sDBFieldType;
  963. }
  964. }
  965. }
  966. return $aColumns;
  967. }
  968. /**
  969. * Get the list of Date and Datetime SQL columns
  970. */
  971. public function GetDateSQLColumns()
  972. {
  973. $aDateAttributes = array();
  974. $sClass = $this->GetTargetClass();
  975. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  976. {
  977. if ($oAttDef instanceof AttributeDateTime)
  978. {
  979. $aDateAttributes[] = $sAttCode;
  980. }
  981. }
  982. return $this->GetSQLColumns($aDateAttributes);
  983. }
  984. public function IsRunning()
  985. {
  986. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='running'";
  987. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  988. if ($oSet->Count() < 1)
  989. {
  990. $bRet = false;
  991. }
  992. else
  993. {
  994. $bRet = true;
  995. }
  996. return $bRet;
  997. }
  998. public function GetLatestLog()
  999. {
  1000. $oLog = null;
  1001. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id";
  1002. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  1003. if ($oSet->Count() >= 1)
  1004. {
  1005. $oLog = $oSet->Fetch();
  1006. }
  1007. return $oLog;
  1008. }
  1009. // TO DO: remove if still unused
  1010. /**
  1011. * Retrieve from the log, the date of the last completed import
  1012. * @return DateTime
  1013. */
  1014. public function GetLastCompletedImportDate()
  1015. {
  1016. $date = null;
  1017. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='completed'";
  1018. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('end_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 0 /* limitCount */, 0 /* limitStart */);
  1019. if ($oSet->Count() >= 1)
  1020. {
  1021. $oLog = $oSet->Fetch();
  1022. $date = $oLog->Get('end_date');
  1023. }
  1024. return $date;
  1025. }
  1026. }
  1027. class SynchroAttribute extends cmdbAbstractObject
  1028. {
  1029. public static function Init()
  1030. {
  1031. $aParams = array
  1032. (
  1033. "category" => "core/cmdb,view_in_gui",
  1034. "key_type" => "autoincrement",
  1035. "name_attcode" => "",
  1036. "state_attcode" => "",
  1037. "reconc_keys" => array(),
  1038. "db_table" => "priv_sync_att",
  1039. "db_key_field" => "id",
  1040. "db_finalclass_field" => "",
  1041. "display_template" => "",
  1042. );
  1043. MetaModel::Init_Params($aParams);
  1044. MetaModel::Init_InheritAttributes();
  1045. 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())));
  1046. MetaModel::Init_AddAttribute(new AttributeExternalField("sync_source_name", array("allowed_values"=>null, "extkey_attcode"=> 'sync_source_id', "target_attcode"=>"name")));
  1047. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  1048. MetaModel::Init_AddAttribute(new AttributeBoolean("update", array("allowed_values"=>null, "sql"=>"update", "default_value"=>true, "is_null_allowed"=>false, "depends_on"=>array())));
  1049. MetaModel::Init_AddAttribute(new AttributeBoolean("reconcile", array("allowed_values"=>null, "sql"=>"reconcile", "default_value"=>false, "is_null_allowed"=>false, "depends_on"=>array())));
  1050. 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())));
  1051. // Display lists
  1052. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for the complete details
  1053. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1054. // Search criteria
  1055. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1056. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1057. }
  1058. }
  1059. class SynchroAttExtKey extends SynchroAttribute
  1060. {
  1061. public static function Init()
  1062. {
  1063. $aParams = array
  1064. (
  1065. "category" => "core/cmdb,view_in_gui",
  1066. "key_type" => "autoincrement",
  1067. "name_attcode" => "",
  1068. "state_attcode" => "",
  1069. "reconc_keys" => array(),
  1070. "db_table" => "priv_sync_att_extkey",
  1071. "db_key_field" => "id",
  1072. "db_finalclass_field" => "",
  1073. "display_template" => "",
  1074. );
  1075. MetaModel::Init_Params($aParams);
  1076. MetaModel::Init_InheritAttributes();
  1077. MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  1078. // Display lists
  1079. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
  1080. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1081. // Search criteria
  1082. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1083. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1084. }
  1085. public function GetReconciliationFormElement($sTargetClass, $sFieldName)
  1086. {
  1087. $sHtml = "<select name=\"$sFieldName\">\n";
  1088. $sSelected = (''== $this->Get('reconciliation_attcode')) ? ' selected' : '';
  1089. $sHtml .= "<option value=\"\" $sSelected>".Dict::S('Core:SynchroAttExtKey:ReconciliationById')."</option>\n";
  1090. foreach(MetaModel::ListAttributeDefs($sTargetClass) as $sAttCode => $oAttDef)
  1091. {
  1092. if ($oAttDef->IsScalar())
  1093. {
  1094. $sSelected = ($sAttCode == $this->Get('reconciliation_attcode')) ? ' selected' : '';
  1095. $sHtml .= "<option value=\"$sAttCode\" $sSelected>".MetaModel::GetLabel($sTargetClass, $sAttCode)."</option>\n";
  1096. }
  1097. }
  1098. $sHtml .= "</select>\n";
  1099. return $sHtml;
  1100. }
  1101. }
  1102. class SynchroAttLinkSet extends SynchroAttribute
  1103. {
  1104. public static function Init()
  1105. {
  1106. $aParams = array
  1107. (
  1108. "category" => "core/cmdb,view_in_gui",
  1109. "key_type" => "autoincrement",
  1110. "name_attcode" => "",
  1111. "state_attcode" => "",
  1112. "reconc_keys" => array(),
  1113. "db_table" => "priv_sync_att_linkset",
  1114. "db_key_field" => "id",
  1115. "db_finalclass_field" => "",
  1116. "display_template" => "",
  1117. );
  1118. MetaModel::Init_Params($aParams);
  1119. MetaModel::Init_InheritAttributes();
  1120. MetaModel::Init_AddAttribute(new AttributeString("row_separator", array("allowed_values"=>null, "sql"=>"row_separator", "default_value"=>'|', "is_null_allowed"=>true, "depends_on"=>array())));
  1121. MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
  1122. MetaModel::Init_AddAttribute(new AttributeString("value_separator", array("allowed_values"=>null, "sql"=>"value_separator", "default_value"=>':', "is_null_allowed"=>true, "depends_on"=>array())));
  1123. MetaModel::Init_AddAttribute(new AttributeString("attribute_qualifier", array("allowed_values"=>null, "sql"=>"attribute_qualifier", "default_value"=>'\'', "is_null_allowed"=>true, "depends_on"=>array())));
  1124. // Display lists
  1125. 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
  1126. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1127. // Search criteria
  1128. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1129. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1130. }
  1131. }
  1132. //class SynchroLog extends Event
  1133. class SynchroLog extends DBObject
  1134. {
  1135. public static function Init()
  1136. {
  1137. $aParams = array
  1138. (
  1139. "category" => "core/cmdb,view_in_gui",
  1140. "key_type" => "autoincrement",
  1141. "name_attcode" => "",
  1142. "state_attcode" => "",
  1143. "reconc_keys" => array(),
  1144. "db_table" => "priv_sync_log",
  1145. "db_key_field" => "id",
  1146. "db_finalclass_field" => "",
  1147. "display_template" => "",
  1148. );
  1149. MetaModel::Init_Params($aParams);
  1150. MetaModel::Init_InheritAttributes();
  1151. // MetaModel::Init_AddAttribute(new AttributeString("userinfo", array("allowed_values"=>null, "sql"=>"userinfo", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  1152. 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())));
  1153. MetaModel::Init_AddAttribute(new AttributeDateTime("start_date", array("allowed_values"=>null, "sql"=>"start_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1154. MetaModel::Init_AddAttribute(new AttributeDateTime("end_date", array("allowed_values"=>null, "sql"=>"end_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1155. 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())));
  1156. MetaModel::Init_AddAttribute(new AttributeInteger("status_curr_job", array("allowed_values"=>null, "sql"=>"status_curr_job", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1157. MetaModel::Init_AddAttribute(new AttributeInteger("status_curr_pos", array("allowed_values"=>null, "sql"=>"status_curr_pos", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1158. 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())));
  1159. 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())));
  1160. 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())));
  1161. 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())));
  1162. 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())));
  1163. 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())));
  1164. 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())));
  1165. 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())));
  1166. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_created_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_created_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1167. 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())));
  1168. 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())));
  1169. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_updated_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_updated_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1170. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_unchanged_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_unchanged_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1171. // 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())));
  1172. 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())));
  1173. 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())));
  1174. 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())));
  1175. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_new_updated_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_new_updated_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1176. 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())));
  1177. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_new_unchanged_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_new_unchanged_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1178. MetaModel::Init_AddAttribute(new AttributeText("last_error", array("allowed_values"=>null, "sql"=>"last_error", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1179. MetaModel::Init_AddAttribute(new AttributeLongText("traces", array("allowed_values"=>null, "sql"=>"traces", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1180. MetaModel::Init_AddAttribute(new AttributeInteger("memory_usage_peak", array("allowed_values"=>null, "sql"=>"memory_usage_peak", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1181. // Display lists
  1182. 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',
  1183. '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
  1184. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_replica_seen')); // Attributes to be displayed for a list
  1185. // Search criteria
  1186. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1187. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1188. }
  1189. /**
  1190. * Helper
  1191. */
  1192. function GetErrorCount()
  1193. {
  1194. return $this->Get('stats_nb_obj_deleted_errors')
  1195. + $this->Get('stats_nb_obj_obsoleted_errors')
  1196. + $this->Get('stats_nb_obj_created_errors')
  1197. + $this->Get('stats_nb_obj_updated_errors')
  1198. + $this->Get('stats_nb_replica_reconciled_errors');
  1199. }
  1200. /**
  1201. * Increments a statistics counter
  1202. */
  1203. function Inc($sCode)
  1204. {
  1205. $this->Set($sCode, 1+$this->Get($sCode));
  1206. }
  1207. /**
  1208. * Implement traces management
  1209. */
  1210. protected $m_aTraces = array();
  1211. public function AddTrace($sMsg, $oReplica = null)
  1212. {
  1213. if (MetaModel::GetConfig()->Get('synchro_trace') == 'none')
  1214. {
  1215. return;
  1216. }
  1217. if ($oReplica)
  1218. {
  1219. $sDestClass = $oReplica->Get('dest_class');
  1220. if (!empty($sDestClass))
  1221. {
  1222. $sPrefix = $oReplica->GetKey().','.$sDestClass.'::'.$oReplica->Get('dest_id').',';
  1223. }
  1224. else
  1225. {
  1226. $sPrefix = $oReplica->GetKey().',,';
  1227. }
  1228. }
  1229. else
  1230. {
  1231. $sPrefix = ',,';
  1232. }
  1233. $this->m_aTraces[] = $sPrefix.$sMsg;
  1234. }
  1235. public function GetTraces()
  1236. {
  1237. return $this->m_aTraces;
  1238. }
  1239. protected function TraceToText()
  1240. {
  1241. if (MetaModel::GetConfig()->Get('synchro_trace') != 'save')
  1242. {
  1243. // none, or display only
  1244. return;
  1245. }
  1246. $sPrevTrace = $this->Get('traces');
  1247. $oAttDef = MetaModel::GetAttributeDef(get_class($this), 'traces');
  1248. $iMaxSize = $oAttDef->GetMaxSize();
  1249. if (strlen($sPrevTrace) > 0)
  1250. {
  1251. $sTrace = $sPrevTrace."\n".implode("\n", $this->m_aTraces);
  1252. }
  1253. else
  1254. {
  1255. $sTrace = implode("\n", $this->m_aTraces);
  1256. }
  1257. if (strlen($sTrace) >= $iMaxSize)
  1258. {
  1259. $sTrace = substr($sTrace, 0, $iMaxSize - 255)."...\nTruncated (size: ".strlen($sTrace).')';
  1260. }
  1261. $this->Set('traces', $sTrace);
  1262. //DBUpdate may be called many times... the operation should not be repeated
  1263. $this->m_aTraces = array();
  1264. }
  1265. protected function OnInsert()
  1266. {
  1267. $this->TraceToText();
  1268. parent::OnInsert();
  1269. }
  1270. protected function OnUpdate()
  1271. {
  1272. $this->TraceToText();
  1273. $sMemPeak = max($this->Get('memory_usage_peak'), ExecutionKPI::memory_get_peak_usage());
  1274. $this->Set('memory_usage_peak', $sMemPeak);
  1275. parent::OnUpdate();
  1276. }
  1277. }
  1278. class SynchroReplica extends DBObject implements iDisplay
  1279. {
  1280. static $aSearches = array(); // Cache of OQL queries used for reconciliation (per data source)
  1281. protected $aWarnings;
  1282. public static function Init()
  1283. {
  1284. $aParams = array
  1285. (
  1286. "category" => "core/cmdb,view_in_gui",
  1287. "key_type" => "autoincrement",
  1288. "name_attcode" => "",
  1289. "state_attcode" => "",
  1290. "reconc_keys" => array(),
  1291. "db_table" => "priv_sync_replica",
  1292. "db_key_field" => "id",
  1293. "db_finalclass_field" => "",
  1294. "display_template" => "",
  1295. );
  1296. MetaModel::Init_Params($aParams);
  1297. MetaModel::Init_InheritAttributes();
  1298. 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())));
  1299. MetaModel::Init_AddAttribute(new AttributeExternalField("base_class", array("allowed_values"=>null, "extkey_attcode"=> 'sync_source_id', "target_attcode"=>"scope_class")));
  1300. MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1301. 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())));
  1302. 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())));
  1303. 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())));
  1304. 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())));
  1305. 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())));
  1306. MetaModel::Init_AddAttribute(new AttributeString("status_last_warning", array("allowed_values"=>null, "sql"=>"status_last_warning", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1307. 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())));
  1308. 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())));
  1309. // Display lists
  1310. MetaModel::Init_SetZListItems('details', array('' .
  1311. 'col:0'=> array(
  1312. 'fieldset:SynchroDataSource:Definition' => array('sync_source_id','dest_id','dest_class'),
  1313. 'fieldset:SynchroDataSource:Status' => array('status','status_last_seen','status_dest_creator','status_last_error','status_last_warning'),
  1314. 'fieldset:SynchroDataSource:Information' => array('info_creation_date','info_last_modified'))
  1315. )
  1316. );
  1317. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'dest_id', 'dest_class', 'status_last_seen', 'status', 'status_dest_creator', 'status_last_error', 'status_last_warning')); // Attributes to be displayed for a list
  1318. // Search criteria
  1319. MetaModel::Init_SetZListItems('standard_search', array('sync_source_id', 'status_last_seen', 'status', 'status_dest_creator', 'dest_class', 'dest_id', 'status_last_error', 'status_last_warning')); // Criteria of the std search form
  1320. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1321. }
  1322. public function __construct($aRow = null, $sClassAlias = '', $aAttToLoad = null, $aExtendedDataSpec = null)
  1323. {
  1324. parent::__construct($aRow, $sClassAlias, $aAttToLoad, $aExtendedDataSpec);
  1325. $this->aWarnings = array();
  1326. }
  1327. protected function AddWarning($sWarningMessage)
  1328. {
  1329. $this->aWarnings[] = $sWarningMessage;
  1330. }
  1331. protected function ResetWarnings()
  1332. {
  1333. $this->aWarnings = array();
  1334. }
  1335. protected function HasWarnings()
  1336. {
  1337. return (count($this->aWarnings) > 0);
  1338. }
  1339. protected function RecordWarnings()
  1340. {
  1341. $sWarningMessage = '';
  1342. $MAX_WARNING_LENGTH = 255;
  1343. switch(count($this->aWarnings))
  1344. {
  1345. case 0:
  1346. $sWarningMessage = '';
  1347. break;
  1348. case 1:
  1349. $sWarningMessage = $this->aWarnings[0];
  1350. break;
  1351. default:
  1352. $sWarningMessage = count($this->aWarnings)." warnings: ".implode(' ', $this->aWarnings);
  1353. break;
  1354. }
  1355. if (strlen($sWarningMessage) > $MAX_WARNING_LENGTH)
  1356. {
  1357. $sWarningMessage = substr($sWarningMessage, 0, $MAX_WARNING_LENGTH - 3).'...';
  1358. }
  1359. $this->Set('status_last_warning', $sWarningMessage);
  1360. }
  1361. public function DBInsert()
  1362. {
  1363. throw new CoreException('A synchronization replica must be created only by the mean of triggers');
  1364. }
  1365. // Overload the deletion -> the replica has been created by the mean of a trigger,
  1366. // it will be deleted by the mean of a trigger too
  1367. protected function DBDeleteSingleObject()
  1368. {
  1369. $this->OnDelete();
  1370. if (!MetaModel::DBIsReadOnly())
  1371. {
  1372. $oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'), false);
  1373. if ($oDataSource)
  1374. {
  1375. $sTable = $oDataSource->GetDataTable();
  1376. $sSQL = "DELETE FROM `$sTable` WHERE id = '{$this->GetKey()}'";
  1377. CMDBSource::Query($sSQL);
  1378. }
  1379. // else the whole datasource has probably been already deleted
  1380. }
  1381. $this->AfterDelete();
  1382. $this->m_bIsInDB = false;
  1383. $this->m_iKey = null;
  1384. }
  1385. public function SetLastError($sMessage, $oException = null)
  1386. {
  1387. if ($oException)
  1388. {
  1389. $sText = $sMessage.$oException->getMessage();
  1390. }
  1391. else
  1392. {
  1393. $sText = $sMessage;
  1394. }
  1395. if (strlen($sText) > 255)
  1396. {
  1397. $sText = substr($sText, 0, 200).'...('.strlen($sText).' chars)...';
  1398. }
  1399. $this->Set('status_last_error', $sText);
  1400. }
  1401. public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange, &$oStatLog)
  1402. {
  1403. $this->ResetWarnings();
  1404. switch($this->Get('status'))
  1405. {
  1406. case 'new':
  1407. $this->Set('status_dest_creator', false);
  1408. // If needed, construct the query used for the reconciliation
  1409. if (!isset(self::$aSearches[$oDataSource->GetKey()]))
  1410. {
  1411. $aCriterias = array();
  1412. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1413. {
  1414. $aCriterias[] = ($sFilterCode == 'primary_key' ? 'id' : $sFilterCode).' = :'.$sFilterCode;
  1415. }
  1416. $sOQL = "SELECT ".$oDataSource->GetTargetClass()." WHERE ".implode(' AND ', $aCriterias);
  1417. self::$aSearches[$oDataSource->GetKey()] = DBObjectSearch::FromOQL($sOQL);
  1418. }
  1419. // Get the criterias for the search
  1420. $aFilterValues = array();
  1421. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1422. {
  1423. $value = $this->GetValueFromExtData($sFilterCode, $oSyncAtt, $oStatLog);
  1424. if (!is_null($value))
  1425. {
  1426. $aFilterValues[$sFilterCode] = $value;
  1427. }
  1428. else
  1429. {
  1430. // TO DO: can we retry this ??
  1431. // Reconciliation could not be performed - log and EXIT
  1432. $oStatLog->AddTrace("Could not reconcile on null value for attribute '$sFilterCode'", $this);
  1433. $this->SetLastError("Could not reconcile on null value for attribute '$sFilterCode'");
  1434. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1435. return;
  1436. }
  1437. }
  1438. $oDestSet = new DBObjectSet(self::$aSearches[$oDataSource->GetKey()], array(), $aFilterValues);
  1439. $iCount = $oDestSet->Count();
  1440. $aConditions = array();
  1441. foreach($aFilterValues as $sCode => $sValue)
  1442. {
  1443. $aConditions[] = $sCode.'='.$sValue;
  1444. }
  1445. $sConditionDesc = implode(' AND ', $aConditions);
  1446. // How many objects match the reconciliation criterias
  1447. switch($iCount)
  1448. {
  1449. case 0:
  1450. $oStatLog->AddTrace("Nothing found on: $sConditionDesc", $this);
  1451. if ($oDataSource->Get('action_on_zero') == 'create')
  1452. {
  1453. $bCreated = $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1454. if ($bCreated)
  1455. {
  1456. if ($this->HasWarnings())
  1457. {
  1458. $oStatLog->Inc('stats_nb_obj_created_warnings');
  1459. }
  1460. }
  1461. else
  1462. {
  1463. // Creation error has precedence over any warning
  1464. $this->ResetWarnings();
  1465. }
  1466. }
  1467. else // assumed to be 'error'
  1468. {
  1469. $oStatLog->AddTrace("Failed to reconcile (no match)", $this);
  1470. // Recoverable error
  1471. $this->SetLastError('Could not find a match for reconciliation');
  1472. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1473. }
  1474. break;
  1475. case 1:
  1476. $oStatLog->AddTrace("Found 1 object on: $sConditionDesc", $this);
  1477. if ($oDataSource->Get('action_on_one') == 'update')
  1478. {
  1479. $oDestObj = $oDestSet->Fetch();
  1480. $bModified = $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1481. $this->Set('dest_id', $oDestObj->GetKey());
  1482. $this->Set('dest_class', get_class($oDestObj));
  1483. if ($this->HasWarnings())
  1484. {
  1485. if ($bModified)
  1486. {
  1487. $oStatLog->Inc('stats_nb_obj_new_updated_warnings');
  1488. }
  1489. else
  1490. {
  1491. $oStatLog->Inc('stats_nb_obj_new_unchanged_warnings');
  1492. }
  1493. }
  1494. }
  1495. else
  1496. {
  1497. // assumed to be 'error'
  1498. $oStatLog->AddTrace("Failed to reconcile (1 match)", $this);
  1499. // Recoverable error
  1500. $this->SetLastError('Found a match while expecting several');
  1501. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1502. }
  1503. break;
  1504. default:
  1505. $oStatLog->AddTrace("Found $iCount objects on: $sConditionDesc", $this);
  1506. if ($oDataSource->Get('action_on_multiple') == 'error')
  1507. {
  1508. $oStatLog->AddTrace("Failed to reconcile (N>1 matches)", $this);
  1509. // Recoverable error
  1510. $this->SetLastError($iCount.' destination objects match the reconciliation criterias: '.$sConditionDesc);
  1511. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1512. }
  1513. elseif ($oDataSource->Get('action_on_multiple') == 'create')
  1514. {
  1515. $bCreated = $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1516. if ($bCreated)
  1517. {
  1518. if ($this->HasWarnings())
  1519. {
  1520. $oStatLog->Inc('stats_nb_obj_created_warnings');
  1521. }
  1522. }
  1523. else
  1524. {
  1525. // Creation error has precedence over any warning
  1526. $this->ResetWarnings();
  1527. }
  1528. }
  1529. else
  1530. {
  1531. // assumed to be 'take_first'
  1532. $oDestObj = $oDestSet->Fetch();
  1533. $bModified = $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1534. $this->Set('dest_id', $oDestObj->GetKey());
  1535. $this->Set('dest_class', get_class($oDestObj));
  1536. if ($this->HasWarnings())
  1537. {
  1538. if ($bModified)
  1539. {
  1540. $oStatLog->Inc('stats_nb_obj_new_updated_warnings');
  1541. }
  1542. else
  1543. {
  1544. $oStatLog->Inc('stats_nb_obj_new_unchanged_warnings');
  1545. }
  1546. }
  1547. }
  1548. }
  1549. $this->RecordWarnings();
  1550. break;
  1551. case 'synchronized': // try to recover synchronized replicas with warnings
  1552. case 'modified':
  1553. $oDestObj = MetaModel::GetObject($oDataSource->GetTargetClass(), $this->Get('dest_id'));
  1554. if ($oDestObj == null)
  1555. {
  1556. $this->Set('status', 'orphan'); // The destination object has been deleted !
  1557. $this->SetLastError('Destination object deleted unexpectedly');
  1558. $oStatLog->Inc('stats_nb_obj_updated_errors');
  1559. }
  1560. else
  1561. {
  1562. $bModified = $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj', 'stats_nb_obj_updated_errors');
  1563. if ($this->HasWarnings())
  1564. {
  1565. if ($bModified)
  1566. {
  1567. $oStatLog->Inc('stats_nb_obj_updated_warnings');
  1568. }
  1569. else
  1570. {
  1571. $oStatLog->Inc('stats_nb_obj_unchanged_warnings');
  1572. }
  1573. }
  1574. }
  1575. $this->RecordWarnings();
  1576. break;
  1577. default: // Do nothing in all other cases
  1578. }
  1579. }
  1580. /**
  1581. * Updates the destination object with the Extended data found in the synchro_data_XXXX table
  1582. */
  1583. protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, &$oStatLog, $sStatsCode, $sStatsCodeError)
  1584. {
  1585. $aValueTrace = array();
  1586. $bModified = false;
  1587. try
  1588. {
  1589. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1590. {
  1591. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1592. if (!is_null($value))
  1593. {
  1594. if ($oSyncAtt->Get('update_policy') == 'write_if_empty')
  1595. {
  1596. $oAttDef = MetaModel::GetAttributeDef(get_class($oDestObj), $sAttCode);
  1597. if ($oAttDef->IsNull($oDestObj->Get($sAttCode)))
  1598. {
  1599. // The value is still "empty" in the target object, we are allowed to write the new value
  1600. $oDestObj->Set($sAttCode, $value);
  1601. $aValueTrace[] = "$sAttCode: $value";
  1602. }
  1603. }
  1604. else
  1605. {
  1606. $oDestObj->Set($sAttCode, $value);
  1607. $aValueTrace[] = "$sAttCode: $value";
  1608. }
  1609. }
  1610. }
  1611. // Really modified ?
  1612. if ($oDestObj->IsModified())
  1613. {
  1614. $oDestObj->DBUpdateTracked($oChange);
  1615. $bModified = true;
  1616. $oStatLog->AddTrace('Updated object - Values: {'.implode(', ', $aValueTrace).'}', $this);
  1617. if (($sStatsCode != '') &&(MetaModel::IsValidAttCode(get_class($oStatLog), $sStatsCode.'_updated')))
  1618. {
  1619. $oStatLog->Inc($sStatsCode.'_updated');
  1620. }
  1621. $this->Set('info_last_modified', date('Y-m-d H:i:s'));
  1622. }
  1623. else
  1624. {
  1625. $oStatLog->AddTrace('Unchanged object', $this);
  1626. if (($sStatsCode != '') &&(MetaModel::IsValidAttCode(get_class($oStatLog), $sStatsCode.'_unchanged')))
  1627. {
  1628. $oStatLog->Inc($sStatsCode.'_unchanged');
  1629. }
  1630. }
  1631. $this->Set('status_last_error', '');
  1632. $this->Set('status', 'synchronized');
  1633. }
  1634. catch(Exception $e)
  1635. {
  1636. $oStatLog->AddTrace("Failed to update destination object: {$e->getMessage()}", $this);
  1637. $this->SetLastError('Unable to update destination object: ', $e);
  1638. $oStatLog->Inc($sStatsCodeError);
  1639. }
  1640. return $bModified;
  1641. }
  1642. /**
  1643. * Creates the destination object populating it with the Extended data found in the synchro_data_XXXX table
  1644. * @return bool Whether or not the object was created
  1645. */
  1646. protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange, &$oStatLog)
  1647. {
  1648. $bCreated = false;
  1649. $oDestObj = MetaModel::NewObject($sClass);
  1650. try
  1651. {
  1652. $aValueTrace = array();
  1653. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1654. {
  1655. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1656. if (!is_null($value))
  1657. {
  1658. $oDestObj->Set($sAttCode, $value);
  1659. $aValueTrace[] = "$sAttCode: $value";
  1660. }
  1661. }
  1662. $iNew = $oDestObj->DBInsertTracked($oChange);
  1663. $this->Set('dest_id', $oDestObj->GetKey());
  1664. $this->Set('dest_class', get_class($oDestObj));
  1665. $this->Set('status_dest_creator', true);
  1666. $this->Set('status_last_error', '');
  1667. $this->Set('status', 'synchronized');
  1668. $this->Set('info_creation_date', date('Y-m-d H:i:s'));
  1669. $bCreated = true;
  1670. $oStatLog->AddTrace("Created (".implode(', ', $aValueTrace).")", $this);
  1671. $oStatLog->Inc('stats_nb_obj_created');
  1672. }
  1673. catch(Exception $e)
  1674. {
  1675. $oStatLog->AddTrace("Failed to create $sClass ({$e->getMessage()})", $this);
  1676. $this->SetLastError('Unable to create destination object: ', $e);
  1677. $oStatLog->Inc('stats_nb_obj_created_errors');
  1678. }
  1679. return $bCreated;
  1680. }
  1681. /**
  1682. * Update the destination object with given values
  1683. */
  1684. public function UpdateDestObject($aValues, $oChange, &$oStatLog)
  1685. {
  1686. try
  1687. {
  1688. if ($this->Get('dest_class') == '')
  1689. {
  1690. $this->SetLastError('No destination object to update');
  1691. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1692. }
  1693. else
  1694. {
  1695. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1696. foreach($aValues as $sAttCode => $value)
  1697. {
  1698. if (!MetaModel::IsValidAttCode(get_class($oDestObj), $sAttCode))
  1699. {
  1700. throw new Exception("Unknown attribute code '$sAttCode'");
  1701. }
  1702. $oDestObj->Set($sAttCode, $value);
  1703. }
  1704. $this->Set('info_last_modified', date('Y-m-d H:i:s'));
  1705. $oDestObj->DBUpdateTracked($oChange);
  1706. $oStatLog->AddTrace("Replica marked as obsolete", $this);
  1707. $oStatLog->Inc('stats_nb_obj_obsoleted');
  1708. }
  1709. }
  1710. catch(Exception $e)
  1711. {
  1712. $this->SetLastError('Unable to update the destination object: ', $e);
  1713. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1714. }
  1715. }
  1716. /**
  1717. * Delete the destination object
  1718. */
  1719. public function DeleteDestObject($oChange, &$oStatLog)
  1720. {
  1721. if($this->Get('status_dest_creator'))
  1722. {
  1723. try
  1724. {
  1725. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1726. $oCheckDeletionPlan = new DeletionPlan();
  1727. if ($oDestObj->CheckToDelete($oCheckDeletionPlan))
  1728. {
  1729. $oActualDeletionPlan = new DeletionPlan();
  1730. $oDestObj->DBDeleteTracked($oChange, null, $oActualDeletionPlan);
  1731. $this->DBDeleteTracked($oChange);
  1732. $oStatLog->Inc('stats_nb_obj_deleted');
  1733. }
  1734. else
  1735. {
  1736. $sIssues = implode("\n", $oCheckDeletionPlan->GetIssues());
  1737. throw(new Exception($sIssues));
  1738. }
  1739. }
  1740. catch(Exception $e)
  1741. {
  1742. $this->SetLastError('Unable to delete the destination object: ', $e);
  1743. $this->Set('status', 'obsolete');
  1744. $this->DBUpdateTracked($oChange);
  1745. $oStatLog->Inc('stats_nb_obj_deleted_errors');
  1746. }
  1747. }
  1748. else
  1749. {
  1750. $this->DBDeleteTracked($oChange);
  1751. $oStatLog->Inc('stats_nb_replica_disappeared_no_action');
  1752. }
  1753. }
  1754. /**
  1755. * Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
  1756. * Note: sExtAttCode could be a standard attcode, or 'primary_key'
  1757. */
  1758. protected function GetValueFromExtData($sExtAttCode, $oSyncAtt, &$oStatLog)
  1759. {
  1760. // $aData should contain attributes defined either for reconciliation or create/update
  1761. $aData = $this->GetExtendedData();
  1762. if ($sExtAttCode == 'primary_key')
  1763. {
  1764. return $aData['primary_key'];
  1765. }
  1766. // $sExtAttCode is a valid attribute code
  1767. //
  1768. $sClass = $this->Get('base_class');
  1769. $oAttDef = MetaModel::GetAttributeDef($sClass, $sExtAttCode);
  1770. if (!is_null($oSyncAtt) && ($oSyncAtt instanceof SynchroAttExtKey))
  1771. {
  1772. $rawValue = $aData[$sExtAttCode];
  1773. if (is_null($rawValue))
  1774. {
  1775. // Null means "ignore" this attribute
  1776. return null;
  1777. }
  1778. $sReconcAttCode = $oSyncAtt->Get('reconciliation_attcode');
  1779. if (!empty($sReconcAttCode))
  1780. {
  1781. $sRemoteClass = $oAttDef->GetTargetClass();
  1782. $oObj = MetaModel::GetObjectByColumn($sRemoteClass, $sReconcAttCode, $rawValue, false);
  1783. if ($oObj)
  1784. {
  1785. $retValue = $oObj->GetKey();
  1786. }
  1787. else
  1788. {
  1789. if ($rawValue != '')
  1790. {
  1791. // Note: differs from null (in which case the value would be left unchanged)
  1792. $oStatLog->AddTrace("Could not find [unique] object for '$sExtAttCode': searched on $sReconcAttCode = '$rawValue'", $this);
  1793. $this->AddWarning("Could not find [unique] object for '$sExtAttCode': searched on $sReconcAttCode = '$rawValue'");
  1794. }
  1795. $retValue = 0;
  1796. }
  1797. }
  1798. else
  1799. {
  1800. $retValue = $rawValue;
  1801. }
  1802. }
  1803. elseif (!is_null($oSyncAtt) && ($oSyncAtt instanceof SynchroAttLinkSet))
  1804. {
  1805. $rawValue = $aData[$sExtAttCode];
  1806. if (is_null($rawValue))
  1807. {
  1808. // Null means "ignore" this attribute
  1809. return null;
  1810. }
  1811. // MakeValueFromString() throws an exception in case of failure
  1812. $retValue = $oAttDef->MakeValueFromString($rawValue, $oSyncAtt->Get('row_separator'), $oSyncAtt->Get('attribute_separator'), $oSyncAtt->Get('value_separator'), $oSyncAtt->Get('attribute_qualifier'));
  1813. }
  1814. else
  1815. {
  1816. $aColumns = $oAttDef->GetImportColumns();
  1817. foreach($aColumns as $sColumn => $sFormat)
  1818. {
  1819. // In any case, a null column means "ignore this attribute"
  1820. //
  1821. if (is_null($aData[$sColumn]))
  1822. {
  1823. return null;
  1824. }
  1825. }
  1826. $retValue = $oAttDef->FromImportToValue($aData, $sExtAttCode);
  1827. }
  1828. return $retValue;
  1829. }
  1830. /**
  1831. * Maps the given context parameter name to the appropriate filter/search code for this class
  1832. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  1833. * @return string Filter code, i.e. 'customer_id'
  1834. */
  1835. public static function MapContextParam($sContextParam)
  1836. {
  1837. if ($sContextParam == 'menu')
  1838. {
  1839. return null;
  1840. }
  1841. else
  1842. {
  1843. return $sContextParam;
  1844. }
  1845. }
  1846. /**
  1847. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  1848. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  1849. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1850. * To Be overridden by derived classes
  1851. * @param void
  1852. * @return String The desired higlight class for the object/row
  1853. */
  1854. public function GetHilightClass()
  1855. {
  1856. // Possible return values are:
  1857. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1858. return HILIGHT_CLASS_NONE; // Not hilighted by default
  1859. }
  1860. public static function GetUIPage()
  1861. {
  1862. return '../synchro/replica.php';
  1863. }
  1864. function DisplayDetails(WebPage $oPage, $bEditMode = false)
  1865. {
  1866. // Object's details
  1867. //$this->DisplayBareHeader($oPage, $bEditMode);
  1868. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  1869. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  1870. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  1871. $this->DisplayBareProperties($oPage, $bEditMode);
  1872. }
  1873. function DisplayBareProperties(WebPage $oPage, $bEditMode = false, $sPrefix = '', $aExtraParams = array())
  1874. {
  1875. if ($bEditMode) return; // Not editable
  1876. $oPage->add('<table style="vertical-align:top"><tr style="vertical-align:top"><td>');
  1877. $aDetails = array();
  1878. $sClass = get_class($this);
  1879. $oPage->add('<fieldset>');
  1880. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PrivateDetails').'</legend>');
  1881. $aZList = MetaModel::FlattenZlist(MetaModel::GetZListItems($sClass, 'details'));
  1882. foreach( $aZList as $sAttCode)
  1883. {
  1884. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1885. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1886. }
  1887. $oPage->Details($aDetails);
  1888. $oPage->add('</fieldset>');
  1889. if (strlen($this->Get('dest_class')) > 0)
  1890. {
  1891. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'), false);
  1892. if (is_object($oDestObj))
  1893. {
  1894. $oPage->add('<fieldset>');
  1895. $oPage->add('<legend>'.Dict::Format('Core:SynchroReplica:TargetObject', $oDestObj->GetHyperlink()).'</legend>');
  1896. $oDestObj->DisplayBareProperties($oPage, false, $sPrefix, $aExtraParams);
  1897. $oPage->add('<fieldset>');
  1898. }
  1899. }
  1900. $oPage->add('</td><td>');
  1901. $oPage->add('<fieldset>');
  1902. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PublicData').'</legend>');
  1903. $oSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1904. $sSQLTable = $oSource->GetDataTable();
  1905. $aData = $this->LoadExtendedDataFromTable($sSQLTable);
  1906. $aHeaders = array('attcode' => array('label' => 'Attribute Code', 'description' => ''),
  1907. 'data' => array('label' => 'Value', 'description' => ''));
  1908. $aRows = array();
  1909. foreach($aData as $sKey => $value)
  1910. {
  1911. $aRows[] = array('attcode' => $sKey, 'data' => $value);
  1912. }
  1913. $oPage->Table($aHeaders, $aRows);
  1914. $oPage->add('</fieldset>');
  1915. $oPage->add('</td></tr></table>');
  1916. }
  1917. public function LoadExtendedDataFromTable($sSQLTable)
  1918. {
  1919. $sSQL = "SELECT * FROM $sSQLTable WHERE id=".$this->GetKey();
  1920. $rQuery = CMDBSource::Query($sSQL);
  1921. return CMDBSource::FetchArray($rQuery);
  1922. }
  1923. }
  1924. /**
  1925. * Context of an ongoing synchronization
  1926. * Two usages:
  1927. * 1) Public usage: execute the synchronization
  1928. * $oSynchroExec = new SynchroExecution($oDataSource[, $iLastFullLoad]);
  1929. * $oSynchroExec->Process($iMaxChunkSize);
  1930. *
  1931. * 2) Internal usage: continue the synchronization (split into chunks, each performed in a separate process)
  1932. * This is implemented in the page priv_sync_chunk.php
  1933. * $oSynchroExec = SynchroExecution::Resume($oDataSource, $iLastFullLoad, $iSynchroLog, $iChange, $iMaxToProcess, $iJob, $iNextInJob);
  1934. * $oSynchroExec->Process()
  1935. */
  1936. class SynchroExecution
  1937. {
  1938. protected $m_oDataSource = null;
  1939. protected $m_oLastFullLoadStartDate = null;
  1940. protected $m_oChange = null;
  1941. protected $m_oStatLog = null;
  1942. // Context computed one for optimization and report inconsistencies ASAP
  1943. protected $m_aExtDataSpec = array();
  1944. protected $m_aReconciliationKeys = array();
  1945. protected $m_aAttributes = array();
  1946. protected $m_iCountAllReplicas = 0;
  1947. /**
  1948. * Constructor
  1949. * @param SynchroDataSource $oDataSource Synchronization task
  1950. * @param DateTime $oLastFullLoadStartDate Date of the last full load (start date/time), if known
  1951. * @return void
  1952. */
  1953. public function __construct($oDataSource, $oLastFullLoadStartDate = null)
  1954. {
  1955. $this->m_oDataSource = $oDataSource;
  1956. $this->m_oLastFullLoadStartDate = $oLastFullLoadStartDate;
  1957. }
  1958. /**
  1959. * Create the persistant information records, for the current synchronization
  1960. * In fact, those records ARE defining what is the "current" synchronization
  1961. */
  1962. protected function PrepareLogs()
  1963. {
  1964. if (!is_null($this->m_oChange))
  1965. {
  1966. return;
  1967. }
  1968. // Create a change used for logging all the modifications/creations happening during the synchro
  1969. $this->m_oChange = MetaModel::NewObject("CMDBChange");
  1970. $this->m_oChange->Set("date", time());
  1971. $sUserString = CMDBChange::GetCurrentUserName();
  1972. $this->m_oChange->Set("userinfo", $sUserString.' '.Dict::S('Core:SyncDataExchangeComment'));
  1973. $iChangeId = $this->m_oChange->DBInsert();
  1974. // Start logging this execution (stats + protection against reentrance)
  1975. //
  1976. $this->m_oStatLog = new SynchroLog();
  1977. $this->m_oStatLog->Set('sync_source_id', $this->m_oDataSource->GetKey());
  1978. $this->m_oStatLog->Set('start_date', time());
  1979. $this->m_oStatLog->Set('status', 'running');
  1980. $this->m_oStatLog->Set('stats_nb_replica_seen', 0);
  1981. $this->m_oStatLog->Set('stats_nb_replica_total', 0);
  1982. $this->m_oStatLog->Set('stats_nb_obj_deleted', 0);
  1983. $this->m_oStatLog->Set('stats_nb_obj_deleted_errors', 0);
  1984. $this->m_oStatLog->Set('stats_nb_obj_obsoleted', 0);
  1985. $this->m_oStatLog->Set('stats_nb_obj_obsoleted_errors', 0);
  1986. $this->m_oStatLog->Set('stats_nb_obj_created', 0);
  1987. $this->m_oStatLog->Set('stats_nb_obj_created_errors', 0);
  1988. $this->m_oStatLog->Set('stats_nb_obj_created_warnings', 0);
  1989. $this->m_oStatLog->Set('stats_nb_obj_updated', 0);
  1990. $this->m_oStatLog->Set('stats_nb_obj_updated_warnings', 0);
  1991. $this->m_oStatLog->Set('stats_nb_obj_updated_errors', 0);
  1992. $this->m_oStatLog->Set('stats_nb_obj_unchanged_warnings', 0);
  1993. // $this->m_oStatLog->Set('stats_nb_replica_reconciled', 0);
  1994. $this->m_oStatLog->Set('stats_nb_replica_reconciled_errors', 0);
  1995. $this->m_oStatLog->Set('stats_nb_replica_disappeared_no_action', 0);
  1996. $this->m_oStatLog->Set('stats_nb_obj_new_updated', 0);
  1997. $this->m_oStatLog->Set('stats_nb_obj_new_updated_warnings', 0);
  1998. $this->m_oStatLog->Set('stats_nb_obj_new_unchanged',0);
  1999. $this->m_oStatLog->Set('stats_nb_obj_new_unchanged_warnings',0);
  2000. $sSelectTotal = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  2001. $oSetTotal = new DBObjectSet(DBObjectSearch::FromOQL($sSelectTotal), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey()));
  2002. $this->m_iCountAllReplicas = $oSetTotal->Count();
  2003. $this->m_oStatLog->Set('stats_nb_replica_total', $this->m_iCountAllReplicas);
  2004. $this->m_oStatLog->DBInsertTracked($this->m_oChange);
  2005. }
  2006. /**
  2007. * Prevent against the reentrance... or allow the current task to do things forbidden by the others !
  2008. */
  2009. public static $m_oCurrentTask = null;
  2010. public static function GetCurrentTaskId()
  2011. {
  2012. if (is_object(self::$m_oCurrentTask))
  2013. {
  2014. return self::$m_oCurrentTask->GetKey();
  2015. }
  2016. else
  2017. {
  2018. return null;
  2019. }
  2020. }
  2021. /**
  2022. * Prepare structures in memory, to speedup the processing of a given replica
  2023. */
  2024. public function PrepareProcessing($bFirstPass = true)
  2025. {
  2026. if ($this->m_oDataSource->Get('status') == 'obsolete')
  2027. {
  2028. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceObsolete'));
  2029. }
  2030. if (!UserRights::IsAdministrator() && $this->m_oDataSource->Get('user_id') != UserRights::GetUserId())
  2031. {
  2032. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceAccessRestriction'));
  2033. }
  2034. // Get the list of SQL columns
  2035. $sClass = $this->m_oDataSource->GetTargetClass();
  2036. $aAttCodesExpected = array();
  2037. $aAttCodesToReconcile = array();
  2038. $aAttCodesToUpdate = array();
  2039. $sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND (update = 1 OR reconcile = 1)";
  2040. $oSetAtt = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey()) /* aArgs */);
  2041. while ($oSyncAtt = $oSetAtt->Fetch())
  2042. {
  2043. if ($oSyncAtt->Get('update'))
  2044. {
  2045. $aAttCodesToUpdate[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  2046. }
  2047. if ($oSyncAtt->Get('reconcile'))
  2048. {
  2049. $aAttCodesToReconcile[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  2050. }
  2051. $aAttCodesExpected[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  2052. }
  2053. $aColumns = $this->m_oDataSource->GetSQLColumns(array_keys($aAttCodesExpected));
  2054. $aExtDataFields = array_keys($aColumns);
  2055. $aExtDataFields[] = 'primary_key';
  2056. $this->m_aExtDataSpec = array(
  2057. 'table' => $this->m_oDataSource->GetDataTable(),
  2058. 'join_key' => 'id',
  2059. 'fields' => $aExtDataFields
  2060. );
  2061. // Get the list of attributes, determine reconciliation keys and update targets
  2062. //
  2063. if ($this->m_oDataSource->Get('reconciliation_policy') == 'use_attributes')
  2064. {
  2065. $this->m_aReconciliationKeys = $aAttCodesToReconcile;
  2066. }
  2067. elseif ($this->m_oDataSource->Get('reconciliation_policy') == 'use_primary_key')
  2068. {
  2069. // Override the settings made at the attribute level !
  2070. $this->m_aReconciliationKeys = array("primary_key" => null);
  2071. }
  2072. if ($bFirstPass)
  2073. {
  2074. $this->m_oStatLog->AddTrace("Update of: {".implode(', ', array_keys($aAttCodesToUpdate))."}");
  2075. $this->m_oStatLog->AddTrace("Reconciliation on: {".implode(', ', array_keys($this->m_aReconciliationKeys))."}");
  2076. }
  2077. if (count($aAttCodesToUpdate) == 0)
  2078. {
  2079. $this->m_oStatLog->AddTrace("No attribute to update");
  2080. throw new SynchroExceptionNotStarted('There is no attribute to update');
  2081. }
  2082. if (count($this->m_aReconciliationKeys) == 0)
  2083. {
  2084. $this->m_oStatLog->AddTrace("No attribute for reconciliation");
  2085. throw new SynchroExceptionNotStarted('No attribute for reconciliation');
  2086. }
  2087. $this->m_aAttributes = array();
  2088. foreach($aAttCodesToUpdate as $sAttCode => $oSyncAtt)
  2089. {
  2090. $oAttDef = MetaModel::GetAttributeDef($this->m_oDataSource->GetTargetClass(), $sAttCode);
  2091. if ($oAttDef->IsWritable())
  2092. {
  2093. $this->m_aAttributes[$sAttCode] = $oSyncAtt;
  2094. }
  2095. }
  2096. // Compute and keep track of the limit date taken into account for obsoleting replicas
  2097. //
  2098. if ($this->m_oLastFullLoadStartDate == null)
  2099. {
  2100. // No previous import known, use the full_load_periodicity value... and the current date
  2101. $this->m_oLastFullLoadStartDate = new DateTime(); // Now
  2102. $iLoadPeriodicity = $this->m_oDataSource->Get('full_load_periodicity'); // Duration in seconds
  2103. if ($iLoadPeriodicity > 0)
  2104. {
  2105. $sInterval = "-$iLoadPeriodicity seconds";
  2106. $this->m_oLastFullLoadStartDate->Modify($sInterval);
  2107. }
  2108. else
  2109. {
  2110. $this->m_oLastFullLoadStartDate = new DateTime('1970-01-01');
  2111. }
  2112. }
  2113. if ($bFirstPass)
  2114. {
  2115. $this->m_oStatLog->AddTrace("Limit Date: ".$this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s'));
  2116. }
  2117. }
  2118. /**
  2119. * Perform a synchronization between the data stored in the replicas (&synchro_data_xxx_xx table)
  2120. * and the iTop objects. If the lastFullLoadStartDate is NOT specified then the full_load_periodicity
  2121. * is used to determine which records are obsolete.
  2122. * @return void
  2123. */
  2124. public function Process()
  2125. {
  2126. $this->PrepareLogs();
  2127. self::$m_oCurrentTask = $this->m_oDataSource;
  2128. try
  2129. {
  2130. $this->DoSynchronize();
  2131. $this->m_oStatLog->Set('end_date', time());
  2132. $this->m_oStatLog->Set('status', 'completed');
  2133. $this->m_oStatLog->DBUpdateTracked($this->m_oChange);
  2134. $iErrors = $this->m_oStatLog->GetErrorCount();
  2135. if ($iErrors > 0)
  2136. {
  2137. $sIssuesOQL = "SELECT SynchroReplica WHERE sync_source_id=".$this->m_oDataSource->GetKey()." AND status_last_error!=''";
  2138. $sAbsoluteUrl = utils::GetAbsoluteUrlAppRoot();
  2139. $sIssuesURL = "{$sAbsoluteUrl}synchro/replica.php?operation=oql&datasource=".$this->m_oDataSource->GetKey()."&oql=".urlencode($sIssuesOQL);
  2140. $sSeeIssues = "<p></p>";
  2141. $sStatistics = "<h1>Statistics</h1>\n";
  2142. $sStatistics .= "<ul>\n";
  2143. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('start_date').": ".$this->m_oStatLog->Get('start_date')."</li>\n";
  2144. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('end_date').": ".$this->m_oStatLog->Get('end_date')."</li>\n";
  2145. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_replica_seen').": ".$this->m_oStatLog->Get('stats_nb_replica_seen')."</li>\n";
  2146. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_replica_total').": ".$this->m_oStatLog->Get('stats_nb_replica_total')."</li>\n";
  2147. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_deleted').": ".$this->m_oStatLog->Get('stats_nb_obj_deleted')."</li>\n";
  2148. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_deleted_errors').": ".$this->m_oStatLog->Get('stats_nb_obj_deleted_errors')."</li>\n";
  2149. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_obsoleted').": ".$this->m_oStatLog->Get('stats_nb_obj_obsoleted')."</li>\n";
  2150. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_obsoleted_errors').": ".$this->m_oStatLog->Get('stats_nb_obj_obsoleted_errors')."</li>\n";
  2151. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_created').": ".$this->m_oStatLog->Get('stats_nb_obj_created')." (".$this->m_oStatLog->Get('stats_nb_obj_created_warnings')." warnings)"."</li>\n";
  2152. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_created_errors').": ".$this->m_oStatLog->Get('stats_nb_obj_created_errors')."</li>\n";
  2153. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_updated').": ".$this->m_oStatLog->Get('stats_nb_obj_updated')." (".$this->m_oStatLog->Get('stats_nb_obj_updated_warnings')." warnings)"."</li>\n";
  2154. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_updated_errors').": ".$this->m_oStatLog->Get('stats_nb_obj_updated_errors')."</li>\n";
  2155. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_replica_reconciled_errors').": ".$this->m_oStatLog->Get('stats_nb_replica_reconciled_errors')."</li>\n";
  2156. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_replica_disappeared_no_action').": ".$this->m_oStatLog->Get('stats_nb_replica_disappeared_no_action')."</li>\n";
  2157. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_new_updated').": ".$this->m_oStatLog->Get('stats_nb_obj_new_updated')." (".$this->m_oStatLog->Get('stats_nb_obj_new_updated_warnings')." warnings)"."</li>\n";
  2158. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_new_unchanged').": ".$this->m_oStatLog->Get('stats_nb_obj_new_unchanged')." (".$this->m_oStatLog->Get('stats_nb_obj_new_unchanged_warnings')." warnings)"."</li>\n";
  2159. $sStatistics .= "</ul>\n";
  2160. $this->m_oDataSource->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);
  2161. }
  2162. else
  2163. {
  2164. //$this->m_oDataSource->SendNotification('success', '<p>The synchronization has been successfully executed.</p>');
  2165. }
  2166. }
  2167. catch (SynchroExceptionNotStarted $e)
  2168. {
  2169. // Set information for reporting... but delete the object in DB
  2170. $this->m_oStatLog->Set('end_date', time());
  2171. $this->m_oStatLog->Set('status', 'error');
  2172. $this->m_oStatLog->Set('last_error', $e->getMessage());
  2173. $this->m_oStatLog->DBDeleteTracked($this->m_oChange);
  2174. $this->m_oDataSource->SendNotification('fatal error', '<p>The synchronization could not start: \''.$e->getMessage().'\'</p><p>Please check its configuration</p>');
  2175. }
  2176. catch (Exception $e)
  2177. {
  2178. $this->m_oStatLog->Set('end_date', time());
  2179. $this->m_oStatLog->Set('status', 'error');
  2180. $this->m_oStatLog->Set('last_error', $e->getMessage());
  2181. $this->m_oStatLog->DBUpdateTracked($this->m_oChange);
  2182. $this->m_oDataSource->SendNotification('exception', '<p>The synchronization has been interrupted: \''.$e->getMessage().'\'</p><p>Please contact the application support team</p>');
  2183. }
  2184. self::$m_oCurrentTask = null;
  2185. return $this->m_oStatLog;
  2186. }
  2187. /**
  2188. * Do the entire synchronization job
  2189. */
  2190. protected function DoSynchronize()
  2191. {
  2192. $this->m_oStatLog->Set('status_curr_job', 1);
  2193. $this->m_oStatLog->Set('status_curr_pos', -1);
  2194. $iMaxChunkSize = utils::ReadParam('max_chunk_size', 0, true /* allow CLI */);
  2195. if ($iMaxChunkSize > 0)
  2196. {
  2197. // Split the execution into several processes
  2198. // Each process will call DoSynchronizeChunk()
  2199. // The loop will end when a process does not reply "continue" on the last line of its output
  2200. if (!utils::IsModeCLI())
  2201. {
  2202. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncSplitModeCLIOnly'));
  2203. }
  2204. $aArguments = array();
  2205. $aArguments['source'] = $this->m_oDataSource->GetKey();
  2206. $aArguments['log'] = $this->m_oStatLog->GetKey();
  2207. $aArguments['change'] = $this->m_oChange->GetKey();
  2208. $aArguments['chunk'] = $iMaxChunkSize;
  2209. if ($this->m_oLastFullLoadStartDate)
  2210. {
  2211. $aArguments['last_full_load'] = $this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  2212. }
  2213. else
  2214. {
  2215. $aArguments['last_full_load'] = '';
  2216. }
  2217. $this->m_oStatLog->DBUpdate($this->m_oChange);
  2218. $iStepCount = 0;
  2219. do
  2220. {
  2221. $aArguments['step_count'] = $iStepCount;
  2222. $iStepCount++;
  2223. list ($iRes, $aOut) = utils::ExecITopScript('synchro/priv_sync_chunk.php', $aArguments);
  2224. // Reload the log that has been modified by the processes
  2225. $this->m_oStatLog->Reload();
  2226. $sLastRes = strtolower(trim(end($aOut)));
  2227. switch($sLastRes)
  2228. {
  2229. case 'continue':
  2230. $bContinue = true;
  2231. break;
  2232. case 'finished':
  2233. $bContinue = false;
  2234. break;
  2235. default:
  2236. $this->m_oStatLog->AddTrace("The script did not reply with the expected keywords:");
  2237. $aIndentedOut = array();
  2238. foreach ($aOut as $sOut)
  2239. {
  2240. $aIndentedOut[] = "-> $sOut";
  2241. $this->m_oStatLog->AddTrace(">>> $sOut");
  2242. }
  2243. throw new Exception("Encountered an error in an underspinned process:\n".implode("\n", $aIndentedOut));
  2244. }
  2245. }
  2246. while ($bContinue);
  2247. }
  2248. else
  2249. {
  2250. $this->PrepareProcessing(/* first pass */);
  2251. $this->DoJob1();
  2252. $this->DoJob2();
  2253. $this->DoJob3();
  2254. }
  2255. }
  2256. /**
  2257. * Do the synchronization job, limited to some amount of work
  2258. * This verb has been designed to be called from within a separate process
  2259. * @return true if the process has to be continued
  2260. */
  2261. public function DoSynchronizeChunk($oLog, $oChange, $iMaxChunkSize)
  2262. {
  2263. // Initialize the structures...
  2264. self::$m_oCurrentTask = $this->m_oDataSource;
  2265. $this->m_oStatLog = $oLog;
  2266. $this->m_oChange = $oChange;
  2267. // Prepare internal structures (not the first pass)
  2268. $this->PrepareProcessing(false);
  2269. $iCurrJob = $this->m_oStatLog->Get('status_curr_job');
  2270. $iCurrPos = $this->m_oStatLog->Get('status_curr_pos');
  2271. $this->m_oStatLog->AddTrace("Synchronizing chunk - curr_job:$iCurrJob, curr_pos:$iCurrPos, max_chunk_size:$iMaxChunkSize");
  2272. $bContinue = false;
  2273. switch ($iCurrJob)
  2274. {
  2275. case 1:
  2276. default:
  2277. $this->DoJob1($iMaxChunkSize, $iCurrPos);
  2278. $bContinue = true;
  2279. break;
  2280. case 2:
  2281. $this->DoJob2($iMaxChunkSize, $iCurrPos);
  2282. $bContinue = true;
  2283. break;
  2284. case 3:
  2285. $bContinue = $this->DoJob3($iMaxChunkSize, $iCurrPos);
  2286. break;
  2287. }
  2288. $this->m_oStatLog->DBUpdate($this->m_oChange);
  2289. self::$m_oCurrentTask = null;
  2290. return $bContinue;
  2291. }
  2292. /**
  2293. * Do the synchronization job #1: Obsolete replica "untouched" for some time
  2294. * @param integer $iMaxReplica Limit the number of replicas to process
  2295. * @param integer $iCurrPos Current position where to resume the processing
  2296. * @return true if the process must be continued
  2297. */
  2298. protected function DoJob1($iMaxReplica = null, $iCurrPos = -1)
  2299. {
  2300. $sLimitDate = $this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  2301. // Get all the replicas that were not seen in the last import and mark them as obsolete
  2302. $sDeletePolicy = $this->m_oDataSource->Get('delete_policy');
  2303. if ($sDeletePolicy != 'ignore')
  2304. {
  2305. $sSelectToObsolete = "SELECT SynchroReplica WHERE id > :curr_pos AND sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
  2306. $oSetScope = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate, 'curr_pos' => $iCurrPos));
  2307. $iCountScope = $oSetScope->Count();
  2308. if (($this->m_iCountAllReplicas > 10) && ($this->m_iCountAllReplicas == $iCountScope))
  2309. {
  2310. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncTooManyMissingReplicas'));
  2311. }
  2312. if ($iMaxReplica)
  2313. {
  2314. // Consider a given subset, starting from replica iCurrPos, limited to the count of iMaxReplica
  2315. // The replica have to be ordered by id
  2316. $oSetToProcess = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array('id'=>true) /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate, 'curr_pos' => $iCurrPos));
  2317. $oSetToProcess->SetLimit($iMaxReplica);
  2318. }
  2319. else
  2320. {
  2321. $oSetToProcess = $oSetScope;
  2322. }
  2323. $iLastReplicaProcessed = -1;
  2324. while($oReplica = $oSetToProcess->Fetch())
  2325. {
  2326. $iLastReplicaProcessed = $oReplica->GetKey();
  2327. switch ($sDeletePolicy)
  2328. {
  2329. case 'update':
  2330. case 'update_then_delete':
  2331. $this->m_oStatLog->AddTrace("Destination object to be updated", $oReplica);
  2332. $aToUpdate = array();
  2333. $aToUpdateSpec = explode(';', $this->m_oDataSource->Get('delete_policy_update')); //ex: 'status:obsolete;description:stopped',
  2334. foreach($aToUpdateSpec as $sUpdateSpec)
  2335. {
  2336. $aUpdateSpec = explode(':', $sUpdateSpec);
  2337. if (count($aUpdateSpec) == 2)
  2338. {
  2339. $sAttCode = $aUpdateSpec[0];
  2340. $sValue = $aUpdateSpec[1];
  2341. $aToUpdate[$sAttCode] = $sValue;
  2342. }
  2343. }
  2344. $oReplica->Set('status_last_error', '');
  2345. if ($oReplica->Get('dest_id') == '')
  2346. {
  2347. $oReplica->Set('status', 'obsolete');
  2348. $this->m_oStatLog->Inc('stats_nb_replica_disappeared_no_action');
  2349. }
  2350. else
  2351. {
  2352. $oReplica->UpdateDestObject($aToUpdate, $this->m_oChange, $this->m_oStatLog);
  2353. if ($oReplica->Get('status_last_error') == '')
  2354. {
  2355. // Change the status of the replica IIF
  2356. $oReplica->Set('status', 'obsolete');
  2357. }
  2358. }
  2359. $oReplica->DBUpdateTracked($this->m_oChange);
  2360. break;
  2361. case 'delete':
  2362. default:
  2363. $this->m_oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  2364. $oReplica->DeleteDestObject($this->m_oChange, $this->m_oStatLog);
  2365. }
  2366. }
  2367. if ($iMaxReplica)
  2368. {
  2369. if ($iMaxReplica < $iCountScope)
  2370. {
  2371. // Continue with this job!
  2372. $this->m_oStatLog->Set('status_curr_pos', $iLastReplicaProcessed);
  2373. return true;
  2374. }
  2375. }
  2376. } // if ($sDeletePolicy != 'ignore'
  2377. //Count "seen" objects
  2378. $sSelectSeen = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen >= :last_import";
  2379. $oSetSeen = new DBObjectSet(DBObjectSearch::FromOQL($sSelectSeen), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate));
  2380. $this->m_oStatLog->Set('stats_nb_replica_seen', $oSetSeen->Count());
  2381. // Job complete!
  2382. $this->m_oStatLog->Set('status_curr_job', 2);
  2383. $this->m_oStatLog->Set('status_curr_pos', -1);
  2384. return false;
  2385. }
  2386. /**
  2387. * Do the synchronization job #2: Create and modify object for new/modified replicas
  2388. * @param integer $iMaxReplica Limit the number of replicas to process
  2389. * @param integer $iCurrPos Current position where to resume the processing
  2390. * @return true if the process must be continued
  2391. */
  2392. protected function DoJob2($iMaxReplica = null, $iCurrPos = -1)
  2393. {
  2394. $sLimitDate = $this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  2395. // Get all the replicas that are 'new' or modified or synchronized with a warning
  2396. //
  2397. $sSelectToSync = "SELECT SynchroReplica WHERE id > :curr_pos AND (status = 'new' OR status = 'modified' OR (status = 'synchronized' AND status_last_warning != '')) AND sync_source_id = :source_id AND status_last_seen >= :last_import";
  2398. $oSetScope = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate, 'curr_pos' => $iCurrPos), $this->m_aExtDataSpec);
  2399. $iCountScope = $oSetScope->Count();
  2400. if ($iMaxReplica)
  2401. {
  2402. // Consider a given subset, starting from replica iCurrPos, limited to the count of iMaxReplica
  2403. // The replica have to be ordered by id
  2404. $oSetToProcess = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array('id'=>true) /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate, 'curr_pos' => $iCurrPos), $this->m_aExtDataSpec);
  2405. $oSetToProcess->SetLimit($iMaxReplica);
  2406. }
  2407. else
  2408. {
  2409. $oSetToProcess = $oSetScope;
  2410. }
  2411. $iLastReplicaProcessed = -1;
  2412. while($oReplica = $oSetToProcess->Fetch())
  2413. {
  2414. $iLastReplicaProcessed = $oReplica->GetKey();
  2415. $oReplica->Synchro($this->m_oDataSource, $this->m_aReconciliationKeys, $this->m_aAttributes, $this->m_oChange, $this->m_oStatLog);
  2416. $oReplica->DBUpdateTracked($this->m_oChange);
  2417. }
  2418. if ($iMaxReplica)
  2419. {
  2420. if ($iMaxReplica < $iCountScope)
  2421. {
  2422. // Continue with this job!
  2423. $this->m_oStatLog->Set('status_curr_pos', $iLastReplicaProcessed);
  2424. return true;
  2425. }
  2426. }
  2427. // Job complete!
  2428. $this->m_oStatLog->Set('status_curr_job', 3);
  2429. $this->m_oStatLog->Set('status_curr_pos', -1);
  2430. return false;
  2431. }
  2432. /**
  2433. * Do the synchronization job #3: Delete replica depending on the obsolescence scheme
  2434. * @param integer $iMaxReplica Limit the number of replicas to process
  2435. * @param integer $iCurrPos Current position where to resume the processing
  2436. * @return true if the process must be continued
  2437. */
  2438. protected function DoJob3($iMaxReplica = null, $iCurrPos = -1)
  2439. {
  2440. $sDeletePolicy = $this->m_oDataSource->Get('delete_policy');
  2441. if ($sDeletePolicy != 'update_then_delete')
  2442. {
  2443. // Job complete!
  2444. $this->m_oStatLog->Set('status_curr_job', 0);
  2445. $this->m_oStatLog->Set('status_curr_pos', -1);
  2446. return false;
  2447. }
  2448. $bFirstPass = ($iCurrPos == -1);
  2449. // Get all the replicas that are to be deleted
  2450. //
  2451. $oDeletionDate = $this->m_oLastFullLoadStartDate;
  2452. $iDeleteRetention = $this->m_oDataSource->Get('delete_policy_retention'); // Duration in seconds
  2453. if ($iDeleteRetention > 0)
  2454. {
  2455. $sInterval = "-$iDeleteRetention seconds";
  2456. $oDeletionDate->Modify($sInterval);
  2457. }
  2458. $sDeletionDate = $oDeletionDate->Format('Y-m-d H:i:s');
  2459. if ($bFirstPass)
  2460. {
  2461. $this->m_oStatLog->AddTrace("Deletion date: $sDeletionDate");
  2462. }
  2463. $sSelectToDelete = "SELECT SynchroReplica WHERE id > :curr_pos AND sync_source_id = :source_id AND status IN ('obsolete') AND status_last_seen < :last_import";
  2464. $oSetScope = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sDeletionDate, 'curr_pos' => $iCurrPos));
  2465. $iCountScope = $oSetScope->Count();
  2466. if ($iMaxReplica)
  2467. {
  2468. // Consider a given subset, starting from replica iCurrPos, limited to the count of iMaxReplica
  2469. // The replica have to be ordered by id
  2470. $oSetToProcess = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array('id'=>true) /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sDeletionDate, 'curr_pos' => $iCurrPos));
  2471. $oSetToProcess->SetLimit($iMaxReplica);
  2472. }
  2473. else
  2474. {
  2475. $oSetToProcess = $oSetScope;
  2476. }
  2477. $iLastReplicaProcessed = -1;
  2478. while($oReplica = $oSetToProcess->Fetch())
  2479. {
  2480. $iLastReplicaProcessed = $oReplica->GetKey();
  2481. $this->m_oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  2482. $oReplica->DeleteDestObject($this->m_oChange, $this->m_oStatLog);
  2483. }
  2484. if ($iMaxReplica)
  2485. {
  2486. if ($iMaxReplica < $iCountScope)
  2487. {
  2488. // Continue with this job!
  2489. $this->m_oStatLog->Set('status_curr_pos', $iLastReplicaProcessed);
  2490. return true;
  2491. }
  2492. }
  2493. // Job complete!
  2494. $this->m_oStatLog->Set('status_curr_job', 0);
  2495. $this->m_oStatLog->Set('status_curr_pos', -1);
  2496. return false;
  2497. }
  2498. }
  2499. $oAdminMenu = new MenuGroup('AdminTools', 80 /* fRank */, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  2500. new OQLMenuNode('DataSources', 'SELECT SynchroDataSource', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  2501. // new OQLMenuNode('Replicas', 'SELECT SynchroReplica', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroReplica', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  2502. // new WebPageMenuNode('Test:RunSynchro', '../synchro/synchro_exec.php', $oAdminMenu->GetIndex(), 13 /* fRank */, 'SynchroDataSource');
  2503. ?>