Skip to content
Merged
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
4 changes: 4 additions & 0 deletions modelbaker/dataobjects/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(
qmlstylefile: Optional[str] = None,
styles: dict[str, dict[str, str]] = {},
is_enum: bool = False,
base_class: str = None,
) -> None:
self.provider = provider
self.uri = uri
Expand All @@ -94,6 +95,7 @@ def __init__(
self.is_domain = is_domain
self.is_structure = is_structure
self.is_enum = is_enum
self.base_class = base_class

self.is_nmrel = is_nmrel
self.srid = srid
Expand Down Expand Up @@ -144,6 +146,7 @@ def dump(self) -> dict:
definition["qmlstylefile"] = self.qmlstylefile
definition["styles"] = self.styles
definition["form"] = self.__form.dump()
definition["base_class"] = self.base_class
return definition

def load(self, definition: dict) -> None:
Expand All @@ -165,6 +168,7 @@ def load(self, definition: dict) -> None:
self.qmlstylefile = definition["qmlstylefile"]
self.styles = definition["styles"]
self.__form.load(definition["form"])
self.base_class = definition["base_class"]

def create(self) -> Union[QgsRasterLayer, QgsVectorLayer]:
if self.definitionfile:
Expand Down
1 change: 1 addition & 0 deletions modelbaker/dbconnector/db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def get_tables_info(self):
table_alias
model
relevance
base_class
"""
return []

Expand Down
3 changes: 3 additions & 0 deletions modelbaker/dbconnector/gpkg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def _get_tables_info(self):
attrs.sqlname as attribute_name,
{relevance_field},
{topics},
i.baseclass as base_class,
{translations} -- Optional. Trailing comma omitted on purpose.
""".format(
relevance_field="""CASE WHEN c.iliname IN (
Expand Down Expand Up @@ -229,6 +230,8 @@ def _get_tables_info(self):
AND alias.tag = 'ch.ehi.ili2db.dispName'
LEFT JOIN T_ILI2DB_CLASSNAME c
ON s.name == c.sqlname
LEFT JOIN T_ILI2DB_INHERITANCE i
ON c.iliname = i.thisclass
LEFT JOIN T_ILI2DB_ATTRNAME attrs
ON c.iliname = attrs.iliname
{translations}""".format(
Expand Down
3 changes: 3 additions & 0 deletions modelbaker/dbconnector/mssql_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def get_tables_info(self):
ln + " , left(c.iliname, charindex('.', c.iliname)-1) AS model"
)
stmt += ln + " , c.iliname AS ili_name"
stmt += ln + " , inh.baseclass AS base_class"
stmt += ln + " , STUFF("
stmt += ln + " (SELECT ';' + CAST(cp.setting AS VARCHAR(MAX))"
stmt += ln + " FROM {schema}.t_ili2db_column_prop cp"
Expand Down Expand Up @@ -253,6 +254,8 @@ def get_tables_info(self):
stmt += ln + " AND alias.tag = 'ch.ehi.ili2db.dispName'"
stmt += ln + "LEFT JOIN {schema}.t_ili2db_classname AS c"
stmt += ln + " ON tbls.TABLE_NAME = c.sqlname"
stmt += ln + "LEFT JOIN {schema}.t_ili2db_inheritance AS inh"
stmt += ln + " ON c.iliname = inh.thisclass"
stmt += ln + "LEFT JOIN {schema}.t_ili2db_attrname AS attrs"
stmt += ln + " ON c.iliname = attrs.iliname"
stmt += ln + "LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS clm"
Expand Down
11 changes: 11 additions & 0 deletions modelbaker/dbconnector/pg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def get_tables_info(self):
coord_decimals = ""
alias_left_join = ""
model_name = ""
base_class = ""
model_where = ""
base_class_left_join = ""
attribute_name = ""
attribute_left_join = ""
translations_left_join = ""
Expand Down Expand Up @@ -202,6 +204,7 @@ def get_tables_info(self):
self.schema
)
model_name = "left(c.iliname, strpos(c.iliname, '.')-1) AS model,"
base_class = "inh.baseclass as base_class,"
relevance = """
CASE WHEN c.iliname IN (
WITH names AS (
Expand Down Expand Up @@ -290,6 +293,10 @@ def get_tables_info(self):
ON tbls.tablename = c.sqlname""".format(
self.schema
)
base_class_left_join = """LEFT JOIN {}.t_ili2db_inheritance inh
ON c.iliname = inh.thisclass""".format(
self.schema
)
attribute_name = "attrs.sqlname as attribute_name,"
attribute_left_join = """LEFT JOIN {}.t_ili2db_attrname attrs
ON c.iliname = attrs.iliname""".format(
Expand Down Expand Up @@ -321,6 +328,7 @@ def get_tables_info(self):
{kind_settings_field}
{table_alias}
{model_name}
{base_class}
{ili_name}
{extent}
{attribute_name}
Expand All @@ -339,6 +347,7 @@ def get_tables_info(self):
{domain_left_join}
{alias_left_join}
{model_where}
{base_class_left_join}
{attribute_left_join}
{translations_left_join}
LEFT JOIN public.geometry_columns g
Expand All @@ -352,6 +361,7 @@ def get_tables_info(self):
kind_settings_field=kind_settings_field,
table_alias=table_alias,
model_name=model_name,
base_class=base_class,
ili_name=ili_name,
extent=extent,
coord_decimals=coord_decimals,
Expand All @@ -362,6 +372,7 @@ def get_tables_info(self):
alias_left_join=alias_left_join,
translations_left_join=translations_left_join,
model_where=model_where,
base_class_left_join=base_class_left_join,
attribute_name=attribute_name,
attribute_left_join=attribute_left_join,
schema_where=schema_where,
Expand Down
65 changes: 65 additions & 0 deletions modelbaker/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ def layers(self, filter_layer_list: list = []) -> list[Layer]:
if not relevant_topics and base_topic and base_topic.count(".") > 0:
relevant_topics.append(base_topic)

base_class = record.get("base_class", None)

# If raw_naming is True, the layername should be the tablename
# Otherwise get table name in this order:
# - translation if exists,
Expand Down Expand Up @@ -312,6 +314,7 @@ def layers(self, filter_layer_list: list = []) -> list[Layer]:
all_topics,
relevant_topics,
is_enum=is_enum,
base_class=base_class,
)

# Configure fields for current table
Expand Down Expand Up @@ -635,6 +638,68 @@ def relations(self, layers, filter_layer_list=[]):
}
return (relations, bags_of_enum)

