Skip to content

Commit

Permalink
feat: migration du catalogue de styles (annexe styles.json) vers extra
Browse files Browse the repository at this point in the history
  • Loading branch information
ocruze committed Jan 8, 2025
1 parent cc5b0e6 commit 329c935
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 87 deletions.
77 changes: 13 additions & 64 deletions src/Controller/Entrepot/StyleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use App\Exception\CartesApiException;
use App\Services\EntrepotApi\AnnexeApiService;
use App\Services\EntrepotApi\CartesMetadataApiService;
use App\Services\EntrepotApi\CartesServiceApiService;
use App\Services\EntrepotApi\ConfigurationApiService;
use App\Services\EntrepotApi\DatastoreApiService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -29,6 +29,7 @@ public function __construct(
private ConfigurationApiService $configurationApiService,
private AnnexeApiService $annexeApiService,
private CartesMetadataApiService $cartesMetadataApiService,
private CartesServiceApiService $cartesServiceApiService,
) {
}

Expand All @@ -49,14 +50,7 @@ public function add(string $datastoreId, string $offeringId, Request $request):
$datasheetName = $configuration['tags'][CommonTags::DATASHEET_NAME];

// Recuperation des styles de la configuration
$path = "/configuration/$configId/styles.json";
$styleAnnexes = $this->annexeApiService->getAll($datastoreId, null, $path);

$styles = [];
if (count($styleAnnexes)) {
$content = $this->annexeApiService->download($datastoreId, $styleAnnexes[0]['_id']);
$styles = json_decode($content, true);
}
$styles = $this->cartesServiceApiService->getStyles($datastoreId, $configuration);

// Suppression du style courant
$this->cleanStyleTags($styles);
Expand All @@ -77,9 +71,7 @@ public function add(string $datastoreId, string $offeringId, Request $request):
$styles[] = $newStyle;
$this->_addUrls($datastore, $styles);

// Re ecriture dans le fichier
$annexeId = count($styleAnnexes) ? $styleAnnexes[0]['_id'] : null;
$this->writeStyleFile($datastoreId, $annexeId, $styles, $path);
$this->updateStyles($datastoreId, $configuration['_id'], $styles);

