Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major cleanup on subobjects #101

Open
wants to merge 8 commits into
base: development
Choose a base branch
from
Open
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
200 changes: 200 additions & 0 deletions lib/Db/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

namespace OCA\OpenRegister\Db;

use DateTime;
use Exception;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
use OCP\IURLGenerator;

class File extends Entity implements JsonSerializable
{
/**
* @var string The unique identifier for the file.
*/
protected string $uuid;

/**
* @var string The name of the file.
*/
protected string $filename;

/**
* @var string The URL to download the file.
*/
protected string $downloadUrl;

/**
* @var string The URL to share the file.
*/
protected string $shareUrl;

/**
* @var string The URL to access the file.
*/
protected string $accessUrl;

/**
* @var string The file extension (e.g., .txt, .jpg).
*/
protected string $extension;

/**
* @var string The checksum of the file for integrity verification.
*/
protected string $checksum;

/**
* @var string The source of the file.
*/
protected string $source;

/**
* @var string The ID of the user associated with the file.
*/
protected string $userId;

/**
* @var DateTime The date and time when the file was created.
*/
protected DateTime $created;

/**
* @var DateTime The date and time when the file was last updated.
*/
protected DateTime $updated;

/**
* Constructor for the File entity.
*/
public function __construct() {
$this->addType('uuid', 'string');
$this->addType('filename', 'string');
$this->addType('downloadUrl', 'string');
$this->addType('shareUrl', 'string');
$this->addType('accessUrl', 'string');
$this->addType('extension', 'string');
$this->addType('checksum', 'string');
$this->addType('source', 'int');
$this->addType('userId', 'string');
$this->addType('created', 'datetime');
$this->addType('updated', 'datetime');
}

/**
* Retrieves the fields that should be treated as JSON.
*
* @return array List of JSON field names.
*/
public function getJsonFields(): array
{
return array_keys(
array_filter($this->getFieldTypes(), function ($field) {
return $field === 'json';
})
);
}

/**
* Populates the entity with data from an array.
*
* @param array $object Data to populate the entity.
*
* @return self The hydrated entity.
*/
public function hydrate(array $object): self
{
$jsonFields = $this->getJsonFields();

foreach ($object as $key => $value) {
if (in_array($key, $jsonFields) === true && $value === []) {
$value = [];
}

$method = 'set'.ucfirst($key);

try {
$this->$method($value);
} catch (Exception $exception) {
// Log or handle the exception.
}
}

return $this;
}

/**
* Serializes the entity to a JSON-compatible array.
*
* @return array The serialized entity data.
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'uuid' => $this->uuid,
'filename' => $this->filename,
'downloadUrl' => $this->downloadUrl,
'shareUrl' => $this->shareUrl,
'accessUrl' => $this->accessUrl,
'extension' => $this->extension,
'checksum' => $this->checksum,
'source' => $this->source,
'userId' => $this->userId,
'created' => isset($this->created) === true ? $this->created->format('c') : null,
'updated' => isset($this->updated) === true ? $this->updated->format('c') : null,
];
}

/**
* Generates a JSON schema for the File entity.
*
* @param IURLGenerator $IURLGenerator The URL generator instance.
*
* @return string The JSON schema as a string.
*/
public static function getSchema(IURLGenerator $IURLGenerator): string {
return json_encode([
'$id' => $IURLGenerator->getBaseUrl().'/apps/openconnector/api/files/schema',
'$schema' => 'https://json-schema.org/draft/2020-12/schema',
'type' => 'object',
'required' => [
'filename',
'accessUrl',
],
'properties' => [
'filename' => [
'type' => 'string',
'minLength' => 1,
'maxLength' => 255
],
'downloadUrl' => [
'type' => 'string',
'format' => 'uri',
],
'shareUrl' => [
'type' => 'string',
'format' => 'uri',
],
'accessUrl' => [
'type' => 'string',
'format' => 'uri',
],
'extension' => [
'type' => 'string',
'maxLength' => 10,
],
'checksum' => [
'type' => 'string',
],
'source' => [
'type' => 'number',
],
'userId' => [
'type' => 'string',
]
]
]);
}
}
154 changes: 154 additions & 0 deletions lib/Db/FileMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace OCA\OpenRegister\Db;

