1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192 |
- <?php
- // Copyright (C) 2010 Combodo SARL
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; version 3 of the License.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- /**
- * Data Exchange - synchronization with external applications (incoming data)
- *
- * @author Erwan Taloc <erwan.taloc@combodo.com>
- * @author Romain Quetiez <romain.quetiez@combodo.com>
- * @author Denis Flaven <denis.flaven@combodo.com>
- * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
- */
- class SynchroDataSource extends cmdbAbstractObject
- {
- public static function Init()
- {
- $aParams = array
- (
- "category" => "core/cmdb,view_in_gui",
- "key_type" => "autoincrement",
- "name_attcode" => array('name'),
- "state_attcode" => "",
- "reconc_keys" => array(),
- "db_table" => "priv_sync_datasource",
- "db_key_field" => "id",
- "db_finalclass_field" => "realclass",
- "display_template" => "",
- "icon" => "../images/synchro.png",
- );
- MetaModel::Init_Params($aParams);
- //MetaModel::Init_InheritAttributes();
- MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeString("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
- 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())));
- 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())));
- MetaModel::Init_AddAttribute(new AttributeClass("scope_class", array("class_category"=>"bizmodel", "more_values"=>"", "sql"=>"scope_class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeString("scope_restriction", array("allowed_values"=>null, "sql"=>"scope_restriction", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
-
- //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())));
- // Format: '1 hour', '2 weeks', '3 hoursABCDEF'... Cf DateTime->Modify()
- MetaModel::Init_AddAttribute(new AttributeString("full_load_periodicity", array("allowed_values"=>null, "sql"=>"full_load_periodicity", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
-
- // MetaModel::Init_AddAttribute(new AttributeString("reconciliation_list", array("allowed_values"=>null, "sql"=>"reconciliation_list", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
- 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())));
- 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())));
- 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())));
- 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())));
-
- 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())));
- 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())));
- // Format: '1 hour', '2 weeks', '3 hoursABCDEF'... Cf DateTime->Modify()
- MetaModel::Init_AddAttribute(new AttributeString("delete_policy_retention", array("allowed_values"=>null, "sql"=>"delete_policy_retention", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
- 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())));
- MetaModel::Init_AddAttribute(new AttributeLinkedSet("status_list", array("linked_class"=>"SynchroLog", "ext_key_to_me"=>"sync_source_id", "allowed_values"=>null, "count_min"=>0, "count_max"=>0, "depends_on"=>array())));
- // Display lists
- MetaModel::Init_SetZListItems('details', array('name', 'description', 'scope_class', 'scope_restriction', 'status', 'user_id', 'full_load_periodicity', 'reconciliation_policy', 'action_on_zero', 'action_on_one', 'action_on_multiple', 'delete_policy', 'delete_policy_update', 'delete_policy_retention' /*'attribute_list'*/, 'status_list')); // Attributes to be displayed for the complete details
- MetaModel::Init_SetZListItems('list', array('scope_class', 'status', 'user_id', 'full_load_periodicity')); // Attributes to be displayed for a list
- // Search criteria
- MetaModel::Init_SetZListItems('standard_search', array('name', 'status', 'scope_class', 'user_id')); // Criteria of the std search form
- // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
- }
- public function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
- {
- if (!$this->IsNew())
- {
- $oPage->SetCurrentTab(Dict::S('Core:SynchroAttributes'));
- $oAttributeSet = $this->Get('attribute_list');
- $aAttributes = array();
- while($oAttribute = $oAttributeSet->Fetch())
- {
- $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
- }
- $aAttribs = array(
- 'attcode' => array('label'=>'Attribute', 'description' => 'Field of the object'),
- 'reconciliation' => array('label'=>'Reconciliation ?', 'description' => 'Used for searching'),
- 'update' => array('label'=>'Update ?', 'description' => 'Used to update the object'),
- 'update_policy' => array('label'=>'Update Policy', 'description' => 'Behavior of the updated field'),
- );
- $aValues = array();
- foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
- {
- if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
- {
- if (isset($aAttributes[$sAttCode]))
- {
- $oAttribute = $aAttributes[$sAttCode];
- }
- else
- {
- $oAttribute = new SynchroAttribute();
- $oAttribute->Set('sync_source_id', $this->GetKey());
- $oAttribute->Set('attcode', $sAttCode);
- $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
- $oAttribute->Set('update', 1);
- $oAttribute->Set('update_policy', 'master_locked');
- }
- if (!$bEditMode)
- {
- // Read-only mode
- $aRow['reconciliation'] = $oAttribute->Get('reconcile') == 1 ? Dict::S('Core:SynchroReconcile:Yes') : Dict::S('Core:SynchroReconcile:No');
- $aRow['update'] = $oAttribute->Get('update') == 1 ? Dict::S('Core:SynchroUpdate:Yes') : Dict::S('Core:SynchroUpdate:No');
- $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
- $aRow['update_policy'] = $oAttribute->GetAsHTML('update_policy');
- }
- else
- {
- // Read-only mode
- $sAttCode = $oAttribute->Get('attcode');
- $sChecked = $oAttribute->Get('reconcile') == 1 ? 'checked' : '';
- $aRow['reconciliation'] = "<input type=\"checkbox\" name=\"reconciliation[$sAttCode]\" $sChecked/>";
- $sChecked = $oAttribute->Get('update') == 1 ? 'checked' : '';
- $aRow['update'] = "<input type=\"checkbox\" name=\"update[$sAttCode]\" $sChecked/>";
- $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
- $oAttDef = MetaModel::GetAttributeDef(get_class($oAttribute), 'update_policy');
- $aRow['update_policy'] = cmdbAbstractObject::GetFormElementForField($oPage, get_class($oAttribute), 'update_policy', $oAttDef, $oAttribute->Get('update_policy'), '', 'update_policy_'.$sAttCode, "[$sAttCode]");
- }
- $aValues[] = $aRow;
- }
- }
- $oPage->Table($aAttribs, $aValues);
- $oPage->SetCurrentTab(Dict::S('Core:SynchroStatus'));
-
- $sSelectSynchroLog = 'SELECT SynchroLog WHERE sync_source_id = :source_id';
- $oSetSynchroLog = new CMDBObjectSet(DBObjectSearch::FromOQL($sSelectSynchroLog), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()));
-
- if ($oSetSynchroLog->Count() > 0)
- {
- $oLastLog = $oSetSynchroLog->Fetch();
- $sStartDate = $oLastLog->Get('start_date');
- $oLastLog->Get('stats_nb_replica_seen');
- if ($oLastLog->Get('status') == 'running')
- {
- // Still running !
- $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroRunningStartedOn_Date', $sStartDate).'</h2>');
- }
- else
- {
- $sEndDate = $oLastLog->Get('end_date');
- $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroEndedOn_Date', $sEndDate).'</h2>');
- }
- $iDeleted = $oLastLog->Get('stats_nb_obj_deleted');
- $iObsoleted = $oLastLog->Get('stats_nb_obj_obsoleted');
- $iDisappearedErrors = $oLastLog->Get('stats_nb_obj_obsoleted_errors') + $oLastLog->Get('stats_nb_obj_deleted_errors');
- $iUpdated = $oLastLog->Get('stats_nb_obj_updated');
- $iUpdatedErrors = $oLastLog->Get('stats_nb_obj_updated_errors');
- $iReconciled = $oLastLog->Get('stats_nb_replica_reconciled');
- $iReconciledErrors = $oLastLog->Get('stats_nb_replica_reconciled_errors');
- $iCreated = $oLastLog->Get('stats_nb_obj_created');
- $iCreatedErrors = $oLastLog->Get('stats_nb_obj_created_errors');
- $iDisappeared = $iDisappearedErrors + $iObsoleted + $iDeleted;
- $iNewErrors = $iCreatedErrors + $iReconciledErrors;
- $iNew = $iCreated + $iCreatedErrors + $iReconciled + $iReconciledErrors;
- $iExisting = $oLastLog->Get('stats_nb_replica_seen') - $iNew;
- $iUnchanged = $iExisting - $iUpdated - $iUpdatedErrors;
- $iIgnored = $oLastLog->Get('stats_nb_replica_total') - $iNew - $iExisting - $iDisappeared;
- $fOpacity = 0.3;
- $sNewOpacity = ($iNew ==0) ? "opacity:$fOpacity;" : "";
- $sExistingOpacity = ($iExisting ==0) ? "opacity:$fOpacity;" : "";
- $sDisappearedOpacity = ($iDisappeared ==0) ? "opacity:$fOpacity;" : "";
- $sIgnoredOpacity = ($iIgnored ==0) ? "opacity:$fOpacity;" : "";
- $sCreateOpacity = ($iCreated ==0) ? "opacity:$fOpacity;" : "";
- $sReconciledOpacity = ($iReconciled ==0) ? "opacity:$fOpacity;" : "";
- $sNewErrorsOpacity = ($iNewErrors ==0) ? "opacity:$fOpacity;" : "";
- $sUnchangedOpacity = ($iUnchanged ==0) ? "opacity:$fOpacity;" : "";
- $sUpdatedOpacity = ($iUpdated ==0) ? "opacity:$fOpacity;" : "";
- $sUpdatedErrorsOpacity = ($iUpdatedErrors ==0) ? "opacity:$fOpacity;" : "";
- $sDeletedOpacity = ($iDeleted ==0) ? "opacity:$fOpacity;" : "";
- $sObsoletedOpacity = ($iObsoleted ==0) ? "opacity:$fOpacity;" : "";
- $sDisappearedErrorsOpacity = ($iDisappearedErrors ==0) ? "opacity:$fOpacity;" : "";
- $oPage->add(
- <<<EOF
- <style>
- .synoptics, .synoptics tr td { background: transparent; padding:10px; font-size:1em; vertical-align:middle; color:#fff; text-align:center;}
- .synoptics tr td.arrow { color:#333; border-top: 1px dashed #333; width:100px; }
- </style>
- <table class="synoptics">
- <tr>
- <td style="background-color:#999;$sIgnoredOpacity">Ignored ($iIgnored)</td><td colspan="2"> </td>
- </tr>
- <tr>
- <td style="background-color:#630;$sDisappearedOpacity" rowspan="3">Disappeared ($iDisappeared)</td><td rowspan="3" class="arrow">=></td><td style="background-color:#300;$sDeletedOpacity">Deleted ($iDeleted)</td>
- </tr>
- <tr>
- <td style="background-color:#630;$sObsoletedOpacity">Obsoleted ($iObsoleted)</td>
- </tr>
- <tr>
- <td style="background-color:#C00;$sDisappearedErrorsOpacity">Errors ($iDisappearedErrors)</td>
- </tr>
- <tr>
- <td style="background-color:#093;$sExistingOpacity" rowspan="3">Existing ($iExisting)</td><td rowspan="3" class="arrow">=></td><td style="background-color:#393;$sUnchangedOpacity">Unchanged ($iUnchanged)</td>
- </tr>
- <tr>
- <td style="background-color:#3C3;$sUpdatedOpacity">Updated ($iUpdated)</td>
- </tr>
- <tr>
- <td style="background-color:#C00;$sUpdatedErrorsOpacity">Errors ($iUpdatedErrors)</td>
- </tr>
- <tr>
- <td style="background-color:#039;$sNewOpacity"rowspan="3">New ($iNew)</td><td rowspan="3" class="arrow">=></td><td style="background-color:#C00;$sNewErrorsOpacity">Errors ($iNewErrors)</td>
- </tr>
- <tr>
- <td style="background-color:#33F;$sReconciledOpacity">Reconciled ($iReconciled)</td>
- </tr>
- <tr>
- <td style="background-color:#339;$sCreateOpacity">Created ($iCreated)</td>
- </tr>
- </table>
- EOF
- );
- }
- else
- {
- $oPage->p('<h2>'.Dict::S('Core:Synchro:NeverRun').'</h2>');
- }
-
- }
- parent::DisplayBareRelations($oPage, $bEditMode);
- }
-
- public function GetAttributeFlags($sAttCode)
- {
- if (($sAttCode == 'scope_class') && (!$this->IsNew()))
- {
- return OPT_ATT_READONLY;
- }
- return parent::GetAttributeFlags($sAttCode);
- }
-
- public function UpdateObject($sFormPrefix = '')
- {
- parent::UpdateObject($sFormPrefix);
- // And now read the other post parameters...
- $oAttributeSet = $this->Get('attribute_list');
- $aAttributes = array();
- while($oAttribute = $oAttributeSet->Fetch())
- {
- $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
- }
- $aReconcile = utils::ReadPostedParam('reconciliation', array());
- $aUpdate = utils::ReadPostedParam('update', array());
- $aUpdatePolicy = utils::ReadPostedParam('attr_update_policy', array());
- // update_policy cannot be empty, so there is one entry per attribute, use this to iterate
- // through all the writable attributes
- foreach($aUpdatePolicy as $sAttCode => $sValue)
- {
- if(!isset($aAttributes[$sAttCode]))
- {
- $oAttribute = new SynchroAttribute();
- $oAttribute->Set('sync_source_id', $this->GetKey());
- $oAttribute->Set('attcode', $sAttCode);
- }
- else
- {
- $oAttribute = $aAttributes[$sAttCode];
- }
- $bReconcile = 0;
- if (isset($aReconcile[$sAttCode]))
- {
- $bReconcile = $aReconcile[$sAttCode] == 'on' ? 1 : 0;
- }
- $bUpdate = 0 ; // Default / initial value
- if (isset($aUpdate[$sAttCode]))
- {
- $bUpdate = $aUpdate[$sAttCode] == 'on' ? 1 : 0;
- }
- $oAttribute->Set('reconcile', $bReconcile);
- $oAttribute->Set('update', $bUpdate);
- $oAttribute->Set('update_policy', $sValue);
- $oAttributeSet->AddObject($oAttribute);
- }
- $this->Set('attribute_list', $oAttributeSet);
- }
- public function GetTargetClass()
- {
- return $this->Get('scope_class');
- }
- public function GetDataTable()
- {
- $sName = strtolower($this->GetTargetClass());
- $sName = str_replace('\'"&@|\\/ ', '_', $sName); // Remove forbidden characters from the table name
- $sName .= '_'.$this->GetKey(); // Add a suffix for unicity
- $sTable = MetaModel::GetConfig()->GetDBSubName()."synchro_data_$sName"; // Add the prefix if any
- return $sTable;
- }
- /**
- * When inserting a new datasource object, also create the SynchroAttribute objects
- * for each field of the target class
- */
- protected function OnInsert()
- {
- // Create all the SynchroAttribute records
- $oAttributeSet = $this->Get('attribute_list');
- foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
- {
- if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
- {
- $oAttribute = new SynchroAttribute();
- $oAttribute->Set('sync_source_id', $this->GetKey());
- $oAttribute->Set('attcode', $sAttCode);
- $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
- $oAttribute->Set('update', 1);
- $oAttribute->Set('update_policy', 'master_locked');
- $oAttributeSet->AddObject($oAttribute);
- }
- }
- $this->Set('attribute_list', $oAttributeSet);
- }
- /**
- * When the new datasource has been created, let's create the synchro_data table
- * that will hold the data records and the correspoding triggers which will maintain
- * both tables in sync
- */
- protected function AfterInsert()
- {
- parent::AfterInsert();
- $sTable = $this->GetDataTable();
- $aColumns = $this->GetSQLColumns();
-
- $aFieldDefs = array();
- // Allow '0', otherwise mysql will render an error when the id is not given
- // (the trigger is expected to set the value, but it is not executed soon enough)
- $aFieldDefs[] = "id INTEGER(11) NOT NULL DEFAULT 0 ";
- $aFieldDefs[] = "`primary_key` VARCHAR(255) NULL DEFAULT NULL";
- foreach($aColumns as $sColumn => $ColSpec)
- {
- $aFieldDefs[] = "`$sColumn` $ColSpec NULL DEFAULT NULL";
- }
- $aFieldDefs[] = "INDEX (id)";
- $aFieldDefs[] = "INDEX (primary_key)";
- $sFieldDefs = implode(', ', $aFieldDefs);
- $sCreateTable = "CREATE TABLE `$sTable` ($sFieldDefs) ENGINE = innodb;";
- CMDBSource::Query($sCreateTable);
- $sTriggerInsert = "CREATE TRIGGER `{$sTable}_bi` BEFORE INSERT ON $sTable";
- $sTriggerInsert .= " FOR EACH ROW";
- $sTriggerInsert .= " BEGIN";
- $sTriggerInsert .= " INSERT INTO priv_sync_replica (sync_source_id, status_last_seen, `status`) VALUES ({$this->GetKey()}, NOW(), 'new');";
- $sTriggerInsert .= " SET NEW.id = LAST_INSERT_ID();";
- $sTriggerInsert .= " END;";
- CMDBSource::Query($sTriggerInsert);
- $aModified = array();
- foreach($aColumns as $sColumn => $ColSpec)
- {
- // <=> is a null-safe 'EQUALS' operator (there is no equivalent for "DIFFERS FROM")
- $aModified[] = "NOT(NEW.`$sColumn` <=> OLD.`$sColumn`)";
- }
- $sIsModified = '('.implode(') OR (', $aModified).')';
- // Update the replica
- //
- // status is forced to "new" if the replica was obsoleted directly from the state "new" (dest_id = null)
- // otherwise, if status was either 'obsolete' or 'synchronized' it is turned into 'modified' or 'synchronized' depending on the changes
- // otherwise, the status is left as is
- $sTriggerUpdate = "CREATE TRIGGER `{$sTable}_bu` BEFORE UPDATE ON $sTable";
- $sTriggerUpdate .= " FOR EACH ROW";
- $sTriggerUpdate .= " BEGIN";
- $sTriggerUpdate .= " IF @itopuser is null THEN";
- $sTriggerUpdate .= " UPDATE priv_sync_replica SET status_last_seen = NOW(), `status` = IF(`status` = 'obsolete', IF(`dest_id` IS NULL, 'new', 'modified'), IF(`status` IN ('synchronized') AND ($sIsModified), 'modified', `status`)) WHERE sync_source_id = {$this->GetKey()} AND id = OLD.id;";
- $sTriggerUpdate .= " SET NEW.id = OLD.id;"; // make sure this id won't change
- $sTriggerUpdate .= " END IF;";
- $sTriggerUpdate .= " END;";
- CMDBSource::Query($sTriggerUpdate);
- $sTriggerInsert = "CREATE TRIGGER `{$sTable}_ad` AFTER DELETE ON $sTable";
- $sTriggerInsert .= " FOR EACH ROW";
- $sTriggerInsert .= " BEGIN";
- $sTriggerInsert .= " DELETE FROM priv_sync_replica WHERE id = OLD.id;";
- $sTriggerInsert .= " END;";
- CMDBSource::Query($sTriggerInsert);
- }
-
- protected function AfterDelete()
- {
- parent::AfterDelete();
- $sTable = $this->GetDataTable();
- $sDropTable = "DROP TABLE `$sTable`";
- CMDBSource::Query($sDropTable);
- // TO DO - check that triggers get dropped with the table
- }
- /**
- * Perform a synchronization between the data stored in the replicas (&synchro_data_xxx_xx table)
- * and the iTop objects. If the lastFullLoadStartDate is NOT specified then the full_load_periodicity
- * is used to determine which records are obsolete.
- * @param Hash $aTraces Debugs/Trace information, one or more entries per replica
- * @param DateTime $oLastFullLoadStartDate Date of the last full load (start date/time), if known
- * @return void
- */
- public function Synchronize(&$aTraces, $oLastFullLoadStartDate = null)
- {
- // Create a change used for logging all the modifications/creations happening during the synchro
- $oMyChange = MetaModel::NewObject("CMDBChange");
- $oMyChange->Set("date", time());
- $sUserString = CMDBChange::GetCurrentUserName();
- $oMyChange->Set("userinfo", $sUserString);
- $iChangeId = $oMyChange->DBInsert();
- // Start logging this execution (stats + protection against reentrance)
- //
- $oStatLog = new SynchroLog();
- $oStatLog->Set('sync_source_id', $this->GetKey());
- $oStatLog->Set('start_date', time());
- $oStatLog->Set('status', 'running');
- $oStatLog->Set('stats_nb_replica_seen', 0);
- $oStatLog->Set('stats_nb_replica_total', 0);
- $oStatLog->Set('stats_nb_obj_deleted', 0);
- $oStatLog->Set('stats_nb_obj_deleted_errors', 0);
- $oStatLog->Set('stats_nb_obj_obsoleted', 0);
- $oStatLog->Set('stats_nb_obj_obsoleted_errors', 0);
- $oStatLog->Set('stats_nb_obj_created', 0);
- $oStatLog->Set('stats_nb_obj_created_errors', 0);
- $oStatLog->Set('stats_nb_obj_updated', 0);
- $oStatLog->Set('stats_nb_obj_updated_errors', 0);
- $oStatLog->Set('stats_nb_replica_reconciled', 0);
- $oStatLog->Set('stats_nb_replica_reconciled_errors', 0);
-
- $sSelectTotal = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
- $oSetTotal = new DBObjectSet(DBObjectSearch::FromOQL($sSelectTotal), array() /* order by*/, array('source_id' => $this->GetKey()));
- $oStatLog->Set('stats_nb_replica_total', $oSetTotal->Count());
- $oStatLog->DBInsertTracked($oMyChange);
- try
- {
- $this->DoSynchronize($oLastFullLoadStartDate, $oMyChange, $oStatLog, $aTraces);
- $oStatLog->Set('end_date', time());
- $oStatLog->Set('status', 'completed');
- $oStatLog->DBUpdateTracked($oMyChange);
- }
- catch (Exception $e)
- {
- $oStatLog->Set('end_date', time());
- $oStatLog->Set('status', 'completed');
- $oStatLog->DBUpdateTracked($oMyChange);
- }
- return $oStatLog;
- }
- protected function DoSynchronize($oLastFullLoadStartDate, $oMyChange, &$oStatLog, &$aTraces)
- {
- // Get all the replicas that were not seen in the last import and mark them as obsolete
- if ($oLastFullLoadStartDate == null)
- {
- // No previous import known, use the full_load_periodicity value... and the current date
- $oLastFullLoadStartDate = new DateTime(); // Now
- // TO DO: how do we support localization here ??
- $sLoadPeriodicity = trim($this->Get('full_load_periodicity'));
- if (strlen($sLoadPeriodicity) > 0)
- {
- $sInterval = '-'.$sLoadPeriodicity;
- // Note: the PHP doc states that Modify return FALSE in case of error
- // but, this is actually NOT the case
- // Therefore, I do compare before and after, considering that the
- // format is incorrect when the datetime remains unchanged
- $sBefore = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
- $oLastFullLoadStartDate->Modify($sInterval);
- $sAfter = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
- if ($sBefore == $sAfter)
- {
- throw new CoreException("Data exchange: Wrong interval specification", array('interval' => $sInterval, 'source_id' => $this->GetKey()));
- }
- }
- }
- $sLimitDate = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
- $aTraces[] = "Limit Date: $sLimitDate";
- $sSelectToObsolete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
- $oSetToObsolete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
- while($oReplica = $oSetToObsolete->Fetch())
- {
- // TO DO: take the appropriate action based on the 'delete_policy' field
- $sUpdateOnObsolete = $this->Get('delete_policy');
- if ( ($sUpdateOnObsolete == 'update') || ($sUpdateOnObsolete == 'update_then_delete') )
- {
- $aTraces[] = "Destination object: (dest_id:".$oReplica->Get('dest_id').") to be updated";
- $aToUpdate = array();
- $aToUpdate = explode(';', $this->Get('delete_policy_update')); //ex: 'status:obsolete;description:stopped',
- foreach($aToUpdate as $sUpdateSpec)
- {
- $aUpdateSpec = explode(':', $sUpdateSpec);
- if (count($aUpdateSpec) == 2)
- {
- $sAttCode = $aUpdateSpec[0];
- $sValue = $aUpdateSpec[1];
- $aToUpdate[$sAttCode] = $sValue;
- }
- }
- $oReplica->UpdateDestObject($aToUpdate, $oMyChange, $oStatLog, $aTraces, 'stats_nb_obj_obsoleted');
- }
- $oReplica->Set('status', 'obsolete');
- $oReplica->DBUpdateTracked($oMyChange);
- }
- //Count "seen" objects
- $sSelectSeen = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen >= :last_import";
- $oSetSeen = new DBObjectSet(DBObjectSearch::FromOQL($sSelectSeen), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
- $oStatLog->Set('stats_nb_replica_seen', $oSetSeen->Count());
-
- // Get all the replicas that are 'new' or modified
- //
- // Get the list of SQL columns
- $sClass = $this->GetTargetClass();
- $aAttCodes = array();
- $sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND update = 1";
- $oSetAtt = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */);
- while ($oSyncAtt = $oSetAtt->Fetch())
- {
- $aAttCodes[] = $oSyncAtt->Get('attcode');
- }
- $aColumns = $this->GetSQLColumns($aAttCodes);
- $aExtDataFields = array_keys($aColumns);
- $aExtDataFields[] = 'primary_key';
- $aExtDataSpec = array(
- 'table' => $this->GetDataTable(),
- 'join_key' => 'id',
- 'fields' => $aExtDataFields
- );
- // Get the list of reconciliation keys
- $aReconciliationKeys = array();
- if ($this->Get('reconciliation_policy') == 'use_attributes')
- {
- $sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND reconcile = 1";
- $oAttSet = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */);
- while ($oSyncAtt = $oAttSet->Fetch())
- {
- $aReconciliationKeys[] = $oSyncAtt->Get('attcode');
- }
- }
- elseif ($this->Get('reconciliation_policy') == 'use_primary_key')
- {
- $aReconciliationKeys[] = "primary_key";
- }
- $aTraces[] = "Reconciliation on: {".implode(', ', $aReconciliationKeys)."}";
-
- $aAttributes = array();
- foreach($aAttCodes as $sAttCode)
- {
- $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
- if ($oAttDef->IsWritable() && $oAttDef->IsScalar())
- {
- $aAttributes[] = $sAttCode;
- }
- }
-
- $sSelectToSync = "SELECT SynchroReplica WHERE (status = 'new' OR status = 'modified') AND sync_source_id = :source_id";
- $oSetToSync = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, $aExtDataSpec, 0 /* limitCount */, 0 /* limitStart */);
- while($oReplica = $oSetToSync->Fetch())
- {
- $oReplica->Synchro($this, $aReconciliationKeys, $aAttributes, $oMyChange, $oStatLog, $aTraces);
- }
-
- // Get all the replicas that are to be deleted
- //
- $oDeletionDate = $oLastFullLoadStartDate;
- $sDeleteRetention = trim($this->Get('delete_policy_retention'));
- if (strlen($sDeleteRetention) > 0)
- {
- $sInterval = '-'.$sDeleteRetention;
- // Note: the PHP doc states that Modify return FALSE in case of error
- // but, this is actually NOT the case
- // Therefore, I do compare before and after, considering that the
- // format is incorrect when the datetime remains unchanged
- $sBefore = $oDeletionDate->Format('Y-m-d H:i:s');
- $oDeletionDate->Modify($sInterval);
- $sAfter = $oDeletionDate->Format('Y-m-d H:i:s');
- if ($sBefore == $sAfter)
- {
- throw new CoreException("Data exchange: Wrong interval specification", array('interval' => $sInterval, 'source_id' => $this->GetKey()));
- }
- }
- $sDeletionDate = $oDeletionDate->Format('Y-m-d H:i:s');
- $aTraces[] = "sDeletionDate: $sDeletionDate";
- $sSelectToDelete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('obsolete') AND status_last_seen < :last_import";
- $oSetToDelete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sDeletionDate));
- while($oReplica = $oSetToDelete->Fetch())
- {
- $sUpdateOnObsolete = $this->Get('delete_policy');
- if ( ($sUpdateOnObsolete == 'delete') || ($sUpdateOnObsolete == 'update_then_delete') )
- {
- $aTraces[] = "Destination object: (dest_id:".$oReplica->Get('dest_id').") to be DELETED";
- $oReplica->DeleteDestObject($oMyChange, $oStatLog, $aTraces);
- }
- $aTraces[] = "Replica id:".$oReplica->GetKey()." (dest_id:".$oReplica->Get('dest_id').") to be deleted";
- $oReplica->DBDeleteTracked($oMyChange);
- }
- }
-
- /**
- * Get the list of SQL columns corresponding to a particular list of attribute codes
- * Defaults to the whole list of columns for the current class
- */
- public function GetSQLColumns($aAttributeCodes = null)
- {
- $aColumns = array();
- $sClass = $this->GetTargetClass();
- if (is_null($aAttributeCodes))
- {
- $aAttributeCodes = array();
- foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
- {
- if ($sAttCode == 'finalclass') continue;
- $aAttributeCodes[] = $sAttCode;
- }
- }
- foreach($aAttributeCodes as $sAttCode)
- {
- $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
-
- foreach($oAttDef->GetSQLColumns() as $sField => $sDBFieldType)
- {
- $aColumns[$sField] = $sDBFieldType;
- }
- }
- return $aColumns;
- }
-
- public function IsRunning()
- {
- $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='running'";
- $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
- if ($oSet->Count() < 1)
- {
- $bRet = false;
- }
- else
- {
- $bRet = true;
- }
- return $bRet;
- }
-
- public function GetLatestLog()
- {
- $oLog = null;
-
- $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id";
- $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
- if ($oSet->Count() >= 1)
- {
- $oLog = $oSet->Fetch();
- }
- return $oLog;
- }
-
- // TO DO: remove if still unused
- /**
- * Retrieve from the log, the date of the last completed import
- * @return DateTime
- */
- public function GetLastCompletedImportDate()
- {
- $date = null;
- $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='completed'";
- $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('end_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 0 /* limitCount */, 0 /* limitStart */);
- if ($oSet->Count() >= 1)
- {
- $oLog = $oSet->Fetch();
- $date = $oLog->Get('end_date');
- }
- else
- {
- // TO DO: remove trace
- echo "<p>No completed log found</p>\n";
- }
- return $date;
- }
- }
- class SynchroAttribute extends cmdbAbstractObject
- {
- public static function Init()
- {
- $aParams = array
- (
- "category" => "core/cmdb,view_in_gui",
- "key_type" => "autoincrement",
- "name_attcode" => "",
- "state_attcode" => "",
- "reconc_keys" => array(),
- "db_table" => "priv_sync_att",
- "db_key_field" => "id",
- "db_finalclass_field" => "",
- "display_template" => "",
- );
- MetaModel::Init_Params($aParams);
- MetaModel::Init_InheritAttributes();
- MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeBoolean("update", array("allowed_values"=>null, "sql"=>"update", "default_value"=>true, "is_null_allowed"=>false, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeBoolean("reconcile", array("allowed_values"=>null, "sql"=>"reconcile", "default_value"=>false, "is_null_allowed"=>false, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeEnum("update_policy", array("allowed_values"=>new ValueSetEnum('master_locked,master_unlocked,write_once'), "sql"=>"update_policy", "default_value"=>"master_locked", "is_null_allowed"=>false, "depends_on"=>array())));
- // Display lists
- MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for the complete details
- MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
- // Search criteria
- // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
- // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
- }
- }
- class SynchroAttExtKey extends SynchroAttribute
- {
- public static function Init()
- {
- $aParams = array
- (
- "category" => "core/cmdb,view_in_gui",
- "key_type" => "autoincrement",
- "name_attcode" => "",
- "state_attcode" => "",
- "reconc_keys" => array(),
- "db_table" => "priv_sync_att_extkey",
- "db_key_field" => "id",
- "db_finalclass_field" => "",
- "display_template" => "",
- );
- MetaModel::Init_Params($aParams);
- MetaModel::Init_InheritAttributes();
- MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
- // Display lists
- MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
- MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
- // Search criteria
- // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
- // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
- }
- }
- class SynchroAttLinkSet extends SynchroAttribute
- {
- public static function Init()
- {
- $aParams = array
- (
- "category" => "core/cmdb,view_in_gui",
- "key_type" => "autoincrement",
- "name_attcode" => "",
- "state_attcode" => "",
- "reconc_keys" => array(),
- "db_table" => "priv_sync_att_linkset",
- "db_key_field" => "id",
- "db_finalclass_field" => "",
- "display_template" => "",
- );
- MetaModel::Init_Params($aParams);
- MetaModel::Init_InheritAttributes();
- MetaModel::Init_AddAttribute(new AttributeString("row_separator", array("allowed_values"=>null, "sql"=>"row_separator", "default_value"=>'|', "is_null_allowed"=>true, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
- // Display lists
- MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'row_separator', 'attribute_separator')); // Attributes to be displayed for the complete details
- MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
- // Search criteria
- // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
- // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
- }
- }
- //class SynchroLog extends Event
- class SynchroLog extends cmdbAbstractObject
- {
- public static function Init()
- {
- $aParams = array
- (
- "category" => "core/cmdb,view_in_gui",
- "key_type" => "autoincrement",
- "name_attcode" => "",
- "state_attcode" => "",
- "reconc_keys" => array(),
- "db_table" => "priv_sync_log",
- "db_key_field" => "id",
- "db_finalclass_field" => "",
- "display_template" => "",
- );
- MetaModel::Init_Params($aParams);
- MetaModel::Init_InheritAttributes();
- // MetaModel::Init_AddAttribute(new AttributeString("userinfo", array("allowed_values"=>null, "sql"=>"userinfo", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeDateTime("start_date", array("allowed_values"=>null, "sql"=>"start_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeDateTime("end_date", array("allowed_values"=>null, "sql"=>"end_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('running,completed'), "sql"=>"status", "default_value"=>"running", "is_null_allowed"=>false, "depends_on"=>array())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- // Display lists
- 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',
- 'stats_nb_obj_created_errors', 'stats_nb_replica_reconciled_errors', 'stats_nb_obj_updated_errors', 'stats_nb_obj_obsoleted_errors', 'stats_nb_obj_deleted_errors')); // Attributes to be displayed for the complete details
- MetaModel::Init_SetZListItems('list', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_replica_seen')); // Attributes to be displayed for a list
- // Search criteria
- // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
- // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
- }
-
- /**
- * Increments a statistics counter
- */
- function Inc($sCode)
- {
- $this->Set($sCode, 1+$this->Get($sCode));
- }
- }
- class SynchroReplica extends DBObject
- {
- static $aSearches = array(); // Cache of OQL queries used for reconciliation (per data source)
-
- public static function Init()
- {
- $aParams = array
- (
- "category" => "core/cmdb,view_in_gui",
- "key_type" => "autoincrement",
- "name_attcode" => "",
- "state_attcode" => "",
- "reconc_keys" => array(),
- "db_table" => "priv_sync_replica",
- "db_key_field" => "id",
- "db_finalclass_field" => "",
- "display_template" => "",
- );
- MetaModel::Init_Params($aParams);
- MetaModel::Init_InheritAttributes();
- MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
- MetaModel::Init_AddAttribute(new AttributeClass("dest_class", array("class_category"=>"bizmodel", "more_values"=>"", "sql"=>"dest_class", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- 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())));
- MetaModel::Init_AddAttribute(new AttributeDateTime("info_last_synchro", array("allowed_values"=>null, "sql"=>"info_last_synchro", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
- // Display lists
- MetaModel::Init_SetZListItems('details', array('sync_source_id', 'dest_id', 'dest_class', 'status_last_seen', 'status', 'status_dest_creator', 'status_last_error', 'info_creation_date', 'info_last_modified', 'info_last_synchro')); // Attributes to be displayed for the complete details
- MetaModel::Init_SetZListItems('list', array('sync_source_id', 'dest_id', 'dest_class', 'status_last_seen', 'status', 'status_dest_creator', 'status_last_error')); // Attributes to be displayed for a list
- // Search criteria
- MetaModel::Init_SetZListItems('standard_search', array('sync_source_id', 'status_last_seen', 'status', 'status_dest_creator', 'dest_class', 'dest_id', 'status_last_error')); // Criteria of the std search form
- // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
- }
- public function DBInsert()
- {
- throw new CoreException('A synchronization replica must be created only by the mean of triggers');
- }
- // Overload the deletion -> the replica has been created by the mean of a trigger,
- // it will be deleted by the mean of a trigger too
- public function DBDelete()
- {
- $oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
- $sTable = $oDataSource->GetDataTable();
- $sSQL = "DELETE FROM `$sTable` WHERE id = '{$this->GetKey()}'";
- CMDBSource::Query($sSQL);
- $this->m_bIsInDB = false;
- $this->m_iKey = null;
- }
- public function SetLastError($sMessage, $oException = null)
- {
- if ($oException)
- {
- $sText = $sMessage.$oException->getMessage();
- }
- else
- {
- $sText = $sMessage;
- }
- if (strlen($sText) > 255)
- {
- $sText = substr($sText, 0, 200).'...('.strlen($sText).' chars)...';
- }
- $this->Set('status_last_error', $sText);
- }
-
- public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange, &$oStatLog, &$aTraces)
- {
- switch($this->Get('status'))
- {
- case 'new':
- // If needed, construct the query used for the reconciliation
- if (!isset(self::$aSearches[$oDataSource->GetKey()]))
- {
- foreach($aReconciliationKeys as $sFilterCode)
- {
- $aCriterias[] = ($sFilterCode == 'primary_key' ? 'id' : $sFilterCode).' = :'.$sFilterCode;
- }
- $sOQL = "SELECT ".$oDataSource->GetTargetClass()." WHERE ".implode(' AND ', $aCriterias);
- self::$aSearches[$oDataSource->GetKey()] = DBObjectSearch::FromOQL($sOQL);
- }
- // Get the criterias for the search
- $aFilterValues = array();
- foreach($aReconciliationKeys as $sFilterCode)
- {
- $value = $this->GetValueFromExtData($sFilterCode);
- if (!is_null($value))
- {
- $aFilterValues[$sFilterCode] = $value;
- }
- else
- {
- // Reconciliation could not be performed - log and EXIT
- $aTraces[] = "Could not reconcile on null value: ".$sFilterCode;
- $this->SetLastError('Could not reconcile on null value: '.$sFilterCode);
- $oStatLog->Inc('stats_nb_replica_reconciled_errors');
- return;
- }
- }
- $oDestSet = new DBObjectSet(self::$aSearches[$oDataSource->GetKey()], array(), $aFilterValues);
- $iCount = $oDestSet->Count();
- $aConditions = array();
- foreach($aFilterValues as $sCode => $sValue)
- {
- $aConditions[] = $sCode.'='.$sValue;
- }
- $sConditionDesc = implode(' AND ', $aConditions);
- // How many objects match the reconciliation criterias
- switch($iCount)
- {
- case 0:
- $aTraces[] = "Nothing found on: $sConditionDesc";
- if ($oDataSource->Get('action_on_zero') == 'create')
- {
- $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog, $aTraces);
- }
- else // assumed to be 'error'
- {
- $aTraces[] = "Failed to reconcile (no match)";
- $this->SetLastError('Could not find a match for reconciliation');
- $oStatLog->Inc('stats_nb_replica_reconciled_errors');
- }
- break;
-
- case 1:
- $aTraces[] = "Found 1 object on: $sConditionDesc";
- if ($oDataSource->Get('action_on_one') == 'update')
- {
- $oDestObj = $oDestSet->Fetch();
- $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, $aTraces, 'stats_nb_replica_reconciled');
- $this->Set('dest_id', $oDestObj->GetKey());
- $this->Set('status_dest_creator', false);
- $this->Set('dest_class', get_class($oDestObj));
-
- $oStatLog->Inc('stats_nb_replica_reconciled');
- }
- else
- {
- // assumed to be 'error'
- $aTraces[] = "Failed to reconcile (1 match)";
- $this->SetLastError('Found a match while expecting several');
- $oStatLog->Inc('stats_nb_replica_reconciled_errors');
- }
- break;
-
- default:
- $aTraces[] = "Found $iCount objects on: $sConditionDesc";
- if ($oDataSource->Get('action_on_multiple') == 'error')
- {
- $aTraces[] = "Failed to reconcile (N>1 matches)";
- $this->SetLastError($iCount.' destination objects match the reconciliation criterias: '.$sConditionDesc);
- $oStatLog->Inc('stats_nb_replica_reconciled_errors');
- }
- elseif ($oDataSource->Get('action_on_multiple') == 'create')
- {
- $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog, $aTraces);
- }
- else
- {
- // assumed to be 'take_first'
- $oDestObj = $oDestSet->Fetch();
- $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, $aTraces, 'stats_nb_replica_reconciled');
- $this->Set('dest_id', $oDestObj->GetKey());
- $this->Set('status_dest_creator', false);
- $this->Set('dest_class', get_class($oDestObj));
- }
- }
- break;
-
- case 'modified':
- $oDestObj = MetaModel::GetObject($oDataSource->GetTargetClass(), $this->Get('dest_id'));
- if ($oDestObj == null)
- {
- $this->Set('status', 'orphan'); // The destination object has been deleted !
- $this->SetLastError('Destination object deleted unexpectedly');
- $oStatLog->Inc('stats_nb_obj_updated_errors');
- }
- else
- {
- $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, $aTraces, 'stats_nb_obj_updated');
- }
- break;
-
- default: // Do nothing in all other cases
- }
- $this->DBUpdateTracked($oChange);
- }
-
- /**
- * Updates the destination object with the Extended data found in the synchro_data_XXXX table
- */
- protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, &$oStatLog, &$aTraces, $sStatsCode)
- {
- $aValueTrace = array();
- foreach($aAttributes as $sAttCode)
- {
- $value = $this->GetValueFromExtData($sAttCode);
- if (!is_null($value))
- {
- $oDestObj->Set($sAttCode, $value);
- $aValueTrace[] = "$sAttCode: $value";
- }
- }
- try
- {
- $oDestObj->DBUpdateTracked($oChange);
- $aTraces[] = "Updated object ".$oDestObj->GetHyperLink()." (".implode(', ', $aValueTrace).")";
- $oStatLog->Inc($sStatsCode);
- $this->Set('status_last_error', '');
- $this->Set('status', 'synchronized');
- }
- catch(Exception $e)
- {
- $aTraces[] = "Failed to update destination object: {$e->getMessage()}";
- $this->SetLastError('Unable to update destination object: ', $e);
- $oStatLog->Inc($sStatsCode.'_errors');
- }
- }
- /**
- * Creates the destination object populating it with the Extended data found in the synchro_data_XXXX table
- */
- protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange, &$oStatLog, &$aTraces)
- {
- $oDestObj = MetaModel::NewObject($sClass);
- try
- {
- $aValueTrace = array();
- foreach($aAttributes as $sAttCode)
- {
- $value = $this->GetValueFromExtData($sAttCode);
- if (!is_null($value))
- {
- $oDestObj->Set($sAttCode, $value);
- $aValueTrace[] = "$sAttCode: $value";
- }
- }
- $iNew = $oDestObj->DBInsertTracked($oChange);
- $aTraces[] = "Created $sClass::$iNew (".implode(', ', $aValueTrace).")";
- $this->Set('dest_id', $oDestObj->GetKey());
- $this->Set('dest_class', get_class($oDestObj));
- $this->Set('status_dest_creator', true);
- $this->Set('status_last_error', '');
- $this->Set('status', 'synchronized');
- $oStatLog->Inc('stats_nb_obj_created');
- }
- catch(Exception $e)
- {
- $aTraces[] = "Failed to create $sClass ({$e->getMessage()})";
- $this->SetLastError('Unable to create destination object: ', $e);
- $oStatLog->Inc('stats_nb_obj_created_errors');
- }
- }
-
- /**
- * Update the destination object with given values
- */
- public function UpdateDestObject($aValues, $oChange, &$oStatLog, &$aTraces, $sStatCode)
- {
- try
- {
- $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
- foreach($aValues as $sAttCode => $value)
- {
- $oDestObj->Set($sAttCode, $value);
- }
- $oDestObj->DBUpdateTracked($oChange);
- $aTraces[] = "Replica id:".$this->GetKey()." (dest_id:".$this->Get('dest_id').") marked as obsolete";
- $oStatLog->Inc($sStatCode);
- }
- catch(Exception $e)
- {
- $this->SetLastError('Unable to update the destination object: ', $e);
- $oStatLog->Inc($sStatCode.'_errors');
- }
- }
- /**
- * Delete the destination object
- */
- public function DeleteDestObject($oChange, &$oStatLog, &$aTraces)
- {
- if($this->Get('status_dest_creator'))
- {
- $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
- try
- {
- $oDestObj->DBDeleteTracked($oChange);
- $oStatLog->Inc('stats_nb_obj_deleted');
- }
- catch(Exception $e)
- {
- $this->SetLastError('Unable to delete the destination object: ', $e);
- $oStatLog->Inc('stats_nb_obj_deleted_errors');
- }
- }
- }
- /**
- * Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
- */
- protected function GetValueFromExtData($sColumnName)
- {
- $aData = $this->GetExtendedData();
- return $aData[$sColumnName];
- }
- }
- // TO DO: finalize.... admins only ? which options ? troubleshoot WebPageMenuNode::__construct(.... sEnableClass...) ?
- //if (UserRights::IsAdministrator())
- {
- $oAdminMenu = new MenuGroup('AdminTools', 80 /* fRank */);
- new OQLMenuNode('DataSources', 'SELECT SynchroDataSource', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
- new WebPageMenuNode('Test:RunSynchro', '../synchro/synchro_exec.php', $oAdminMenu->GetIndex(), 13 /* fRank */, 'SynchroDataSource');
- }
- ?>
|