Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ class SchemaListener implements IEventListener {
// Handle the event
}
}
```
```

### 2. Register the Event Listener

After creating your listener class, you need to register it with Nextcloud's event dispatcher system. This is done in your application's `lib/AppInfo/Application.php` file by registering the listener in the `register()` method:

```php
$container->query(EventDispatcherInterface::class)->addListener(SchemaCreatedEvent::class, [SchemaListener::class, 'handle']);
/* @var IEventDispatcher $dispatcher */
$dispatcher = $this->getContainer()->get(IEventDispatcher::class);
$dispatcher->addServiceListener(eventName: SchemaCreatedEvent::class, className: SchemaCreatedEvent::class);
```

This line registers the `SchemaListener` class to handle `SchemaCreatedEvent` events. You can add similar lines for other events to register your listener for those specific events.
Expand Down
22 changes: 11 additions & 11 deletions lib/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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;
Expand Down Expand Up @@ -111,7 +111,7 @@ public function index(ObjectService $objectService, SearchService $searchService
public function show(string $id): JSONResponse
{
try {
return new JSONResponse($this->objectEntityMapper->find(idOrUuid: (int) $id)->getObjectArray());
return new JSONResponse($this->objectEntityMapper->find((int) $id)->getObjectArray());
} catch (DoesNotExistException $exception) {
return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public function create(ObjectService $objectService): JSONResponse
unset($data['id']);
}

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

if ($mapping !== null && $mappingService !== null) {
Expand Down Expand Up @@ -195,7 +195,7 @@ public function update(int $id, ObjectService $objectService): JSONResponse
unset($data['id']);
}

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

if ($mapping !== null && $mappingService !== null) {
Expand All @@ -211,9 +211,9 @@ public function update(int $id, ObjectService $objectService): JSONResponse
return new JSONResponse(['message' => $exception->getMessage(), 'validationErrors' => $formatter->format($exception->getErrors())], 400);
}

$this->auditTrailMapper->createAuditTrail(new: $objectEntity, old: $oldObject);
// $this->auditTrailMapper->createAuditTrail(new: $objectEntity, old: $oldObject);

return new JSONResponse($objectEntity->getOBjectArray());
return new JSONResponse($objectEntity->getObjectArray());
}

/**
Expand Down Expand Up @@ -330,11 +330,11 @@ public function logs(int $id): JSONResponse
}
}



/**
* Retrieves all available mappings
*
*
* This method returns a JSON response containing all available mappings in the system.
*
* @NoAdminRequired
Expand All @@ -346,15 +346,15 @@ 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,
Expand Down
41 changes: 29 additions & 12 deletions lib/Db/ObjectEntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ObjectEntityMapper extends QBMapper
* @param IEventDispatcher $eventDispatcher The event dispatcher
*/
public function __construct(
IDBConnection $db,
IDBConnection $db,
MySQLJsonService $mySQLJsonService,
IEventDispatcher $eventDispatcher
) {
Expand Down Expand Up @@ -248,6 +248,19 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
return $this->findEntities(query: $qb);
}

/**
* @inheritDoc
*/
public function insert(Entity $entity): Entity
{
$entity = parent::insert($entity);
// Dispatch creation event
$this->eventDispatcher->dispatchTyped(new ObjectCreatedEvent($entity));

return $entity;

}

/**
* Creates an object from an array
*
Expand All @@ -264,15 +277,25 @@ public function createFromArray(array $object): ObjectEntity

$obj = $this->insert($obj);

// Dispatch creation event
$this->eventDispatcher->dispatch(
ObjectCreatedEvent::class,
new ObjectCreatedEvent($obj)
);

return $obj;
}

/**
* @inheritDoc
*/
public function update(Entity $entity): Entity
{
$oldObject = $this->find($entity->getId());

$entity = parent::update($entity);
// Dispatch creation event
$this->eventDispatcher->dispatchTyped(new ObjectUpdatedEvent($entity, $oldObject));

return $entity;

}

/**
* Updates an object from an array
*
Expand All @@ -295,12 +318,6 @@ public function updateFromArray(int $id, array $object): ObjectEntity

$newObject = $this->update($newObject);

// Dispatch update event
$this->eventDispatcher->dispatch(
ObjectUpdatedEvent::class,
new ObjectUpdatedEvent($newObject, $oldObject)
);

return $newObject;
}

Expand Down
60 changes: 36 additions & 24 deletions lib/Db/RegisterMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace OCA\OpenRegister\Db;

use OCA\OpenRegister\Db\Register;
use OCA\OpenRegister\Event\SchemaCreatedEvent;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCA\OpenRegister\Db\SchemaMapper;
use OCA\OpenRegister\Db\Schema;
use OCP\IDBConnection;
use Symfony\Component\Uid\Uuid;
use OCP\EventDispatcher\IEventDispatcher;
Expand All @@ -33,7 +32,7 @@ class RegisterMapper extends QBMapper
* @param IEventDispatcher $eventDispatcher The event dispatcher
*/
public function __construct(
IDBConnection $db,
IDBConnection $db,
SchemaMapper $schemaMapper,
IEventDispatcher $eventDispatcher
) {
Expand Down Expand Up @@ -106,6 +105,19 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
return $this->findEntities(query: $qb);
}

/**
* @inheritdoc
*/
public function insert(Entity $entity): Entity
{
$entity = parent::insert($entity);

// Dispatch creation event
$this->eventDispatcher->dispatchTyped(new RegisterCreatedEvent($entity));

return $entity;
}

/**
* Create a new register from an array of data
*
Expand All @@ -122,16 +134,24 @@ public function createFromArray(array $object): Register
}

$register = $this->insert(entity: $register);

// Dispatch creation event
$this->eventDispatcher->dispatch(
RegisterCreatedEvent::class,
new RegisterCreatedEvent($register)
);

return $register;
}

/**
* @inheritdoc
*/
public function update(Entity $entity): Entity
{
$oldSchema = $this->find($entity->getId());
$entity = parent::update($entity);

// Dispatch update event
$this->eventDispatcher->dispatchTyped(new RegisterUpdatedEvent($entity, $oldSchema));

return $entity;
}

/**
* Update an existing register from an array of data
*
Expand All @@ -141,8 +161,7 @@ public function createFromArray(array $object): Register
*/
public function updateFromArray(int $id, array $object): Register
{
$oldRegister = $this->find($id);
$newRegister = clone $oldRegister;
$newRegister = $this->find($id);
$newRegister->hydrate($object);

if (isset($object['version']) === false) {
Expand All @@ -152,30 +171,23 @@ public function updateFromArray(int $id, array $object): Register
}

$newRegister = $this->update($newRegister);

// Dispatch update event
$this->eventDispatcher->dispatch(
RegisterUpdatedEvent::class,
new RegisterUpdatedEvent($newRegister, $oldRegister)
);

return $newRegister;
}

/**
* Delete a register
*
* @param Register $register The register to delete
* @param Register $entity The register to delete
* @return Register The deleted register
*/
public function delete(Entity $register): Register
public function delete(Entity $entity): Register
{
$result = parent::delete($register);
$result = parent::delete($entity);

// Dispatch deletion event
$this->eventDispatcher->dispatch(
RegisterDeletedEvent::class,
new RegisterDeletedEvent($register)
$this->eventDispatcher->dispatchTyped(
new RegisterDeletedEvent($entity)
);

return $result;
Expand Down
49 changes: 31 additions & 18 deletions lib/Db/SchemaMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
return $this->findEntities(query: $qb);
}

/**
* @inheritdoc
*/
public function insert(Entity $entity): Entity
{
$entity = parent::insert($entity);

// Dispatch creation event
$this->eventDispatcher->dispatchTyped(new SchemaCreatedEvent($entity));

return $entity;
}

/**
* Creates a schema from an array
*
Expand All @@ -127,16 +140,24 @@ public function createFromArray(array $object): Schema
}

$schema = $this->insert(entity: $schema);

// Dispatch creation event
$this->eventDispatcher->dispatch(
SchemaCreatedEvent::class,
new SchemaCreatedEvent($schema)
);

return $schema;
}

/**
* @inheritdoc
*/
public function update(Entity $entity): Entity
{
$oldSchema = $this->find($entity->getId());
$entity = parent::update($entity);

// Dispatch update event
$this->eventDispatcher->dispatchTyped(new SchemaUpdatedEvent($entity, $oldSchema));

return $entity;
}

/**
* Updates a schema from an array
*
Expand All @@ -146,8 +167,7 @@ public function createFromArray(array $object): Schema
*/
public function updateFromArray(int $id, array $object): Schema
{
$oldSchema = $this->find($id);
$newSchema = clone $oldSchema;
$newSchema = $this->find($id);
$newSchema->hydrate($object);

if (isset($object['version']) === false) {
Expand All @@ -157,12 +177,6 @@ public function updateFromArray(int $id, array $object): Schema
}

$newSchema = $this->update($newSchema);

// Dispatch update event
$this->eventDispatcher->dispatch(
SchemaUpdatedEvent::class,
new SchemaUpdatedEvent($newSchema, $oldSchema)
);

return $newSchema;
}
Expand All @@ -173,13 +187,12 @@ public function updateFromArray(int $id, array $object): Schema
* @param Schema $schema The schema to delete
* @return Schema The deleted schema
*/
public function delete(Entity $schema): Schema
public function delete(Entity $schema): Schema
{
$result = parent::delete($schema);

// Dispatch deletion event
$this->eventDispatcher->dispatch(
SchemaDeletedEvent::class,
$this->eventDispatcher->dispatchTyped(
new SchemaDeletedEvent($schema)
);

Expand Down