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;
}
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);
}
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);
}
Aggregations