|
17 | 17 | */ |
18 | 18 | package org.apache.atlas.repository.impexp; |
19 | 19 |
|
| 20 | +import org.apache.atlas.ApplicationProperties; |
| 21 | +import org.apache.atlas.AtlasException; |
20 | 22 | import org.apache.atlas.model.instance.AtlasEntity; |
21 | 23 | import org.apache.atlas.model.instance.AtlasRelatedObjectId; |
| 24 | +import org.apache.atlas.model.instance.AtlasRelationship; |
| 25 | +import org.apache.atlas.model.instance.AtlasStruct; |
22 | 26 | import org.apache.atlas.model.typedef.AtlasBaseTypeDef; |
23 | 27 | import org.apache.atlas.model.typedef.AtlasEntityDef; |
| 28 | +import org.apache.atlas.repository.graph.GraphHelper; |
| 29 | +import org.apache.atlas.repository.graphdb.AtlasEdge; |
| 30 | +import org.apache.atlas.repository.graphdb.AtlasVertex; |
24 | 31 | import org.apache.atlas.repository.impexp.ExportService.TraversalDirection; |
| 32 | +import org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2; |
25 | 33 | import org.apache.atlas.type.AtlasTypeRegistry; |
26 | 34 | import org.apache.atlas.type.AtlasTypeUtil; |
| 35 | +import org.apache.commons.collections.CollectionUtils; |
27 | 36 | import org.slf4j.Logger; |
28 | 37 | import org.slf4j.LoggerFactory; |
29 | 38 |
|
30 | 39 | import java.util.ArrayList; |
31 | 40 | import java.util.Collection; |
| 41 | +import java.util.Iterator; |
32 | 42 | import java.util.List; |
33 | 43 |
|
34 | 44 | import static org.apache.atlas.repository.impexp.ExportService.ExportContext; |
|
40 | 50 | public class RelationshipAttributesExtractor implements ExtractStrategy { |
41 | 51 | private static final Logger LOG = LoggerFactory.getLogger(RelationshipAttributesExtractor.class); |
42 | 52 |
|
| 53 | + private static final String DISPLAY_NAME_PROPERTY = "Asset.__s_name"; |
| 54 | + private static final String RELATIONSHIP_GUID_PROPERTY = "_r__guid"; |
| 55 | + private static final String QUALIFIED_NAME_PROPERTY = "Referenceable.qualifiedName"; |
| 56 | + |
43 | 57 | private final AtlasTypeRegistry typeRegistry; |
44 | 58 |
|
45 | 59 | public RelationshipAttributesExtractor(AtlasTypeRegistry typeRegistry) { |
@@ -148,6 +162,93 @@ private List<AtlasRelatedObjectId> getRelatedObjectIds(AtlasEntity entity) { |
148 | 162 | } |
149 | 163 | } |
150 | 164 |
|
| 165 | + String typeName = entity.getTypeName(); |
| 166 | + List<String> relatedTypes = checkForRelationshipTypes(typeName); |
| 167 | + if (CollectionUtils.isNotEmpty(relatedTypes)) { |
| 168 | + String alreadyPopulatedType = checkAlreadyPopulatedRelationshipType(entity); |
| 169 | + relatedTypes.remove(alreadyPopulatedType); |
| 170 | + } |
| 171 | + String entityGuid = entity.getGuid(); |
| 172 | + List<AtlasRelatedObjectId> relatedObjectIdList = populateRelatedObjectIds(typeName, entityGuid, relatedTypes); |
| 173 | + relatedObjectIds.addAll(relatedObjectIdList); |
| 174 | + return relatedObjectIds; |
| 175 | + } |
| 176 | + |
| 177 | + private String checkAlreadyPopulatedRelationshipType(AtlasEntity entity) { |
| 178 | + String typeName = entity.getTypeName(); |
| 179 | + String relationshipAttr; |
| 180 | + try { |
| 181 | + relationshipAttr = ApplicationProperties.get().getString("atlas.relationship.attr.for." + typeName); |
| 182 | + } catch (AtlasException e) { |
| 183 | + throw new RuntimeException(e); |
| 184 | + } |
| 185 | + Object o = entity.getRelationshipAttribute(relationshipAttr); |
| 186 | + String relatedTypeName = null; |
| 187 | + if (o instanceof AtlasRelatedObjectId) { |
| 188 | + AtlasRelatedObjectId relatedObjectId = (AtlasRelatedObjectId) o; |
| 189 | + relatedTypeName = relatedObjectId.getTypeName(); |
| 190 | + } else if (o instanceof Collection) { |
| 191 | + List<AtlasRelatedObjectId> relatedObjectIds = (List<AtlasRelatedObjectId>) o; |
| 192 | + if (!CollectionUtils.isEmpty(relatedObjectIds)) { |
| 193 | + relatedTypeName = relatedObjectIds.get(0).getTypeName(); |
| 194 | + } |
| 195 | + } |
| 196 | + return relatedTypeName; |
| 197 | + } |
| 198 | + |
| 199 | + private List<String> checkForRelationshipTypes(String typeName) { |
| 200 | + List<String> relatedTypeList = null; |
| 201 | + try { |
| 202 | + String[] relatedTypes = ApplicationProperties.get().getStringArray("atlas.relationship.types.for." + typeName); |
| 203 | + if (relatedTypes != null && relatedTypes.length > 0) { |
| 204 | + relatedTypeList = new ArrayList<>(); |
| 205 | + for (String relatedType : relatedTypes) { |
| 206 | + relatedTypeList.add(relatedType); |
| 207 | + } |
| 208 | + } |
| 209 | + } catch (AtlasException e) { |
| 210 | + throw new RuntimeException(e); |
| 211 | + } |
| 212 | + return relatedTypeList; |
| 213 | + } |
| 214 | + |
| 215 | + private List<AtlasRelatedObjectId> populateRelatedObjectIds(String typeName, String entityGuid, List<String> relatedTypes) { |
| 216 | + List<AtlasRelatedObjectId> relatedObjectIds = new ArrayList<>(); |
| 217 | + AtlasVertex vertex = AtlasGraphUtilsV2.findByGuid(entityGuid); |
| 218 | + if (CollectionUtils.isEmpty(relatedTypes)) { |
| 219 | + return relatedObjectIds; |
| 220 | + } |
| 221 | + for (String relatedType : relatedTypes) { |
| 222 | + String edgeLabel; |
| 223 | + try { |
| 224 | + edgeLabel = ApplicationProperties.get().getString("atlas.relationship.edge.label.between." + typeName + "." + relatedType); |
| 225 | + } catch (AtlasException e) { |
| 226 | + throw new RuntimeException(e); |
| 227 | + } |
| 228 | + Iterator<AtlasEdge> edges = GraphHelper.getIncomingEdgesByLabel(vertex, edgeLabel); |
| 229 | + while (edges.hasNext()) { |
| 230 | + AtlasEdge relationshipEdge = edges.next(); |
| 231 | + AtlasVertex relationshipVertex = relationshipEdge.getOutVertex(); |
| 232 | + AtlasRelatedObjectId relatedObjectId = new AtlasRelatedObjectId(); |
| 233 | + AtlasEntity.Status vertexStatus = GraphHelper.getStatus(relationshipVertex); |
| 234 | + relatedObjectId.setEntityStatus(vertexStatus); |
| 235 | + String displayText = relationshipVertex.getProperty(DISPLAY_NAME_PROPERTY, String.class); |
| 236 | + relatedObjectId.setDisplayText(displayText); |
| 237 | + relatedObjectId.setRelationshipType(GraphHelper.getTypeName(relationshipEdge)); |
| 238 | + String relationshipGuid = relationshipEdge.getProperty(RELATIONSHIP_GUID_PROPERTY, String.class); |
| 239 | + relatedObjectId.setRelationshipGuid(relationshipGuid); |
| 240 | + AtlasRelationship.Status edgeStatus = GraphHelper.getEdgeStatus(relationshipEdge); |
| 241 | + relatedObjectId.setRelationshipStatus(edgeStatus); |
| 242 | + AtlasStruct relationshipAttributes = new AtlasStruct(); |
| 243 | + relationshipAttributes.setTypeName(GraphHelper.getTypeName(relationshipEdge)); |
| 244 | + relatedObjectId.setRelationshipAttributes(relationshipAttributes); |
| 245 | + String qualifiedName = relationshipVertex.getProperty(QUALIFIED_NAME_PROPERTY, String.class); |
| 246 | + relatedObjectId.setQualifiedName(qualifiedName); |
| 247 | + relatedObjectId.setGuid(GraphHelper.getGuid(relationshipVertex)); |
| 248 | + relatedObjectId.setTypeName(GraphHelper.getTypeName(relationshipVertex)); |
| 249 | + relatedObjectIds.add(relatedObjectId); |
| 250 | + } |
| 251 | + } |
151 | 252 | return relatedObjectIds; |
152 | 253 | } |
153 | 254 | } |
0 commit comments