Skip to content

Commit

Permalink
Merge branch 'hotfix/load-directory'
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Nov 1, 2024
2 parents 22e952c + e0a2cfa commit c028e4a
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 202 deletions.
14 changes: 12 additions & 2 deletions lib/Db/AttachmentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(IDBConnection $db)
* @throws DoesNotExistException If the entity is not found
* @throws MultipleObjectsReturnedException If multiple entities are found
*/
public function find($id): Attachment
public function find($id): Attachment|null
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -50,7 +50,11 @@ public function find($id): Attachment
$qb->expr()->eq('uuid', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
));

return $this->findEntity(query: $qb);
try {
return $this->findEntity($qb);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return null;
}
}

/**
Expand Down Expand Up @@ -125,6 +129,12 @@ public function createFromArray(array $object): Attachment
public function updateFromArray(int $id, array $object, bool $updateVersion = true): Attachment
{
$attachment = $this->find($id);
// Fallback to create if the attachment does not exist
if ($attachment === null) {
$object['uuid'] = $id;
return $this->createFromArray($object);
}

$attachment->hydrate($object);

if ($updateVersion === true) {
Expand Down
16 changes: 13 additions & 3 deletions lib/Db/CatalogMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(IDBConnection $db)
* @param int|string $id The ID or UUID of the Catalog
* @return Catalog The found Catalog entity
*/
public function find($id): Catalog
public function find($id): Catalog|null
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -46,7 +46,11 @@ public function find($id): Catalog
$qb->expr()->eq('uuid', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
));

return $this->findEntity(query: $qb);
try {
return $this->findEntity($qb);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return null;
}
}

/**
Expand Down Expand Up @@ -138,7 +142,13 @@ public function createFromArray(array $object): Catalog
*/
public function updateFromArray(int $id, array $object, bool $updateVersion = true): Catalog
{
$catalog = $this->find($id);
$catalog = $this->find($id);
// Fallback to create if the catalog does not exist
if ($catalog === null) {
$object['uuid'] = $id;
return $this->createFromArray($object);
}

$catalog->hydrate($object);

if ($updateVersion === true) {
Expand Down
14 changes: 12 additions & 2 deletions lib/Db/ListingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(IDBConnection $db)
* @param int|string $id The ID or UUID of the Listing
* @return Listing The found Listing entity
*/
public function find($id): Listing
public function find($id): Listing|null
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -49,7 +49,11 @@ public function find($id): Listing
$qb->expr()->eq('uuid', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
));

return $this->findEntity(query: $qb);
try {
return $this->findEntity($qb);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return null;
}
}

/**
Expand Down Expand Up @@ -233,6 +237,12 @@ public function createFromArray(array $object): Listing
public function updateFromArray(int|string $id, array $object, bool $updateVersion = true): Listing
{
$listing = $this->find($id);
// Fallback to create if the listing does not exist
if ($listing === null) {
$object['uuid'] = $id;
return $listing = $this->createFromArray($object);
}

$listing->hydrate($object);

if ($updateVersion === true) {
Expand Down
14 changes: 12 additions & 2 deletions lib/Db/OrganizationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(IDBConnection $db)
* @throws \OCP\AppFramework\Db\DoesNotExistException If the entity is not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException If multiple entities are found
*/
public function find($id): Organization
public function find($id): Organization|null
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -48,7 +48,11 @@ public function find($id): Organization
$qb->expr()->eq('uuid', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
));

return $this->findEntity(query: $qb);
try {
return $this->findEntity($qb);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return null;
}
}

/**
Expand Down Expand Up @@ -134,6 +138,12 @@ public function createFromArray(array $object): Organization
public function updateFromArray(int $id, array $object, bool $updateVersion = true): Organization
{
$organization = $this->find($id);
// Fallback to create if the organization does not exist
if ($organization === null) {
$object['uuid'] = $id;
return $this->createFromArray($object);
}

$organization->hydrate($object);

if ($updateVersion === true) {
Expand Down
14 changes: 12 additions & 2 deletions lib/Db/PublicationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(IDBConnection $db)
* @param int|string $id The ID or UUID of the Publication
* @return Publication The found Publication entity
*/
public function find($id): Publication
public function find($id): Publication|null
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -47,7 +47,11 @@ public function find($id): Publication
$qb->expr()->eq('uuid', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
));

return $this->findEntity($qb);
try {
return $this->findEntity($qb);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return null;
}
}

