/**
  *
  * @param string[] $parameters
  */
 public static function GetContentList($parameters)
 {
     $logger = \Swiftriver\Core\Setup::GetLogger();
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContentList [Method invoked]", \PEAR_LOG_DEBUG);
     $baseSql = "from content left join " . "content_source on content.id = content_source.content_id left join " . "source on content_source.source_id = source.id";
     $filters = array();
     $state = key_exists("state", $parameters) ? $parameters["state"] : null;
     if ($state != null) {
         $filters[] = "content.state = '{$state}'";
     }
     $minVeracity = key_exists("minVeracity", $parameters) ? $parameters["minVeracity"] : null;
     if ($minVeracity != null || $minVeracity === 0) {
         $filters[] = $minVeracity === 0 ? "(source.score >= {$minVeracity} OR source.score IS NULL)" : "source.score >= {$minVeracity}";
     }
     $maxVeracity = key_exists("maxVeracity", $parameters) ? $parameters["maxVeracity"] : null;
     if ($maxVeracity != null) {
         $filters[] = $minVeracity === 0 ? "(source.score <= {$maxVeracity} OR source.score IS NULL)" : "source.score <= {$maxVeracity}";
     }
     $type = key_exists("type", $parameters) ? $parameters["type"] : null;
     if ($type != null) {
         $filters[] = "source.type = '{$type}'";
     }
     $subType = key_exists("subType", $parameters) ? $parameters["subType"] : null;
     if ($subType != null) {
         $filters[] = "source.subType = '{$subType}'";
     }
     $source = key_exists("source", $parameters) ? $parameters["source"] : null;
     if ($source != null) {
         $filters[] = "source.textId = '{$source}'";
     }
     $pageSize = key_exists("pageSize", $parameters) ? $parameters["pageSize"] : null;
     $pageStart = key_exists("pageStart", $parameters) ? $parameters["pageStart"] : null;
     $pagination = $pageSize != null ? "limit " . ($pageStart == null ? "0" : $pageStart) . ", {$pageSize}" : "";
     $orderBy = key_exists("orderBy", $parameters) ? $parameters["orderBy"] : null;
     if ($orderBy == null) {
         $orderBy = "date desc";
     }
     $sql = $baseSql;
     for ($i = 0; $i < count($filters); $i++) {
         $addition = $i == 0 ? "WHERE" : "AND";
         $sql .= " " . $addition . " " . $filters[$i];
     }
     $selectSql = "select content.textId " . $sql;
     $allIds = RedBeanController::DataBaseAdapter()->getCol($selectSql, array());
     $totalCount = count($allIds);
     $selectSql .= " order by content." . $orderBy . " " . $pagination;
     $navigation = array();
     if ($subType == null) {
         $typeSql = "select source.type as name, source.type as id, count(source.type) as count " . $sql . " group by source.type order by count desc";
         $types = array("type" => "list", "key" => "type", "selected" => $type != null, "facets" => RedBeanController::DataBaseAdapter()->get($typeSql, array()));
         $navigation["Channels"] = $types;
     }
     if ($type != null && $source == null) {
         $subTypeSql = "select source.subType as name, source.subType as id, count(source.subType) as count " . $sql . " group by source.subType order by count desc";
         $subTypes = array("type" => "list", "key" => "subType", "selected" => $subType != null, "facets" => RedBeanController::DataBaseAdapter()->get($subTypeSql, array()));
         $navigation["Sub Channels"] = $subTypes;
     }
     if ($subType != null && $type != null) {
         $sourceSql = "select source.name as name, source.textId as id, count(source.name) as count " . $sql . " group by source.name order by count desc";
         $sources = array("type" => "list", "key" => "source", "selected" => $source != null, "facets" => RedBeanController::DataBaseAdapter()->get($sourceSql, array()));
         $navigation["Sources"] = $sources;
     }
     $subjectIds = RedBeanController::DataBaseAdapter()->getCol($selectSql, array());
     $content = DataContext::GetContent($subjectIds, $orderBy);
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContentList [Method finished]", \PEAR_LOG_DEBUG);
     return array("totalCount" => $totalCount, "contentItems" => $content, "navigation" => $navigation);
 }
 /**
  * Given a set of content items, this method will persist
  * them to the data store, if they already exists then this
  * method should update the values in the data store.
  *
  * @param \Swiftriver\Core\ObjectModel\Content[] $content
  */
 public static function SaveContent($content)
 {
     $logger = \Swiftriver\Core\Setup::GetLogger();
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::SaveContent [Method invoked]", \PEAR_LOG_DEBUG);
     //initiate the redbean dal contoller
     $rb = RedBeanController::RedBean();
     //loop throught each item of content
     foreach ($content as $item) {
         $i = reset(RedBeanController::Finder()->where("content", "textId = :id", array(":id" => $item->id)));
         if (!$i || $i == null) {
             //Create a new data store object
             $i = $rb->dispense("content");
         }
         //Add any properties we wish to be un encoded to the data store object
         $i = DataContext::AddPropertiesToDataSoreItem($i, $item, array("textId" => "id", "state" => "state", "date" => "date"));
         //Add the encoded content item to the data store object
         $i->json = json_encode($item);
         //Save the data store object
         $rb->store($i);
         //get the source from the content
         $source = $item->source;
         //get the source from the db
         $s = reset(RedBeanController::Finder()->where("source", "textId = :id", array(":id" => $source->id)));
         //If the source does not exists, create it.
         if (!$s || $s == null) {
             //create the new data store object for the source
             $s = $rb->dispense("source");
         }
         $s = DataContext::AddPropertiesToDataSoreItem($s, $source, array("textId" => "id", "score" => "score"));
         //add the encoded source object to the data sotre object
         $s->json = json_encode($source);
         //save the source
         $rb->store($s);
         //create the association between content and source
         RedBeanController::Associate($i, $s);
     }
 }