Example usage for org.springframework.data.util TypeInformation isMap

List of usage examples for org.springframework.data.util TypeInformation isMap

Introduction

In this page you can find the example usage for org.springframework.data.util TypeInformation isMap.

Prototype

boolean isMap();

Source Link

Document

Returns whether the property is a link java.util.Map .

Usage

From source file:io.twipple.springframework.data.clusterpoint.convert.DefaultClusterpointEntityConverter.java

/**
 * Read an incoming {@link ClusterpointDocument} into the target entity.
 *
 * @param type   the type information of the target entity.
 * @param source the document to convert.
 * @param parent an optional parent object.
 * @param <R>    the entity type./*from  w ww .  j a  v a2 s.co  m*/
 * @return the converted entity.
 */
@NotNull
@SuppressWarnings("unchecked")
protected <R> R read(@NotNull TypeInformation<R> type, @NotNull ClusterpointDocument source, Object parent) {

    Assert.notNull(type);
    Assert.notNull(source);

    TypeInformation<? extends R> typeToUse = typeMapper.readType(source, type);
    Class<? extends R> rawType = typeToUse.getType();

    // TODO: handle custom conversions for DOMDocument and DOMElement.
    // Since ClusterpointDocument is a holder of a DOMDocument, it might happen that there is a custom conversion
    // for either DOMDocument or the root DOMElement.
    if (conversions.hasCustomReadTarget(source.getClass(), rawType)) {
        return conversionService.convert(source, rawType);
    }

    if (typeToUse.isMap()) {
        return (R) readMap(typeToUse, source, parent);
    }

    if (typeToUse.isCollectionLike()) {
        return (R) readCollection(typeToUse, source, parent);
    }

    ClusterpointPersistentEntity<R> entity = (ClusterpointPersistentEntity<R>) mappingContext
            .getPersistentEntity(typeToUse);
    if (entity == null) {
        throw new MappingException("No mapping metadata found for " + rawType.getName());
    }
    return read(entity, source, parent);
}

From source file:org.springframework.data.crate.core.convert.CrateDocumentConverter.java

/**
 * // w w  w  .  ja v  a 2s .  c  o m
 * @param root container for the converted payload
 * @param payload value to be converted to {@link CrateDocument}
 */
@SuppressWarnings("unchecked")
private void toCrateDocument(CrateDocument root, Object payload) {

    Map<String, Object> map = (Map<String, Object>) payload;

    for (Entry<String, Object> entry : map.entrySet()) {

        TypeInformation<?> type = getTypeInformation(entry.getValue().getClass());

        if (type.isMap()) {
            CrateDocument document = new CrateDocument();
            toCrateDocument(document, entry.getValue());
            logger.debug("converted '{}' to CrateDocument", entry.getKey());
            root.put(entry.getKey(), document);
        } else if (type.isCollectionLike()) {
            CrateArray array = new CrateArray();
            toCrateArray(array, entry.getValue());
            logger.debug("converted '{}' to CrateArray", entry.getKey());
            root.put(entry.getKey(), array);
        } else {
            // simple type
            root.put(entry.getKey(), entry.getValue());
        }
    }
}

From source file:org.springframework.data.crate.core.convert.CrateDocumentConverter.java

/**
 * Nesting Array or Collection types is not supported by crate. It is safe to assume that the payload
 * will contain either a Map or a primitive type. Map types will be converted to {@link CrateDocument}
 * while simple types will be added without any conversion
 * @param array {@link CrateArray} for adding either Map or Simple types
 * @param payload containing either a Map or primitive type.
 *///from w w  w  .  j  av  a 2  s.c o  m
@SuppressWarnings("unchecked")
private void toCrateArray(CrateArray array, Object payload) {

    Collection<Object> objects = (Collection<Object>) (payload.getClass().isArray() ? asList((Object[]) payload)
            : payload);

    for (Object object : objects) {

        TypeInformation<?> type = getTypeInformation(object.getClass());

        if (type.isMap()) {
            CrateDocument document = new CrateDocument();
            toCrateDocument(document, object);
            array.add(document);
        } else {
            array.add(object);
        }
    }
}

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

