Commit 7d3a04c4 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

WIP API

parent 9b004c67
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -736,7 +736,6 @@ const CONFIG_SYNCHROS = [
            'table' => 'INDIVIDU',
            'connection' => 'default',
            'source_code_column' => 'SOURCE_CODE',
//            'where'              => "d.source_code like 'UCN::%'", // todo: à virer
        ],
    ],
    [
+5 −4
Original line number Diff line number Diff line
@@ -34,8 +34,9 @@ create table tmp_inscription_administrative
(
    id bigserial,
    insert_date timestamp(0) default ('now'::text)::timestamp without time zone,

    source_id bigint not null,
    source_code varchar(64) not null, -- Id Pegase inscription (merge request en cours)
    source_code varchar(128) not null, -- Id Pegase inscription (merge request en cours)

    doctorant_id varchar(64) not null,
    ecole_doct_id varchar(32),
@@ -79,12 +80,12 @@ alter table tmp_inscription_administrative
    add constraint tmp_inscription_administrative_source_id_fk
        foreign key (source_id) references source;


--drop table inscription_administrative;
create table inscription_administrative
(
    id bigserial constraint inscription_administrative_pkey primary key,
    source_id bigint not null constraint inscription_administrative_source_fk references source,
    source_code varchar(64) not null,
    source_code varchar(128) not null,
    doctorant_id bigint not null constraint inscription_administrative_doctorant_fk references doctorant,
    ecole_doct_id bigint constraint inscription_administrative_ecole_doct_fk references ecole_doct,
    no_candidat varchar(32),
@@ -136,7 +137,7 @@ create index inscription_administrative_hdfk_idx
create index inscription_administrative_hmfk_idx
    on inscription_administrative (histo_modificateur_id);


--drop view src_inscription_administrative;
create or replace view src_inscription_administrative as
SELECT null::bigint as id,
    tmp.source_code,
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ return [
    'service_manager' => [
        'factories' => [
            \SygalApiImpl\V1\Rest\InscriptionAdministrative\InscriptionAdministrativeResource::class => \SygalApiImpl\V1\Rest\InscriptionAdministrative\InscriptionAdministrativeResourceFactory::class,
            \SygalApiImpl\V1\Rest\InscriptionAdministrative\Service\ImportFacade::class => \SygalApiImpl\V1\Rest\InscriptionAdministrative\Service\ImportFacadeFactory::class
            \SygalApiImpl\V1\Rest\InscriptionAdministrative\Facade\ImportFacade::class => \SygalApiImpl\V1\Rest\InscriptionAdministrative\Facade\ImportFacadeFactory::class
        ],
    ],
    'bjyauthorize'    => [
+61 −0
Original line number Diff line number Diff line
<?php

namespace SygalApiImpl\V1\Facade;

use Doctrine\DBAL\Connection;
use stdClass;
use UnicaenDbImport\Domain\Import;
use UnicaenDbImport\Domain\Synchro;
use UnicaenDbImport\Service\ImportService;
use UnicaenDbImport\Service\SynchroService;

abstract class AbstractImportFacade
{
    protected Connection $destinationConnection;
    protected ImportService $importService;
    protected SynchroService $synchroService;

    public function setImportService(ImportService $importService): void
    {
        $this->importService = $importService;
    }

    public function setSynchroService(SynchroService $synchroService): void
    {
        $this->synchroService = $synchroService;
    }

    public function setDestinationConnection(Connection $destinationConnection): void
    {
        $this->destinationConnection = $destinationConnection;
    }

    /**
     * @throws \Exception
     */
    abstract public function import(stdClass $data);

    /**
     * @throws \Exception
     */
    protected function runImport(Import $import)
    {
        $result = $this->importService->runImport($import);

        if ($exception = $result->getFailureException()) {
            throw $exception;
        }
    }

    /**
     * @throws \Exception
     */
    protected function runSynchro(Synchro $import)
    {
        $result = $this->synchroService->runSynchro($import);

        if ($exception = $result->getFailureException()) {
            throw $exception;
        }
    }
}
 No newline at end of file
+132 −47
Original line number Diff line number Diff line
<?php

namespace SygalApiImpl\V1\Rest\InscriptionAdministrative\Service;
namespace SygalApiImpl\V1\Rest\InscriptionAdministrative\Facade;

use Doctrine\DBAL\Connection;
use Exception;
use Import\Filter\PrefixEtabColumnValueFilter;
use stdClass;
use SygalApi\V1\Rest\InscriptionAdministrative\Extractor\DoctorantExtractor;
use SygalApi\V1\Rest\InscriptionAdministrative\Extractor\IndividuExtractor;
use SygalApi\V1\Rest\InscriptionAdministrative\Extractor\InscriptionExtractor;
use SygalApiImpl\V1\Facade\AbstractImportFacade;
use UnicaenDbImport\Connection\ApiConnection;
use UnicaenDbImport\Domain\Destination;
use UnicaenDbImport\Domain\Import;
use UnicaenDbImport\Domain\Operation;
use UnicaenDbImport\Domain\Source;
use UnicaenDbImport\Service\ImportService;
use UnicaenDbImport\Domain\Synchro;

class ImportFacade
class ImportFacade extends AbstractImportFacade
{
    private Connection $destinationConnection;
    private ImportService $importService;
    protected IndividuExtractor $individuExtractor;
    protected DoctorantExtractor $doctorantExtractor;
    protected InscriptionExtractor $inscriptionExtractor;

    public function setImportService(ImportService $importService): void
    public function __construct()
    {
        $this->importService = $importService;
    }

    public function setDestinationConnection(Connection $destinationConnection): void
    {
        $this->destinationConnection = $destinationConnection;
        $this->individuExtractor = new IndividuExtractor();
        $this->doctorantExtractor = new DoctorantExtractor();
        $this->inscriptionExtractor = new InscriptionExtractor();
    }

    /**
@@ -52,12 +51,21 @@ class ImportFacade
     */
    private function importIndividu(stdClass $data)
    {
        $preparedData = $this->individuExtractor->extract($data);
        try {
            $import = $this->createImportIndividu($data);
            $import = $this->createImportIndividu($preparedData);
            $this->runImport($import);
        } catch (Exception $e) {
            throw new Exception("Une erreur est survenue lors de l'import de l'individu : " . $e->getMessage(), null, $e);
        }

        $sourceId = $preparedData['source_id'];
        try {
            $synchro = $this->createSynchroIndividu($sourceId);
            $this->runSynchro($synchro);
        } catch (Exception $e) {
            throw new Exception("Une erreur est survenue lors de la synchro de l'individu : " . $e->getMessage(), null, $e);
        }
    }

    /**
@@ -65,12 +73,21 @@ class ImportFacade
     */
    private function importDoctorant(stdClass $data)
    {
        $preparedData = $this->doctorantExtractor->extract($data);
        try {
            $import = $this->createImportDoctorant($data);
            $import = $this->createImportDoctorant($preparedData);
            $this->runImport($import);
        } catch (Exception $e) {
            throw new Exception("Une erreur est survenue lors de l'import du doctorant : " . $e->getMessage(), null, $e);
        }

        $sourceId = $preparedData['source_id'];
        try {
            $synchro = $this->createSynchroDoctorant($sourceId);
            $this->runSynchro($synchro);
        } catch (Exception $e) {
            throw new Exception("Une erreur est survenue lors de la synchro du doctorant : " . $e->getMessage(), null, $e);
        }
    }

    /**
@@ -78,25 +95,35 @@ class ImportFacade
     */
    private function importInscription(stdClass $data)
    {
        $preparedData = $this->inscriptionExtractor->extract($data);
        try {
            $import = $this->createImportInscription($data);
            $import = $this->createImportInscription($preparedData);
            $this->runImport($import);
        } catch (Exception $e) {
            error_log($e);
            throw new Exception("Une erreur est survenue lors de l'import de l'inscription : " . $e->getMessage(), null, $e);
        }

        $sourceId = $preparedData['source_id'];
        try {
            $synchro = $this->createSynchroInscription($sourceId);
            $this->runSynchro($synchro);
        } catch (Exception $e) {
            throw new Exception("Une erreur est survenue lors de la synchro de l'inscription : " . $e->getMessage(), null, $e);
        }
    }

    /**
     * @throws \UnicaenDbImport\Config\ConfigException
     */
    private function createImportIndividu(stdClass $data): Import
    private function createImportIndividu(array $data): Import
    {
        $filter = new PrefixEtabColumnValueFilter([
            'source_code',
            //'source_id', // pas le source_id !
        ]);
        $filter->setParams([PrefixEtabColumnValueFilter::PARAM_CODE_ETABLISSEMENT => $data->instancePegase]);
        $sourceId = $data['source_id'];
        $filter->setParams([PrefixEtabColumnValueFilter::PARAM_CODE_ETABLISSEMENT => $sourceId]);

        $source = Source::fromConfig([
            'name' => 'vxcvxcvxc',
@@ -113,8 +140,7 @@ class ImportFacade
            'id_sequence' => false,
        ]);

        $preparedData = $this->prepareDataForImportIndividu($data);
        $source->setData([$preparedData]);
        $source->setData([$data]);

        return Import::fromConfig([
            'name' => 'individu',
@@ -123,24 +149,52 @@ class ImportFacade
        ]);
    }

    private function prepareDataForImportIndividu(stdClass $data): array
    /**
     * @throws \UnicaenDbImport\Config\ConfigException
     */
    private function createSynchroIndividu(string $sourceId): Synchro
    {
        $hydrator = new IndividuExtractor();
        $where = "d.source_id = ( select id from source where code = '$sourceId' )";

        return $hydrator->extract($data);
        $source = Source::fromConfig([
            'name' => 'Application',
            'code' => 'app',
            'table' => 'src_individu',
            'connection' => $this->destinationConnection,
            'source_code_column' => 'source_code',
        ]);
        $destination = Destination::fromConfig([
            'name' => 'Application',
            'table' => 'individu',
            'connection' => $this->destinationConnection,
            'source_code_column' => 'source_code',
            'where' => $where,
        ]);

        return Synchro::fromConfig([
            'name' => uniqid('synchro_'),
            'source' => $source,
            'destination' => $destination,
            'operations' => [
                Operation::OPERATION_INSERT,
                Operation::OPERATION_UPDATE,
                Operation::OPERATION_UNDELETE,
            ],
        ]);
    }

    /**
     * @throws \UnicaenDbImport\Config\ConfigException
     */
    private function createImportDoctorant(stdClass $data): Import
    private function createImportDoctorant(array $data): Import
    {
        $filter = new PrefixEtabColumnValueFilter([
            'source_code',
            //'source_id', // pas le source_id !
            'individu_id',
        ]);
        $filter->setParams([PrefixEtabColumnValueFilter::PARAM_CODE_ETABLISSEMENT => $data->instancePegase]);
        $sourceId = $data['source_id'];
        $filter->setParams([PrefixEtabColumnValueFilter::PARAM_CODE_ETABLISSEMENT => $sourceId]);

        $source = Source::fromConfig([
            'name' => 'vxcvxcvxc',
@@ -157,8 +211,7 @@ class ImportFacade
            'id_sequence' => false,
        ]);

        $preparedData = $this->prepareDataForImportDoctorant($data);
        $source->setData([$preparedData]);
        $source->setData([$data]);

        return Import::fromConfig([
            'name' => 'doctorant',
@@ -167,17 +220,39 @@ class ImportFacade
        ]);
    }

    private function prepareDataForImportDoctorant(stdClass $data): array
    /**
     * @throws \UnicaenDbImport\Config\ConfigException
     */
    private function createSynchroDoctorant(string $sourceId): Synchro
    {
        $hydrator = new DoctorantExtractor();
        $where = "d.source_id = ( select id from source where code = '$sourceId' )";

        return $hydrator->extract($data);
        $source = Source::fromConfig([
            'name' => 'Application',
            'code' => 'app',
            'table' => 'SRC_DOCTORANT',
            'connection' => $this->destinationConnection,
            'source_code_column' => 'source_code',
        ]);
        $destination = Destination::fromConfig([
            'name' => 'Application',
            'table' => 'DOCTORANT',
            'connection' => $this->destinationConnection,
            'source_code_column' => 'source_code',
            'where' => $where,
        ]);

        return Synchro::fromConfig([
            'name' => uniqid('synchro_'),
            'source' => $source,
            'destination' => $destination,
        ]);
    }

    /**
     * @throws \UnicaenDbImport\Config\ConfigException
     */
    private function createImportInscription(stdClass $data): Import
    private function createImportInscription(array $data): Import
    {
        $filter = new PrefixEtabColumnValueFilter([
            'source_code',
@@ -185,7 +260,8 @@ class ImportFacade
            'ecole_doctorale_id',
            //'source_id', // pas le source_id !
        ]);
        $filter->setParams([PrefixEtabColumnValueFilter::PARAM_CODE_ETABLISSEMENT => $data->instancePegase]);
        $sourceId = $data['source_id'];
        $filter->setParams([PrefixEtabColumnValueFilter::PARAM_CODE_ETABLISSEMENT => $sourceId]);

        $source = Source::fromConfig([
            'name' => 'vxcvxcvxc',
@@ -202,8 +278,7 @@ class ImportFacade
            'id_sequence' => false,
        ]);

        $preparedData = $this->prepareDataForImportInscription($data);
        $source->setData([$preparedData]);
        $source->setData([$data]);

        return Import::fromConfig([
            'name' => 'inscription',
@@ -212,22 +287,32 @@ class ImportFacade
        ]);
    }

    private function prepareDataForImportInscription(stdClass $data): array
    {
        $hydrator = new InscriptionExtractor();

        return $hydrator->extract($data);
    }

    /**
     * @throws \Exception
     * @throws \UnicaenDbImport\Config\ConfigException
     */
    private function runImport(Import $import)
    private function createSynchroInscription(string $sourceId): Synchro
    {
        $result = $this->importService->runImport($import);
        $where = "d.source_id = ( select id from source where code = '$sourceId' )";

        if ($exception = $result->getFailureException()) {
            throw $exception;
        }
        $source = Source::fromConfig([
            'name' => 'Application',
            'code' => 'app',
            'table' => 'src_inscription_administrative',
            'connection' => $this->destinationConnection,
            'source_code_column' => 'source_code',
        ]);
        $destination = Destination::fromConfig([
            'name' => 'Application',
            'table' => 'inscription_administrative',
            'connection' => $this->destinationConnection,
            'source_code_column' => 'source_code',
            'where' => $where,
        ]);

        return Synchro::fromConfig([
            'name' => uniqid('synchro_'),
            'source' => $source,
            'destination' => $destination,
        ]);
    }
}
 No newline at end of file
Loading