Example usage for org.springframework.data.util ClassTypeInformation from

List of usage examples for org.springframework.data.util ClassTypeInformation from

Introduction

In this page you can find the example usage for org.springframework.data.util ClassTypeInformation from.

Prototype

public static <S> ClassTypeInformation<S> from(Class<S> type) 

Source Link

Document

Simple factory method to easily create new instances of ClassTypeInformation .

Usage

From source file:org.springframework.data.mapping.PropertyPath.java

/**
 * Extracts the {@link PropertyPath} chain from the given source {@link String} and type.
 * /*ww  w .j  a v  a  2 s . com*/
 * @param source
 * @param type
 * @return
 */
public static PropertyPath from(String source, Class<?> type) {
    return from(source, ClassTypeInformation.from(type));
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

/**
 * Root entry method into write conversion. Adds a type discriminator to the {@link DBObject}. Shouldn't be called for
 * nested conversions.//from  w  ww  .j ava 2s  .  co  m
 * 
 * @see org.springframework.data.mongodb.core.core.convert.MongoWriter#write(java.lang.Object, com.mongodb.DBObject)
 */
public void write(final Object obj, final DBObject dbo) {

    if (null == obj) {
        return;
    }

    boolean handledByCustomConverter = conversions.getCustomWriteTarget(obj.getClass(), DBObject.class) != null;
    TypeInformation<? extends Object> type = ClassTypeInformation.from(obj.getClass());

    if (!handledByCustomConverter && !(dbo instanceof BasicDBList)) {
        typeMapper.writeType(type, dbo);
    }

    writeInternal(obj, dbo, type);
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

@SuppressWarnings({ "unchecked" })
protected void writePropertyInternal(Object obj, DBObject dbo, MongoPersistentProperty prop) {

    if (obj == null) {
        return;//from  w w  w  .j  a  v a 2  s.co m
    }

    String name = prop.getFieldName();
    TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass());
    TypeInformation<?> type = prop.getTypeInformation();

    if (valueType.isCollectionLike()) {
        DBObject collectionInternal = createCollection(asCollection(obj), prop);
        dbo.put(name, collectionInternal);
        return;
    }

    if (valueType.isMap()) {
        BasicDBObject mapDbObj = new BasicDBObject();
        writeMapInternal((Map<Object, Object>) obj, mapDbObj, type);
        dbo.put(name, mapDbObj);
        return;
    }

    if (prop.isDbReference()) {
        DBRef dbRefObj = createDBRef(obj, prop.getDBRef());
        if (null != dbRefObj) {
            dbo.put(name, dbRefObj);
            return;
        }
    }

    // Lookup potential custom target type
    Class<?> basicTargetType = conversions.getCustomWriteTarget(obj.getClass(), null);

    if (basicTargetType != null) {
        dbo.put(name, conversionService.convert(obj, basicTargetType));
        return;
    }

    BasicDBObject propDbObj = new BasicDBObject();
    addCustomTypeKeyIfNecessary(type, obj, propDbObj);

    MongoPersistentEntity<?> entity = isSubtype(prop.getType(), obj.getClass())
            ? mappingContext.getPersistentEntity(obj.getClass())
            : mappingContext.getPersistentEntity(type);

    writeInternal(obj, propDbObj, entity);
    dbo.put(name, propDbObj);
}

From source file:org.springframework.data.neo4j.support.Neo4jTemplate.java

@Override
@SuppressWarnings("unchecked")
public <T> T fetch(T value) {
    if (value == null)
        return null;
    final Class<T> targetType = (Class<T>) value.getClass();
    final TypeInformation<T> targetTypeInformation = ClassTypeInformation.from(targetType);

    final Neo4jEntityPersister entityPersister = infrastructure.getEntityPersister();
    if (targetTypeInformation.isCollectionLike()) {
        Iterable<?> collection = (Iterable<?>) value;
        for (Object entry : collection) {
            fetch(entry);//  ww w .  j a  va2 s  .c  o m
        }
        return value;
    } else {
        final PropertyContainer state = getPersistentState(value);
        if (state != null) {
            return entityPersister.loadEntity(value, (Node) state, MappingPolicy.LOAD_POLICY,
                    (Neo4jPersistentEntityImpl<T>) getPersistentEntity(targetType));
        } else {
            // todo do nothing?
            throw new MappingException("No state information available in " + value);
        }
    }
}

From source file:org.springframework.data.solr.core.convert.MappingSolrConverter.java

@SuppressWarnings("unchecked")
@Override/*from w w  w .  j  av a2  s .  co  m*/
public void write(Object source, @SuppressWarnings("rawtypes") Map target) {
    if (source == null) {
        return;
    }

    if (hasCustomWriteTarget(source.getClass(), SolrInputDocument.class)
            && canConvert(source.getClass(), SolrInputDocument.class)) {
        SolrInputDocument convertedDocument = convert(source, SolrInputDocument.class);
        target.putAll(convertedDocument);
        return;

    }

    TypeInformation<? extends Object> type = ClassTypeInformation.from(source.getClass());
    write(type, source, target);
}