Skip to content

Commit

Permalink
Implementing the data export for EDK as a part of issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxist committed Feb 4, 2016
1 parent fcbbc7c commit 1527847
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 9 deletions.
3 changes: 3 additions & 0 deletions db/schema/7.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CREATE TABLE IF NOT EXISTS `cantiga_edk_area_notes` (
`areaId` int(11) NOT NULL,
`noteType` tinyint(4) NOT NULL,
`content` text NOT NULL,
`lastUpdatedAt` int(11) NULL,
PRIMARY KEY (`areaId`,`noteType`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Expand Down Expand Up @@ -95,6 +96,8 @@ CREATE TABLE IF NOT EXISTS `cantiga_edk_removed_participants` (
KEY `removedById` (`removedById`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `cantiga_edk_route_notes` ADD `lastUpdatedAt` INT NULL ;

ALTER TABLE `cantiga_edk_area_notes`
ADD CONSTRAINT `cantiga_edk_area_notes_fk1` FOREIGN KEY (`areaId`) REFERENCES `cantiga_areas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Expand Down
23 changes: 22 additions & 1 deletion src/Cantiga/ExportBundle/Event/ExportEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,26 @@ class ExportEvent extends Event
{
private $blocks = array();
private $reporter = null;
private $projectId;
private $lastExportAt;

public function __construct($reporterCallback)
public function __construct($projectId, $lastExportAt, $reporterCallback)
{
$this->projectId = $projectId;
$this->lastExportAt = $lastExportAt;
$this->reporter = $reporterCallback;
}

public function getProjectId()
{
return $this->projectId;
}

public function getLastExportAt()
{
return $this->lastExportAt;
}

public function addBlock($name, ExportBlock $block)
{
if (isset($this->blocks[$name])) {
Expand All @@ -47,6 +61,13 @@ public function addBlock($name, ExportBlock $block)
$callback('Exporting block '.$name.': IDs '.$block->countIds().'; Updates '.$block->countUpdates());
}

/**
* Fetches the export block with the given name.
*
* @param string $name Name of the block to return.
* @return ExportBlock
* @throws InvalidArgumentException
*/
public function getBlock($name)
{
if (!isset($this->blocks[$name])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Cantiga/ExportBundle/ExportEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ class ExportEvents
/**
* Call for adding the blocks to export to the external system.
*/
const EXPORT_ONGOING = 'export.ongoing';
const EXPORT_ONGOING = 'cantiga.export.ongoing';
}
2 changes: 1 addition & 1 deletion src/Cantiga/ExportBundle/Repository/ExportEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function exportData($export, $reporter)
}
}

$event = new ExportEvent($reporter);
$event = new ExportEvent($export['projectId'], $export['lastExportedAt'], $reporter);
$event->addBlock('area', $block);

$event = $this->eventDispatcher->dispatch(ExportEvents::EXPORT_ONGOING, $event);
Expand Down
2 changes: 1 addition & 1 deletion src/WIO/EdkBundle/EdkTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ class EdkTables
const ROUTE_TBL = 'cantiga_edk_routes';
const ROUTE_NOTE_TBL = 'cantiga_edk_route_notes';
const ROUTE_COMMENT_TBL = 'cantiga_edk_route_comments';

const REGISTRATION_SETTINGS_TBL = 'cantiga_edk_registration_settings';
const AREA_NOTE_TBL = 'cantiga_edk_area_notes';
}
5 changes: 3 additions & 2 deletions src/WIO/EdkBundle/Entity/EdkAreaNotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ public function saveEditableNote(Connection $conn, $type, $content)
$conn->delete(EdkTables::AREA_NOTE_TBL, array('routeId' => $this->getId(), 'noteType' => $type));
unset($this->notes[$type]);
} else {
$stmt = $conn->prepare('INSERT INTO `' . EdkTables::AREA_NOTE_TBL . '` (`areaId`, `noteType`, `content`) VALUES(:routeId, :noteType, :content)'
. ' ON DUPLICATE KEY UPDATE `content` = VALUES(`content`)');
$stmt = $conn->prepare('INSERT INTO `' . EdkTables::AREA_NOTE_TBL . '` (`areaId`, `noteType`, `content`, `lastUpdatedAt`) VALUES(:routeId, :noteType, :content, :lastUpdatedAt)'
. ' ON DUPLICATE KEY UPDATE `content` = VALUES(`content`), `lastUpdatedAt` = VALUES(`lastUpdatedAt`)');
$stmt->bindValue(':routeId', $this->area->getId());
$stmt->bindValue(':noteType', $type);
$stmt->bindValue(':content', $content);
$stmt->bindValue(':lastUpdatedAt', time());
$stmt->execute();
$this->notes[$type] = $content;
}
Expand Down
5 changes: 3 additions & 2 deletions src/WIO/EdkBundle/Entity/EdkRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,12 @@ public function saveEditableNote(Connection $conn, $type, $content)
$conn->delete(EdkTables::ROUTE_NOTE_TBL, array('routeId' => $this->getId(), 'noteType' => $type));
unset($this->notes[$type]);
} else {
$stmt = $conn->prepare('INSERT INTO `' . EdkTables::ROUTE_NOTE_TBL . '` (`routeId`, `noteType`, `content`) VALUES(:routeId, :noteType, :content)'
. ' ON DUPLICATE KEY UPDATE `content` = VALUES(`content`)');
$stmt = $conn->prepare('INSERT INTO `' . EdkTables::ROUTE_NOTE_TBL . '` (`routeId`, `noteType`, `content`, `lastUpdatedAt`) VALUES(:routeId, :noteType, :content, :lastUpdatedAt)'
. ' ON DUPLICATE KEY UPDATE `content` = VALUES(`content`), `lastUpdatedAt` = VALUES(`lastUpdatedAt`)');
$stmt->bindValue(':routeId', $this->getId());
$stmt->bindValue(':noteType', $type);
$stmt->bindValue(':content', $content);
$stmt->bindValue(':lastUpdatedAt', time());
$stmt->execute();
$this->notes[$type] = $content;
}
Expand Down
61 changes: 61 additions & 0 deletions src/WIO/EdkBundle/EventListener/ExportListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/*
* This file is part of Cantiga Project. Copyright 2015 Tomasz Jedrzejewski.
*
* Cantiga Project 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; either version 3 of the License, or
* (at your option) any later version.
*
* Cantiga Project 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 Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
namespace WIO\EdkBundle\EventListener;

use Cantiga\ExportBundle\Event\ExportEvent;
use WIO\EdkBundle\Repository\EdkExportRepository;

/**
* Handles the external data export.
*
* @author Tomasz Jędrzejewski
*/
class ExportListener
{
/**
* @var EdkExportRepository
*/
private $repo;

public function __construct(EdkExportRepository $repository)
{
$this->repo = $repository;
}


/**
* Handles the data export of additional area descriptions, routes and territories to the external systems.
*
* @param \WIO\EdkBundle\EventListener\ExportEvent $event
*/
public function onProjectExported(ExportEvent $event)
{
$areaBlock = $event->getBlock('area');

$territoryBlock = $this->repo->exportTerritories($event->getProjectId());
$routeBlock = $this->repo->exportRoutes($event->getLastExportAt(), $areaBlock->getIds());
$areaDescBlock = $this->repo->exportAreaDescriptions($event->getLastExportAt(), $areaBlock->getIds());
$routeDescBlock = $this->repo->exportRouteDescriptions($event->getLastExportAt(), $routeBlock->getIds());

$event->addBlock('territory', $territoryBlock);
$event->addBlock('route', $routeBlock);
$event->addBlock('areaDesc', $areaDescBlock);
$event->addBlock('routeDesc', $routeDescBlock);
}
}
3 changes: 2 additions & 1 deletion src/WIO/EdkBundle/EventListener/ModelListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use Cantiga\CoreBundle\Entity\Area;
use Cantiga\CoreBundle\Event\AreaEvent;
use Cantiga\ExportBundle\Event\ExportEvent;
use Cantiga\MilestoneBundle\Entity\NewMilestoneStatus;
use Cantiga\MilestoneBundle\Event\ActivationEvent;
use Cantiga\MilestoneBundle\MilestoneEvents;
Expand All @@ -39,7 +40,7 @@ class ModelListener
*/
private $eventDispatcher;

public function __construct(EventDispatcherInterface $eventDispatcher)
public function __construct(EventDispatcherInterface $eventDispatcher, EdkExportRepository $exportRepository)
{
$this->eventDispatcher = $eventDispatcher;
}
Expand Down
107 changes: 107 additions & 0 deletions src/WIO/EdkBundle/Repository/EdkExportRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/*
* This file is part of Cantiga Project. Copyright 2015 Tomasz Jedrzejewski.
*
* Cantiga Project 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; either version 3 of the License, or
* (at your option) any later version.
*
* Cantiga Project 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 Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
namespace WIO\EdkBundle\Repository;

use Cantiga\CoreBundle\CoreTables;
use Cantiga\ExportBundle\Entity\ExportBlock;
use Doctrine\DBAL\Connection;
use PDO;
use WIO\EdkBundle\EdkTables;

/**
* @author Tomasz Jędrzejewski
*/
class EdkExportRepository
{
/**
* @var Connection
*/
private $conn;

public function __construct(Connection $conn)
{
$this->conn = $conn;
}

public function exportTerritories($projectId)
{
$stmt = $this->conn->prepare('SELECT `id`, `name` FROM `'.CoreTables::TERRITORY_TBL.'` WHERE `projectId` = :id');
$stmt->bindValue(':id', $projectId);
$stmt->execute();

$block = new ExportBlock();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$block->addId($row['id']);
$block->addUpdate($row);
}
$stmt->closeCursor();
return $block;
}

public function exportAreaDescriptions($lastExport, $areaIds)
{
$block = new ExportBlock();
if (sizeof($areaIds) > 0) {
$stmt = $this->conn->query('SELECT `areaId`, `noteType`, `content`, `lastUpdatedAt` FROM `'.EdkTables::AREA_NOTE_TBL.'` WHERE `areaId` IN ('.implode(',', $areaIds).') ');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$block->addId(['areaId' => $row['areaId'], 'noteType' => $row['noteType']]);
if ($row['lastUpdatedAt'] > $lastExport) {
$block->addUpdate($row);
}
}
$stmt->closeCursor();
}
return $block;
}

public function exportRoutes($lastExport, $areaIds)
{
$block = new ExportBlock();
if (sizeof ($areaIds) > 0) {
$stmt = $this->conn->query('SELECT r.*, a.territoryId, x.registrationType, x.startTime, x.endTime, x.externalRegistrationUrl FROM `'.EdkTables::ROUTE_TBL.'` r '
. 'INNER JOIN `'.CoreTables::AREA_TBL.'` a ON a.`id` = r.`areaId` '
. 'LEFT JOIN `'.EdkTables::REGISTRATION_SETTINGS_TBL.'` x ON x.`routeId` = r.`id` '
. 'WHERE r.`areaId` IN ('.implode(',', $areaIds).') AND r.`approved` = 1');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$block->addId($row['id']);
if ($row['updatedAt'] > $lastExport) {
$block->addUpdate($row);
}
}
$stmt->closeCursor();
}
return $block;
}

public function exportRouteDescriptions($lastExport, $routeIds)
{
$block = new ExportBlock();
if (sizeof ($routeIds) > 0) {
$stmt = $this->conn->query('SELECT `routeId`, `noteType`, `content`, `lastUpdatedAt` FROM `'.EdkTables::ROUTE_NOTE_TBL.'` WHERE `routeId` IN ('.implode(',', $routeIds).') ');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$block->addId(['routeId' => $row['routeId'], 'noteType' => $row['noteType']]);
if ($row['lastUpdatedAt'] > $lastExport) {
$block->addUpdate($row);
}
}
$stmt->closeCursor();
}
return $block;
}
}
8 changes: 8 additions & 0 deletions src/WIO/EdkBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ services:
wio.edk.repo.route:
class: WIO\EdkBundle\Repository\EdkRouteRepository
arguments: ["@database_connection", "@cantiga.transaction", "@event_dispatcher", "@cantiga.time", "@cantiga.files"]
wio.edk.repo.export:
class: WIO\EdkBundle\Repository\EdkExportRepository
arguments: ["@database_connection"]
wio.edk.extension.area_information_map:
class: WIO\EdkBundle\Extension\AreaInformationMapExtension
arguments: ["@translator"]
Expand All @@ -25,6 +28,11 @@ services:
arguments: ["@event_dispatcher"]
tags:
- { name: kernel.event_listener, event: cantiga.area.updated, method: onAreaUpdated }
wio.edk.export_listener:
class: WIO\EdkBundle\EventListener\ExportListener
arguments: ["@wio.edk.repo.export"]
tags:
- { name: kernel.event_listener, event: cantiga.export.ongoing, method: onProjectExported }
wio.edk.workspace_listener:
class: WIO\EdkBundle\EventListener\WorkspaceListener
tags:
Expand Down

0 comments on commit 1527847

Please sign in to comment.