Skip to content

Commit e088f71

Browse files
authored
Issue #790: Fix association logic for transformations (#843)
##### Description of Change Missed logic flows for validating transformation attributes that should take into account entity or attribute 'Extension' fields. Remove API support for v1 of entityIdPath. ##### Related Issues #790 ##### Type of Change - [x] Bug fix (non-breaking change which fixes an issue) ##### Project Area(s) Affected - [x] API endpoints --- ##### Checklist - [x] commit message follows commit guidelines (see commitlint.config.mjs) - [x] pre-commit hooks have been run successfully ##### Testing - [x] Manual testing performed
2 parents 92198d8 + 7eead76 commit e088f71

6 files changed

Lines changed: 102 additions & 423 deletions

File tree

components/lif/mdr_services/entity_association_service.py

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
EntityAssociationDTO,
88
UpdateEntityAssociationDTO,
99
)
10-
from lif.mdr_dto.transformation_dto import TransformationAttributeDTO
1110
from lif.mdr_services.entity_service import get_entity_by_id
1211
from lif.mdr_services.helper_service import check_datamodel_by_id, check_entity_by_id
1312
from lif.mdr_utils.logger_config import get_logger
@@ -83,97 +82,6 @@ async def retrieve_all_entity_associations(
8382
return associations
8483

8584

86-
async def validate_entity_associations_for_transformation_attribute(
87-
session: AsyncSession, transformation_attribute: TransformationAttributeDTO
88-
) -> bool:
89-
immediate_parent_entity_id = transformation_attribute.EntityId
90-
immediate_parent_entity = await get_entity_by_id(session=session, id=immediate_parent_entity_id)
91-
entity_path = transformation_attribute.EntityIdPath
92-
entities = entity_path.split(".")
93-
grandparent_entity_name = entities[-2] if len(entities) > 1 else None
94-
logger.info(
95-
f"Validating for entity association with current entity name: {entities[-1]} and grandparent entity name: {grandparent_entity_name}"
96-
)
97-
98-
query = select(EntityAssociation).where(
99-
EntityAssociation.ChildEntityId == immediate_parent_entity_id, EntityAssociation.Deleted == False
100-
)
101-
result = await session.execute(query)
102-
entity_associations = result.scalars().all()
103-
matching_entity_association = None
104-
for entity_association in entity_associations:
105-
# if entity_association.Relationship is not None and does not start with "has" or "relevant"
106-
if entity_association.Relationship is not None and not (
107-
entity_association.Relationship.startswith("has") or entity_association.Relationship.startswith("relevant")
108-
):
109-
final_entity_name = entity_association.Relationship + immediate_parent_entity.Name
110-
logger.info(f"Found entity association with final entity name: {final_entity_name}")
111-
if final_entity_name == entities[-1]:
112-
grandparent_entity = await get_entity_by_id(session=session, id=entity_association.ParentEntityId)
113-
logger.info(f"Found grandparent entity with name: {grandparent_entity.Name}")
114-
if grandparent_entity.Name == grandparent_entity_name:
115-
matching_entity_association = entity_association
116-
break
117-
elif entities[-1] == immediate_parent_entity.Name:
118-
grandparent_entity = await get_entity_by_id(session=session, id=entity_association.ParentEntityId)
119-
logger.info(f"Found grandparent entity with name: {grandparent_entity.Name}")
120-
if grandparent_entity.Name == grandparent_entity_name:
121-
matching_entity_association = entity_association
122-
break
123-
124-
logger.info(f"Matching entity association found: {matching_entity_association}")
125-
if not matching_entity_association:
126-
logger.error(
127-
f"No matching entity association found for immediate parent entity ID {immediate_parent_entity_id} with name {entities[-1]} and grandparent entity name {grandparent_entity_name}"
128-
)
129-
return False
130-
131-
# Loop through the remaining values in the entities array going backwards to check for the rest of the entity associations
132-
for i in range(
133-
len(entities) - 2, 0, -1
134-
): # Start from the second last entity and go backwards to the second from the first entity
135-
current_entity_id = matching_entity_association.ParentEntityId
136-
current_entity = await get_entity_by_id(session=session, id=current_entity_id)
137-
grandparent_entity_name = entities[i - 1] if i - 1 >= 0 else None
138-
logger.info(
139-
f"Checking for entity association with current entity name: {entities[i]} and grandparent entity name: {grandparent_entity_name}"
140-
)
141-
query = select(EntityAssociation).where(
142-
EntityAssociation.ChildEntityId == current_entity_id, EntityAssociation.Deleted == False
143-
)
144-
result = await session.execute(query)
145-
entity_associations = result.scalars().all()
146-
found_matching_association = False
147-
for entity_association in entity_associations:
148-
if entity_association.Relationship is not None and not (
149-
entity_association.Relationship.startswith("has")
150-
or entity_association.Relationship.startswith("relevant")
151-
):
152-
final_entity_name = entity_association.Relationship + current_entity.Name
153-
logger.info(f"Found entity association with final entity name: {final_entity_name}")
154-
if final_entity_name == entities[i]:
155-
grandparent_entity = await get_entity_by_id(session=session, id=entity_association.ParentEntityId)
156-
logger.info(f"Found grandparent entity with name: {grandparent_entity.Name}")
157-
if grandparent_entity.Name == grandparent_entity_name:
158-
matching_entity_association = entity_association
159-
found_matching_association = True
160-
break
161-
elif entities[i] == current_entity.Name:
162-
grandparent_entity = await get_entity_by_id(session=session, id=entity_association.ParentEntityId)
163-
logger.info(f"Found grandparent entity with name: {grandparent_entity.Name}")
164-
if grandparent_entity.Name == grandparent_entity_name:
165-
matching_entity_association = entity_association
166-
found_matching_association = True
167-
break
168-
if not found_matching_association:
169-
logger.error(
170-
f"No matching entity association found for parent entity with name {entities[i]} and grandparent entity name {grandparent_entity_name}"
171-
)
172-
return False
173-
174-
return True
175-
176-
17785
async def create_entity_association(session: AsyncSession, data: CreateEntityAssociationDTO):
17886
# Resolve parent and child entity IDs
17987

components/lif/mdr_services/entity_service.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import List
2+
23
from fastapi import HTTPException
34
from lif.datatypes.mdr_sql_model import (
45
Attribute,
@@ -13,7 +14,7 @@
1314
from lif.mdr_services.helper_service import check_datamodel_by_id
1415
from lif.mdr_utils.logger_config import get_logger
1516
from sqlalchemy.ext.asyncio import AsyncSession
16-
from sqlmodel import or_, select, func
17+
from sqlmodel import func, or_, select
1718

1819
logger = get_logger(__name__)
1920

@@ -239,13 +240,6 @@ async def check_entity_exists(session: AsyncSession, unique_name: str, data_mode
239240
# return False # No entity with the same name exists
240241

241242

242-
async def is_entity_by_unique_name(session: AsyncSession, unique_name: str):
243-
query = select(Entity).where(Entity.UniqueName == unique_name, Entity.Deleted == False)
244-
result = await session.execute(query)
245-
entity = result.scalars().first()
246-
return entity is not None
247-
248-
249243
# async def get_list_of_attribute(session: AsyncSession, id: int):
250244
# entity = await get_entity_by_id(session,id)
251245
# attribute_list = await get_list_of_attributes_for_entity(session=session, entity_id=id)

components/lif/mdr_services/helper_service.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,6 @@ async def check_attribute_by_id(session: AsyncSession, id: int):
4444
return attribute
4545

4646

47-
async def check_entity_attribute_association(session: AsyncSession, entity_id: int, attribute_id: int):
48-
query = select(EntityAttributeAssociation).where(
49-
EntityAttributeAssociation.EntityId == entity_id, EntityAttributeAssociation.AttributeId == attribute_id
50-
)
51-
result = await session.execute(query)
52-
associations = result.fetchall()
53-
if not associations:
54-
raise HTTPException(
55-
status_code=404,
56-
detail=f"EntityAttributeAssociation with EntityId {entity_id} and AttributeId {attribute_id} not found",
57-
)
58-
return associations
59-
60-
6147
async def check_value_set_by_id(session: AsyncSession, id: int):
6248
value_set = await session.get(ValueSet, id)
6349
if not value_set:

0 commit comments

Comments
 (0)