Skip to content

Commit

Permalink
Merge pull request #57 from ConductionNL/feature/REGISTERS-47/object-…
Browse files Browse the repository at this point in the history
…uploading-mapping

object uploading
  • Loading branch information
remko48 authored Nov 14, 2024
2 parents b68a929 + a50bcab commit 6b080af
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 1 deletion.
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
['name' => 'dashboard#page', 'url' => '/', 'verb' => 'GET'],
['name' => 'registers#objects', 'url' => '/api/registers-objects/{register}/{schema}', 'verb' => 'GET'],
['name' => 'objects#logs', 'url' => '/api/objects-logs/{id}', 'verb' => 'GET', 'requirements' => ['id' => '[^/]+']],
['name' => 'objects#mappings', 'url' => '/api/objects/mappings', 'verb' => 'GET'],
['name' => 'objects#auditTrails', 'url' => '/api/objects/audit-trails/{id}', 'verb' => 'GET', 'requirements' => ['id' => '[^/]+']],
['name' => 'schemas#upload', 'url' => '/api/schemas/upload', 'verb' => 'POST'],
['name' => 'schemas#uploadUpdate', 'url' => '/api/schemas/{id}/upload', 'verb' => 'PUT', 'requirements' => ['id' => '[^/]+']],
Expand Down
84 changes: 83 additions & 1 deletion lib/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\DB\Exception;
use OCP\IAppConfig;
use OCP\IRequest;
use OCP\IRequest;
use OCP\App\IAppManager;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Opis\JsonSchema\Errors\ErrorFormatter;
use Symfony\Component\Uid\Uuid;
use Psr\Container\ContainerInterface;

class ObjectsController extends Controller
{
Expand All @@ -32,6 +36,8 @@ public function __construct(
$appName,
IRequest $request,
private readonly IAppConfig $config,
private readonly IAppManager $appManager,
private readonly ContainerInterface $container,
private readonly ObjectEntityMapper $objectEntityMapper,
private readonly AuditTrailMapper $auditTrailMapper,
private readonly ObjectAuditLogMapper $objectAuditLogMapper
Expand Down Expand Up @@ -126,6 +132,9 @@ public function create(ObjectService $objectService): JSONResponse
{
$data = $this->request->getParams();
$object = $data['object'];
$mapping = $data['mapping'] ?? null;
$register = $data['register'];
$schema = $data['schema'];

foreach ($data as $key => $value) {
if (str_starts_with($key, '_')) {
Expand All @@ -137,6 +146,17 @@ public function create(ObjectService $objectService): JSONResponse
unset($data['id']);
}

// If mapping ID is provided, transform the object using the mapping
$mappingService = $this->getOpenConnectorMappingService();

if ($mapping !== null && $mappingService !== null) {
$mapping = $mappingService->getMapping($mapping);

$object = $mappingService->executeMapping($mapping, $object);
$data['register'] = $register;
$data['schema'] = $schema;
}

// Save the object
try {
$objectEntity = $objectService->saveObject(register: $data['register'], schema: $data['schema'], object: $object);
Expand Down Expand Up @@ -164,6 +184,7 @@ public function update(int $id): JSONResponse
{
$data = $this->request->getParams();
$object = $data['object'];
$mapping = $data['mapping'] ?? null;

foreach ($data as $key => $value) {
if (str_starts_with($key, '_')) {
Expand All @@ -174,6 +195,14 @@ public function update(int $id): JSONResponse
unset($data['id']);
}

// If mapping ID is provided, transform the object using the mapping
$mappingService = $this->getOpenConnectorMappingService();

if ($mapping !== null && $mappingService !== null) {
$mapping = $mappingService->getMapping($mapping);
$data = $mappingService->executeMapping($mapping, $object);
}

// save it
$oldObject = $this->objectEntityMapper->find($id);
$objectEntity = $this->objectEntityMapper->updateFromArray(id: $id, object: $data);
Expand Down Expand Up @@ -266,4 +295,57 @@ public function logs(int $id): JSONResponse
return new JSONResponse(['error' => 'Logs not found'], 404);
}
}



/**
* Retrieves all available mappings
*
* This method returns a JSON response containing all available mappings in the system.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the list of mappings
*/
public function mappings(): JSONResponse
{
// Get mapping service, which will return null based on implementation
$mappingService = $this->getOpenConnectorMappingService();

// Initialize results array
$results = [];

// If mapping service exists, get all mappings using find() method
if ($mappingService !== null) {
$results = $mappingService->getMappings();
}

// Return response with results array and total count
return new JSONResponse([
'results' => $results,
'total' => count($results)
]);
}

/**
* Attempts to retrieve the OpenRegister service from the container.
*
* @return mixed|null The OpenRegister service if available, null otherwise.
* @throws ContainerExceptionInterface|NotFoundExceptionInterface
*/
public function getOpenConnectorMappingService(): ?\OCA\OpenConnector\Service\MappingService
{
if (in_array(needle: 'openconnector', haystack: $this->appManager->getInstalledApps()) === true) {
try {
// Attempt to get the OpenRegister service from the container
return $this->container->get('OCA\OpenConnector\Service\MappingService');
} catch (Exception $e) {
// If the service is not available, return null
return null;
}
}

return null;
}
}
3 changes: 3 additions & 0 deletions src/modals/Modals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { navigationStore } from '../store/store.js'
<DeleteSource />
<EditObject />
<DeleteObject />
<UploadObject v-if="navigationStore.modal === 'uploadObject'" />
<ViewObjectAuditTrail v-if="navigationStore.modal === 'viewObjectAuditTrail'" />
</div>
</template>
Expand All @@ -34,6 +35,7 @@ import EditSource from './source/EditSource.vue'
import DeleteSource from './source/DeleteSource.vue'
import EditObject from './object/EditObject.vue'
import DeleteObject from './object/DeleteObject.vue'
import UploadObject from './object/UploadObject.vue'
import ViewObjectAuditTrail from './objectAuditTrail/ViewObjectAuditTrail.vue'
export default {
Expand All @@ -51,6 +53,7 @@ export default {
DeleteSource,
EditObject,
DeleteObject,
UploadObject,
ViewObjectAuditTrail,
},
}
Expand Down
Loading

0 comments on commit 6b080af

Please sign in to comment.