-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSchemasController.php
More file actions
1171 lines (1071 loc) · 47.4 KB
/
SchemasController.php
File metadata and controls
1171 lines (1071 loc) · 47.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* SchemasController handles REST API endpoints for schema management
*
* Controller for managing schema operations in the OpenRegister app.
* Provides endpoints for CRUD operations, schema exploration, caching,
* import/export, and statistics.
*
* @category Controller
* @package OCA\OpenRegister\Controller
*
* @author Conduction Development Team <dev@conduction.nl>
* @copyright 2024 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @version GIT: <git-id>
*
* @link https://OpenRegister.app
*/
namespace OCA\OpenRegister\Controller;
use Exception;
use DateTime;
use GuzzleHttp\Exception\GuzzleException;
use OCA\OpenRegister\Db\Schema;
use OCA\OpenRegister\Db\SchemaMapper;
use OCA\OpenRegister\Db\MagicMapper;
use OCA\OpenRegister\Service\DownloadService;
use OCA\OpenRegister\Service\ObjectService;
use OCA\OpenRegister\Service\OrganisationService;
use OCA\OpenRegister\Service\Schemas\SchemaCacheHandler;
use OCA\OpenRegister\Service\Schemas\FacetCacheHandler;
use OCA\OpenRegister\Service\SchemaService;
use OCA\OpenRegister\Service\UploadService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\DB\Exception as DBException;
use OCA\OpenRegister\Exception\DatabaseConstraintException;
use OCP\IAppConfig;
use OCP\IRequest;
use Symfony\Component\Uid\Uuid;
use OCA\OpenRegister\Db\AuditTrailMapper;
use Psr\Log\LoggerInterface;
/**
* SchemasController handles REST API endpoints for schema management
*
* Provides REST API endpoints for managing schemas including CRUD operations,
* schema exploration, caching, import/export, and statistics.
*
* @category Controller
* @package OCA\OpenRegister\Controller
*
* @author Conduction Development Team <dev@conduction.nl>
* @copyright 2024 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @version GIT: <git-id>
*
* @link https://OpenRegister.app
*
* @psalm-suppress UnusedClass
*
* @suppressWarnings(PHPMD.ExcessiveClassLength)
* @suppressWarnings(PHPMD.ExcessiveClassComplexity)
* @suppressWarnings(PHPMD.TooManyPublicMethods)
* @suppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class SchemasController extends Controller
{
/**
* Constructor
*
* Initializes controller with required dependencies for schema operations.
* Calls parent constructor to set up base controller functionality.
*
* @param string $appName Application name
* @param IRequest $request HTTP request object
* @param IAppConfig $config App configuration for settings
* @param SchemaMapper $schemaMapper Schema mapper for database operations
* @param MagicMapper $objectEntityMapper Object entity mapper for object queries
* @param DownloadService $downloadService Download service for file downloads
* @param UploadService $uploadService Upload service for file uploads
* @param AuditTrailMapper $auditTrailMapper Audit trail mapper for log statistics
* @param OrganisationService $organisationService Organisation service for multi-tenancy
* @param SchemaCacheHandler $schemaCacheService Schema cache handler for caching operations
* @param FacetCacheHandler $facetCacheSvc Schema facet cache service for facet caching
* @param SchemaService $schemaService Schema service for exploration operations
* @param LoggerInterface $logger Logger for error tracking
*
* @return void
*
* @suppressWarnings(PHPMD.ExcessiveParameterList) Nextcloud DI requires constructor injection
*/
public function __construct(
string $appName,
IRequest $request,
private readonly IAppConfig $config,
private readonly SchemaMapper $schemaMapper,
private readonly MagicMapper $objectEntityMapper,
private readonly DownloadService $downloadService,
private readonly UploadService $uploadService,
private readonly AuditTrailMapper $auditTrailMapper,
private readonly OrganisationService $organisationService,
private readonly SchemaCacheHandler $schemaCacheService,
private readonly FacetCacheHandler $facetCacheSvc,
private readonly SchemaService $schemaService,
private readonly LoggerInterface $logger
) {
// Call parent constructor to initialize base controller.
parent::__construct(appName: $appName, request: $request);
}//end __construct()
/**
* Retrieves a list of all schemas
*
* Returns a JSON response containing an array of all schemas in the system.
* Supports pagination, filtering, and extended properties (stats, extendedBy).
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @PublicPage
*
* @return JSONResponse JSON response with array of schemas
*
* @psalm-return JSONResponse<200,
* array{results: array<array{id: int, uuid: null|string,
* uri: null|string, slug: null|string, title: null|string,
* description: null|string, version: null|string,
* summary: null|string, icon: null|string, required: array,
* properties: array, archive: array|null, source: null|string,
* hardValidation: bool, immutable: bool, searchable: bool,
* updated: null|string, created: null|string, maxDepth: int,
* owner: null|string, application: null|string,
* organisation: null|string,
* groups: array<string, list<string>>|null,
* authorization: array|null, deleted: null|string,
* published: null|string, depublished: null|string,
* configuration: array|null|string, allOf: array|null,
* oneOf: array|null, anyOf: array|null}>}, array<never, never>>
*
* @suppressWarnings(PHPMD.CyclomaticComplexity)
* @suppressWarnings(PHPMD.NPathComplexity)
*/
public function index(): JSONResponse
{
// Get request parameters for filtering and searching.
$params = $this->request->getParams();
// Extract pagination and search parameters.
$limit = null;
if (isset($params['_limit']) === true) {
$limit = (int) $params['_limit'];
}
$offset = null;
if (isset($params['_offset']) === true) {
$offset = (int) $params['_offset'];
}
$page = null;
if (isset($params['_page']) === true) {
$page = (int) $params['_page'];
}
// Note: search parameter not currently used in this endpoint.
// Extract extend parameter for additional properties.
$extend = $params['_extend'] ?? [];
// Normalize extend to array if string.
if (is_string($extend) === true) {
$extend = [$extend];
}
// Convert page to offset if provided (page-based pagination).
if ($page !== null && $limit !== null) {
$offset = ($page - 1) * $limit;
}
// Extract filters from request parameters.
$filters = $params['filters'] ?? [];
// Retrieve schemas using mapper with pagination and filters.
$schemas = $this->schemaMapper->findAll(
limit: $limit,
offset: $offset,
filters: $filters,
searchConditions: [],
searchParams: [],
_extend: [],
_multitenancy: false
);
// Serialize schemas to arrays.
$schemasArr = array_map(
function ($schema) {
return $schema->jsonSerialize();
},
$schemas
);
// Batch-load all extendedBy relationships in a single query instead of N queries.
$allExtendedBy = $this->schemaMapper->findAllExtendedBy();
foreach ($schemasArr as &$schema) {
// @psalm-suppress InvalidArrayOffset
$schema['@self'] = $schema['@self'] ?? [];
$schema['@self']['extendedBy'] = $allExtendedBy[$schema['id']] ?? [];
}
unset($schema);
// Break the reference.
// If '@self.stats' is requested, attach statistics to each schema.
if (in_array('@self.stats', $extend, true) === true) {
// Collect all schema IDs for batch queries.
$schemaIds = array_map(fn($schema) => $schema['id'], $schemasArr);
// Batch-load all statistics in 3 queries instead of N*2 queries.
$registerCounts = $this->schemaMapper->getRegisterCountPerSchema();
$objectStats = $this->objectEntityMapper->getStatisticsGroupedBySchema(schemaIds: $schemaIds);
$logStats = $this->auditTrailMapper->getStatisticsGroupedBySchema(schemaIds: $schemaIds);
foreach ($schemasArr as &$schema) {
$schema['stats'] = [
'objects' => $objectStats[$schema['id']] ?? [
'total' => 0,
'size' => 0,
'invalid' => 0,
'deleted' => 0,
'locked' => 0,
'published' => 0,
],
'logs' => $logStats[$schema['id']] ?? ['total' => 0, 'size' => 0],
'files' => [ 'total' => 0, 'size' => 0 ],
// Add the number of registers referencing this schema.
'registers' => $registerCounts[$schema['id']] ?? 0,
];
}
}//end if
return new JSONResponse(data: ['results' => $schemasArr]);
}//end index()
/**
* Retrieves a single schema by ID
*
* @param int|string $id The ID of the schema
*
* @return JSONResponse JSON response with schema data
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @PublicPage
*/
public function show($id): JSONResponse
{
try {
$extend = $this->request->getParam(key: '_extend', default: []);
if (is_string($extend) === true) {
$extend = [$extend];
}
$schema = $this->schemaMapper->find(id: $id, _extend: [], _multitenancy: false);
$schemaArr = $schema->jsonSerialize();
// Add extendedBy property showing UUIDs of schemas that extend this schema.
// Note: @psalm-suppress InvalidArrayOffset used here for dynamic array access.
$schemaArr['@self'] = $schemaArr['@self'] ?? [];
$schemaArr['@self']['extendedBy'] = $this->schemaMapper->findExtendedBy($id);
// Add property source metadata to distinguish native vs inherited properties.
// This is especially useful for schemas using allOf composition.
if (($schema->getAllOf() ?? null) !== null && count($schema->getAllOf()) > 0) {
$schemaArr['@self']['propertyMetadata'] = $this->schemaMapper->getPropertySourceMetadata($schema);
}
// If '@self.stats' is requested, attach statistics to the schema.
if (in_array('@self.stats', $extend, true) === true) {
// Get register counts for all schemas in one call.
$registerCounts = $this->schemaMapper->getRegisterCountPerSchema();
$schemaArr['stats'] = [
'objects' => $this->objectEntityMapper->getStatistics(registerId: null, schemaId: $schemaArr['id']),
'logs' => $this->auditTrailMapper->getStatistics(registerId: null, schemaId: $schemaArr['id']),
'files' => [ 'total' => 0, 'size' => 0 ],
// Add the number of registers referencing this schema.
'registers' => $registerCounts[$schemaArr['id']] ?? 0,
];
}
return new JSONResponse(data: $schemaArr);
} catch (DoesNotExistException $e) {
return new JSONResponse(data: ['error' => 'Schema not found'], statusCode: 404);
} catch (\OCA\OpenRegister\Exception\ValidationException $e) {
// ValidationException is thrown when schema is not found (includes debugging info).
return new JSONResponse(data: ['error' => 'Schema not found'], statusCode: 404);
} catch (Exception $e) {
$this->logger->error(
message: '[SchemasController] Failed to retrieve schema',
context: [
'file' => __FILE__,
'line' => __LINE__,
'schema_id' => $id,
'error_message' => $e->getMessage(),
]
);
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 500);
}//end try
}//end show()
/**
* Creates a new schema
*
* This method creates a new schema based on POST data.
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @suppressWarnings(PHPMD.StaticAccess) DatabaseConstraintException factory method is standard pattern
* @suppressWarnings(PHPMD.CyclomaticComplexity)
*
* @return JSONResponse JSON response with created schema or error
*
* @psalm-return JSONResponse<201, Schema,
* array<never, never>>|JSONResponse<int, array{error: string},
* array<never, never>>
*/
public function create(): JSONResponse
{
// Get request parameters.
$data = $this->request->getParams();
// DEBUG: Log incoming request to track duplicate creation.
$this->logger->info(
message: '[SchemasController::create] Starting schema creation',
context: [
'file' => __FILE__,
'line' => __LINE__,
'title' => $data['title'] ?? 'no title',
'has_organisation' => isset($data['organisation']),
'organisation' => $data['organisation'] ?? 'not set',
]
);
// Remove internal parameters (starting with '_').
foreach (array_keys($data) as $key) {
if (str_starts_with($key, '_') === true) {
unset($data[$key]);
}
}
// Remove ID if present to ensure a new record is created.
if (($data['id'] ?? null) !== null) {
unset($data['id']);
}
try {
// Create a new schema from the data.
$schema = $this->schemaMapper->createFromArray(object: $data);
/*
* NOTE: Organization should already be set from the request data.
* The update() call below was causing duplicate schema creation with different timestamps.
* Since createFromArray() already handles organization assignment, this is commented out.
// Set organisation from active organisation for multi-tenancy (if not already set).
if ($schema->getOrganisation() === null || $schema->getOrganisation() === '') {
$organisationUuid = $this->organisationService->getOrganisationForNewEntity();
$schema->setOrganisation($organisationUuid);
$schema = $this->schemaMapper->update($schema);
}
*/
return new JSONResponse(data: $schema, statusCode: 201);
} catch (DBException $e) {
// Handle database constraint violations with user-friendly messages.
$constraintException = DatabaseConstraintException::fromDatabaseException(dbException: $e, entityType: 'schema');
return new JSONResponse(
data: ['error' => $constraintException->getMessage()],
statusCode: $constraintException->getHttpStatusCode()
);
} catch (DatabaseConstraintException $e) {
// Handle our custom database constraint exceptions.
return new JSONResponse(
data: ['error' => $e->getMessage()],
statusCode: $e->getHttpStatusCode()
);
} catch (Exception $e) {
// Log the actual error for debugging.
$this->logger->error(
message: '[SchemasController] Schema creation failed',
context: [
'file' => __FILE__,
'line' => __LINE__,
'error_message' => $e->getMessage(),
'error_code' => $e->getCode(),
'trace' => $e->getTraceAsString(),
]
);
// Check if this is a validation error by examining the message.
if (str_contains($e->getMessage(), 'Invalid') === true
|| str_contains($e->getMessage(), 'must be') === true
|| str_contains($e->getMessage(), 'required') === true
|| str_contains($e->getMessage(), 'format') === true
|| str_contains($e->getMessage(), 'Property at') === true
|| str_contains($e->getMessage(), 'authorization') === true
) {
// Return 400 Bad Request for validation errors with actual error message.
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 400);
}
// For database constraint violations, return 409 Conflict.
if (str_contains($e->getMessage(), 'constraint') === true
|| str_contains($e->getMessage(), 'duplicate') === true
|| str_contains($e->getMessage(), 'unique') === true
) {
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 409);
}
// Return 500 for other unexpected errors with actual error message.
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 500);
}//end try
}//end create()
/**
* Updates an existing schema
*
* This method updates an existing schema based on its ID.
*
* @param int $id The ID of the schema to update
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @suppressWarnings(PHPMD.StaticAccess) DatabaseConstraintException factory method is standard pattern
* @suppressWarnings(PHPMD.CyclomaticComplexity)
*
* @return JSONResponse JSON response with updated schema or error
*
* @psalm-return JSONResponse<200, Schema,
* array<never, never>>|JSONResponse<int, array{error: string},
* array<never, never>>
*/
public function update(int $id): JSONResponse
{
// Get request parameters.
$data = $this->request->getParams();
// Remove internal parameters (starting with '_').
foreach (array_keys($data) as $key) {
if (str_starts_with($key, '_') === true) {
unset($data[$key]);
}
}
// Remove immutable fields to prevent tampering.
unset($data['id']);
unset($data['organisation']);
unset($data['owner']);
unset($data['created']);
try {
// Update the schema with the provided data.
$updatedSchema = $this->schemaMapper->updateFromArray(id: $id, object: $data);
// **CACHE INVALIDATION**: Clear all schema-related caches when schema is updated.
$this->schemaCacheService->invalidateForSchemaChange(schemaId: $updatedSchema->getId(), operation: 'update');
$this->facetCacheSvc->invalidateForSchemaChange(
schemaId: $updatedSchema->getId(),
operation: 'update'
);
return new JSONResponse(data: $updatedSchema);
} catch (DBException $e) {
// Handle database constraint violations with user-friendly messages.
$constraintException = DatabaseConstraintException::fromDatabaseException(
dbException: $e,
entityType: 'schema'
);
return new JSONResponse(
data: ['error' => $constraintException->getMessage()],
statusCode: $constraintException->getHttpStatusCode()
);
} catch (DatabaseConstraintException $e) {
// Handle our custom database constraint exceptions.
return new JSONResponse(
data: ['error' => $e->getMessage()],
statusCode: $e->getHttpStatusCode()
);
} catch (Exception $e) {
// Log the actual error for debugging.
$this->logger->error(
message: '[SchemasController] Schema update failed',
context: [
'file' => __FILE__,
'line' => __LINE__,
'schema_id' => $id,
'error_message' => $e->getMessage(),
'error_code' => $e->getCode(),
'trace' => $e->getTraceAsString(),
]
);
// Check if this is a validation error by examining the message.
if (str_contains($e->getMessage(), 'Invalid') === true
|| str_contains($e->getMessage(), 'must be') === true
|| str_contains($e->getMessage(), 'required') === true
|| str_contains($e->getMessage(), 'format') === true
|| str_contains($e->getMessage(), 'Property at') === true
|| str_contains($e->getMessage(), 'authorization') === true
) {
// Return 400 Bad Request for validation errors with actual error message.
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 400);
}
// For database constraint violations, return 409 Conflict.
if (str_contains($e->getMessage(), 'constraint') === true
|| str_contains($e->getMessage(), 'duplicate') === true
|| str_contains($e->getMessage(), 'unique') === true
) {
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 409);
}
// Return 500 for other unexpected errors with actual error message.
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 500);
}//end try
}//end update()
/**
* Patch (partially update) a schema
*
* @param int $id The ID of the schema to patch
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @return JSONResponse JSON response with patched schema or error
*
* @psalm-return JSONResponse<200, Schema,
* array<never, never>>|JSONResponse<int, array{error: string},
* array<never, never>>
*/
public function patch(int $id): JSONResponse
{
return $this->update(id: $id);
}//end patch()
/**
* Deletes a schema
*
* This method deletes a schema based on its ID.
*
* @param int $id The ID of the schema to delete
*
* @throws Exception If there is an error deleting the schema
*
* @return JSONResponse An empty JSON response
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @psalm-return JSONResponse<200|409|500, array{error?: string}, array<never, never>>
*/
public function destroy(int $id): JSONResponse
{
try {
// Find the schema by ID, delete it, and invalidate caches.
$schemaToDelete = $this->schemaMapper->find(id: $id);
$this->schemaMapper->delete($schemaToDelete);
// **CACHE INVALIDATION**: Clear all schema-related caches when schema is deleted.
$this->schemaCacheService->invalidateForSchemaChange(
schemaId: $schemaToDelete->getId(),
operation: 'delete'
);
$this->facetCacheSvc->invalidateForSchemaChange(
schemaId: $schemaToDelete->getId(),
operation: 'delete'
);
// Return an empty response.
return new JSONResponse(data: []);
} catch (\OCA\OpenRegister\Exception\ValidationException $e) {
// Return 409 Conflict for cascade protection (objects still attached).
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 409);
} catch (\Exception $e) {
// Return 500 for other errors.
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 500);
}//end try
}//end destroy()
/**
* Updates an existing Schema object using a json text/string as input
*
* Uses 'file', 'url' or else 'json' from POST body.
*
* @param int|null $id The ID of the schema to update, or null for a new schema
*
* @throws Exception If there is a database error
*
* @throws GuzzleException If there is an HTTP request error
*
* @return JSONResponse The JSON response with the updated schema
*
* @NoAdminRequired
*
* @NoCSRFRequired
*/
public function uploadUpdate(?int $id=null): JSONResponse
{
return $this->upload(id: $id);
}//end uploadUpdate()
/**
* Creates a new Schema object or updates an existing one
*
* Uses 'file', 'url' or else 'json' from POST body.
*
* @param int|null $id The ID of the schema to update, or null for a new schema
*
* @throws Exception If there is a database error
*
* @throws GuzzleException If there is an HTTP request error
*
* @return JSONResponse The JSON response with the created or updated schema
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @suppressWarnings(PHPMD.StaticAccess) Uuid::v4 and DatabaseConstraintException factory are standard patterns
* @suppressWarnings(PHPMD.ExcessiveMethodLength)
* @suppressWarnings(PHPMD.CyclomaticComplexity)
* @suppressWarnings(PHPMD.NPathComplexity)
*/
public function upload(?int $id=null): JSONResponse
{
if ($id !== null) {
// If ID is provided, find the existing schema.
$schema = $this->schemaMapper->find($id);
} else {
// Otherwise, create a new schema.
$schema = new Schema();
$schema->setUuid(Uuid::v4()->toRfc4122());
}
// Get the uploaded JSON data.
$phpArray = $this->uploadService->getUploadedJson($this->request->getParams());
if ($phpArray instanceof JSONResponse) {
// Return any error response from the upload service.
return $phpArray;
}
// Set default title if not provided or empty.
if (empty($phpArray['title']) === true) {
$phpArray['title'] = 'New Schema';
}
try {
// Update the schema with the data from the uploaded JSON.
$schema->hydrate($phpArray);
// Track whether this is a new schema before potential insert.
$isNewSchema = ($schema->getId() === null);
if ($isNewSchema === true) {
// Insert a new schema if no ID is set.
$schema = $this->schemaMapper->insert($schema);
// Set organisation from active organisation for multi-tenancy (if not already set).
if ($schema->getOrganisation() === null || $schema->getOrganisation() === '') {
$organisationUuid = $this->organisationService->getOrganisationForNewEntity();
$schema->setOrganisation($organisationUuid);
$schema = $this->schemaMapper->update($schema);
}
// **CACHE INVALIDATION**: Clear all schema-related caches when schema is created.
$this->schemaCacheService->invalidateForSchemaChange(schemaId: $schema->getId(), operation: 'create');
$this->facetCacheSvc->invalidateForSchemaChange(schemaId: $schema->getId(), operation: 'create');
}
if ($isNewSchema === false) {
// Update the existing schema.
$schema = $this->schemaMapper->update($schema);
// **CACHE INVALIDATION**: Clear all schema-related caches when schema is updated.
$this->schemaCacheService->invalidateForSchemaChange(schemaId: $schema->getId(), operation: 'update');
$this->facetCacheSvc->invalidateForSchemaChange(
schemaId: $schema->getId(),
operation: 'update'
);
}
return new JSONResponse(data: $schema);
} catch (DBException $e) {
// Handle database constraint violations with user-friendly messages.
$constraintException = DatabaseConstraintException::fromDatabaseException(
dbException: $e,
entityType: 'schema'
);
return new JSONResponse(
data: ['error' => $constraintException->getMessage()],
statusCode: $constraintException->getHttpStatusCode()
);
} catch (DatabaseConstraintException $e) {
// Handle our custom database constraint exceptions.
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: $e->getHttpStatusCode());
} catch (Exception $e) {
// Log the actual error for debugging.
$this->logger->error(
message: '[SchemasController] Schema upload failed',
context: [
'file' => __FILE__,
'line' => __LINE__,
'schema_id' => $id,
'error_message' => $e->getMessage(),
'error_code' => $e->getCode(),
'trace' => $e->getTraceAsString(),
]
);
// Check if this is a validation error by examining the message.
if (str_contains($e->getMessage(), 'Invalid') === true
|| str_contains($e->getMessage(), 'must be') === true
|| str_contains($e->getMessage(), 'required') === true
|| str_contains($e->getMessage(), 'format') === true
|| str_contains($e->getMessage(), 'Property at') === true
|| str_contains($e->getMessage(), 'authorization') === true
) {
// Return 400 Bad Request for validation errors with actual error message.
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 400);
}
// For database constraint violations, return 409 Conflict.
if (str_contains($e->getMessage(), 'constraint') === true
|| str_contains($e->getMessage(), 'duplicate') === true
|| str_contains($e->getMessage(), 'unique') === true
) {
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 409);
}
// Return 500 for other unexpected errors with actual error message.
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 500);
}//end try
}//end upload()
/**
* Creates and return a json file for a Schema
*
* @param int $id The ID of the schema to return json file for
*
* @throws Exception If there is an error retrieving the schema
*
* @return JSONResponse A JSON response containing the schema as JSON
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @psalm-return JSONResponse<200, Schema,
* array<never, never>>|JSONResponse<404,
* array{error: 'Schema not found'}, array<never, never>>
*/
public function download(int $id): JSONResponse
{
// Note: Accept header not currently used - always returns JSON.
try {
// Find the schema by ID.
$schema = $this->schemaMapper->find($id);
} catch (Exception $e) {
// Return a 404 error if the schema doesn't exist.
return new JSONResponse(data: ['error' => 'Schema not found'], statusCode: 404);
}
// Return the schema as JSON.
return new JSONResponse(data: $schema);
}//end download()
/**
* Get schemas that have properties referencing the given schema
*
* This method finds schemas that contain properties with $ref values pointing
* to the specified schema, indicating a relationship between schemas.
*
* @param int|string $id The ID, UUID, or slug of the schema to find relationships for
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @return JSONResponse JSON response with related schemas
*/
public function related(int|string $id): JSONResponse
{
try {
// Find related schemas using the SchemaMapper (incoming references).
$incomingSchemas = $this->schemaMapper->getRelated($id);
$incomingSchemasArray = array_map(fn($schema) => $schema->jsonSerialize(), $incomingSchemas);
// Find outgoing references: schemas that this schema refers to.
$targetSchema = $this->schemaMapper->find($id);
$properties = $targetSchema->getProperties() ?? [];
$allSchemas = $this->schemaMapper->findAll();
$outgoingSchemas = [];
foreach ($allSchemas as $schema) {
// Skip self.
if ($schema->getId() === $targetSchema->getId()) {
continue;
}
// Use the same reference logic as getRelated, but reversed.
if ($this->schemaMapper->hasReferenceToSchema(
properties: $properties,
targetSchemaId: (string) $schema->getId(),
targetSchemaUuid: $schema->getUuid() ?? '',
targetSchemaSlug: $schema->getSlug() ?? ''
) === true
) {
$outgoingSchemas[$schema->getId()] = $schema;
}
}
$outgoingSchemasArray = array_map(fn($schema) => $schema->jsonSerialize(), array_values($outgoingSchemas));
return new JSONResponse(
data: [
'incoming' => $incomingSchemasArray,
'outgoing' => $outgoingSchemasArray,
'total' => count($incomingSchemasArray) + count($outgoingSchemasArray),
]
);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
// Return a 404 error if the target schema doesn't exist.
return new JSONResponse(data: ['error' => 'Schema not found'], statusCode: 404);
} catch (Exception $e) {
// Return a 500 error for other exceptions.
return new JSONResponse(data: ['error' => 'Internal server error: '.$e->getMessage()], statusCode: 500);
}//end try
}//end related()
/**
* Get statistics for a specific schema
*
* @param int $id The schema ID
*
* @throws \OCP\AppFramework\Db\DoesNotExistException When the schema is not found
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @return JSONResponse JSON response with schema statistics
*/
public function stats(int $id): JSONResponse
{
try {
// Get the schema.
$schema = $this->schemaMapper->find($id);
if ($schema === null) {
return new JSONResponse(data: ['error' => 'Schema not found'], statusCode: 404);
}
// Get detailed object statistics for this schema using the existing method.
$objectStats = $this->objectEntityMapper->getStatistics(registerId: null, schemaId: $id);
// Calculate comprehensive statistics for this schema.
$stats = [
'objectCount' => $objectStats['total'],
// Keep for backward compatibility.
'objects_count' => $objectStats['total'],
// Alternative field name for compatibility.
'objects' => [
'total' => $objectStats['total'],
'invalid' => $objectStats['invalid'],
'deleted' => $objectStats['deleted'],
'published' => $objectStats['published'],
'locked' => $objectStats['locked'],
'size' => $objectStats['size'],
],
'logs' => $this->auditTrailMapper->getStatistics(registerId: null, schemaId: $id),
'files' => ['total' => 0, 'size' => 0],
// Placeholder for future file statistics.
'registers' => $this->schemaMapper->getRegisterCountPerSchema()[$id] ?? 0,
];
return new JSONResponse(data: $stats);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return new JSONResponse(data: ['error' => 'Schema not found'], statusCode: 404);
} catch (Exception $e) {
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 500);
}//end try
}//end stats()
/**
* Explore schema properties to discover new properties in objects
*
* Analyzes all objects belonging to a schema to discover properties that exist
* in the object data but are not defined in the schema. This is useful for
* identifying properties that were added during imports or when validation
* was disabled.
*
* @param int $id The ID of the schema to explore
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @return JSONResponse JSON response with exploration results
*/
public function explore(int $id): JSONResponse
{
try {
$this->logger->info(
message: '[SchemasController] Starting schema exploration for schema ID: '.$id,
context: ['file' => __FILE__, 'line' => __LINE__]
);
$explorationResults = $this->schemaService->exploreSchemaProperties($id);
$this->logger->info(
message: '[SchemasController] Schema exploration completed successfully',
context: ['file' => __FILE__, 'line' => __LINE__]
);
return new JSONResponse(data: $explorationResults);
} catch (\Exception $e) {
$this->logger->error(
message: '[SchemasController] Schema exploration failed: '.$e->getMessage(),
context: ['file' => __FILE__, 'line' => __LINE__]
);
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 500);
}//end try
}//end explore()
/**
* Update schema properties based on exploration results
*
* Applies user-confirmed property updates to a schema based on exploration
* results. This allows schemas to be updated with newly discovered properties.
*
* @param int $id The ID of the schema to update
*
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @return JSONResponse JSON response with updated schema
*/
public function updateFromExploration(int $id): JSONResponse
{
try {
// Get property updates from request.
$propertyUpdates = $this->request->getParam(key: 'properties', default: []);
if (empty($propertyUpdates) === true) {
return new JSONResponse(data: ['error' => 'No property updates provided'], statusCode: 400);
}
$updateCount = count($propertyUpdates);
$this->logger->info(
message: "[SchemasController] Updating schema {$id} with {$updateCount} property updates",
context: ['file' => __FILE__, 'line' => __LINE__]
);
$updatedSchema = $this->schemaService->updateSchemaFromExploration(
schemaId: $id,
propertyUpdates: $propertyUpdates
);
// Clear schema cache to ensure fresh data.
$this->schemaCacheService->clearSchemaCache($id);
$this->logger->info(
message: '[SchemasController] Schema '.$id.' successfully updated with exploration results',
context: ['file' => __FILE__, 'line' => __LINE__]
);
return new JSONResponse(
data: [
'success' => true,
'schema' => $updatedSchema->jsonSerialize(),
'message' => 'Schema updated successfully with '.count($propertyUpdates).' properties',
]
);
} catch (\Exception $e) {
$this->logger->error(
message: '[SchemasController] Failed to update schema from exploration: '.$e->getMessage(),
context: ['file' => __FILE__, 'line' => __LINE__]
);