|
7 | 7 | EntityAssociationDTO, |
8 | 8 | UpdateEntityAssociationDTO, |
9 | 9 | ) |
10 | | -from lif.mdr_dto.transformation_dto import TransformationAttributeDTO |
11 | 10 | from lif.mdr_services.entity_service import get_entity_by_id |
12 | 11 | from lif.mdr_services.helper_service import check_datamodel_by_id, check_entity_by_id |
13 | 12 | from lif.mdr_utils.logger_config import get_logger |
@@ -83,97 +82,6 @@ async def retrieve_all_entity_associations( |
83 | 82 | return associations |
84 | 83 |
|
85 | 84 |
|
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 | | - |
177 | 85 | async def create_entity_association(session: AsyncSession, data: CreateEntityAssociationDTO): |
178 | 86 | # Resolve parent and child entity IDs |
179 | 87 |
|
|
0 commit comments