use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.
the class EntityGraphMapper method mapMapValue.
private Map<String, Object> mapMapValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
if (LOG.isDebugEnabled()) {
LOG.debug("==> mapMapValue({})", ctx);
}
@SuppressWarnings("unchecked") Map<Object, Object> newVal = (Map<Object, Object>) ctx.getValue();
Map<String, Object> newMap = new HashMap<>();
AtlasMapType mapType = (AtlasMapType) ctx.getAttrType();
try {
AtlasAttribute attribute = ctx.getAttribute();
List<String> currentKeys = GraphHelper.getListProperty(ctx.getReferringVertex(), ctx.getVertexProperty());
Map<String, Object> currentMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(currentKeys)) {
for (String key : currentKeys) {
String propertyNameForKey = GraphHelper.getQualifiedNameForMapKey(ctx.getVertexProperty(), GraphHelper.encodePropertyKey(key));
Object propertyValueForKey = getMapValueProperty(mapType.getValueType(), ctx.getReferringVertex(), propertyNameForKey);
currentMap.put(key, propertyValueForKey);
}
}
if (MapUtils.isNotEmpty(newVal)) {
boolean isReference = AtlasGraphUtilsV1.isReference(mapType.getValueType());
AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
for (Map.Entry<Object, Object> entry : newVal.entrySet()) {
String key = entry.getKey().toString();
String propertyName = GraphHelper.getQualifiedNameForMapKey(ctx.getVertexProperty(), GraphHelper.encodePropertyKey(key));
AtlasEdge existingEdge = getEdgeIfExists(mapType, currentMap, key);
AttributeMutationContext mapCtx = new AttributeMutationContext(ctx.getOp(), ctx.getReferringVertex(), attribute, entry.getValue(), propertyName, mapType.getValueType(), existingEdge);
//Add/Update/Remove property value
Object newEntry = mapCollectionElementsToVertex(mapCtx, context);
setMapValueProperty(mapType.getValueType(), ctx.getReferringVertex(), propertyName, newEntry);
newMap.put(key, newEntry);
// update the inverse reference value.
if (isReference && newEntry instanceof AtlasEdge && inverseRefAttribute != null) {
AtlasEdge newEdge = (AtlasEdge) newEntry;
addInverseReference(mapCtx, inverseRefAttribute, newEdge);
}
}
}
Map<String, Object> finalMap = removeUnusedMapEntries(attribute, ctx.getReferringVertex(), ctx.getVertexProperty(), currentMap, newMap);
for (Object newEntry : newMap.values()) {
updateInConsistentOwnedMapVertices(ctx, mapType, newEntry);
}
Set<String> newKeys = new LinkedHashSet<>(newMap.keySet());
newKeys.addAll(finalMap.keySet());
// for dereference on way out
GraphHelper.setListProperty(ctx.getReferringVertex(), ctx.getVertexProperty(), new ArrayList<>(newKeys));
} catch (AtlasException e) {
throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
}
if (LOG.isDebugEnabled()) {
LOG.debug("<== mapMapValue({})", ctx);
}
return newMap;
}
use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.
the class AtlasStructType method createDefaultValue.
private Object createDefaultValue(AtlasAttributeDef attributeDef) {
Object ret = null;
if (attributeDef != null) {
AtlasAttribute attribute = allAttributes.get(attributeDef.getName());
if (attribute != null) {
AtlasType dataType = attribute.getAttributeType();
ret = dataType.createDefaultValue();
}
}
return ret;
}
use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.
the class AtlasStructType method validateValueForUpdate.
@Override
public boolean validateValueForUpdate(Object obj, String objName, List<String> messages) {
boolean ret = true;
Map<String, Object> attributes = null;
if (obj != null) {
if (obj instanceof AtlasStruct) {
AtlasStruct structObj = (AtlasStruct) obj;
attributes = structObj.getAttributes();
} else if (obj instanceof Map) {
attributes = AtlasTypeUtil.toStructAttributes((Map) obj);
} else {
ret = false;
messages.add(objName + "=" + obj + ": invalid value for type " + getTypeName());
}
if (MapUtils.isNotEmpty(attributes)) {
for (Map.Entry<String, Object> e : attributes.entrySet()) {
String attrName = e.getKey();
Object attrValue = e.getValue();
AtlasAttribute attribute = allAttributes.get(attrName);
if (attrValue == null) {
continue;
}
if (attribute != null) {
AtlasType dataType = attribute.getAttributeType();
String fieldName = objName + "." + attrName;
ret = dataType.validateValueForUpdate(attrValue, fieldName, messages) && ret;
}
}
}
}
return ret;
}
use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.
the class AtlasStructFormatConverter method fromV2ToV1.
protected Map<String, Object> fromV2ToV1(AtlasStructType structType, Map<String, Object> attributes, ConverterContext context) throws AtlasBaseException {
Map<String, Object> ret = null;
if (MapUtils.isNotEmpty(attributes)) {
ret = new HashMap<>();
// Only process the requested/set attributes
for (String attrName : attributes.keySet()) {
AtlasAttribute attr = structType.getAttribute(attrName);
if (attr == null) {
LOG.warn("ignored unknown attribute {}.{}", structType.getTypeName(), attrName);
continue;
}
AtlasType attrType = attr.getAttributeType();
Object v2Value = attributes.get(attr.getName());
Object v1Value;
AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
v1Value = attrConverter.fromV2ToV1(v2Value, attrType, context);
ret.put(attr.getName(), v1Value);
}
}
return ret;
}
use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.
the class AtlasEntityStoreV1 method updateEntityAttributeByGuid.
@Override
@GraphTransaction
public EntityMutationResponse updateEntityAttributeByGuid(String guid, String attrName, Object attrValue) throws AtlasBaseException {
if (LOG.isDebugEnabled()) {
LOG.debug("==> updateEntityAttributeByGuid({}, {}, {})", guid, attrName, attrValue);
}
AtlasEntityWithExtInfo entityInfo = getById(guid);
if (entityInfo == null || entityInfo.getEntity() == null) {
throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
}
AtlasEntity entity = entityInfo.getEntity();
AtlasEntityType entityType = (AtlasEntityType) typeRegistry.getType(entity.getTypeName());
AtlasAttribute attr = entityType.getAttribute(attrName);
if (attr == null) {
throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_ATTRIBUTE, attrName, entity.getTypeName());
}
AtlasType attrType = attr.getAttributeType();
AtlasEntity updateEntity = new AtlasEntity();
updateEntity.setGuid(guid);
updateEntity.setTypeName(entity.getTypeName());
switch(attrType.getTypeCategory()) {
case PRIMITIVE:
updateEntity.setAttribute(attrName, attrValue);
break;
case OBJECT_ID_TYPE:
AtlasObjectId objId;
if (attrValue instanceof String) {
objId = new AtlasObjectId((String) attrValue, attr.getAttributeDef().getTypeName());
} else {
objId = (AtlasObjectId) attrType.getNormalizedValue(attrValue);
}
updateEntity.setAttribute(attrName, objId);
break;
default:
throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UPDATE_NOT_SUPPORTED, attrName, attrType.getTypeName());
}
return createOrUpdate(new AtlasEntityStream(updateEntity), true);
}
Aggregations