All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mongodb.morphia.converters.KeyConverter Maven / Gradle / Ivy

The newest version!
package org.mongodb.morphia.converters;

import com.mongodb.DBRef;
import org.mongodb.morphia.Key;
import org.mongodb.morphia.mapping.MappedField;

/**
 * @author Uwe Schaefer, ([email protected])
 * @author scotthernandez
 */
public class KeyConverter extends TypeConverter {

    /**
     * Creates the Converter.
     */
    public KeyConverter() {
        super(Key.class);
    }

    @Override
    public Object decode(final Class targetClass, final Object o, final MappedField optionalExtraInfo) {
        if (o == null) {
            return null;
        }
        if (!(o instanceof DBRef)) {
            throw new ConverterException(String.format("cannot convert %s to Key because it isn't a DBRef", o.toString()));
        }


        DBRef ref = (DBRef) o;

        MappedField actualType = getActualType(optionalExtraInfo);


        final Class keyType = actualType != null
                                 ? actualType.getConcreteType()
                                 : getMapper().getClassFromCollection(ref.getCollectionName());

        final Key key = new Key(keyType, ref.getCollectionName(), ref.getId());

        return key;
    }

    private MappedField getActualType(final MappedField field) {
        if (field == null) {
            return null;
        }
        MappedField mappedField = field.getTypeParameters().get(0);
        if (mappedField.getTypeParameters().size() != 0) {
            mappedField = getActualType(mappedField);
        }
        return mappedField;
    }

    @Override
    public Object encode(final Object t, final MappedField optionalExtraInfo) {
        if (t == null) {
            return null;
        }
        return getMapper().keyToDBRef((Key) t);
    }

}