From 2dc79e583902bd591f9c56943f9296654b0781e5 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Mon, 22 Jul 2019 11:36:26 +0200 Subject: Add \OCP\Calendar\IMetadataProvider interface and provider for common metadata keys Signed-off-by: Georg Ehrke --- lib/public/Calendar/IMetadataProvider.php | 65 +++++++++++++ lib/public/Calendar/Resource/IResourceMetadata.php | 103 +++++++++++++++++++++ lib/public/Calendar/Room/IRoom.php | 8 +- lib/public/Calendar/Room/IRoomMetadata.php | 57 ++++++++++++ 4 files changed, 229 insertions(+), 4 deletions(-) create mode 100644 lib/public/Calendar/IMetadataProvider.php create mode 100644 lib/public/Calendar/Resource/IResourceMetadata.php create mode 100644 lib/public/Calendar/Room/IRoomMetadata.php (limited to 'lib/public') diff --git a/lib/public/Calendar/IMetadataProvider.php b/lib/public/Calendar/IMetadataProvider.php new file mode 100644 index 00000000000..fcf56b47e11 --- /dev/null +++ b/lib/public/Calendar/IMetadataProvider.php @@ -0,0 +1,65 @@ + + * + * @author Georg Ehrke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\Calendar; + +/** + * Interface IMetadataProvider + * + * Provider for metadata of a resource or a room + * + * @package OCP\Calendar + * @since 17.0.0 + */ +interface IMetadataProvider { + + /** + * Get a list of all metadata keys available for this room + * + * Room backends are allowed to return custom keys, beyond the ones + * defined in this class. If they do, they should make sure to use their + * own namespace. + * + * @return String[] - A list of available keys + * @since 17.0.0 + */ + public function getAllAvailableMetadataKeys():array; + + /** + * Get whether or not a metadata key is set for this room + * + * @param string $key - The key to check for + * @return bool - Whether or not key is available + * @since 17.0.0 + */ + public function hasMetadataForKey(string $key):boolean; + + /** + * Get the value for a metadata key + * + * @param string $key - The key to check for + * @return string|null - The value stored for the key, null if no value stored + * @since 17.0.0 + */ + public function getMetadataForKey(string $key):?string; +} diff --git a/lib/public/Calendar/Resource/IResourceMetadata.php b/lib/public/Calendar/Resource/IResourceMetadata.php new file mode 100644 index 00000000000..a48bcfb6827 --- /dev/null +++ b/lib/public/Calendar/Resource/IResourceMetadata.php @@ -0,0 +1,103 @@ + + * + * @author Georg Ehrke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\Calendar\Resource; + +/** + * Interface IResourceMetadata + * + * This interface provides keys for common metadata. + * Resource Backends are not limited to this list and can provide + * any metadata they want. + * + * @package OCP\Calendar\Resource + * @since 17.0.0 + */ +interface IResourceMetadata { + + /** + * Type of resource + * + * Allowed values for this key include: + * - projector + * - tv + * - vehicle + * - other + * + * @since 17.0.0 + */ + public const RESOURCE_TYPE = '{http://nextcloud.com/ns}resource-type'; + + /** + * If resource is of type vehicle, this describes the type of vehicle + * + * Allowed values: + * - bicycle + * - scooter + * - motorbike + * - car + * - plane + * - helicopter + * - other + * + * @since 17.0.0 + */ + public const VEHICLE_TYPE = '{http://nextcloud.com/ns}resource-vehicle-type'; + + /** + * Make of the vehicle + * + * @since 17.0.0 + */ + public const VEHICLE_MAKE = '{http://nextcloud.com/ns}resource-vehicle-make'; + + /** + * Model of the vehicle + * + * @since 17.0.0 + */ + public const VEHICLE_MODEL = '{http://nextcloud.com/ns}resource-vehicle-model'; + + /** + * Whether or not the car is electric + * + * use '1' for electric, '0' for non-electric + * + * @since 17.0.0 + */ + public const VEHICLE_IS_ELECTRIC = '{http://nextcloud.com/ns}resource-vehicle-is-electric'; + + /** + * Range of vehicle with a full tank + * + * @since 17.0.0 + */ + public const VEHICLE_RANGE = '{http://nextcloud.com/ns}resource-vehicle-range'; + + /** + * Seating capacity of the vehicle + * + * @since 17.0.0 + */ + public const VEHICLE_SEATING_CAPACITY = '{http://nextcloud.com/ns}resource-vehicle-seating-capacity'; +} diff --git a/lib/public/Calendar/Room/IRoom.php b/lib/public/Calendar/Room/IRoom.php index d860bb6fc57..1475f0e7167 100644 --- a/lib/public/Calendar/Room/IRoom.php +++ b/lib/public/Calendar/Room/IRoom.php @@ -32,7 +32,7 @@ namespace OCP\Calendar\Room; interface IRoom { /** - * get the room id + * Get a unique ID for the room * * This id has to be unique within the backend * @@ -42,7 +42,7 @@ interface IRoom { public function getId():string; /** - * get the display name for a room + * Get the display name for the room * * @return string * @since 14.0.0 @@ -61,9 +61,9 @@ interface IRoom { public function getGroupRestrictions():array; /** - * get email-address for room + * Get the email-address for the room * - * The email address has to be globally unique + * The email-address has to be globally unique * * @return string * @since 14.0.0 diff --git a/lib/public/Calendar/Room/IRoomMetadata.php b/lib/public/Calendar/Room/IRoomMetadata.php new file mode 100644 index 00000000000..3d501d4d12f --- /dev/null +++ b/lib/public/Calendar/Room/IRoomMetadata.php @@ -0,0 +1,57 @@ + + * + * @author Georg Ehrke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\Calendar\Room; + +/** + * Interface IRoomMetadata + * + * This interface provides keys for common metadata. + * Room Backends are not limited to this list and can provide + * any metadata they want. + * + * @package OCP\Calendar\Room + * @since 17.0.0 + */ +interface IRoomMetadata { + + /** + * Type of room + * + * Allowed values for this key include: + * - meeting-room + * - lecture-hall + * - seminar-room + * - other + * + * @since 17.0.0 + */ + public const ROOM_TYPE = '{http://nextcloud.com/ns}room-type'; + + /** + * Seating capacity of the room + * + * @since 17.0.0 + */ + public const CAPACITY = '{http://nextcloud.com/ns}room-seating-capacity'; +} -- cgit v1.2.3 From ca060a2a8392d15e124f45a8901dc47a494fa530 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Mon, 29 Jul 2019 15:39:43 +0200 Subject: sync metadata of rooms / resources with cache Signed-off-by: Georg Ehrke --- .../UpdateCalendarResourcesRoomsBackgroundJob.php | 502 ++++++++++++--------- lib/public/Calendar/IMetadataProvider.php | 2 +- 2 files changed, 295 insertions(+), 209 deletions(-) (limited to 'lib/public') diff --git a/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php b/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php index 3a9e3def05d..1327a095056 100644 --- a/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php +++ b/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php @@ -1,6 +1,6 @@ + * @copyright 2019, Georg Ehrke * * @author Georg Ehrke * @@ -26,6 +26,8 @@ namespace OCA\DAV\BackgroundJob; use OC\BackgroundJob\TimedJob; use OCA\DAV\CalDAV\CalDavBackend; use OCP\Calendar\BackendTemporarilyUnavailableException; +use OCP\Calendar\IMetadataProvider; +use OCP\Calendar\Resource\IBackend as IResourceBackend; use OCP\Calendar\Resource\IManager as IResourceManager; use OCP\Calendar\Resource\IResource; use OCP\Calendar\Room\IManager as IRoomManager; @@ -41,23 +43,11 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { private $roomManager; /** @var IDBConnection */ - private $db; + private $dbConnection; /** @var CalDavBackend */ private $calDavBackend; - /** @var string */ - private $resourceDbTable; - - /** @var string */ - private $resourcePrincipalUri; - - /** @var string */ - private $roomDbTable; - - /** @var string */ - private $roomPrincipalUri; - /** * UpdateCalendarResourcesRoomsBackgroundJob constructor. * @@ -66,16 +56,14 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { * @param IDBConnection $dbConnection * @param CalDavBackend $calDavBackend */ - public function __construct(IResourceManager $resourceManager, IRoomManager $roomManager, - IDBConnection $dbConnection, CalDavBackend $calDavBackend) { + public function __construct(IResourceManager $resourceManager, + IRoomManager $roomManager, + IDBConnection $dbConnection, + CalDavBackend $calDavBackend) { $this->resourceManager = $resourceManager; $this->roomManager = $roomManager; - $this->db = $dbConnection; + $this->dbConnection = $dbConnection; $this->calDavBackend = $calDavBackend; - $this->resourceDbTable = 'calendar_resources'; - $this->resourcePrincipalUri = 'principals/calendar-resources'; - $this->roomDbTable = 'calendar_rooms'; - $this->roomPrincipalUri = 'principals/calendar-rooms'; // run once an hour $this->setInterval(60 * 60); @@ -84,211 +72,132 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { /** * @param $argument */ - public function run($argument) { - $this->runResources(); - $this->runRooms(); + public function run($argument):void { + $this->runForBackend( + $this->resourceManager, + 'calendar_resources', + 'calendar_resources_md', + 'resource_id', + 'principals/calendar-resources' + ); + $this->runForBackend( + $this->roomManager, + 'calendar_rooms', + 'calendar_rooms_md', + 'room_id', + 'principals/calendar-rooms' + ); } /** - * run timed job for resources + * Run background-job for one specific backendManager + * either ResourceManager or RoomManager + * + * @param IResourceManager|IRoomManager $backendManager + * @param string $dbTable + * @param string $dbTableMetadata + * @param string $foreignKey + * @param string $principalPrefix */ - private function runResources() { - $resourceBackends = $this->resourceManager->getBackends(); - $cachedResources = $this->getCached($this->resourceDbTable); - $cachedResourceIds = $this->getCachedResourceIds($cachedResources); + private function runForBackend($backendManager, + string $dbTable, + string $dbTableMetadata, + string $foreignKey, + string $principalPrefix):void { + $backends = $backendManager->getBackends(); + + foreach($backends as $backend) { + $backendId = $backend->getBackendIdentifier(); - $remoteResourceIds = []; - foreach($resourceBackends as $resourceBackend) { try { - $remoteResourceIds[$resourceBackend->getBackendIdentifier()] = - $resourceBackend->listAllResources(); + if ($backend instanceof IResourceBackend) { + $list = $backend->listAllResources(); + } else { + $list = $backend->listAllRooms(); + } } catch(BackendTemporarilyUnavailableException $ex) { - // If the backend is temporarily unavailable - // ignore this backend in this execution - unset($cachedResourceIds[$resourceBackend->getBackendIdentifier()]); + continue; } - } - - $sortedResources = $this->sortByNewDeletedExisting($cachedResourceIds, $remoteResourceIds); - foreach($sortedResources['new'] as $backendId => $newResources) { - foreach ($newResources as $newResource) { - $backend = $this->resourceManager->getBackend($backendId); - if ($backend === null) { + $cachedList = $this->getAllCachedByBackend($dbTable, $backendId); + $newIds = array_diff($list, $cachedList); + $deletedIds = array_diff($cachedList, $list); + $editedIds = array_intersect($list, $cachedList); + + foreach($newIds as $newId) { + try { + if ($backend instanceof IResourceBackend) { + $resource = $backend->getResource($newId); + } else { + $resource = $backend->getRoom($newId); + } + + $metadata = []; + if ($resource instanceof IMetadataProvider) { + $metadata = $this->getAllMetadataOfBackend($resource); + } + } catch(BackendTemporarilyUnavailableException $ex) { continue; } - $resource = $backend->getResource($newResource); - $this->addToCache($this->resourceDbTable, $resource); + $id = $this->addToCache($dbTable, $backendId, $resource); + $this->addMetadataToCache($dbTableMetadata, $foreignKey, $id, $metadata); + // we don't create the calendar here, it is created lazily + // when an event is actually scheduled with this resource / room } - } - foreach($sortedResources['deleted'] as $backendId => $deletedResources) { - foreach ($deletedResources as $deletedResource) { - $this->deleteFromCache($this->resourceDbTable, - $this->resourcePrincipalUri, $backendId, $deletedResource); - } - } - foreach($sortedResources['edited'] as $backendId => $editedResources) { - foreach ($editedResources as $editedResource) { - $backend = $this->resourceManager->getBackend($backendId); - if ($backend === null) { - continue; - } - $resource = $backend->getResource($editedResource); - $this->updateCache($this->resourceDbTable, $resource); - } - } - } + foreach($deletedIds as $deletedId) { + $id = $this->getIdForBackendAndResource($dbTable, $backendId, $deletedId); + $this->deleteFromCache($dbTable, $id); + $this->deleteMetadataFromCache($dbTableMetadata, $foreignKey, $id); - /** - * run timed job for rooms - */ - private function runRooms() { - $roomBackends = $this->roomManager->getBackends(); - $cachedRooms = $this->getCached($this->roomDbTable); - $cachedRoomIds = $this->getCachedRoomIds($cachedRooms); - - $remoteRoomIds = []; - foreach($roomBackends as $roomBackend) { - try { - $remoteRoomIds[$roomBackend->getBackendIdentifier()] = - $roomBackend->listAllRooms(); - } catch(BackendTemporarilyUnavailableException $ex) { - // If the backend is temporarily unavailable - // ignore this backend in this execution - unset($cachedRoomIds[$roomBackend->getBackendIdentifier()]); + $principalName = implode('-', [$backendId, $deletedId]); + $this->deleteCalendarDataForResource($principalPrefix, $principalName); } - } - $sortedRooms = $this->sortByNewDeletedExisting($cachedRoomIds, $remoteRoomIds); - - foreach($sortedRooms['new'] as $backendId => $newRooms) { - foreach ($newRooms as $newRoom) { - $backend = $this->roomManager->getBackend($backendId); - if ($backend === null) { + foreach($editedIds as $editedId) { + $id = $this->getIdForBackendAndResource($dbTable, $backendId, $editedId); + + try { + if ($backend instanceof IResourceBackend) { + $resource = $backend->getResource($editedId); + } else { + $resource = $backend->getRoom($editedId); + } + + $metadata = []; + if ($resource instanceof IMetadataProvider) { + $metadata = $this->getAllMetadataOfBackend($resource); + } + } catch(BackendTemporarilyUnavailableException $ex) { continue; } - $resource = $backend->getRoom($newRoom); - $this->addToCache($this->roomDbTable, $resource); - } - } - foreach($sortedRooms['deleted'] as $backendId => $deletedRooms) { - foreach ($deletedRooms as $deletedRoom) { - $this->deleteFromCache($this->roomDbTable, - $this->roomPrincipalUri, $backendId, $deletedRoom); - } - } - foreach($sortedRooms['edited'] as $backendId => $editedRooms) { - foreach ($editedRooms as $editedRoom) { - $backend = $this->roomManager->getBackend($backendId); - if ($backend === null) { - continue; - } - - $resource = $backend->getRoom($editedRoom); - $this->updateCache($this->roomDbTable, $resource); - } - } - } + $this->updateCache($dbTable, $id, $resource); - /** - * get cached db rows for resources / rooms - * @param string $tableName - * @return array - */ - private function getCached($tableName):array { - $query = $this->db->getQueryBuilder(); - $query->select('*')->from($tableName); - - $rows = []; - $stmt = $query->execute(); - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { - $rows[] = $row; - } - - return $rows; - } - - /** - * @param array $cachedResources - * @return array - */ - private function getCachedResourceIds(array $cachedResources):array { - $cachedResourceIds = []; - foreach ($cachedResources as $cachedResource) { - if (!isset($cachedResourceIds[$cachedResource['backend_id']])) { - $cachedResourceIds[$cachedResource['backend_id']] = []; - } - - $cachedResourceIds[$cachedResource['backend_id']][] = - $cachedResource['resource_id']; - } - - return $cachedResourceIds; - } - - /** - * @param array $cachedRooms - * @return array - */ - private function getCachedRoomIds(array $cachedRooms):array { - $cachedRoomIds = []; - foreach ($cachedRooms as $cachedRoom) { - if (!isset($cachedRoomIds[$cachedRoom['backend_id']])) { - $cachedRoomIds[$cachedRoom['backend_id']] = []; - } - - $cachedRoomIds[$cachedRoom['backend_id']][] = - $cachedRoom['resource_id']; - } - - return $cachedRoomIds; - } - - /** - * sort list of ids by whether they appear only in the backend / - * only in the cache / in both - * - * @param array $cached - * @param array $remote - * @return array - */ - private function sortByNewDeletedExisting(array $cached, array $remote):array { - $sorted = [ - 'new' => [], - 'deleted' => [], - 'edited' => [], - ]; - - $backendIds = array_merge(array_keys($cached), array_keys($remote)); - foreach($backendIds as $backendId) { - if (!isset($cached[$backendId])) { - $sorted['new'][$backendId] = $remote[$backendId]; - } elseif (!isset($remote[$backendId])) { - $sorted['deleted'][$backendId] = $cached[$backendId]; - } else { - $sorted['new'][$backendId] = array_diff($remote[$backendId], $cached[$backendId]); - $sorted['deleted'][$backendId] = array_diff($cached[$backendId], $remote[$backendId]); - $sorted['edited'][$backendId] = array_intersect($remote[$backendId], $cached[$backendId]); + if ($resource instanceof IMetadataProvider) { + $cachedMetadata = $this->getAllMetadataOfCache($dbTableMetadata, $foreignKey, $id); + $this->updateMetadataCache($dbTableMetadata, $foreignKey, $id, $metadata, $cachedMetadata); + } } } - - return $sorted; } /** * add entry to cache that exists remotely but not yet in cache * * @param string $table + * @param string $backendId * @param IResource|IRoom $remote + * @return int Insert id */ - private function addToCache($table, $remote) { - $query = $this->db->getQueryBuilder(); + private function addToCache(string $table, + string $backendId, + $remote):int { + $query = $this->dbConnection->getQueryBuilder(); $query->insert($table) ->values([ - 'backend_id' => $query->createNamedParameter($remote->getBackend()->getBackendIdentifier()), + 'backend_id' => $query->createNamedParameter($backendId), 'resource_id' => $query->createNamedParameter($remote->getId()), 'email' => $query->createNamedParameter($remote->getEMail()), 'displayname' => $query->createNamedParameter($remote->getDisplayName()), @@ -298,37 +207,70 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { )) ]) ->execute(); + return $query->getLastInsertId(); + } + + /** + * @param string $table + * @param string $foreignKey + * @param int $foreignId + * @param array $metadata + */ + private function addMetadataToCache(string $table, + string $foreignKey, + int $foreignId, + array $metadata):void { + foreach($metadata as $key => $value) { + $query = $this->dbConnection->getQueryBuilder(); + $query->insert($table) + ->values([ + $foreignKey => $query->createNamedParameter($foreignId), + 'key' => $query->createNamedParameter($key), + 'value' => $query->createNamedParameter($value), + ]) + ->execute(); + } } /** * delete entry from cache that does not exist anymore remotely * * @param string $table - * @param string $principalUri - * @param string $backendId - * @param string $resourceId + * @param int $id */ - private function deleteFromCache($table, $principalUri, $backendId, $resourceId) { - $query = $this->db->getQueryBuilder(); + private function deleteFromCache(string $table, + int $id):void { + $query = $this->dbConnection->getQueryBuilder(); $query->delete($table) - ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId))) - ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId))) + ->where($query->expr()->eq('id', $query->createNamedParameter($id))) ->execute(); + } - $calendar = $this->calDavBackend->getCalendarByUri($principalUri, implode('-', [$backendId, $resourceId])); - if ($calendar !== null) { - $this->calDavBackend->deleteCalendar($calendar['id']); - } + /** + * @param string $table + * @param string $foreignKey + * @param int $id + */ + private function deleteMetadataFromCache(string $table, + string $foreignKey, + int $id):void { + $query = $this->dbConnection->getQueryBuilder(); + $query->delete($table) + ->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id))) + ->execute(); } /** * update an existing entry in cache * * @param string $table + * @param int $id * @param IResource|IRoom $remote */ - private function updateCache($table, $remote) { - $query = $this->db->getQueryBuilder(); + private function updateCache(string $table, + int $id, + $remote):void { + $query = $this->dbConnection->getQueryBuilder(); $query->update($table) ->set('email', $query->createNamedParameter($remote->getEMail())) ->set('displayname', $query->createNamedParameter($remote->getDisplayName())) @@ -336,11 +278,57 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { $this->serializeGroupRestrictions( $remote->getGroupRestrictions() ))) - ->where($query->expr()->eq('backend_id', $query->createNamedParameter($remote->getBackend()->getBackendIdentifier()))) - ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($remote->getId()))) + ->where($query->expr()->eq('id', $query->createNamedParameter($id))) ->execute(); } + /** + * @param string $dbTable + * @param string $foreignKey + * @param int $id + * @param array $metadata + * @param array $cachedMetadata + */ + private function updateMetadataCache(string $dbTable, + string $foreignKey, + int $id, + array $metadata, + array $cachedMetadata):void { + $newMetadata = array_diff_key($metadata, $cachedMetadata); + $deletedMetadata = array_diff_key($cachedMetadata, $metadata); + + foreach ($newMetadata as $key => $value) { + $query = $this->dbConnection->getQueryBuilder(); + $query->insert($dbTable) + ->values([ + $foreignKey => $query->createNamedParameter($id), + 'key' => $query->createNamedParameter($key), + 'value' => $query->createNamedParameter($value), + ]) + ->execute(); + } + + foreach($deletedMetadata as $key => $value) { + $query = $this->dbConnection->getQueryBuilder(); + $query->delete($dbTable) + ->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id))) + ->andWhere($query->expr()->eq('key', $query->createNamedParameter($key))) + ->execute(); + } + + $existingKeys = array_keys(array_intersect_key($metadata, $cachedMetadata)); + foreach($existingKeys as $existingKey) { + if ($metadata[$existingKey] !== $cachedMetadata[$existingKey]) { + $query = $this->dbConnection->getQueryBuilder(); + $query->update($dbTable) + ->set('value', $query->createNamedParameter($metadata[$existingKey])) + ->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id))) + ->andWhere($query->expr()->eq('key', $query->createNamedParameter($existingKey))) + ->execute(); + } + } + } + /** * serialize array of group restrictions to store them in database * @@ -350,4 +338,102 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { private function serializeGroupRestrictions(array $groups):string { return \json_encode($groups); } + + /** + * Gets all metadata of a backend + * + * @param IResource|IRoom $resource + * @return array + */ + private function getAllMetadataOfBackend($resource):array { + if (!($resource instanceof IMetadataProvider)) { + return []; + } + + $keys = $resource->getAllAvailableMetadataKeys(); + $metadata = []; + foreach($keys as $key) { + $metadata[$key] = $resource->getMetadataForKey($key); + } + + return $metadata; + } + + /** + * @param string $table + * @param string $foreignKey + * @param int $id + * @return array + */ + private function getAllMetadataOfCache(string $table, + string $foreignKey, + int $id):array { + $query = $this->dbConnection->getQueryBuilder(); + $query->select(['key', 'value']) + ->from($table) + ->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id))); + $stmt = $query->execute(); + $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); + + $metadata = []; + foreach($rows as $row) { + $metadata[$row['key']] = $row['value']; + } + + return $metadata; + } + + /** + * Gets all cached rooms / resources by backend + * + * @param $tableName + * @param $backendId + * @return array + */ + private function getAllCachedByBackend(string $tableName, + string $backendId):array { + $query = $this->dbConnection->getQueryBuilder(); + $query->select('resource_id') + ->from($tableName) + ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId))); + $stmt = $query->execute(); + + return array_map(function($row) { + return $row['resource_id']; + }, $stmt->fetchAll(\PDO::FETCH_NAMED)); + } + + /** + * @param $principalPrefix + * @param $principalUri + */ + private function deleteCalendarDataForResource(string $principalPrefix, + string $principalUri):void { + $calendar = $this->calDavBackend->getCalendarByUri( + implode('/', [$principalPrefix, $principalUri]), + CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI); + + if ($calendar !== null) { + $this->calDavBackend->deleteCalendar($calendar['id']); + } + } + + /** + * @param $table + * @param $backendId + * @param $resourceId + * @return int + */ + private function getIdForBackendAndResource(string $table, + string $backendId, + string $resourceId):int { + $query = $this->dbConnection->getQueryBuilder(); + $query->select('id') + ->from($table) + ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId))) + ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId))); + $stmt = $query->execute(); + + return $stmt->fetch(\PDO::FETCH_NAMED)['id']; + } } diff --git a/lib/public/Calendar/IMetadataProvider.php b/lib/public/Calendar/IMetadataProvider.php index fcf56b47e11..a2c4debeb82 100644 --- a/lib/public/Calendar/IMetadataProvider.php +++ b/lib/public/Calendar/IMetadataProvider.php @@ -52,7 +52,7 @@ interface IMetadataProvider { * @return bool - Whether or not key is available * @since 17.0.0 */ - public function hasMetadataForKey(string $key):boolean; + public function hasMetadataForKey(string $key):bool; /** * Get the value for a metadata key -- cgit v1.2.3 From 475984ef1996dca4e6da7b58ae498e54ef8bd40b Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Thu, 1 Aug 2019 15:25:43 +0200 Subject: Provide standard metadata keys for location of room Signed-off-by: Georg Ehrke --- lib/public/Calendar/Room/IRoomMetadata.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'lib/public') diff --git a/lib/public/Calendar/Room/IRoomMetadata.php b/lib/public/Calendar/Room/IRoomMetadata.php index 3d501d4d12f..a6c0fa83865 100644 --- a/lib/public/Calendar/Room/IRoomMetadata.php +++ b/lib/public/Calendar/Room/IRoomMetadata.php @@ -54,4 +54,25 @@ interface IRoomMetadata { * @since 17.0.0 */ public const CAPACITY = '{http://nextcloud.com/ns}room-seating-capacity'; + + /** + * The physical address of the building this room is located in + * + * @since 17.0.0 + */ + public const BUILDING_ADDRESS = '{http://nextcloud.com/ns}room-building-address'; + + /** + * The story of the building this rooms is located in + * + * @since 17.0.0 + */ + public const BUILDING_STORY = '{http://nextcloud.com/ns}room-building-story'; + + /** + * The room-number + * + * @since 17.0.0 + */ + public const BUILDING_ROOM_NUMBER = '{http://nextcloud.com/ns}room-building-room-number'; } -- cgit v1.2.3 From b246e58a1ed1e37401503ac0212d6b61bfe83cb7 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Thu, 1 Aug 2019 15:26:11 +0200 Subject: Provide standard metadata keys for contact person of a resource Signed-off-by: Georg Ehrke --- lib/public/Calendar/Resource/IResourceMetadata.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib/public') diff --git a/lib/public/Calendar/Resource/IResourceMetadata.php b/lib/public/Calendar/Resource/IResourceMetadata.php index a48bcfb6827..2cf6c24f9b0 100644 --- a/lib/public/Calendar/Resource/IResourceMetadata.php +++ b/lib/public/Calendar/Resource/IResourceMetadata.php @@ -100,4 +100,19 @@ interface IResourceMetadata { * @since 17.0.0 */ public const VEHICLE_SEATING_CAPACITY = '{http://nextcloud.com/ns}resource-vehicle-seating-capacity'; + + /** + * Contact information about the person who is responsible to administer / maintain this resource + * This key stores a textual description of name and possible ways to contact the person + * + * @since 17.0.0 + */ + public const CONTACT_PERSON = '{http://nextcloud.com/ns}resource-contact-person'; + + /** + * Link to the vcard of the contact person + * + * @since 17.0.0 + */ + public const CONTACT_PERSON_VCARD = '{http://nextcloud.com/ns}resource-contact-person-vcard'; } -- cgit v1.2.3 From 3e5946bd0d91bd52bd6d3adf5486c604b59d6651 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Fri, 2 Aug 2019 20:01:32 +0200 Subject: Add standard metadata key for room-features Signed-off-by: Georg Ehrke --- lib/public/Calendar/Room/IRoomMetadata.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib/public') diff --git a/lib/public/Calendar/Room/IRoomMetadata.php b/lib/public/Calendar/Room/IRoomMetadata.php index a6c0fa83865..660a533cce8 100644 --- a/lib/public/Calendar/Room/IRoomMetadata.php +++ b/lib/public/Calendar/Room/IRoomMetadata.php @@ -75,4 +75,21 @@ interface IRoomMetadata { * @since 17.0.0 */ public const BUILDING_ROOM_NUMBER = '{http://nextcloud.com/ns}room-building-room-number'; + + /** + * Features provided by the room. + * This is a stringified list of features. + * Example: "PHONE,VIDEO-CONFERENCING" + * + * Standard features include: + * - PHONE: This room is fitted with a phone + * - VIDEO-CONFERENCING: This room is fitted with a video-conferencing system + * - TV: This room is fitted with a TV + * - PROJECTOR: This room is fitted with a projector + * - WHITEBOARD: This room is fitted with a whiteboard + * - WHEELCHAIR-ACCESSIBLE: This room is wheelchair-accessible + * + * @since 17.0.0 + */ + public const FEATURES = '{http://nextcloud.com/ns}room-features'; } -- cgit v1.2.3