public ITypedReferenceableInstance convert()

in typesystem/src/main/java/org/apache/atlas/typesystem/types/ClassType.java [96:177]


    public ITypedReferenceableInstance convert(Object val, Multiplicity m) throws AtlasException {

        if (val != null) {
            if (val instanceof ITypedReferenceableInstance) {
                ITypedReferenceableInstance tr = (ITypedReferenceableInstance) val;
                if (!tr.getTypeName().equals(getName())) {
                     /*
                     * If val is a subType instance; invoke convert on it.
                     */
                    ClassType valType = typeSystem.getDataType(superTypeClass, tr.getTypeName());
                    if (valType.superTypePaths.containsKey(name)) {
                        return valType.convert(val, m);
                    }
                    throw new ValueConversionException(this, val);
                }
                return tr;
            } else if (val instanceof Struct) {
                Struct s = (Struct) val;
                Referenceable r = null;
                Id id = null;

                if (!s.typeName.equals(getName())) {
                    /*
                     * If val is a subType instance; invoke convert on it.
                     */
                    ClassType valType = typeSystem.getDataType(superTypeClass, s.typeName);
                    if (valType.superTypePaths.containsKey(name)) {
                        return valType.convert(s, m);
                    }
                    throw new ValueConversionException(this, val);
                }

                if (val instanceof Referenceable) {
                    r = (Referenceable) val;
                    id = r.getId();
                }

                ITypedReferenceableInstance tr =
                        r != null ? createInstanceWithTraits(id, null, r, r.getTraits().toArray(new String[0])) :
                                createInstance(id);

                if (id != null && id.isAssigned()) {
                    return tr;
                }

                for (Map.Entry<String, AttributeInfo> e : fieldMapping.fields.entrySet()) {
                    String attrKey = e.getKey();
                    AttributeInfo i = e.getValue();
                    Object aVal = s.get(attrKey);
                    if (aVal != null && i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) {
                        if (!i.isComposite) {
                            aVal = ((IReferenceableInstance) aVal).getId();
                        }
                    }

                    if(!i.multiplicity.nullAllowed() && !s.getValuesMap().containsKey(attrKey)){
                        throw new ValueConversionException.NullConversionException(i.multiplicity,
                                String.format(" Value expected for required attribute %s", i.name));
                    } else {
                        try {
                            if (s.getValuesMap().containsKey(attrKey)) {
                                tr.set(attrKey, aVal);
                            }
                        } catch (ValueConversionException ve) {
                            throw new ValueConversionException(this, val, ve);
                        }
                    }
                }

                return tr;
            } else if (val instanceof ReferenceableInstance) {
                validateId(((ReferenceableInstance) val).getId());
                return (ReferenceableInstance) val;
            } else {
                throw new ValueConversionException(this, val, "value's class is " + val.getClass().getName());
            }
        }
        if (!m.nullAllowed()) {
            throw new ValueConversionException.NullConversionException(m);
        }
        return null;
    }