Commit 45776729 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

SynchroService: log des synchros effectuées dans la nouvelle table SYNCHRO_LOG.

parent 0382133f
Loading
Loading
Loading
Loading
Loading
+16 −0
Changes for module/Import/src/Import/Model/Mapping/Import.Model.SynchroLog.dcm.xml: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="Import\Model\SynchroLog" table="SYNCHRO_LOG">
        <id name="id" type="integer" column="ID">
            <generator strategy="SEQUENCE"/>
        </id>
        <field name="logDate" column="LOG_DATE" type="datetime"/>
        <field name="startDate" column="START_DATE" type="datetime"/>
        <field name="finishDate" column="FINISH_DATE" type="datetime"/>
        <field name="status" column="STATUS"/>
        <field name="sql" column="SQL"/>
        <field name="message" column="MESSAGE"/>
    </entity>
</doctrine-mapping>
 No newline at end of file
+147 −0
Changes for module/Import/src/Import/Model/SynchroLog.php: 147 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Import\Model;

/**
 * SynchroLog
 */
class SynchroLog
{
    private $id;

    private $logDate;

    private $startDate;

    private $finishDate;

    private $status;

    private $sql;

    private $message;

    /**
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return \DateTime
     */
    public function getLogDate()
    {
        return $this->logDate;
    }

    /**
     * @param \DateTime $logDate
     * @return self
     */
    public function setLogDate(\DateTime $logDate)
    {
        $this->logDate = $logDate;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getStartDate()
    {
        return $this->startDate;
    }

    /**
     * @param \DateTime $startDate
     * @return self
     */
    public function setStartDate(\DateTime $startDate)
    {
        $this->startDate = $startDate;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getFinishDate()
    {
        return $this->finishDate;
    }

    /**
     * @param \DateTime $finishDate
     * @return self
     */
    public function setFinishDate(\DateTime $finishDate)
    {
        $this->finishDate = $finishDate;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param mixed $status
     * @return self
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * @return string
     */
    public function getSql()
    {
        return $this->sql;
    }

    /**
     * @param string $sql
     * @return self
     */
    public function setSql($sql)
    {
        $this->sql = $sql;

        return $this;
    }

    /**
     * @return string
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * @param string $message
     * @return self
     */
    public function setMessage($message)
    {
        $this->message = $message;

        return $this;
    }

}
+48 −2
Changes for module/Import/src/Import/Service/SynchroService.php: 48 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,11 @@

namespace Import\Service;

use DateTime;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\OptimisticLockException;
use Import\Model\SynchroLog;
use UnicaenApp\Exception\LogicException;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
@@ -68,11 +72,28 @@ class SynchroService
    {
        $plsql = implode(PHP_EOL, array_merge(['BEGIN'], $calls, ['END;']));

        $startDate = new DateTime();

        $connection = $this->entityManager->getConnection();
        $connection->beginTransaction();
        try {
            $this->entityManager->getConnection()->executeQuery($plsql);
            $connection->executeQuery($plsql);
            $connection->commit();
            $status = 'SUCCESS';
            $message = null;
        } catch (DBALException $e) {
            throw new RuntimeException("Erreur rrencontrée lors de l'exécution des procédures de synchro (import)", null, $e);
            $status = 'FAILURE';
            $message = "Erreur rencontrée lors de l'exécution des procédures de synchro (import) : " . PHP_EOL .
                $e->getTraceAsString();
            try {
                $connection->rollBack();
            } catch (ConnectionException $e) {
                $message .= PHP_EOL . "Et en plus le rollback a échoué!";
            }
        }

        $finishDate = new DateTime();
        $this->log($startDate, $finishDate, $plsql, $status, $message);
    }

    /**
@@ -161,4 +182,29 @@ EOS;

        return sprintf($template, str_replace("'", "''", $plsql));
    }

    /**
     * @param DateTime $startDate
     * @param DateTime $finishDate
     * @param string    $sql
     * @param string    $status
     * @param string    $message
     */
    private function log(DateTime $startDate, DateTime $finishDate, $sql, $status, $message = null)
    {
        $log = (new SynchroLog())
            ->setLogDate(new DateTime())
            ->setStartDate($startDate)
            ->setFinishDate($finishDate)
            ->setSql($sql)
            ->setStatus($status)
            ->setMessage($message);

        $this->entityManager->persist($log);
        try {
            $this->entityManager->flush($log);
        } catch (OptimisticLockException $e) {
            throw new RuntimeException("Erreur rencontrée lors de l'enregistrement du SynchroLog.", null, $e);
        }
    }
}
 No newline at end of file