Skip to content

Commit

Permalink
Merge branch 'feature/DIMOC-244/source-parameter' of https://github.c…
Browse files Browse the repository at this point in the history
…om/ConductionNL/opencatalogi into feature/DIMOC-244/source-parameter
  • Loading branch information
bbrands02 committed Aug 15, 2024
2 parents 56e8e03 + 8a8d3bf commit fa97643
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 12 deletions.
10 changes: 9 additions & 1 deletion lib/Controller/PublicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
use OCA\OpenCatalogi\Service\FileService;
use OCA\OpenCatalogi\Service\ObjectService;
use OCA\OpenCatalogi\Service\SearchService;
use OCA\OpenCatalogi\Service\ValidationService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\IAppConfig;
use OCP\IRequest;
use Symfony\Component\Uid\Uuid;
Expand Down Expand Up @@ -458,7 +460,7 @@ public function download(string|int $id, ObjectService $objectService): JSONResp
* @NoAdminRequired
* @NoCSRFRequired
*/
public function create(ObjectService $objectService, ElasticSearchService $elasticSearchService): JSONResponse
public function create(ObjectService $objectService, ElasticSearchService $elasticSearchService, ValidationService $validationService): JSONResponse
{
$data = $this->request->getParams();

Expand All @@ -470,6 +472,12 @@ public function create(ObjectService $objectService, ElasticSearchService $elast
}
}

try {
$data = $validationService->validatePublication($data);
} catch (OCSBadRequestException $exception) {
return new JSONResponse(data: ['message' => $exception->getMessage()], statusCode: 400);
}

if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
Expand Down
24 changes: 13 additions & 11 deletions lib/Db/Listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
class Listing extends Entity implements JsonSerializable
{

protected ?string $title = null;
protected ?string $reference = null;
protected ?string $summary = null;
protected ?string $description = null;
protected ?string $search = null;
protected ?string $directory = null;
protected ?string $title = null;
protected ?string $reference = null;
protected ?string $summary = null;
protected ?string $description = null;
protected ?string $search = null;
protected ?string $directory = null;
protected ?array $metadata = null;
protected ?string $catalogId = null;
protected ?string $status = null;
protected ?string $catalogId = null;
protected ?string $status = null;
protected ?int $statusCode = null;
protected ?DateTime $lastSync = null;
protected ?bool $default = false;
protected ?bool $available = false;
protected ?string $organisation = null;
protected ?bool $default = false;
protected ?bool $available = false;
protected ?string $organisation = null;

public function __construct() {
$this->addType(fieldName: 'title', type: 'string');
Expand All @@ -32,6 +33,7 @@ public function __construct() {
$this->addType(fieldName: 'metadata', type: 'json');
$this->addType(fieldName: 'catalogId', type: 'string');
$this->addType(fieldName: 'status', type: 'string');
$this->addType(fieldName: 'statusCode', type: 'integer');
$this->addType(fieldName: 'lastSync', type: 'datetime');
$this->addType(fieldName: 'default', type: 'boolean');
$this->addType(fieldName: 'available', type: 'boolean');
Expand Down
1 change: 1 addition & 0 deletions lib/Db/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MetaData extends Entity implements JsonSerializable
protected ?string $description = null;
protected ?array $required = [];
protected ?array $properties = [];
protected ?array $archive = [];
protected ?string $source = null;

public function __construct() {
Expand Down
96 changes: 96 additions & 0 deletions lib/Service/ValidationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace OCA\OpenCatalogi\Service;

use OCA\OpenCatalogi\Db\CatalogMapper;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\IAppConfig;

class ValidationService
{
/**
* @var string The name of the application.
*/
private string $appName;

/**
* @var array The current MongoDB Config.
*/
private array $mongodbConfig;

/**
* @param IAppConfig $config The application config
* @param CatalogMapper $catalogMapper The catalog mapper.
* @param ObjectService $objectService The object service.
*/
public function __construct(
private readonly IAppConfig $config,
private readonly CatalogMapper $catalogMapper,
private readonly ObjectService $objectService,
){
$this->appName = 'opencatalogi';

$this->mongodbConfig = [
'base_uri' => $this->config->getValueString(app: $this->appName, key: 'mongodbLocation'),
'headers' => ['api-key' => $this->config->getValueString(app: $this->appName, key: 'mongodbKey')],
'mongodbCluster' => $this->config->getValueString(app: $this->appName, key:'mongodbCluster')
];

}

/**
* @return array The mongodb config.
*/
public function getMongodbConfig(): array
{
return $this->mongodbConfig;
}

/**
* Fetches a catalog from either the local database or mongodb
*
* @param string $id The id of the catalog to be fetched.
* @return array The JSON Serialised catalog.
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getCatalog (string $id): array
{
if ($this->config->hasKey(app: $this->appName, key: 'mongoStorage') !== false
|| $this->config->getValueString(app: $this->appName, key: 'mongoStorage') === '1'
) {
$filter = ['id' => $id, '_schema' => 'catalog'];

return $this->objectService->findObject(filters: $filter, config: $this->getMongodbConfig());
}

return $this->catalogMapper->find(id: $id)->jsonSerialize();
}

/**
* Validates a publication against the rules set for the publication.
*
* @param array $publication The publication to be validated.
* @return array The publication after it has been validated.
*
* @throws OCSBadRequestException Thrown if the object does not validate
*/
public function validatePublication(array $publication): array
{
$catalogId = $publication['catalogi'];
$metadata = $publication['metaData'];

$catalog = $this->getCatalog($catalogId);

// var_dump($catalog['metadata'], $metadata, in_array(needle: $metadata, haystack: $catalog['metadata']));

if(in_array(needle: $metadata, haystack: $catalog['metadata']) === false) {
throw new OCSBadRequestException(message: 'Given metadata object not present in catalog');
}

// var_dump($publication);

return $publication;
}

}

0 comments on commit fa97643

Please sign in to comment.