Skip to content
4 changes: 4 additions & 0 deletions modelbaker/dataobjects/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ def create(
for layer_name, bag_of_enum in self.bags_of_enum.items():
current_layer = None
for attribute, bag_of_enum_info in bag_of_enum.items():
mapping_type = bag_of_enum_info[5]
if mapping_type is None or mapping_type.lower() != "array":
continue

layer_obj = bag_of_enum_info[0]
cardinality = bag_of_enum_info[1]
domain_table = bag_of_enum_info[2]
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 @@ -206,6 +206,7 @@ def get_bags_of_info(self):
target_layer_name
cardinality_max
cardinality_min
mapping_type
"""
return []

Expand Down
7 changes: 4 additions & 3 deletions modelbaker/dbconnector/gpkg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@ def get_bags_of_info(self):
cursor = self.conn.cursor()
cursor.execute(
"""SELECT cprop.tablename as current_layer_name, cprop.columnname as attribute, cprop.setting as target_layer_name,
meta_attrs_cardinality_min.attr_value as cardinality_min, meta_attrs_cardinality_max.attr_value as cardinality_max
meta_attrs_cardinality_min.attr_value as cardinality_min, meta_attrs_cardinality_max.attr_value as cardinality_max,
meta_attrs_array.attr_value as mapping_type
FROM T_ILI2DB_COLUMN_PROP as cprop
LEFT JOIN T_ILI2DB_CLASSNAME as cname
ON cname.sqlname = cprop.tablename
Expand All @@ -635,8 +636,8 @@ def get_bags_of_info(self):
ON LOWER(meta_attrs_cardinality_min.ilielement) = LOWER(cname.iliname||'.'||cprop.columnname) AND meta_attrs_cardinality_min.attr_name = 'ili2db.ili.attrCardinalityMin'
LEFT JOIN T_ILI2DB_META_ATTRS as meta_attrs_cardinality_max
ON LOWER(meta_attrs_cardinality_max.ilielement) = LOWER(cname.iliname||'.'||cprop.columnname) AND meta_attrs_cardinality_max.attr_name = 'ili2db.ili.attrCardinalityMax'
WHERE cprop.tag = 'ch.ehi.ili2db.foreignKey' AND meta_attrs_array.attr_value = 'ARRAY'
"""
WHERE cprop.tag = 'ch.ehi.ili2db.foreignKey'
"""
)
bags_of_info = cursor.fetchall()
cursor.close()
Expand Down
5 changes: 3 additions & 2 deletions modelbaker/dbconnector/pg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,8 @@ def get_bags_of_info(self):
cur.execute(
sql.SQL(
"""SELECT cprop.tablename as current_layer_name, cprop.columnname as attribute, cprop.setting as target_layer_name,
meta_attrs_cardinality_min.attr_value as cardinality_min, meta_attrs_cardinality_max.attr_value as cardinality_max
meta_attrs_cardinality_min.attr_value as cardinality_min, meta_attrs_cardinality_max.attr_value as cardinality_max,
meta_attrs_array.attr_value as mapping_type
FROM {schema}.t_ili2db_column_prop as cprop
LEFT JOIN {schema}.t_ili2db_classname as cname
ON cname.sqlname = cprop.tablename
Expand All @@ -814,7 +815,7 @@ def get_bags_of_info(self):
ON meta_attrs_cardinality_min.ilielement ILIKE cname.iliname||'.'||cprop.columnname AND meta_attrs_cardinality_min.attr_name = 'ili2db.ili.attrCardinalityMin'
LEFT JOIN {schema}.{t_ili2db_meta_attrs} as meta_attrs_cardinality_max
ON meta_attrs_cardinality_max.ilielement ILIKE cname.iliname||'.'||cprop.columnname AND meta_attrs_cardinality_max.attr_name = 'ili2db.ili.attrCardinalityMax'
WHERE cprop.tag = 'ch.ehi.ili2db.foreignKey' AND meta_attrs_array.attr_value = 'ARRAY'
WHERE cprop.tag = 'ch.ehi.ili2db.foreignKey'
"""
).format(
schema=sql.Identifier(self.schema),
Expand Down
45 changes: 27 additions & 18 deletions modelbaker/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,13 @@ def relations(self, layers, filter_layer_list=[]):
if record["current_layer_name"] == layer.name:
new_item_list = [
layer,
record["cardinality_min"]
+ ".."
+ record["cardinality_max"],
record["cardinality_min"] + ".." + record["cardinality_max"]
if record["cardinality_min"] and record["cardinality_max"]
else "",
layer_map[record["target_layer_name"]][0],
self._db_connector.tid,
self._db_connector.dispName,
record["mapping_type"],
]
unique_current_layer_name = "{}_{}".format(
record["current_layer_name"], layer.geometry_column
Expand Down Expand Up @@ -687,30 +688,38 @@ def suppress_catalogue_reference_layers(available_layers, relations, bags_of_enu

# 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.
ref_required = False

# First get the ref pointing to the item
ref_to_item_iliname, ref_to_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"])
ref_to_item_iliname = ref["ili_name"]
ref_to_item = ref["name"]
break

# Check if there is a BAG OF pointing to the item or to the ref
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 (
ref_to_item
== bag_of_data[0].name # Ref as origin of a relation
and item["name"]
!= bag_of_data[2].name # that does not point to the item
): # So ref must be pointing to a layer
ref_required = True

if ref_required:
# 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.
layers_to_remove.append(ref_to_item_iliname)

# 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:
Expand Down
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
markers =
bagof: marks tests dealing with BAGs OF
catalogue: marks tests dealing with catalogue
Loading