try {
$this->cartesMetadataApiService->updateStyleFiles($datastoreId, $datasheetName);
Expand Down Expand Up @@ -111,14 +103,7 @@ public function remove(string $datastoreId, string $offeringId, Request $request
$datasheetName = $configuration['tags'][CommonTags::DATASHEET_NAME];

// Recuperation des styles de la configuration
$path = "/configuration/$configId/styles.json";
$styleAnnexes = $this->annexeApiService->getAll($datastoreId, null, $path);

$styles = [];
if (0 != count($styleAnnexes)) {
$content = $this->annexeApiService->download($datastoreId, $styleAnnexes[0]['_id']);
$styles = json_decode($content, true);
}
$styles = $this->cartesServiceApiService->getStyles($datastoreId, $configuration);

// Recuperation du style
$style = array_values(array_filter($styles, static function ($style) use ($styleName) {
Expand Down Expand Up @@ -149,16 +134,7 @@ public function remove(string $datastoreId, string $offeringId, Request $request
$styles[0]['current'] = true;
}

// Plus de style, on supprime le fichier
$annexeId = count($styleAnnexes) ? $styleAnnexes[0]['_id'] : null;
if ($annexeId && 0 == count($styles)) {
$this->annexeApiService->remove($datastoreId, $styleAnnexes[0]['_id']);

return new JsonResponse([]);
}

// Ecriture des styles mis a jour
$this->writeStyleFile($datastoreId, $annexeId, $styles, $path);
$this->updateStyles($datastoreId, $configuration['_id'], $styles);

try {
$this->cartesMetadataApiService->updateStyleFiles($datastoreId, $datasheetName);
Expand All @@ -185,16 +161,10 @@ public function setCurrentStyle(string $datastoreId, string $offeringId, Request
$offering = $this->configurationApiService->getOffering($datastoreId, $offeringId);

$configId = $offering['configuration']['_id'];
$configuration = $this->configurationApiService->get($datastoreId, $configId);

// Recuperation des styles de la configuration
$path = "/configuration/$configId/styles.json";
$styleAnnexes = $this->annexeApiService->getAll($datastoreId, null, $path);

$styles = [];
if (0 != count($styleAnnexes)) {
$content = $this->annexeApiService->download($datastoreId, $styleAnnexes[0]['_id']);
$styles = json_decode($content, true);
}
$styles = $this->cartesServiceApiService->getStyles($datastoreId, $configuration);

// Recuperation du style
$style = array_filter($styles, static function ($style) use ($styleName) {
Expand All @@ -208,10 +178,8 @@ public function setCurrentStyle(string $datastoreId, string $offeringId, Request
$this->cleanStyleTags($styles);
$this->setCurrent($styles, $styleName);

$annexeId = count($styleAnnexes) ? $styleAnnexes[0]['_id'] : null;

// Ecriture des styles mis a jour
$this->writeStyleFile($datastoreId, $annexeId, $styles, $path);
$this->updateStyles($datastoreId, $configuration['_id'], $styles);

return new JsonResponse($styles);
} catch (ApiException $ex) {
Expand Down Expand Up @@ -292,32 +260,13 @@ private function setCurrent(&$styles, $styleName)
}

/**
* Ecriture du nouveau fichier de style.
* Mise à jour des styles dans extra.
*
* @param string $datastoreId
* @param string $annexeId
* @param array<mixed> $styles
* @param string $path
*/
private function writeStyleFile($datastoreId, $annexeId, $styles, $path): void
private function updateStyles(string $datastoreId, string $configurationId, array $styles): void
{
$fs = new Filesystem();

$directory = $this->getParameter('style_files_path');
if (!$fs->exists($directory)) {
$fs->mkdir($directory);
}

$uuid = uniqid();
$filePath = join(DIRECTORY_SEPARATOR, [realpath($directory), "style-$uuid.json"]);

// Creation du fichier
file_put_contents($filePath, json_encode($styles));

if ($annexeId) { // PUT
$this->annexeApiService->replaceFile($datastoreId, $annexeId, $filePath);
} else {
$this->annexeApiService->add($datastoreId, $filePath, [$path], null);
}
$extra = ['styles' => $styles];
$this->configurationApiService->modify($datastoreId, $configurationId, ['extra' => $extra]);
}
}
56 changes: 33 additions & 23 deletions src/Services/EntrepotApi/CartesServiceApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getService(string $datastoreId, string $offeringId): array

$styles = [];
if (OfferingTypes::WFS === $offering['type'] || OfferingTypes::WMTSTMS === $offering['type']) {
$styles = $this->getStyles($datastoreId, $offering['configuration']['_id']);
$styles = $this->getStyles($datastoreId, $offering['configuration']);
}
$offering['configuration']['styles'] = $styles;

Expand All @@ -73,23 +73,40 @@ public function getService(string $datastoreId, string $offeringId): array
}

/**
* Recherche des styles et ajout de l'url.
* // NOTE Utilisation d'un annexe parce qu'un tag est limité à 99 caractères. Le champ "extra" pourrait être utilisé une fois implémenté dans l'API.
* Recherche des styles et ajout de l'url. Les styles sont désormais stockés dans extra. Cette fonction est prévue pour migrer les styles stockés dans les annexes vers extra.
*
* @param array<mixed> $configuration
*
* @return array<mixed>
*/
public function getStyles(string $datastoreId, string $configId): array
public function getStyles(string $datastoreId, array $configuration): array
{
$path = "/configuration/$configId/styles.json";
$styles = null;

// vérifier si styles est présent dans extra
if (isset($configuration['extra']['styles'])) {
$styles = $configuration['extra']['styles'];
}

// vérifier si styles est présent dans une annexe
$path = "/configuration/{$configuration['_id']}/styles.json";
$styleAnnexes = $this->annexeApiService->getAll($datastoreId, null, $path);

$styles = [];
// si styles est présent dans une annexe et non dans extra, on le stocke dans extra et on supprime l'annexe
if (count($styleAnnexes)) {
$content = $this->annexeApiService->download($datastoreId, $styleAnnexes[0]['_id']);
$styles = json_decode($content, true);
// on ne lit l'annexe styles que si elle n'est pas déjà dans extra
if (null === $styles) {
$content = $this->annexeApiService->download($datastoreId, $styleAnnexes[0]['_id']);
$styles = json_decode($content, true);

$extra = ['styles' => $styles];
$this->configurationApiService->modify($datastoreId, $configuration['_id'], ['extra' => $extra]);
}

$this->annexeApiService->remove($datastoreId, $styleAnnexes[0]['_id']);
}

return $styles;
return $styles ?? [];
}

/**
Expand Down Expand Up @@ -170,7 +187,7 @@ public function wfsUnpublish(string $datastoreId, array $offering, bool $removeS
$this->configurationApiService->remove($datastoreId, $configurationId);

if (true === $removeStyleFiles) {
$this->removeStyleFiles($datastoreId, $configurationId);
$this->removeStyleFiles($datastoreId, $configuration);
}
}

Expand Down Expand Up @@ -249,32 +266,25 @@ public function wmtsTmsUnpublish(string $datastoreId, array $offering, bool $rem
$this->configurationApiService->remove($datastoreId, $configurationId);

if (true === $removeStyleFiles) {
$this->removeStyleFiles($datastoreId, $configurationId);
$this->removeStyleFiles($datastoreId, $configuration);
}
}

/**
* Suppression des styles lies à une configuration.
* Suppression des styles (les fichiers annexes) liés à une configuration.
*
* @param array<mixed> $configuration
*/
private function removeStyleFiles(string $datastoreId, string $configurationId): void
private function removeStyleFiles(string $datastoreId, array $configuration): void
{
$path = "/configuration/$configurationId/styles.json";

$styleAnnexes = $this->annexeApiService->getAll($datastoreId, null, $path);
if (0 === count($styleAnnexes)) {
return;
}

$content = $this->annexeApiService->download($datastoreId, $styleAnnexes[0]['_id']);
$styles = $this->getStyles($datastoreId, $configuration);

$styles = json_decode($content, true);
foreach ($styles as $style) {
if (array_key_exists('layers', $style)) {
foreach ($style['layers'] as $layer) {
$this->annexeApiService->remove($datastoreId, $layer['annexe_id']);
}
}
}
$this->annexeApiService->remove($datastoreId, $styleAnnexes[0]['_id']);
}
}
8 changes: 8 additions & 0 deletions src/Services/EntrepotApi/ConfigurationApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function replace(string $datastoreId, string $configurationId, $body = []
return $this->request('PUT', "datastores/$datastoreId/configurations/$configurationId", $body);
}

/**
* @param array<mixed> $body
*/
public function modify(string $datastoreId, string $configurationId, $body = []): array
{
return $this->request('PATCH', "datastores/$datastoreId/configurations/$configurationId", $body);
}

public function remove(string $datastoreId, string $configurationId): array
{
return $this->request('DELETE', "datastores/$datastoreId/configurations/$configurationId");
Expand Down

0 comments on commit 329c935

Please sign in to comment.