use of org.apache.atlas.type.AtlasType 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);
}
use of org.apache.atlas.type.AtlasType in project incubator-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();
List<Object> currentElements = getArrayElementsProperty(elementType, ctx.getReferringVertex(), ctx.getVertexProperty());
boolean isReference = AtlasGraphUtilsV1.isReference(elementType);
AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
List<Object> newElementsCreated = new ArrayList<>();
if (CollectionUtils.isNotEmpty(newElements)) {
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);
}
newElementsCreated.add(newEntry);
}
}
if (isReference) {
List<AtlasEdge> additionalEdges = removeUnusedArrayEntries(attribute, (List) currentElements, (List) newElementsCreated);
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.type.AtlasType 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.AtlasType in project incubator-atlas by apache.
the class AtlasArrayFormatConverter method fromV1ToV2.
@Override
public Collection fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException {
Collection ret = null;
if (v1Obj != null) {
if (v1Obj instanceof Set) {
ret = new LinkedHashSet();
} else {
ret = new ArrayList();
}
AtlasArrayType arrType = (AtlasArrayType) type;
AtlasType elemType = arrType.getElementType();
AtlasFormatConverter elemConverter = converterRegistry.getConverter(elemType.getTypeCategory());
if (v1Obj instanceof Collection) {
Collection v1List = (Collection) v1Obj;
for (Object v1Elem : v1List) {
Object convertedVal = elemConverter.fromV1ToV2(v1Elem, elemType, ctx);
ret.add(convertedVal);
}
} else {
Object convertedVal = elemConverter.fromV1ToV2(v1Obj, elemType, ctx);
ret.add(convertedVal);
}
}
return ret;
}
use of org.apache.atlas.type.AtlasType in project incubator-atlas by apache.
the class AtlasEntityFormatConverter method fromV1ToV2.
@Override
public AtlasEntity fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext context) throws AtlasBaseException {
AtlasEntity entity = null;
if (v1Obj != null) {
AtlasEntityType entityType = (AtlasEntityType) type;
if (v1Obj instanceof IReferenceableInstance) {
IReferenceableInstance entRef = (IReferenceableInstance) v1Obj;
String guid = entRef.getId()._getId();
if (!context.entityExists(guid)) {
Map<String, Object> v1Attribs = null;
try {
v1Attribs = entRef.getValuesMap();
} catch (AtlasException excp) {
LOG.error("IReferenceableInstance.getValuesMap() failed", excp);
}
entity = new AtlasEntity(entRef.getTypeName(), super.fromV1ToV2(entityType, v1Attribs, context));
entity.setGuid(entRef.getId()._getId());
entity.setStatus(convertState(entRef.getId().getState()));
entity.setCreatedBy(entRef.getSystemAttributes().createdBy);
entity.setCreateTime(entRef.getSystemAttributes().createdTime);
entity.setUpdatedBy(entRef.getSystemAttributes().modifiedBy);
entity.setUpdateTime(entRef.getSystemAttributes().modifiedTime);
entity.setVersion((long) entRef.getId().version);
if (CollectionUtils.isNotEmpty(entRef.getTraits())) {
List<AtlasClassification> classifications = new ArrayList<>();
AtlasFormatConverter traitConverter = converterRegistry.getConverter(TypeCategory.CLASSIFICATION);
for (String traitName : entRef.getTraits()) {
IStruct trait = entRef.getTrait(traitName);
AtlasType classifiType = typeRegistry.getType(traitName);
AtlasClassification classification = (AtlasClassification) traitConverter.fromV1ToV2(trait, classifiType, context);
classifications.add(classification);
}
entity.setClassifications(classifications);
}
} else {
entity = context.getById(guid);
}
} else {
throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "IReferenceableInstance", v1Obj.getClass().getCanonicalName());
}
}
return entity;
}
Aggregations