use OCA\OpenRegister\Db\File;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use Symfony\Component\Uid\Uuid;

class FileMapper extends QBMapper
{
/**
* Constructor for FileMapper.
*
* @param IDBConnection $db Database connection instance.
*/
public function __construct(IDBConnection $db)
{
parent::__construct($db, 'openconnector_jobs');
}

/**
* Finds a File entity by its ID.
*
* @param int $id The ID of the file to find.
*
* @return \OCA\OpenRegister\Db\File The found file entity.
* @throws Exception If a database error occurs.
* @throws DoesNotExistException If no file is found with the given ID.
* @throws MultipleObjectsReturnedException If multiple files are found with the given ID.
*/
public function find(int $id): File
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_jobs')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);

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

/**
* Retrieves all File entities with optional filtering, search, and pagination.
*
* @param int|null $limit Maximum number of results to return.
* @param int|null $offset Number of results to skip.
* @param array|null $filters Key-value pairs to filter results.
* @param array|null $searchConditions Search conditions for query.
* @param array|null $searchParams Parameters for search conditions.
*
* @return array List of File entities.
* @throws Exception If a database error occurs.
*/
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_jobs')
->setMaxResults($limit)
->setFirstResult($offset);

foreach ($filters as $filter => $value) {
if ($value === 'IS NOT NULL') {
$qb->andWhere($qb->expr()->isNotNull($filter));
} elseif ($value === 'IS NULL') {
$qb->andWhere($qb->expr()->isNull($filter));
} else {
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
}
}

if (empty($searchConditions) === false) {
$qb->andWhere('(' . implode(' OR ', $searchConditions) . ')');
foreach ($searchParams as $param => $value) {
$qb->setParameter($param, $value);
}
}

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

/**
* Creates a File entity from an array of data.
*
* @param array $object The data to create the entity from.
*
* @return \OCA\OpenRegister\Db\File The created File entity.
* @throws Exception If a database error occurs.
*/
public function createFromArray(array $object): File
{
$obj = new File();
$obj->hydrate($object);
// Set UUID
if ($obj->getUuid() === null) {
$obj->setUuid(Uuid::v4());
}
return $this->insert(entity: $obj);
}

/**
* Updates a File entity by its ID using an array of data.
*
* @param int $id The ID of the file to update.
* @param array $object The data to update the entity with.
*
* @return \OCA\OpenRegister\Db\File The updated File entity.
* @throws DoesNotExistException If no file is found with the given ID.
* @throws Exception If a database error occurs.
* @throws MultipleObjectsReturnedException If multiple files are found with the given ID.
*/
public function updateFromArray(int $id, array $object): File
{
$obj = $this->find($id);
$obj->hydrate($object);

// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int) $version[2] + 1;
$obj->setVersion(implode('.', $version));

return $this->update($obj);
}

/**
* Gets the total count of all call logs.
*
* @return int The total number of call logs in the database.
* @throws Exception If a database error occurs.
*/
public function getTotalCallCount(): int
{
$qb = $this->db->getQueryBuilder();

// Select count of all logs
$qb->select($qb->createFunction('COUNT(*) as count'))
->from('openconnector_jobs');

$result = $qb->execute();
$row = $result->fetch();

// Return the total count
return (int) $row['count'];
}
}
2 changes: 1 addition & 1 deletion lib/Db/ObjectAuditLogMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function updateFromArray(int $id, array $object): ObjectAuditLog
// Set or update the version
if (isset($object['version']) === false) {
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$version[2] = (int) $version[2] + 1;
$obj->setVersion(implode('.', $version));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Db/ObjectEntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function updateFromArray(int $id, array $object): ObjectEntity
// Set or update the version
if (isset($object['version']) === false) {
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$version[2] = (int) $version[2] + 1;
$obj->setVersion(implode('.', $version));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Db/RegisterMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function updateFromArray(int $id, array $object): Register
// Update the version
if (isset($object['version']) === false) {
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$version[2] = (int) $version[2] + 1;
$obj->setVersion(implode('.', $version));
}

Expand Down
Loading
Loading