Permalink
Browse files

Basic SQL implementation for tasks persistence layer

  • Loading branch information...
1 parent defdc59 commit 2d8fdc405ae5d23c4d8165993de75dd33513e25e @cdujeu cdujeu committed May 18, 2016
@@ -0,0 +1,14 @@
+CREATE TABLE IF NOT EXISTS `ajxp_tasks` (
+ `uid` varchar(255) NOT NULL,
+ `userId` varchar(255) NOT NULL,
+ `wsId` varchar(32) NOT NULL,
+ `status` int(11) NOT NULL,
+ `schedule` text NOT NULL,
+ `action` text NOT NULL,
+ `parameters` mediumtext NOT NULL,
+ `nodes` text NOT NULL,
+ PRIMARY KEY (`uid`),
+ UNIQUE KEY `wsId` (`wsId`),
+ KEY `userId` (`userId`,`status`),
+ FULLTEXT KEY `nodes` (`nodes`)
+) DEFAULT CHARSET=utf8 COMMENT='Task persistence layer';
@@ -4,11 +4,34 @@
<class_definition classname="Pydio\Tasks\TaskController" filename="plugins/core.tasks/src/TaskController.php"/>
<registry_contributions>
<actions>
- <action name="tasks">
+ <action name="tasks_list">
<processing>
- <serverCallback methodName="route" restParams="/"/>
+ <serverCallback methodName="route"/>
+ </processing>
+ </action>
+ <action name="task_info">
+ <processing>
+ <serverCallback methodName="route"/>
+ </processing>
+ </action>
+ <action name="task_create">
+ <processing>
+ <serverCallback methodName="route"/>
+ </processing>
+ </action>
+ <action name="task_update">
+ <processing>
+ <serverCallback methodName="route"/>
+ </processing>
+ </action>
+ <action name="task_delete">
+ <processing>
+ <serverCallback methodName="route"/>
</processing>
</action>
</actions>
+ <hooks>
+ <serverCallback methodName="attachTasksToNode" hookName="node.info"/>
+ </hooks>
</registry_contributions>
</ajxp_plugin>
@@ -19,7 +19,7 @@
* The latest code can be found at <http://pyd.io/>.
*/
-namespace Pydio\Tasks\Tests;
+namespace Pydio\Tasks\Providers;
use Pydio\Tasks\Task;
use Pydio\Tasks\Schedule;
@@ -0,0 +1,180 @@
+<?php
+/*
+ * Copyright 2007-2015 Abstrium <contact (at) pydio.com>
+ * This file is part of Pydio.
+ *
+ * Pydio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Pydio 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with Pydio. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The latest code can be found at <http://pyd.io/>.
+ */
+namespace Pydio\Tasks\Providers;
+
+use Pydio\Access\Core\Model\AJXP_Node;
+use Pydio\Access\Core\Model\Repository;
+use Pydio\Conf\Core\AbstractAjxpUser;
+use Pydio\Tasks\ITasksProvider;
+use Pydio\Tasks\Schedule;
+use Pydio\Tasks\Task;
+
+defined('AJXP_EXEC') or die('Access not allowed');
+
+
+
+
+class SqlTasksProvider implements ITasksProvider
+{
+
+ protected function taskToDBValues(Task $task, $removeId = false){
+ $values = [
+ "userId" => $task->getUserId(),
+ "wsId" => $task->getWsId(),
+ "status" => $task->getStatus(),
+ "schedule" => json_encode($task->getSchedule()),
+ "action" => $task->getAction(),
+ "parameters"=> json_encode($task->getParameters()),
+ "nodes" => "|||".implode("|||", $task->nodes)."|||"
+ ];
+ if(!$removeId){
+ $values = array_merge(["uid" => $task->getId()], $values);
+ }
+ return $values;
+ }
+
+ /**
+ * @param \DibiRow $values
+ * @return Task
+ */
+ protected function taskFromDBValues(\DibiRow $values){
+ $task = new Task();
+ $task->setId($values["uid"]);
+ $task->setUserId($values["userId"]);
+ $task->setWsId($values["wsId"]);
+ $task->setStatus($values["status"]);
+ $task->setSchedule(Schedule::fromJson($values["schedule"]));
+ $task->setAction($values["action"]);
+ $task->setParameters(json_decode($values["parameters"], true));
+ $nodes = explode("|||", trim($values["nodes"], "|||"));
+ foreach ($nodes as $node) $task->attachToNode($node);
+ return $task;
+ }
+
+ /**
+ * @param Task $task
+ * @param Schedule $when
+ * @return Task
+ */
+ public function createTask(Task $task, Schedule $when)
+ {
+ \dibi::query("INSERT INTO [ajxp_tasks] ", $this->taskToDBValues($task));
+ }
+
+ /**
+ * @param string $taskId
+ * @return Task
+ */
+ public function getTaskById($taskId)
+ {
+ $res = \dibi::query('SELECT * FROM [ajxp_tasks] WHERE [uid]=%s', $taskId);
+ foreach ($res->fetchAll() as $row) {
+ return $this->taskFromDBValues($row);
+ }
+ }
+
+ /**
+ * @param Task $task
+ * @return Task
+ */
+ public function updateTask(Task $task)
+ {
+ try{
+ \dibi::query("UPDATE [ajxp_tasks] SET ", $this->taskToDBValues($task, true), " WHERE [uid] =%s", $task->getId());
+ }catch (\DibiException $ex){
+ $sql = $ex->getSql();
+ }
+ }
+
+ /**
+ * @param string $taskId
+ * @param int $status
+ * @return Task
+ */
+ public function updateTaskStatus($taskId, $status)
+ {
+ $task = $this->getTaskById($taskId);
+ $task->setStatus($status);
+ \dibi::query("UPDATE [ajxp_tasks] SET ", $this->taskToDBValues($task, true), " WHERE [uid] =%s", $taskId);
+ }
+
+ /**
+ * @param string $taskId
+ * @return bool
+ */
+ public function deleteTask($taskId)
+ {
+ \dibi::query("DELETE FROM [ajxp_tasks] WHERE uid=%s", $taskId);
+ }
+
+ /**
+ * @return Task[]
+ */
+ public function getPendingTasks()
+ {
+ return $this->getTasks(null, null, Task::STATUS_PENDING);
+ }
+
+ /**
+ * @param AJXP_Node $node
+ * @return Task[]
+ */
+ public function getTasksForNode(AJXP_Node $node)
+ {
+ $tasks = [];
+ try{
+ $res = \dibi::query('SELECT * FROM [ajxp_tasks] WHERE [nodes] LIKE %s', "%|||".$node->getUrl()."|||%");
+ foreach ($res->fetchAll() as $row) {
+ $tasks[] = $this->taskFromDBValues($row);
+ }
+ }catch(\DibiException $e){
+ $sql = $e->getSql();
+ }
+ return $tasks;
+ }
+
+ /**
+ * @param AbstractAjxpUser $user
+ * @param Repository $repository
+ * @param int $status
+ * @return Task[]
+ */
+ public function getTasks($user = null, $repository = null, $status = -1)
+ {
+ $tasks = [];
+ $where = [];
+ if($user !== null){
+ $where[] = array("[userId] = %s", $user->getId());
+ }
+ if($repository !== null){
+ $where[] = array("[wsId] = %s", $repository->getId());
+ }
+ if($status !== -1){
+ $where[] = array("[status] = %i", $status);
+ }
+ $res = \dibi::query('SELECT * FROM [ajxp_tasks] WHERE %and', $where);
+ foreach ($res->fetchAll() as $row) {
+ $tasks[] = $this->taskFromDBValues($row);
+ }
+ return $tasks;
+
+ }
+}
@@ -43,11 +43,11 @@ class Task
/**
* @var string
*/
- public $repositoryId;
+ public $wsId;
/**
* @var string
*/
- public $repositoryIdentifier;
+ public $wsIdentifier;
/**
* @var int
@@ -66,6 +66,11 @@ class Task
*/
public $parameters;
+ /**
+ * @var array
+ */
+ public $nodes = [];
+
public function __construct()
{
$this->status = self::STATUS_PENDING;
@@ -108,33 +113,33 @@ public function setUserId($userId)
/**
* @return string
*/
- public function getRepositoryId()
+ public function getWsId()
{
- return $this->repositoryId;
+ return $this->wsId;
}
/**
- * @param string $repositoryId
+ * @param string $wsId
*/
- public function setRepositoryId($repositoryId)
+ public function setWsId($wsId)
{
- $this->repositoryId = $repositoryId;
+ $this->wsId = $wsId;
}
/**
* @return string
*/
- public function getRepositoryIdentifier()
+ public function getWsIdentifier()
{
- return $this->repositoryIdentifier;
+ return $this->wsIdentifier;
}
/**
- * @param string $repositoryIdentifier
+ * @param string $wsIdentifier
*/
- public function setRepositoryIdentifier($repositoryIdentifier)
+ public function setWsIdentifier($wsIdentifier)
{
- $this->repositoryIdentifier = $repositoryIdentifier;
+ $this->wsIdentifier = $wsIdentifier;
}
/**
@@ -201,5 +206,9 @@ public function setParameters($parameters)
$this->parameters = $parameters;
}
+ public function attachToNode($nodePath){
+ $this->nodes[] = $nodePath;
+ }
+
}
Oops, something went wrong.

0 comments on commit 2d8fdc4

Please sign in to comment.