@SuppressWarnings("unchecked")
protected <S extends Object> S read(TypeInformation<S> type, DBObject dbo) {

    if (null == dbo) {
        return null;
    }//from   ww w  .j  av a 2s  .com

    TypeInformation<? extends S> typeToUse = typeMapper.readType(dbo, type);
    Class<? extends S> rawType = typeToUse.getType();

    if (conversions.hasCustomReadTarget(dbo.getClass(), rawType)) {
        return conversionService.convert(dbo, rawType);
    }

    if (DBObject.class.isAssignableFrom(rawType)) {
        return (S) dbo;
    }

    if (typeToUse.isCollectionLike() && dbo instanceof BasicDBList) {
        return (S) readCollectionOrArray(typeToUse, (BasicDBList) dbo);
    }

    if (typeToUse.isMap()) {
        return (S) readMap(typeToUse, dbo);
    }

    // Retrieve persistent entity info
    MongoPersistentEntity<S> persistentEntity = (MongoPersistentEntity<S>) mappingContext
            .getPersistentEntity(typeToUse);
    if (persistentEntity == null) {
        throw new MappingException("No mapping metadata found for " + rawType.getName());
    }

    return read(persistentEntity, dbo);
}

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   www.  ja va 2  s. c  o  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.mongodb.core.convert.MappingMongoConverter.java

/**
 * Writes the given {@link Map} to the given {@link DBObject} considering the given {@link TypeInformation}.
 * /*from  w  ww.  j a  va 2s. c  o  m*/
 * @param obj must not be {@literal null}.
 * @param dbo must not be {@literal null}.
 * @param propertyType must not be {@literal null}.
 * @return
 */
protected DBObject writeMapInternal(Map<Object, Object> obj, DBObject dbo, TypeInformation<?> propertyType) {

    for (Map.Entry<Object, Object> entry : obj.entrySet()) {
        Object key = entry.getKey();
        Object val = entry.getValue();
        if (conversions.isSimpleType(key.getClass())) {
            // Don't use conversion service here as removal of ObjectToString converter results in some primitive types not
            // being convertable
            String simpleKey = potentiallyEscapeMapKey(key.toString());
            if (val == null || conversions.isSimpleType(val.getClass())) {
                writeSimpleInternal(val, dbo, simpleKey);
            } else if (val instanceof Collection || val.getClass().isArray()) {
                dbo.put(simpleKey, writeCollectionInternal(asCollection(val), propertyType.getMapValueType(),
                        new BasicDBList()));
            } else {
                DBObject newDbo = new BasicDBObject();
                TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType()
                        : ClassTypeInformation.OBJECT;
                writeInternal(val, newDbo, valueTypeInfo);
                dbo.put(simpleKey, newDbo);
            }
        } else {
            throw new MappingException("Cannot use a complex object as a key value.");
        }
    }

    return dbo;
}

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

@SuppressWarnings("unchecked")
public Object query(String statement, Map<String, Object> params, final TypeInformation<?> typeInformation) {
    final TypeInformation<?> actualType = typeInformation.getActualType();
    final Class<Object> targetType = (Class<Object>) actualType.getType();
    final Result<Object> result = queryEngineFor(QueryType.Cypher).query(statement, params);
    final Class<? extends Iterable<Object>> containerType = (Class<? extends Iterable<Object>>) typeInformation
            .getType();/*  ww w  .  j  a  va2 s  .  c om*/
    if (EndResult.class.isAssignableFrom(containerType)) {
        return result;
    }
    if (actualType.isMap()) {
        return result;
    }
    if (typeInformation.isCollectionLike()) {
        return result.to(targetType).as(containerType);
    }
    return result.to(targetType).single();
}