From 510e18ca87a558fd434c94a5467ce419d961d040 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Mon, 9 Dec 2024 17:05:16 +0100 Subject: [PATCH 1/8] Update FileService class docblock --- lib/Service/FileService.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Service/FileService.php b/lib/Service/FileService.php index 1b5fefa..9d547b5 100644 --- a/lib/Service/FileService.php +++ b/lib/Service/FileService.php @@ -23,7 +23,11 @@ use Psr\Log\LoggerInterface; /** - * Service class for handling file operations in OpenCatalogi + * Service for handling file operations in OpenRegister. + * + * This service provides functionalities for managing files and folders within the NextCloud environment, + * including creation, deletion, sharing, and file updates. It integrates with NextCloud's file and + * sharing APIs to provide seamless file management for the application. */ class FileService { From 32f57446f191304a15d7295533a3b0c5f6b4e4d0 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Tue, 10 Dec 2024 11:43:19 +0100 Subject: [PATCH 2/8] Set default value to user --- lib/Db/AuditTrailMapper.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Db/AuditTrailMapper.php b/lib/Db/AuditTrailMapper.php index c62b2f6..ce89d99 100644 --- a/lib/Db/AuditTrailMapper.php +++ b/lib/Db/AuditTrailMapper.php @@ -12,7 +12,7 @@ /** * The AuditTrailMapper class - * + * * @package OCA\OpenRegister\Db */ class AuditTrailMapper extends QBMapper @@ -125,7 +125,7 @@ public function createFromArray(array $object): Log return $this->insert(entity: $log); } - + /** * Creates an audit trail for object changes * @@ -152,7 +152,7 @@ public function createAuditTrail(?ObjectEntity $old = null, ?ObjectEntity $new = if ($action !== 'delete') { $oldArray = $old ? $old->jsonSerialize() : []; $newArray = $new->jsonSerialize(); - + // Compare old and new values to detect changes foreach ($newArray as $key => $value) { if (!isset($oldArray[$key]) || $oldArray[$key] !== $value) { @@ -162,7 +162,7 @@ public function createAuditTrail(?ObjectEntity $old = null, ?ObjectEntity $new = ]; } } - + // For updates, check for removed fields if ($action === 'update') { foreach ($oldArray as $key => $value) { @@ -185,8 +185,8 @@ public function createAuditTrail(?ObjectEntity $old = null, ?ObjectEntity $new = $auditTrail->setObject($objectEntity->getId()); $auditTrail->setAction($action); $auditTrail->setChanged($changed); - $auditTrail->setUser($user->getUID()); - $auditTrail->setUserName($user->getDisplayName()); + $auditTrail->setUser(($user !== null) ? $user->getUID() : 'System'); + $auditTrail->setUserName(($user !== null) ? $user->getDisplayName() : 'System'); $auditTrail->setSession(session_id()); $auditTrail->setRequest(\OC::$server->getRequest()->getId()); $auditTrail->setIpAddress(\OC::$server->getRequest()->getRemoteAddress()); From ec2089150f7907ccc833a86f7261d6b3d7dafa9e Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Tue, 17 Dec 2024 16:47:32 +0100 Subject: [PATCH 3/8] Missing functions for subobjects and result statistics --- lib/Service/ObjectService.php | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index 6356c4b..751376a 100755 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -275,6 +275,23 @@ public function findMultiple(array $ids): array return $result; } + public function findSubObjects(array $ids, string $property): array + { + $schemaObject = $this->schemaMapper->find($this->schema); + $property = $schemaObject->getProperties()[$property]; + + if(isset($property['items'])) { + $ref = explode('/', $property['items']['$ref']); + } else { + $subSchema = explode('/', $property['$ref']); + } + $subSchema = end($ref); + + $subSchemaMapper = $this->getMapper(register: $this->getRegister(), schema: $subSchema); + + return $subSchemaMapper->findMultiple($ids); + } + /** * Get aggregations for objects matching filters * @@ -312,6 +329,54 @@ private function getDataFromObject(mixed $object, ?array $extend = []): mixed return $object->getObject(); } + public function findAllPaginated(array $requestParams): array + { + // Extract specific parameters + $limit = $requestParams['limit'] ?? $requestParams['_limit'] ?? null; + $offset = $requestParams['offset'] ?? $requestParams['_offset'] ?? null; + $order = $requestParams['order'] ?? $requestParams['_order'] ?? []; + $extend = $requestParams['extend'] ?? $requestParams['_extend'] ?? null; + $page = $requestParams['page'] ?? $requestParams['_page'] ?? null; + $search = $requestParams['_search'] ?? null; + + if ($page !== null && isset($limit)) { + $page = (int) $page; + $offset = $limit * ($page - 1); + } + + + // Ensure order and extend are arrays + if (is_string($order)) { + $order = array_map('trim', explode(',', $order)); + } + if (is_string($extend)) { + $extend = array_map('trim', explode(',', $extend)); + } + + // Remove unnecessary parameters from filters + $filters = $requestParams; + unset($filters['_route']); // TODO: Investigate why this is here and if it's needed + unset($filters['_extend'], $filters['_limit'], $filters['_offset'], $filters['_order'], $filters['_page'], $filters['_search']); + unset($filters['extend'], $filters['limit'], $filters['offset'], $filters['order'], $filters['page']); + + $objects = $this->findAll(limit: $limit, offset: $offset, filters: $filters, sort: $order, search: $search, extend: $extend); + $total = $this->count($filters); + $pages = $limit !== null ? ceil($total/$limit) : 1; + + $facets = $this->getAggregations( + filters: $filters, + search: $search + ); + + return [ + 'results' => $objects, + 'facets' => $facets, + 'total' => $total, + 'page' => $page ?? 1, + 'pages' => $pages, + ]; + } + /** * Gets all objects of a specific type. * From 5c62bd977a5c57e7ba74cc9e013bc2b6f8a5d186 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Thu, 19 Dec 2024 09:39:32 +0100 Subject: [PATCH 4/8] Added docblocks --- lib/Service/ObjectService.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index 751376a..bf0cf48 100755 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -275,6 +275,14 @@ public function findMultiple(array $ids): array return $result; } + /** + * Find subobjects for a certain property with given ids + * + * @param array $ids The IDs to fetch the subobjects for + * @param string $property The property in which the objects reside. + * + * @return array The resulting subobjects. + */ public function findSubObjects(array $ids, string $property): array { $schemaObject = $this->schemaMapper->find($this->schema); @@ -329,6 +337,13 @@ private function getDataFromObject(mixed $object, ?array $extend = []): mixed return $object->getObject(); } + /** + * Find all objects conforming to the request parameters, surrounded with pagination data. + * + * @param array $requestParams The request parameters to search with. + * + * @return array The result including pagination data. + */ public function findAllPaginated(array $requestParams): array { // Extract specific parameters From 5e30366cfd1a91527c3afee0c2dac0e37ac488aa Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Thu, 19 Dec 2024 10:16:16 +0100 Subject: [PATCH 5/8] pr review --- lib/Service/ObjectService.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index bf0cf48..5c13307 100755 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -288,7 +288,7 @@ public function findSubObjects(array $ids, string $property): array $schemaObject = $this->schemaMapper->find($this->schema); $property = $schemaObject->getProperties()[$property]; - if(isset($property['items'])) { + if (isset($property['items'])) { $ref = explode('/', $property['items']['$ref']); } else { $subSchema = explode('/', $property['$ref']); @@ -359,7 +359,6 @@ public function findAllPaginated(array $requestParams): array $offset = $limit * ($page - 1); } - // Ensure order and extend are arrays if (is_string($order)) { $order = array_map('trim', explode(',', $order)); From f5bdea2720295933eeff4636bc3342d0b77acbb5 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Thu, 19 Dec 2024 11:21:24 +0100 Subject: [PATCH 6/8] style fix --- lib/Service/ObjectService.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index 5c13307..f38cc57 100755 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -288,7 +288,7 @@ public function findSubObjects(array $ids, string $property): array $schemaObject = $this->schemaMapper->find($this->schema); $property = $schemaObject->getProperties()[$property]; - if (isset($property['items'])) { + if (isset($property['items']) === true) { $ref = explode('/', $property['items']['$ref']); } else { $subSchema = explode('/', $property['$ref']); @@ -360,10 +360,10 @@ public function findAllPaginated(array $requestParams): array } // Ensure order and extend are arrays - if (is_string($order)) { + if (is_string($order) === true) { $order = array_map('trim', explode(',', $order)); } - if (is_string($extend)) { + if (is_string($extend) === true) { $extend = array_map('trim', explode(',', $extend)); } From 7e74e66db679630c2b933cebdfa5e1afa105111a Mon Sep 17 00:00:00 2001 From: Barry Brands Date: Thu, 19 Dec 2024 11:49:24 +0100 Subject: [PATCH 7/8] remove unnecessary setObject --- lib/Service/ObjectService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index f38cc57..38facee 100755 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -487,7 +487,7 @@ public function saveObject(int $register, int $schema, array $object): ObjectEnt // Handle object properties that are either nested objects or files if ($schemaObject->getProperties() !== null && is_array($schemaObject->getProperties()) === true) { $objectEntity = $this->handleObjectRelations($objectEntity, $object, $schemaObject->getProperties(), $register, $schema); - $objectEntity->setObject($object); + // $objectEntity->setObject($object); } $objectEntity->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('openregister.Objects.show', ['id' => $objectEntity->getUuid()]))); From a4e65e539d7caf5e8a379a37563f7dbc6e29553c Mon Sep 17 00:00:00 2001 From: Barry Brands Date: Thu, 19 Dec 2024 11:54:45 +0100 Subject: [PATCH 8/8] remove setObject --- lib/Service/ObjectService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index 38facee..f0ac761 100755 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -487,7 +487,6 @@ public function saveObject(int $register, int $schema, array $object): ObjectEnt // Handle object properties that are either nested objects or files if ($schemaObject->getProperties() !== null && is_array($schemaObject->getProperties()) === true) { $objectEntity = $this->handleObjectRelations($objectEntity, $object, $schemaObject->getProperties(), $register, $schema); - // $objectEntity->setObject($object); } $objectEntity->setUri($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('openregister.Objects.show', ['id' => $objectEntity->getUuid()])));