Skip to content
57 changes: 56 additions & 1 deletion lib/Db/AuditTrailMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,35 @@
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use Symfony\Component\Uid\Uuid;
use OCA\OpenRegister\Db\ObjectEntityMapper;

/**
* The AuditTrailMapper class
*
* @package OCA\OpenRegister\Db
*/
class AuditTrailMapper extends QBMapper
{
public function __construct(IDBConnection $db)
private $objectEntityMapper;

/**
* Constructor for the AuditTrailMapper
*
* @param IDBConnection $db The database connection
* @param ObjectEntityMapper $objectEntityMapper The object entity mapper
*/
public function __construct(IDBConnection $db, ObjectEntityMapper $objectEntityMapper)
{
parent::__construct($db, 'openregister_audit_trails');
$this->objectEntityMapper = $objectEntityMapper;
}

/**
* Finds an audit trail by id
*
* @param int $id The id of the audit trail
* @return Log The audit trail
*/
public function find(int $id): Log
{
$qb = $this->db->getQueryBuilder();
Expand All @@ -29,6 +50,16 @@ public function find(int $id): Log
return $this->findEntity(query: $qb);
}

/**
* Finds all audit trails
*
* @param int|null $limit The limit of the results
* @param int|null $offset The offset of the results
* @param array|null $filters The filters to apply
* @param array|null $searchConditions The search conditions to apply
* @param array|null $searchParams The search parameters to apply
* @return array The audit trails
*/
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -58,6 +89,30 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
return $this->findEntities(query: $qb);
}

