2022-11-11 16:16:31 -05:00
|
|
|
"""
|
|
|
|
City repository with database CRUD operations
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Peter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
|
|
|
|
2023-01-31 13:11:39 -05:00
|
|
|
import datetime
|
2022-11-11 16:16:31 -05:00
|
|
|
import pickle
|
|
|
|
from typing import Union, Dict
|
2023-01-31 13:11:39 -05:00
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
from hub.city_model_structure.city import City as CityHub
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.hub_logger import logger
|
2023-02-01 06:05:12 -05:00
|
|
|
from hub.persistence import Repository
|
|
|
|
from hub.persistence.models import City as Model
|
2023-02-02 13:00:58 -05:00
|
|
|
from hub.persistence.models import CityObject
|
2023-01-31 13:11:39 -05:00
|
|
|
from hub.version import __version__
|
2022-11-11 16:16:31 -05:00
|
|
|
|
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
class City(Repository):
|
2022-11-15 20:48:42 -05:00
|
|
|
_instance = None
|
|
|
|
|
2022-12-07 19:06:17 -05:00
|
|
|
def __init__(self, db_name: str, dotenv_path: str, app_env: str):
|
2022-12-14 17:49:29 -05:00
|
|
|
super().__init__(db_name, dotenv_path, app_env)
|
2022-11-11 16:16:31 -05:00
|
|
|
|
2022-12-07 19:06:17 -05:00
|
|
|
def __new__(cls, db_name, dotenv_path, app_env):
|
2022-11-15 20:48:42 -05:00
|
|
|
"""
|
|
|
|
Implemented for a singleton pattern
|
|
|
|
"""
|
|
|
|
if cls._instance is None:
|
2023-02-01 06:05:12 -05:00
|
|
|
cls._instance = super(City, cls).__new__(cls)
|
2022-11-15 20:48:42 -05:00
|
|
|
return cls._instance
|
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
def insert(self, city: CityHub, application_id, user_id: int) -> Union[Model, Dict]:
|
2023-01-31 13:11:39 -05:00
|
|
|
"""
|
2023-02-01 07:29:06 -05:00
|
|
|
Inserts a city
|
2023-01-31 13:11:39 -05:00
|
|
|
:param city: The complete city instance
|
|
|
|
:param application_id: Application id owning the instance
|
|
|
|
:param user_id: User id owning the instance
|
|
|
|
:return: City and Dictionary
|
|
|
|
"""
|
2022-11-11 16:16:31 -05:00
|
|
|
try:
|
2023-02-01 06:05:12 -05:00
|
|
|
db_city = Model(
|
|
|
|
pickle.dumps(city),
|
|
|
|
city.name,
|
2023-02-02 13:00:58 -05:00
|
|
|
city.level_of_detail.geometry,
|
2023-02-08 13:40:06 -05:00
|
|
|
'None' if city.climate_file is None else str(city.climate_file),
|
2023-02-01 06:05:12 -05:00
|
|
|
application_id,
|
|
|
|
user_id,
|
|
|
|
__version__)
|
2022-11-11 16:16:31 -05:00
|
|
|
|
2023-01-31 13:11:39 -05:00
|
|
|
self.session.add(db_city)
|
|
|
|
self.session.flush()
|
2023-02-02 13:00:58 -05:00
|
|
|
for building in city.buildings:
|
|
|
|
object_usage = ''
|
|
|
|
for internal_zone in building.internal_zones:
|
|
|
|
for usage in internal_zone.usages:
|
|
|
|
object_usage = f'{object_usage}{usage.name}_{usage.percentage} '
|
|
|
|
object_usage = object_usage.rstrip()
|
|
|
|
db_city_object = CityObject(db_city.id,
|
|
|
|
building.name,
|
|
|
|
building.alias,
|
|
|
|
building.type,
|
|
|
|
building.year_of_construction,
|
|
|
|
building.function,
|
|
|
|
object_usage,
|
|
|
|
building.volume,
|
|
|
|
building.floor_area)
|
|
|
|
self.session.add(db_city_object)
|
|
|
|
self.session.flush()
|
2023-01-31 13:11:39 -05:00
|
|
|
self.session.commit()
|
|
|
|
return db_city
|
2022-11-11 16:16:31 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-02-02 13:00:58 -05:00
|
|
|
print(f'An error occurred while creating city: {err}')
|
2023-01-31 13:11:39 -05:00
|
|
|
logger.error(f'An error occurred while creating city: {err}')
|
2022-11-11 16:16:31 -05:00
|
|
|
|
2023-02-01 07:29:06 -05:00
|
|
|
def update(self, city_id: int, city: CityHub):
|
|
|
|
"""
|
|
|
|
Updates a city name (other updates makes no sense)
|
|
|
|
:param city_id: the id of the city to be updated
|
|
|
|
:param city: the city object
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
self.session.query(Model).filter(Model.id == city_id).update({'name': city.name,'updated': now})
|
|
|
|
self.session.commit()
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
logger.error(f'Error while updating city: {err}')
|
|
|
|
|
|
|
|
def delete(self, city_id: int):
|
|
|
|
"""
|
|
|
|
Deletes a City with the id
|
|
|
|
:param city_id: the city id
|
|
|
|
:return: a city
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.session.query(Model).filter(Model.id == city_id).delete()
|
|
|
|
self.session.commit()
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
logger.error(f'Error while fetching city: {err}')
|
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
def get_by_id(self, city_id: int) -> Model:
|
2022-11-11 16:16:31 -05:00
|
|
|
"""
|
|
|
|
Fetch a City based on the id
|
|
|
|
:param city_id: the city id
|
|
|
|
:return: a city
|
|
|
|
"""
|
|
|
|
try:
|
2023-02-01 06:05:12 -05:00
|
|
|
return self.session.execute(select(Model).where(Model.id == city_id)).first()[0]
|
2022-11-11 16:16:31 -05:00
|
|
|
except SQLAlchemyError as err:
|
2022-12-14 17:49:29 -05:00
|
|
|
logger.error(f'Error while fetching city: {err}')
|
2022-11-11 16:16:31 -05:00
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
def _get_by_hub_version_and_name(self, hub_release: str, city_name: str) -> Model:
|
2022-11-11 16:16:31 -05:00
|
|
|
"""
|
2023-01-31 13:11:39 -05:00
|
|
|
Fetch a City based on the name and hub project
|
|
|
|
:param hub_release: the hub release
|
2022-11-11 16:16:31 -05:00
|
|
|
:param city_name: the name of the city
|
|
|
|
:return: a city
|
|
|
|
"""
|
|
|
|
try:
|
2023-02-01 06:05:12 -05:00
|
|
|
return self.session.execute(select(Model)
|
|
|
|
.where(Model.hub_release == hub_release, Model.name == city_name)
|
|
|
|
).first()
|
2022-11-11 16:16:31 -05:00
|
|
|
except SQLAlchemyError as err:
|
2022-12-14 17:49:29 -05:00
|
|
|
logger.error(f'Error while fetching city: {err}')
|
2022-11-11 16:16:31 -05:00
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
def get_by_name(self, city_name: str) -> [Model]:
|
2022-11-11 16:16:31 -05:00
|
|
|
"""
|
|
|
|
Fetch city based on the name
|
|
|
|
:param city_name: the name of the building
|
|
|
|
:return: [ModelCity] with the provided name
|
|
|
|
"""
|
|
|
|
try:
|
2023-02-01 06:05:12 -05:00
|
|
|
result_set = self.session.execute(select(Model).where(Model.name == city_name))
|
2022-11-11 16:16:31 -05:00
|
|
|
return [building[0] for building in result_set]
|
|
|
|
except SQLAlchemyError as err:
|
2022-12-14 17:49:29 -05:00
|
|
|
logger.error(f'Error while fetching city by name: {err}')
|
2022-11-15 20:48:42 -05:00
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
def get_by_user(self, user_id: int) -> [Model]:
|
2023-01-11 16:02:33 -05:00
|
|
|
"""
|
|
|
|
Fetch city based on the user who created it
|
|
|
|
:param user_id: the id of the user
|
|
|
|
:return: [ModelCity] with the provided name
|
|
|
|
"""
|
|
|
|
try:
|
2023-02-01 06:05:12 -05:00
|
|
|
result_set = self.session.execute(select(Model).where(Model.user_id == user_id))
|
2023-01-11 16:02:33 -05:00
|
|
|
return [building[0] for building in result_set]
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
logger.error(f'Error while fetching city by name: {err}')
|