[icinga-checkins] icinga.org: icinga-web/master: * updated relations for multi-connection

git at icinga.org git at icinga.org
Tue Apr 24 15:01:15 CEST 2012


Module: icinga-web
Branch: master
Commit: 2c5b3d3b7844c56b36f145008ef8b9fe1dfd5f9c
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=2c5b3d3b7844c56b36f145008ef8b9fe1dfd5f9c

Author: Jannis Mosshammer <jannis.mosshammer at netways.de>
Date:   Fri Mar 30 13:47:47 2012 +0200

* updated relations for multi-connection

---

 .../Api/actions/RelationProviderAction.class.php   |    4 ++-
 app/modules/Api/config/autoload.xml                |    2 +-
 .../lib/database/IcingaDoctrineDatabase.class.php  |    2 +-
 .../listeners/ConnectionInitListener.class.php     |   25 ++++++++++++++++++++
 .../Api/models/Relation/DataModelModel.class.php   |   16 ++++++++----
 app/modules/Api/validate/RelationProvider.xml      |    8 ++++++
 .../Cronks/lib/js/Cronks/Tackle/Relation/Head.js   |   10 +++++--
 7 files changed, 56 insertions(+), 11 deletions(-)

diff --git a/app/modules/Api/actions/RelationProviderAction.class.php b/app/modules/Api/actions/RelationProviderAction.class.php
index bef203b..93e605f 100644
--- a/app/modules/Api/actions/RelationProviderAction.class.php
+++ b/app/modules/Api/actions/RelationProviderAction.class.php
@@ -11,7 +11,9 @@ class Api_RelationProviderAction extends IcingaApiBaseAction {
 	
 	public function executeWrite(AgaviRequestDataHolder $rd) {
 	    
-	    $model = $this->context->getModel('Relation.DataModel', 'Api');
+	    $model = $this->context->getModel('Relation.DataModel', 'Api',
+            array("connection" => $rd->getParameter("connection","icinga"))
+        );
 	    
 	    try {
 	        $this->setAttribute('data', $model->getRelationDataForObjectId($rd->getParameter('objectId')));
diff --git a/app/modules/Api/config/autoload.xml b/app/modules/Api/config/autoload.xml
index 498bf33..521a5f0 100644
--- a/app/modules/Api/config/autoload.xml
+++ b/app/modules/Api/config/autoload.xml
@@ -10,7 +10,7 @@
         <autoload name="AccessConfigHandler">%core.module_dir%/Api/lib/config/AccessConfigHandler.class.php</autoload>
         <autoload name="DQLViewConfigHandler">%core.module_dir%/Api/lib/config/DQLViewConfigHandler.class.php</autoload>
         <autoload name="DQLViewExtender">%core.module_dir%/Api/lib/database/DQLViewExtender.php</autoload>
-		<autoload name="IcingaDoctrineDatabase">%core.module_dir%/Api/lib/database/IcingaDoctrineDatabase.class.php</autoload>
+        <autoload name="IcingaDoctrineDatabase">%core.module_dir%/Api/lib/database/IcingaDoctrineDatabase.class.php</autoload>
 		<autoload name="IcingaDoctrine_Query">%core.module_dir%/Api/lib/database/IcingaDoctrine_Query.class.php</autoload>
 		<autoload name="Icinga_Doctrine_Table">%core.module_dir%/Api/lib/database/IcingaDoctrineTable.class.php</autoload>
         <autoload name="IcingaDoctrineQueryFilterChain">%core.module_dir%/Api/lib/database/filter/IcingaDoctrineQueryFilterChain.class.php</autoload>
diff --git a/app/modules/Api/lib/database/IcingaDoctrineDatabase.class.php b/app/modules/Api/lib/database/IcingaDoctrineDatabase.class.php
index 08d497c..388d706 100644
--- a/app/modules/Api/lib/database/IcingaDoctrineDatabase.class.php
+++ b/app/modules/Api/lib/database/IcingaDoctrineDatabase.class.php
@@ -19,7 +19,7 @@ class IcingaDoctrineDatabase extends AppKitDoctrineDatabase {
     public function initialize(AgaviDatabaseManager $databaseManager, array $parameters = array()) {
         parent::initialize($databaseManager, $parameters);
         self::$icingaConnections[] = $this->getName();
-
+        
         if ($this->getParameter('use_retained')) {
             $this->use_retained = true;
         }
diff --git a/app/modules/Api/lib/database/listeners/ConnectionInitListener.class.php b/app/modules/Api/lib/database/listeners/ConnectionInitListener.class.php
new file mode 100644
index 0000000..6734ae6
--- /dev/null
+++ b/app/modules/Api/lib/database/listeners/ConnectionInitListener.class.php
@@ -0,0 +1,25 @@
+<?php
+
+class ConnectionInitListener extends Doctrine_EventListener {
+    private $initConnectionSql = null;
+    private $dateFormat = null;
+    public function  __construct($dateFormat = null, $initConnectionSQL = null) {
+        $this->dateFormat = $dateFormat;
+        $this->initConnectionSql = $initConnectionSQL;
+    }
+    public function postConnect(Doctrine_Event $event)  {
+        $invoker = $event->getInvoker();
+        if(!$invoker instanceof Doctrine_Connection) {
+            AppKitLogger::warn("Couldn't call ConnectionListenerHook, no connection found");
+            return;
+        }
+        if($this->initConnectionSql !== null) {
+            AppKitLogger::verbose("Executing connection init command for connection %s : %s",
+                $invoker->getName(),
+                $this->initConnectionSql
+            );
+        }
+        $invoker->setDateFormat($this->dateFormat);
+        $invoker->execute($this->initConnectionSql);
+    }
+}
\ No newline at end of file
diff --git a/app/modules/Api/models/Relation/DataModelModel.class.php b/app/modules/Api/models/Relation/DataModelModel.class.php
index e383bd9..5bef114 100644
--- a/app/modules/Api/models/Relation/DataModelModel.class.php
+++ b/app/modules/Api/models/Relation/DataModelModel.class.php
@@ -1,9 +1,14 @@
 <?php
 
 class Api_Relation_DataModelModel extends IcingaApiBaseModel {
-    
+    private $connection = "icinga";
     public function initialize(AgaviContext $context, array $parameters = array()) {
         parent::initialize($context, $parameters);
+        if(isset($parameters["connection"])) {
+            $this->connection = $parameters["connection"];
+        } else {
+            $this->connection = IcingaDoctrineDatabase::CONNECTION_ICINGA;
+        }
     }
     
     public function getRelationDataForObjectId($objectId) {
@@ -27,8 +32,9 @@ class Api_Relation_DataModelModel extends IcingaApiBaseModel {
         } elseif ($objecttypeId == IcingaConstants::TYPE_SERVICE) {
             $type = 'service';
         }
-        
-        $records = IcingaDoctrine_Query::create()
+        $DBALManager = $this->getContext()->getModel("DBALMetaManager","Api");
+        $DBALManager->switchIcingaDatabase($this->connection);
+        $records = IcingaDoctrine_Query::create($this->connection)
         ->select('c.contact_id, c.alias as contact_alias, co.name1 as contact_name, c.email_address as contact_email_address, co.object_id as contact_object_id, NULL as contactgroup_name, NULL as contactgroup_object_id')
         ->from('IcingaContacts c')
         ->innerJoin('c.object co')
@@ -39,7 +45,7 @@ class Api_Relation_DataModelModel extends IcingaApiBaseModel {
             $out = $records;
         }
         
-        $records = IcingaDoctrine_Query::create()
+        $records = IcingaDoctrine_Query::create($this->connection)
         ->select('c.contact_id, c.alias as contact_alias, co.name1 as contact_name, c.email_address as contact_email_address, co.object_id as contact_object_id,  cg.alias as contactgroup_name, cg.contactgroup_object_id as contactgroup_object_id')
         ->from('IcingaContacts c')
         ->innerJoin('c.object co')
@@ -50,7 +56,7 @@ class Api_Relation_DataModelModel extends IcingaApiBaseModel {
         if (count($records)) {
             $out = array_merge($out, $records);
         }
-        
+        $DBALManager->switchIcingaDatabase(IcingaDoctrineDatabase::CONNECTION_ICINGA);
         return $out;
     }
     
diff --git a/app/modules/Api/validate/RelationProvider.xml b/app/modules/Api/validate/RelationProvider.xml
index 2a81b4b..a006ca8 100644
--- a/app/modules/Api/validate/RelationProvider.xml
+++ b/app/modules/Api/validate/RelationProvider.xml
@@ -14,6 +14,14 @@
                     <error>Could not validate number for objectId</error>
                 </errors>
             </validator>
+
+            <validator class="string" name="connection" required="false">
+                <argument>connection</argument>
+                
+                <errors>
+                    <error>Could not validate connection</error>
+                </errors>
+            </validator>
         </validators>
 	</ae:configuration>
 	
diff --git a/app/modules/Cronks/lib/js/Cronks/Tackle/Relation/Head.js b/app/modules/Cronks/lib/js/Cronks/Tackle/Relation/Head.js
index 61ceb82..6682a0d 100644
--- a/app/modules/Cronks/lib/js/Cronks/Tackle/Relation/Head.js
+++ b/app/modules/Cronks/lib/js/Cronks/Tackle/Relation/Head.js
@@ -13,7 +13,7 @@ Icinga.Cronks.Tackle.Relation.Head = Ext.extend(Ext.Panel, {
 	
 	objectId : null,
 	loaded : false,
-	
+	connection: 'icinga',
     constructor : function(config) {
     	if (Ext.isEmpty(config.type)) {
                 throw ("config.type is needed: host or service!");
@@ -159,7 +159,8 @@ Icinga.Cronks.Tackle.Relation.Head = Ext.extend(Ext.Panel, {
         this.doLayout();
     },
     
-    loadDataForObjectId : function(objectId) {
+    loadDataForObjectId : function(objectId,connection) {
+        this.connection = connection || "icinga";
     	if (Ext.isEmpty(objectId) === false && objectId === this.objectId) {
     		return;
     	}
@@ -185,7 +186,10 @@ Icinga.Cronks.Tackle.Relation.Head = Ext.extend(Ext.Panel, {
     	
     	Ext.Ajax.request({
     		url : String.format('{0}/web/api/relation/{1}', AppKit.util.Config.get('path'), id),
-    		success : function(response, opts) {
+    		params: {
+                connection: this.connection
+            },
+            success : function(response, opts) {
     			var data = Ext.decode(response.responseText);
     			var root = data.result;
     			Ext.each(this.stores, function(c) {





More information about the icinga-checkins mailing list