[icinga-checkins] icinga.org: icinga-web/jmosshammer/apiIntegration: * Added filter implementation

git at icinga.org git at icinga.org
Tue Jan 11 22:46:44 CET 2011


Module: icinga-web
Branch: jmosshammer/apiIntegration
Commit: b2cd6d86a0f86ff87a96bfda828a4c2eb723ad48
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=b2cd6d86a0f86ff87a96bfda828a4c2eb723ad48

Author: jmosshammer <jmosshammer at ws-jmosshammer.(none)>
Date:   Tue Jan 11 22:46:24 2011 +0100

* Added filter implementation

---

 .../Api/models/ApiDataFilterModel.class.php        |  122 ++++++++++++++++++++
 .../Api/models/ApiDataRequestModel.class.php       |   13 ++-
 2 files changed, 132 insertions(+), 3 deletions(-)

diff --git a/app/modules/Api/models/ApiDataFilterModel.class.php b/app/modules/Api/models/ApiDataFilterModel.class.php
new file mode 100644
index 0000000..c9fb617
--- /dev/null
+++ b/app/modules/Api/models/ApiDataFilterModel.class.php
@@ -0,0 +1,122 @@
+<?php
+
+class InvalidApiQueryException extends Exception {};
+
+interface IcingaApiRequestModifier {
+	public function apply(Api_ApiDataRequestModel $request); 
+}
+
+interface I_IcingaApiFilter {
+	public function getFilterString();
+}
+
+class IcingaApiFilter implements IcingaApiRequestModifier implements I_IcingaApiFilter {
+	public const EQUAL 	= "=";
+	public const NOT_EQUAL 	= "!=";
+	public const GREATER	= ">";
+	public const GREATER_EQ	= ">=";
+	public const LOWER	= "<";
+	public const LOWER_EQ	= "<=";
+	public const LIKE 	= "LIKE";
+	public const NOT_LIKE 	= "NOT LIKE";
+	
+	protected $field;
+	protected $value;
+	protected $type;
+	protected $table;
+
+	public function setField($field) {
+		$this->field = $field;
+	}
+	public function getField() {
+		return $this->field;
+	}
+	public function setValue($val) {
+		$this->value = $val;
+	}
+	public function getValue() {
+		return $this->value;
+	}
+	public function setType($type) {
+		$this->type = $type;
+	}
+	public function getType() {
+		return $this->type;
+	}
+	public function setTable($table) {
+		$this->table = $table;
+	}
+	public function getTable() {
+		return $this->table;
+	}
+	
+	public function __construct($field,$value,$type = EQUAL,$table = NULL) {
+		$this->setValue($value);
+		$this->setField($field);
+		$this->setType($type);
+		$this->setTable($table);
+	}
+	
+	public function apply(Api_ApiDataRequestModel $request) {
+		throw InvalidApiQueryException("IcingaApiFilter must be encapsulated in a IcingaApiFilterGroup");
+	} 
+	
+	public function getFilterString() {
+		return ($this->table ? $this->table : "").$this->field." ".$this->method." ".$this->value;
+	} 
+}
+
+class IcingaApiFilterGroup extends  IcingaApiFilter {
+	protected $logicOperator;
+
+	public function apply(Api_ApiDataRequestModel $request) {
+		$request->setFilterString($this->getFilterString);		
+	}
+
+	public function getFilterString() {
+		$str = "(";
+		foreach($this->filters as $filter) {
+			if($str != "(")
+				$str .=  $this->logicOperator;
+			$str .= filter->getFilterString();	
+		}
+		return $str.")";
+	}
+}
+class IcingaApiANDFilterGroup extends IcingaApiFilterGroup {
+	protected $logicOperator = "AND";
+}
+
+class IcingaApiORFilterGroup extends IcingaApiFilterGroup {
+	protected $logicOperator = "OR";
+}
+
+class IcingaApiLimit implements ApiRequestModifier {
+	protected $limit = 0;
+	protected $offset = 0;
+	public function __construct($limit, $offset = 0) {
+		$this->limit = $limit;
+		$this->offset = $offset;
+	}
+	
+	public function apply(Api_ApiDataRequestModel $request) {
+		$request->setLimit($this->limit);
+		$request->setOffset($this->offset);
+	}
+}
+
+class IcingaApiGrouping implements ApiRequestModifier {
+	protected $groups = array();
+	public function __construct($group) {
+		if(!is_array($group))
+			$group = array($group);
+		$this->groups = $group;
+	}
+
+	public function apply(Api_ApiDataRequestModel $request) {
+		$request->setGroups($this->groups);
+	}
+
+}
+
+?>
diff --git a/app/modules/Api/models/ApiDataRequestModel.class.php b/app/modules/Api/models/ApiDataRequestModel.class.php
index 015a14f..450c506 100755
--- a/app/modules/Api/models/ApiDataRequestModel.class.php
+++ b/app/modules/Api/models/ApiDataRequestModel.class.php
@@ -1,6 +1,6 @@
 <?php
 
-class Api_ApiDataRequestModel extends IcingaApiBaseModel implements IcingaApiDataAccessor
+class Api_ApiDataRequestModel extends IcingaApiBaseModel 
 {
 
 
@@ -28,7 +28,14 @@ class Api_ApiDataRequestModel extends IcingaApiBaseModel implements IcingaApiDat
     public function getService($serviceName) {}
 
     public function getHosts($filter = null,&$count = false,$offset=0,$limit=0,array $groupFields = array()) {}
-    public function getHost($hostName) {}
+    public function getHost($db,$hostname) {
+	$DBALMetaManager = $this->getContext()->getModel("DBALMetaManager","Api");
+	$DBALMetaManager->switchIcingaDatabase($db);
+		
+	$dql = Doctrine_Query::create()->select("*")->from("IcingaHosts h")->leftJoin("h.hostgroups a")->where("a.alias = 'Company 1'");
+	$record = $dql->execute(null,Doctrine_Core::HYDRATE_RECORD);
+	return $record;
+    }
 
     public function getComments($filter = null,&$count = false,$offset=0,$limit=0,array $groupFields = array()) {}
     public function getCommentById($id) {}
@@ -43,4 +50,4 @@ class Api_ApiDataRequestModel extends IcingaApiBaseModel implements IcingaApiDat
 
 }
 
-?>
\ No newline at end of file
+?>





More information about the icinga-checkins mailing list