/**
Expand Down Expand Up @@ -223,6 +227,12 @@ public function createFromArray(array $object): Publication
public function updateFromArray(int $id, array $object, bool $updateVersion = true): Publication
{
$publication = $this->find(id: $id);
// Fallback to create if the publication does not exist
if ($publication === null) {
$object['uuid'] = $id;
return $this->createFromArray($object);
}

$publication->hydrate(object: $object);

if ($updateVersion === true) {
Expand Down
14 changes: 12 additions & 2 deletions lib/Db/PublicationTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(IDBConnection $db)
* @throws DoesNotExistException If the entity is not found
* @throws MultipleObjectsReturnedException If multiple entities are found
*/
public function find($id, ?array $extend = []): PublicationType
public function find($id, ?array $extend = []): PublicationType|null
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -49,7 +49,11 @@ public function find($id, ?array $extend = []): PublicationType
$qb->expr()->eq('uuid', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
));

$entity = $this->findEntity(query: $qb);
try {
return $this->findEntity($qb);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return null;
}

// TODO: Implement extending functionality
if (!empty($extend)) {
Expand Down Expand Up @@ -154,6 +158,12 @@ public function createFromArray(array $object): PublicationType
public function updateFromArray(int $id, array $object, bool $updateVersion = true): PublicationType
{
$publicationType = $this->find($id);
// Fallback to create if the publication type does not exist
if ($publicationType === null) {
$object['uuid'] = $id;
return $this->createFromArray($object);
}

$publicationType->hydrate($object);

if ($updateVersion === true) {
Expand Down
14 changes: 12 additions & 2 deletions lib/Db/ThemeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(IDBConnection $db)
* @throws DoesNotExistException If the entity is not found
* @throws MultipleObjectsReturnedException If multiple entities are found
*/
public function find(int $id): Theme
public function find(int $id): Theme|null
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -49,7 +49,11 @@ public function find(int $id): Theme
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);

return $this->findEntity(query: $qb);
try {
return $this->findEntity($qb);
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return null;
}
}

/**
Expand Down Expand Up @@ -140,6 +144,12 @@ public function createFromArray(array $object): Theme
public function updateFromArray(int $id, array $object, bool $updateVersion = true): Theme
{
$theme = $this->find($id);
// Fallback to create if the theme does not exist
if ($theme === null) {
$object['uuid'] = $id;
return $this->createFromArray($object);
}

$theme->hydrate($object);

if ($updateVersion === true) {
Expand Down
34 changes: 20 additions & 14 deletions lib/Service/DirectoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,18 @@ public function validateExternalListing(array $listing): bool
* @return array The updated listing
* @throws DoesNotExistException|MultipleObjectsReturnedException|ContainerExceptionInterface|NotFoundExceptionInterface
*/
public function updateListing(array $newListing, Listing $oldListing): array{
$filteredOldListing = $this->getDirectoryFromListing($oldListing->jsonSerialize());

public function updateListing(array $newListing, array $oldListing): array{
// Let's see if these changed by checking them against the hash
$newHash = hash('sha256', json_encode($newListing));
$oldHash = hash('sha256', json_encode($filteredOldListing));
$oldHash = hash('sha256', json_encode($oldListing));
if ($newHash === $oldHash) {
return $oldListing->jsonSerialize();
return $oldListing;
}

// If we get here, the listing has changed
$oldListing->hydrate($newListing);
// Do not update version, because we copy the version from the source
$listing = $this->objectService->saveObject('listing', $oldListing->jsonSerialize(), false);
$newListing = $this->objectService->saveObject('listing', $oldListing, false);

return $listing->jsonSerialize();
return $newListing->jsonSerialize();
}

/**
Expand Down Expand Up @@ -389,16 +385,26 @@ public function syncExternalDirectory(string $url): array

// Check if we already have this listing
// TODO: This is tricky because it requires a local database call so won't work with open registers
$oldListing = $this->objectService->getObjects(
$oldListings = $this->objectService->getObjects(
objectType: 'listing',
limit: 1,
filters: [
'id'=>$listing['id'],
'directory'=>$listing['directory']
'catalog'=>$listing['id'],
]
);
if ($oldListing !== null && is_array($oldListing) && !empty($oldListing)) {
$this->updateListing($listing, $oldListing[0]);

// Fallback to create if the listing does not exist
$oldListing = null;
if (count($oldListings) > 0) {
$oldListing = $oldListings[0];
} else {
$listing['hash'] = hash('sha256', json_encode($listing));
$listing['catalog'] = $listing['id'];
unset($listing['id']);
}

if ($oldListing !== null) {
$this->updateListing($listing, $oldListing);
// @todo listing will be added to updatedList even if nothing changed...
$updatedListings[] = $listing['directory'].'/'.$listing['id'];

Expand Down
Loading

0 comments on commit c028e4a

Please sign in to comment.