/**
* Finds all audit trails for a given object
*
* @param string $idOrUuid The id or uuid of the object
* @return array The audit trails
*/
public function findAllUuid(string $idOrUuid, ?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
Comment thread
rjzondervan marked this conversation as resolved.
{
try {
$object = $this->objectEntityMapper->find(idOrUuid: $idOrUuid);
$objectId = $object->getId();
$filters['object'] = $objectId;
return $this->findAll($limit, $offset, $filters, $searchConditions, $searchParams);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return [];
}
}

/**
* Creates an audit trail from an array
*
* @param array $object The object to create the audit trail from
* @return Log The created audit trail
*/
public function createFromArray(array $object): Log
{
$log = new Log();
Expand Down
38 changes: 38 additions & 0 deletions lib/Db/ObjectEntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@
use OCP\IDBConnection;
use Symfony\Component\Uid\Uuid;

/**
* The ObjectEntityMapper class
*
* @package OCA\OpenRegister\Db
*/
class ObjectEntityMapper extends QBMapper
{
private IDatabaseJsonService $databaseJsonService;

public const MAIN_FILTERS = ['register', 'schema', 'uuid', 'created', 'updated'];

/**
* Constructor for the ObjectEntityMapper
*
* @param IDBConnection $db The database connection
* @param MySQLJsonService $mySQLJsonService The MySQL JSON service
*/
public function __construct(IDBConnection $db, MySQLJsonService $mySQLJsonService)
{
parent::__construct($db, 'openregister_objects');
Expand Down Expand Up @@ -103,6 +114,13 @@ public function findByRegisterAndSchema(string $register, string $schema): Objec
return $this->findEntities(query: $qb);
}

/**
* Counts all objects
*
* @param array|null $filters The filters to apply
* @param string|null $search The search string to apply
* @return int The number of objects
*/
public function countAll(?array $filters = [], ?string $search = null): int
{
$qb = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -172,6 +190,12 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
return $this->findEntities(query: $qb);
}

/**
* Creates an object from an array
*
* @param array $object The object to create
* @return ObjectEntity The created object
*/
public function createFromArray(array $object): ObjectEntity
{
$obj = new ObjectEntity();
Expand All @@ -182,6 +206,13 @@ public function createFromArray(array $object): ObjectEntity
return $this->insert($obj);
}

/**
* Updates an object from an array
*
* @param int $id The id of the object to update
* @param array $object The object to update
* @return ObjectEntity The updated object
*/
public function updateFromArray(int $id, array $object): ObjectEntity
{
$obj = $this->find($id);
Expand All @@ -195,6 +226,13 @@ public function updateFromArray(int $id, array $object): ObjectEntity
return $this->update($obj);
}

/**
* Gets the facets for the objects
*
* @param array $filters The filters to apply
* @param string|null $search The search string to apply
* @return array The facets
*/
public function getFacets(array $filters = [], ?string $search = null)
{
if(key_exists(key: 'register', array: $filters) === true) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Db/RegisterMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
use OCP\IDBConnection;
use Symfony\Component\Uid\Uuid;

/**
* The RegisterMapper class
*
* @package OCA\OpenRegister\Db
*/
class RegisterMapper extends QBMapper
{
private $schemaMapper;
Expand Down
53 changes: 29 additions & 24 deletions lib/Db/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,38 @@ public function hydrate(array $object): self
return $this;
}


/**
* Serializes the schema to an array
*
* @return array
*/
public function jsonSerialize(): array
{
$properties = [];
foreach ($this->properties as $key => $property) {
$properties[$key] = $property;
if (isset($property['type']) === false) {
$properties[$key] = $property;
continue;
}
switch ($property['format']) {
case 'string':
// For now array as string
case 'array':
$properties[$key]['default'] = (string) $property;
break;
case 'int':
case 'integer':
case 'number':
$properties[$key]['default'] = (int) $property;
break;
case 'bool':
$properties[$key]['default'] = (bool) $property;
break;

}
}
if (isset($this->properties) === true) {
foreach ($this->properties as $key => $property) {
$properties[$key] = $property;
if (isset($property['type']) === false) {
$properties[$key] = $property;
continue;
}
switch ($property['format']) {
case 'string':
// For now array as string
case 'array':
$properties[$key]['default'] = (string) $property;
break;
case 'int':
case 'integer':
case 'number':
$properties[$key]['default'] = (int) $property;
break;
case 'bool':
$properties[$key]['default'] = (bool) $property;
break;
}
Comment thread
rjzondervan marked this conversation as resolved.
}
}

$array = [
'id' => $this->id,
Expand Down
47 changes: 47 additions & 0 deletions lib/Db/SchemaMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@
use OCP\IDBConnection;
use Symfony\Component\Uid\Uuid;

/**
* The SchemaMapper class
*
* @package OCA\OpenRegister\Db
*/
class SchemaMapper extends QBMapper
{
/**
* Constructor for the SchemaMapper
*
* @param IDBConnection $db The database connection
*/
public function __construct(IDBConnection $db)
{
parent::__construct($db, 'openregister_schemas');
}

/**
* Finds a schema by id
*
* @param int $id The id of the schema
* @return Schema The schema
*/
public function find(int $id): Schema
{
$qb = $this->db->getQueryBuilder();
Expand All @@ -28,6 +44,13 @@ public function find(int $id): Schema

return $this->findEntity(query: $qb);
}

/**
* Finds multiple schemas by id
*
* @param array $ids The ids of the schemas
* @return array The schemas
*/
public function findMultiple(array $ids): array
{
$result = [];
Expand All @@ -38,6 +61,17 @@ public function findMultiple(array $ids): array
return $result;
}


/**
* Finds all schemas
*
* @param int|null $limit The limit of the results
* @param int|null $offset The offset of the results
* @param array|null $filters The filters to apply
* @param array|null $searchConditions The search conditions to apply
* @param array|null $searchParams The search parameters to apply
* @return array The schemas
*/
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -67,6 +101,12 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
return $this->findEntities(query: $qb);
}

/**
* Creates a schema from an array
*
* @param array $object The object to create
* @return Schema The created schema
*/
public function createFromArray(array $object): Schema
{
$schema = new Schema();
Expand All @@ -80,6 +120,13 @@ public function createFromArray(array $object): Schema
return $this->insert(entity: $schema);
}

/**
* Updates a schema from an array
*
* @param int $id The id of the schema to update
* @param array $object The object to update
* @return Schema The updated schema
*/
public function updateFromArray(int $id, array $object): Schema
{
$obj = $this->find($id);
Expand Down
Loading