Search in sources :

Example 1 with Cardinality

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality in project atlas by apache.

the class EntityGraphMapper method mapArrayValue.

public List mapArrayValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapArrayValue({})", ctx);
    }
    AtlasAttribute attribute = ctx.getAttribute();
    List newElements = (List) ctx.getValue();
    AtlasArrayType arrType = (AtlasArrayType) attribute.getAttributeType();
    AtlasType elementType = arrType.getElementType();
    boolean isReference = AtlasGraphUtilsV1.isReference(elementType);
    AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
    Cardinality cardinality = attribute.getAttributeDef().getCardinality();
    List<Object> newElementsCreated = new ArrayList<>();
    List<Object> currentElements;
    if (isRelationshipAttribute(attribute)) {
        currentElements = getArrayElementsUsingRelationship(ctx.getReferringVertex(), attribute, elementType);
    } else {
        currentElements = getArrayElementsProperty(elementType, ctx.getReferringVertex(), ctx.getVertexProperty());
    }
    if (CollectionUtils.isNotEmpty(newElements)) {
        if (cardinality == SET) {
            newElements = (List) newElements.stream().distinct().collect(Collectors.toList());
        }
        for (int index = 0; index < newElements.size(); index++) {
            AtlasEdge existingEdge = getEdgeAt(currentElements, index, elementType);
            AttributeMutationContext arrCtx = new AttributeMutationContext(ctx.getOp(), ctx.getReferringVertex(), ctx.getAttribute(), newElements.get(index), ctx.getVertexProperty(), elementType, existingEdge);
            Object newEntry = mapCollectionElementsToVertex(arrCtx, context);
            if (isReference && newEntry instanceof AtlasEdge && inverseRefAttribute != null) {
                // Update the inverse reference value.
                AtlasEdge newEdge = (AtlasEdge) newEntry;
                addInverseReference(inverseRefAttribute, newEdge, getRelationshipAttributes(ctx.getValue()));
            }
            newElementsCreated.add(newEntry);
        }
    }
    if (isReference) {
        List<AtlasEdge> additionalEdges = removeUnusedArrayEntries(attribute, (List) currentElements, (List) newElementsCreated, ctx.getReferringVertex());
        newElementsCreated.addAll(additionalEdges);
    }
    // for dereference on way out
    setArrayElementsProperty(elementType, ctx.getReferringVertex(), ctx.getVertexProperty(), newElementsCreated);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapArrayValue({})", ctx);
    }
    return newElementsCreated;
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) Cardinality(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality) AtlasType(org.apache.atlas.type.AtlasType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute)

Example 2 with Cardinality

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality in project incubator-atlas by apache.

the class AtlasStructType method resolveReferences.

@Override
public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
    Map<String, AtlasAttribute> a = new HashMap<>();
    for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
        AtlasType attrType = typeRegistry.getType(attributeDef.getTypeName());
        AtlasAttribute attribute = new AtlasAttribute(this, attributeDef, attrType);
        Cardinality cardinality = attributeDef.getCardinality();
        if (cardinality == Cardinality.LIST || cardinality == Cardinality.SET) {
            if (!(attrType instanceof AtlasArrayType)) {
                throw new AtlasBaseException(AtlasErrorCode.INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY, getTypeName(), attributeDef.getName());
            }
            AtlasArrayType arrayType = (AtlasArrayType) attrType;
            arrayType.setMinCount(attributeDef.getValuesMinCount());
            arrayType.setMaxCount(attributeDef.getValuesMaxCount());
        }
        a.put(attributeDef.getName(), attribute);
    }
    resolveConstraints(typeRegistry);
    this.allAttributes = Collections.unmodifiableMap(a);
    this.uniqAttributes = getUniqueAttributes(this.allAttributes);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Cardinality(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality) HashMap(java.util.HashMap) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)

Example 3 with Cardinality

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality in project atlas by apache.

the class AtlasStructType method resolveReferences.

@Override
void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
    Map<String, AtlasAttribute> a = new HashMap<>();
    for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
        AtlasType attrType = typeRegistry.getType(attributeDef.getTypeName());
        AtlasAttribute attribute = new AtlasAttribute(this, attributeDef, attrType);
        Cardinality cardinality = attributeDef.getCardinality();
        if (cardinality == Cardinality.LIST || cardinality == Cardinality.SET) {
            if (!(attrType instanceof AtlasArrayType)) {
                throw new AtlasBaseException(AtlasErrorCode.INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY, getTypeName(), attributeDef.getName());
            }
            AtlasArrayType arrayType = (AtlasArrayType) attrType;
            arrayType.setMinCount(attributeDef.getValuesMinCount());
            arrayType.setMaxCount(attributeDef.getValuesMaxCount());
        }
        a.put(attributeDef.getName(), attribute);
    }
    resolveConstraints(typeRegistry);
    this.allAttributes = Collections.unmodifiableMap(a);
    this.uniqAttributes = getUniqueAttributes(this.allAttributes);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Cardinality(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)

Aggregations

Cardinality (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)2 HashMap (java.util.HashMap)1 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)1 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)1 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)1 AtlasType (org.apache.atlas.type.AtlasType)1