Commit be3a1b5e authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

Mise à jour de la commande de synchronisation des organisations, le rapport...

Mise à jour de la commande de synchronisation des organisations, le rapport est plus clair, un mode verbeux est disponible avec l'option -s
parent 36d66776
Loading
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ class OscarOrganizationsSyncCommand extends OscarCommandAbstract
        $this
            ->setDescription("Execute la synchronisation des organisations")
            ->addArgument("connectorname", InputArgument::REQUIRED, "Connector (rest)")
            ->addOption('fullrepport', 's', InputOption::VALUE_NONE, "Rapport complet")
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Forcer la mise à jour')
            ->addOption(
                'no-rebuild',
@@ -60,6 +61,7 @@ class OscarOrganizationsSyncCommand extends OscarCommandAbstract
        $connectorName = $input->getArgument('connectorname');
        $noRebuild = $input->getOption('no-rebuild');
        $force = $input->getOption('force');
        $fullRepport = $input->getOption('fullrepport');

        /** @var OscarConfigurationService $oscarConfig */
        $oscarConfig = $this->getServicemanager()->get(OscarConfigurationService::class);
@@ -78,10 +80,14 @@ class OscarOrganizationsSyncCommand extends OscarCommandAbstract
            foreach ($repport->getRepportStates() as $type => $out) {
                $short = substr($type, 0, 3);
                $io->section("Opération " . strtoupper($type));
                if( $fullRepport === false && !in_array($type, ['errors', 'warnings']) ){
                    $io->writeln("Nbr d'opération : " . count($out));
                } else {
                    foreach ($out as $line) {
                        $io->writeln("$short\t " . date('Y-m-d H:i:s', $line['time']) . " " . $line['message']);
                    }
                }
            }
        } catch (\Exception $e) {
            $noRebuild = true;
            $io->error($e->getMessage());
+65 −14
Original line number Diff line number Diff line
@@ -97,6 +97,11 @@ class ConnectorOrganizationREST extends AbstractConnector
        return $this->getOrganizationService()->getLoggerService();
    }

    private function errorOut(ConnectorRepport $repport, string $message):void {
        $repport->adderror($message);
        $this->getLogger()->error($message);
    }

    /**
     * @param OrganizationRepository $repository
     * @param bool $force
@@ -111,12 +116,9 @@ class ConnectorOrganizationREST extends AbstractConnector
        $url = $this->getParameter('url_organizations');
        $repport->addnotice("URL : $url");

        /////////////////////////////////////
        ////// Patch 2.7 "Lewis" GIT#286 ////
        try {
            $json = $this->getAccessStrategy()->getDataAll();
            $exist = $repository->getUidsConnector($this->getName());
            $jsonDatas = null;

            if( is_object($json) && property_exists($json, 'organizations') ){
                $jsonDatas = $json->organizations;
@@ -144,7 +146,13 @@ class ConnectorOrganizationREST extends AbstractConnector
                    $organization = $repository->newPersistantObject();
                    $action = "add";
                } catch (NonUniqueResultException $e){
                    $this->getLogger()->error("L'organisation avec le code '$organisationId' n'est pas unique.");
                    $message = "L'organisation avec le code '$organisationId' n'est pas unique.";
                    $this->getLogger()->error($message);
                    $repport->adderror($message);
                    continue;
                } catch (\Exception $e) {
                    $this->errorOut($repport, "Problème inconnue avec '$organisationId' : ".$e->getMessage());
                    continue;
                }
                if( !property_exists($data, 'dateupdated') ){
                    $dateupdated = date('Y-m-d H:i:s');
@@ -153,34 +161,75 @@ class ConnectorOrganizationREST extends AbstractConnector
                }

                if($organization->getDateUpdated() < new \DateTime($dateupdated) || $force == true ){

                    try {
                        $organization = $this->hydrateWithDatas($organization, $data);
                    if( property_exists($data, 'type') )
                        $organization->setTypeObj($repository->getTypeObjByLabel($data->type));
                        if( property_exists($data, 'type') ){
                            try {
                                $type = $repository->getTypeObjByLabel($data->type);
                                $organization->setTypeObj($type);
                            } catch (\Exception $e){
                                $this->errorOut($repport, "Impossible de mettre à jour le type pour $organisationId, le type '".$data->type."' n'a pas été trouvédans Oscar : " . $e->getMessage());
                            }
                        }

                        $organization->setDateUpdated(new \DateTime($dateupdated));
                        $repository->flush($organization);
                    } catch (\Exception $e){
                        $this->errorOut($repport, "Impossible de mettre à jour les données de base de '$organisationId' : " . $e->getMessage());
                        continue;
                    }

                    if( $organization->hasUpdatedParentInCycle() ){

                        $this->getLogger()->debug("Mise à jour PARENT");
                        try {
                            $newParentCode = $organization->getUpdatedParentInCycle();
                            if( $newParentCode ){
                                $parent = $this->getOrganizationService()->getOrganizationRepository()->getOrganisationByCode($newParentCode)->getId();
                                $parent = null;
                                try {
                                    $parent = $this->getOrganizationService()
                                        ->getOrganizationRepository()
                                        ->getOrganisationByCode($newParentCode)
                                        ->getId();
                                } catch (\Exception $e) {
                                    $message = "Impossible d'ajouter le parent '$newParentCode' pour l'organisation '$organisationId', le parent n'existe pas dans OSCAR";
                                    $repport->addwarning($message);
                                    $this->getLogger()->warning("$message : " . $e->getMessage());
                                }
                                if( $parent ){
                                    try {
                                        $this->getOrganizationService()->saveSubStructure($parent, $organization->getId());
                                    }catch (\Exception $e) {
                                        $this->errorOut("Un problème est survenu lors de l'enregistrement du parent '$newParentCode' dans l'organisation $organisationId' : " . $e->getMessage());
                                        continue;
                                    }

                                }
                            } else {
                                try {
                                    $this->getOrganizationService()->removeSubStructure(null, $organization->getId());
                                } catch (\Exception $e) {
                                    $this->errorOut("Un problème est survenu lors de la suppression du parent '$newParentCode' dans l'organisation $organisationId' : " . $e->getMessage());
                                    continue;
                                }
                            }
                            $repository->flush($organization);
                        } catch (\Exception $e) {
                            $this->getLogger()->error("Erreur : " . $e->getMessage());
                            $this->getLogger()->error("Erreur inconnue : " . $e->getMessage());
                        }
                    }

                    if( $action == 'add' ){
                        $this->getLogger()->info("Organisation '$organisationId' ajoutée");
                        $message = "Organisation '$organisationId' ajoutée";
                        $this->getLogger()->info($message);
                        $repport->addadded($message);
                    } else {
                        $this->getLogger()->info("Organisation '$organisationId' mise à jour");
                        $message = "Organisation '$organisationId' mise à jour";
                        $this->getLogger()->info($message);
                        $repport->addupdated($message);
                    }
                } else {
                    $repport->addnothing("Organisation '$organisationId' déjà à jour");
                }
            }

@@ -192,13 +241,14 @@ class ConnectorOrganizationREST extends AbstractConnector
                }
                try {
                    $organization = $repository->getObjectByConnectorID($this->getName(), $uid);
                    $this->getLogger()->info("'$organization' n'est plus présent dans les données du connecteur");

                    if ($this->getOptionPurge()) {
                        $idsToDelete[] = $organization->getId();
                    } else {
                        $repport->addnotice("'$uid' n'est plus dans le connecteur");
                    }
                } catch (\Exception $e) {
                    $this->getLogger()->error($e->getMessage());
                    $this->errorOut("Problème avec l'organisation $uid : " . $e->getMessage());
                }
            }
            foreach ($idsToDelete as $id) {
@@ -211,11 +261,12 @@ class ConnectorOrganizationREST extends AbstractConnector
                }
            }
        } catch (\Exception $e ){
            $repport->adderror($e->getMessage());
            $this->errorOut("Erreur inattendue : " . $e->getMessage());
            throw $e;
        }

        $repport->addnotice("FIN du traitement...");
        $repport->addnotice("DUREE : " . $repport->getDuration() . " secondes");
        return $repport;
    }

+18 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ class ConnectorRepport
    private $added;
    private $updated;
    private $removed;
    private $nothing;

    private $start;
    private $end;
@@ -29,6 +30,7 @@ class ConnectorRepport
            'warnings' => $this->getWarnings(),
            'removed' => $this->getRemoved(),
            'notices' => $this->getNotices(),
            'nothing' => $this->getNothing(),
        ];
    }

@@ -80,6 +82,11 @@ class ConnectorRepport
        return $this->removed;
    }

    public function getNothing()
    {
        return $this->nothing;
    }

    public function isSuspect(){
        return count($this->getErrors()) + count($this->getWarnings()) > 0;
    }
@@ -97,11 +104,12 @@ class ConnectorRepport
        $this->added = [];
        $this->updated = [];
        $this->removed = [];
        $this->nothing = [];
        $this->start();
    }

    public function start(){
        $this->start = time();
        $this->start = microtime(true);
    }

    public function addnotice( $message ){
@@ -146,6 +154,13 @@ class ConnectorRepport
        ];
    }

    public function addnothing( $message ){
        $this->nothing[] = [
            'time' => time(),
            'message' => '[-] ' . $message
        ];
    }

    public function addRepport( ConnectorRepport $repport ){
        foreach ( $repport->getRepportStates() as $state=>$rep ){
            $this->$state = array_merge($this->$state, $rep);
@@ -153,12 +168,12 @@ class ConnectorRepport
    }

    public function end(){
        $this->end = time();
        $this->end = microtime(true);
    }

    public function getDuration(){
        if( !$this->end ){
            return time() - $this->start;
            return microtime(true) - $this->start;
        }
        return $this->end - $this->start;
    }