Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development'
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Aug 22, 2024
2 parents 7acd049 + 1eee7d4 commit ca4260d
Show file tree
Hide file tree
Showing 64 changed files with 1,870 additions and 759 deletions.
4 changes: 3 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<bugs>https://github.com/OpenCatalogi/.github/issues</bugs>
<dependencies>
<nextcloud min-version="26" max-version="30"/>
</dependencies>
</dependencies><background-jobs>
<job>OCA\OpenCatalogi\Cron\DirectorySync</job>
</background-jobs>
<navigations>
<navigation>
<id>opencatalogi</id>
Expand Down
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
['name' => 'search#index', 'url' => '/api/search', 'verb' => 'GET'],
['name' => 'search#show', 'url' => '/api/search/{id}', 'verb' => 'GET'],
['name' => 'directory#page', 'url' => '/directory', 'verb' => 'GET'],
['name' => 'directory#add', 'url' => '/api/directory/add', 'verb' => 'POST'],
['name' => 'directory#synchronise', 'url' => '/api/directory/{id}/sync', 'verb' => 'GET'],
['name' => 'configuration#index', 'url' => '/configuration', 'verb' => 'GET'],
['name' => 'configuration#create', 'url' => '/configuration', 'verb' => 'POST'],
['name' => 'search#preflighted_cors', 'url' => '/api/{path}', 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']]
Expand Down
12 changes: 0 additions & 12 deletions lib/Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@

class ConfigurationController extends Controller
{
const TEST_ARRAY = [
"5137a1e5-b54d-43ad-abd1-4b5bff5fcd3f" => [
"id" => "5137a1e5-b54d-43ad-abd1-4b5bff5fcd3f",
"name" => "Configuration one",
"summary" => "summary for one"
],
"7782b511-7034-4d49-a005-e827d5ae603f" => [
"id" => "7782b511-7034-4d49-a005-e827d5ae603f",
"name" => "Configuration two",
"summary" => "summary for two"
]
];

public function __construct(
$appName,
Expand Down
21 changes: 8 additions & 13 deletions lib/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,10 @@
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\AppFramework\Http\ContentSecurityPolicy;

class DashboardController extends Controller
{
const TEST_ARRAY = [
"d021c5ff-a254-4114-a1fb-7a18db152270" => [
"id" => "d021c5ff-a254-4114-a1fb-7a18db152270",
"name" => "Dashboard one",
"summary" => "summary for one"
],
"79c02b33-78ba-4d65-aabd-ff9aae6654f7" => [
"id" => "79c02b33-78ba-4d65-aabd-ff9aae6654f7",
"name" => "Dashboard two",
"summary" => "summary for two"
]
];

public function __construct($appName, IRequest $request)
{
Expand All @@ -34,11 +23,17 @@ public function __construct($appName, IRequest $request)
public function page(?string $getParameter)
{
try {
return new TemplateResponse(
$response =new TemplateResponse(
$this->appName,
'index',
[]
);

$csp = new ContentSecurityPolicy();
$csp->addAllowedConnectDomain('*');
$response->setContentSecurityPolicy($csp);

return $response;
} catch (\Exception $e) {
return new TemplateResponse(
$this->appName,
Expand Down
60 changes: 31 additions & 29 deletions lib/Controller/DirectoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,6 @@

class DirectoryController extends Controller
{
const TEST_ARRAY = [
"64996753-5109-4396-9f07-17040d7fb137" => [
"id" => "64996753-5109-4396-9f07-17040d7fb137",
"title" => "Directory test 1",
"summary" => "A testing directory",
"description" => "A testing directory description",
"search" => "string",
"metadata" => "string",
"status" => "A status",
"lastSync" => "string",
"default" => "string",
"available" => "true"

],
"0dcb7be0-ce06-4453-9ea7-6d66f67aa4ea" =>[
"id" => "0dcb7be0-ce06-4453-9ea7-6d66f67aa4ea",
"title" => "Directory test 2",
"summary" => "A testing directory",
"description" => "A testing directory description",
"search" => "string",
"metadata" => "string",
"status" => "A status",
"lastSync" => "string",
"default" => "string",
"available" => "true"

]
];

public function __construct(
$appName,
IRequest $request,
Expand Down Expand Up @@ -213,4 +184,35 @@ public function destroy(string|int $id, ObjectService $objectService): JSONRespo
// get post from requests
return new JSONResponse($returnData);
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function synchronise(string|int $id, DirectoryService $directoryService, ObjectService $objectService): JSONResponse
{
if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
try {
$object = $this->listingMapper->find(id: (int) $id)->jsonSerialize();
} catch (DoesNotExistException $exception) {
return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404);
}
} else {
$dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation');
$dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey');
$dbConfig['mongodbCluster'] = $this->config->getValueString(app: $this->appName, key: 'mongodbCluster');

$filters['_id'] = (string) $id;

$object = $objectService->findObject(filters: $filters, config: $dbConfig);
}

$url = $object['directory'];

$directoryService->fetchFromExternalDirectory(url: $url, update: true);

return new JsonResponse(data: $object, statusCode: 200);
}
}
30 changes: 27 additions & 3 deletions lib/Controller/MetaDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;
use OCP\IURLGenerator;

class MetaDataController extends Controller
{
Expand Down Expand Up @@ -43,6 +44,7 @@ public function page(?string $getParameter)
}

/**
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*/
Expand Down Expand Up @@ -77,6 +79,7 @@ public function index(ObjectService $objectService, SearchService $searchService
}

/**
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*/
Expand Down Expand Up @@ -107,13 +110,15 @@ public function show(string|int $id, ObjectService $objectService): JSONResponse
* @NoAdminRequired
* @NoCSRFRequired
*/
public function create(ObjectService $objectService): JSONResponse
public function create(ObjectService $objectService, IURLGenerator $urlGenerator): JSONResponse
{

$data = $this->request->getParams();

// Remove fields we should never post
unset($data['id']);


foreach($data as $key => $value) {
if(str_starts_with($key, '_')) {
unset($data[$key]);
Expand All @@ -123,7 +128,18 @@ public function create(ObjectService $objectService): JSONResponse
if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
return new JSONResponse($this->metaDataMapper->createFromArray(object: $data));
$object = $this->metaDataMapper->createFromArray(object: $data);

$id = $object->getId();

if($object->getSource() === null) {
$source = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute(routeName:"opencatalogi.metadata.show", arguments: ['id' => $id]));
$object->setSource($source);
$this->metaDataMapper->update($object);
}


return new JSONResponse($object);
}
$dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation');
$dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey');
Expand All @@ -136,6 +152,14 @@ public function create(ObjectService $objectService): JSONResponse
config: $dbConfig
);

if(isset($data['source']) === false || $data['source'] === null) {
$returnData['source'] = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute(routeName:"opencatalogi.metadata.show", arguments: ['id' => $returnData['id']]));
$returnData = $objectService->saveObject(
data: $data,
config: $dbConfig
);
}

// get post from requests
return new JSONResponse($returnData);
}
Expand All @@ -149,7 +173,7 @@ public function update(string|int $id, ObjectService $objectService): JSONRespon
$data = $this->request->getParams();

// Remove fields we should never post
unset($data['id']);
unset($data['id'],$data['source']);
foreach($data as $key => $value) {
if(str_starts_with($key, '_')) {
unset($data[$key]);
Expand Down
11 changes: 10 additions & 1 deletion lib/Controller/PublicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
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\AppFramework\OCS\OCSNotFoundException;
use OCP\IAppConfig;
use OCP\IRequest;
use Symfony\Component\Uid\Uuid;
Expand Down Expand Up @@ -458,7 +461,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 +473,12 @@ public function create(ObjectService $objectService, ElasticSearchService $elast
}
}

try {
$data = $validationService->validatePublication($data);
} catch (OCSBadRequestException|OCSNotFoundException $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
13 changes: 0 additions & 13 deletions lib/Controller/ThemesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@

class ThemesController extends Controller
{
const TEST_ARRAY = [
"354980e5-c967-4ba5-989b-65c2b0cd2ff4" => [
"id" => "354980e5-c967-4ba5-989b-65c2b0cd2ff4",
"name" => "Input voor OpenCatalogi",
"summary" => "Dit is een selectie van high-value datasets in DCAT-AP 2.0 standaard x"
],
"2ab0011e-9b4c-4c50-a50d-a16fc0be0178" => [
"id" => "2ab0011e-9b4c-4c50-a50d-a16fc0be0178",
"title" => "Publication two",
"description" => "summary for two"
]
];

public function __construct
(
$appName,
Expand Down
33 changes: 33 additions & 0 deletions lib/Cron/DirectorySync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace OCA\OpenCatalogi\Cron;

use OCA\OpenCatalogi\Service\DirectoryService;
use OCP\BackgroundJob\TimedJob;
use OCP\AppFramework\Utility\ITimeFactory;

/**
*
* Docs: https://docs.nextcloud.com/server/latest/developer_manual/basics/backgroundjobs.html
*/
class DirectorySync extends TimedJob {

private DirectoryService $directoryService;

public function __construct(ITimeFactory $time, DirectoryService $directoryService) {
parent::__construct($time);
$this->directoryService = $directoryService;

// Run once an hour
$this->setInterval(3600);

// Only run one instance of this job at a time
$this->setAllowParallelRuns(false);
}

protected function run($arguments) {
$this->directoryService->doCronSync();
}


}
32 changes: 19 additions & 13 deletions lib/Db/Listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,30 @@
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 $metadata = null;
protected ?string $catalogId = null;
protected ?string $status = null;
protected ?string $title = 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 ?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');
$this->addType(fieldName: 'summary', type: 'string');
$this->addType(fieldName: 'description', type: 'string');
$this->addType(fieldName: 'search', type: 'string');
$this->addType(fieldName: 'directory', type: 'string');
$this->addType(fieldName: 'metadata', type: 'string');
$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 All @@ -51,6 +52,10 @@ public function hydrate(array $object): self
{
$jsonFields = $this->getJsonFields();

if(isset($object['metadata']) === false) {
$object['metadata'] = [];
}

foreach($object as $key => $value) {
if (in_array($key, $jsonFields) === true && $value === []) {
$value = null;
Expand Down Expand Up @@ -79,6 +84,7 @@ public function jsonSerialize(): array
'metadata' => $this->metadata,
'catalogId' => $this->catalogId,
'status' => $this->status,
'statusCode' => $this->statusCode,
'lastSync' => $this->lastSync?->format('c'),
'default' => $this->default,
'available' => $this->available,
Expand Down
Loading

0 comments on commit ca4260d

Please sign in to comment.