@staticmethod
def suppress_catalogue_reference_layers(available_layers, relations, bags_of_enum):
# Check for catalogue items and reference layers
catalogue_items = [] # List of dicts
catalogue_refs = [] # List of dicts
for layer in available_layers:
if (
layer.is_domain
and not layer.is_enum
and layer.base_class == "CatalogueObjects_V1.Catalogues.Item"
):
catalogue_items.append({"name": layer.name, "ili_name": layer.ili_name})

if layer.is_structure and layer.base_class in (
"CatalogueObjects_V1.Catalogues.CatalogueReference",
"CatalogueObjects_V1.Catalogues.MandatoryCatalogueReference",
):
catalogue_refs.append({"name": layer.name, "ili_name": layer.ili_name})

layers_to_remove = []

# Remove reference layer if they are not BAG OF
for item in catalogue_items:
is_bag_of = False
for bag_of_layer_k, bag_of_layer_v in bags_of_enum.items():
for bag_of_attr_k, bag_of_data in bag_of_layer_v.items():
if (
item["name"] == bag_of_data[2].name
): # BAG OF's target_layer_name
is_bag_of = True

if is_bag_of:
# It's a BAG OF, leave it, cause users will need it to add data
continue

# The ref has no BAG OF pointing to the item, therefore,
# we'll suppress the ref cause users won't need it to add data.

# First get the ref pointing to the item
for relation in relations:
if relation.referenced_layer.ili_name == item["ili_name"]:
for ref in catalogue_refs:
if relation.referencing_layer.ili_name == ref["ili_name"]:
# We've found the corresponding ref structure
layers_to_remove.append(ref["ili_name"])
break

# Finally, remove the ref layers that we've found are not BAGS OF from the list,
# as well as the relations where they are involved (i.e., referenced/referencing)
for layer_to_remove in layers_to_remove:
available_layers = [
layer for layer in available_layers if layer_to_remove != layer.ili_name
]
relations = [
relation
for relation in relations
if relation.referenced_layer.ili_name != layer_to_remove
and relation.referencing_layer.ili_name != layer_to_remove
]

return available_layers, relations

def generate_node(self, layers, node_name, item_properties):
if item_properties.get("group"):
node = LegendGroup(
Expand Down
Loading