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

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

Introduction

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

Prototype

boolean isCollectionLike();

Source Link

Document

Returns whether the type can be considered a collection, which means it's a container of elements, e.g.

Usage

From source file:com.github.vanroy.springdata.jest.MappingBuilder.java

protected static Class<?> getFieldType(java.lang.reflect.Field field) {
    Class<?> clazz = field.getType();
    TypeInformation typeInformation = ClassTypeInformation.from(clazz);
    if (typeInformation.isCollectionLike()) {
        clazz = GenericCollectionTypeResolver.getCollectionFieldType(field) != null
                ? GenericCollectionTypeResolver.getCollectionFieldType(field)
                : typeInformation.getComponentType().getType();
    }/*from  www  .j a  v a  2s . c o  m*/
    return clazz;
}

From source file:org.springframework.data.rest.webmvc.json.JsonSchema.java

/**
 * Turns the given {@link TypeInformation} into a JSON Schema type string.
 * //from w ww.j  a  v  a 2  s.  c o  m
 * @param typeInformation
 * @return
 * @see http://json-schema.org/latest/json-schema-core.html#anchor8
 */
private static String toJsonSchemaType(TypeInformation<?> typeInformation) {

    Class<?> type = typeInformation.getType();

    if (type == null) {
        return null;
    } else if (typeInformation.isCollectionLike()) {
        return "array";
    } else if (Boolean.class.equals(type) || boolean.class.equals(type)) {
        return "boolean";
    } else if (String.class.equals(type) || isDate(typeInformation) || type.isEnum()) {
        return "string";
    } else if (INTEGER_TYPES.contains(type)) {
        return "integer";
    } else if (ClassUtils.isAssignable(Number.class, type)) {
        return "number";
    } else {
        return "object";
    }
}

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 ww w.  jav  a 2s.c  o  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  . j a v  a2s.co 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.document.mongodb.convert.MappingMongoConverter.java

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

    if (null == dbo) {
        return null;
    }/*from  ww w .  ja va 2  s  . com*/

    TypeInformation<? extends S> typeToUse = getMoreConcreteTargetType(dbo, type);
    Class<? extends S> rawType = typeToUse.getType();
    Class<?> customTarget = getCustomTarget(rawType, DBObject.class);

    if (customTarget != null) {
        return conversionService.convert(dbo, rawType);
    }

    if (typeToUse.isCollectionLike() && dbo instanceof BasicDBList) {
        List<Object> l = new ArrayList<Object>();
        BasicDBList dbList = (BasicDBList) dbo;
        for (Object o : dbList) {
            if (o instanceof DBObject) {

                Object newObj = read(typeToUse.getComponentType(), (DBObject) o);
                Class<?> rawComponentType = typeToUse.getComponentType().getType();

                if (newObj.getClass().isAssignableFrom(rawComponentType)) {
                    l.add(newObj);
                } else {
                    l.add(conversionService.convert(newObj, rawComponentType));
                }
            } else {
                l.add(o);
            }
        }
        return conversionService.convert(l, rawType);
    }

    // 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.document.mongodb.convert.MappingMongoConverter.java

@SuppressWarnings({ "unchecked" })
protected void writePropertyInternal(MongoPersistentProperty prop, Object obj, DBObject dbo) {
    org.springframework.data.document.mongodb.mapping.DBRef dbref = prop.getField()
            .getAnnotation(org.springframework.data.document.mongodb.mapping.DBRef.class);

    String name = prop.getName();
    Class<?> type = prop.getType();
    if (prop.isCollection()) {
        BasicDBList dbList = new BasicDBList();
        Collection<?> coll;
        if (type.isArray()) {
            coll = new ArrayList<Object>();
            for (Object o : (Object[]) obj) {
                ((List<Object>) coll).add(o);
            }/*from w  w  w  . j  a  v  a2  s .  co  m*/
        } else {
            coll = (Collection<?>) obj;
        }
        for (Object propObjItem : coll) {
            if (null != dbref) {
                DBRef dbRef = createDBRef(propObjItem, dbref);
                dbList.add(dbRef);
            } else if (type.isArray() && isSimpleType(prop.getComponentType())) {
                dbList.add(propObjItem);
            } else if (propObjItem instanceof List) {
                List<?> propObjColl = (List<?>) propObjItem;
                TypeInformation<?> typeInfo = ClassTypeInformation.from(propObjItem.getClass());
                while (typeInfo.isCollectionLike()) {
                    typeInfo = typeInfo.getComponentType();
                }
                if (isSimpleType(typeInfo.getType())) {
                    dbList.add(propObjColl);
                } else {
                    BasicDBList propNestedDbList = new BasicDBList();
                    for (Object propNestedObjItem : propObjColl) {
                        BasicDBObject propDbObj = new BasicDBObject();
                        writeInternal(propNestedObjItem, propDbObj);
                        propNestedDbList.add(propDbObj);
                    }
                    dbList.add(propNestedDbList);
                }
            } else if (isSimpleType(propObjItem.getClass())) {
                dbList.add(propObjItem);
            } else {
                BasicDBObject propDbObj = new BasicDBObject();
                writeInternal(propObjItem, propDbObj,
                        mappingContext.getPersistentEntity(prop.getComponentType()));
                dbList.add(propDbObj);
            }
        }
        dbo.put(name, dbList);
        return;
    }

    if (null != obj && obj instanceof Map) {
        BasicDBObject mapDbObj = new BasicDBObject();
        writeMapInternal((Map<Object, Object>) obj, mapDbObj);
        dbo.put(name, mapDbObj);
        return;
    }

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

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

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

    BasicDBObject propDbObj = new BasicDBObject();
    writeInternal(obj, propDbObj, mappingContext.getPersistentEntity(prop.getTypeInformation()));
    dbo.put(name, propDbObj);
}

From source file:org.springframework.data.elasticsearch.core.MappingBuilder.java

private static boolean isEntity(java.lang.reflect.Field field) {
    TypeInformation typeInformation = ClassTypeInformation.from(field.getType());
    TypeInformation<?> actualType = typeInformation.getActualType();
    boolean isComplexType = actualType == null ? false : !SIMPLE_TYPE_HOLDER.isSimpleType(actualType.getType());
    return isComplexType && !actualType.isCollectionLike()
            && !Map.class.isAssignableFrom(typeInformation.getType());
}

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

/**
 * Creates a leaf {@link PropertyPath} (no nested ones with the given name and owning type.
 * //  w  ww .  j  ava  2 s. c o  m
 * @param name must not be {@literal null} or empty.
 * @param owningType must not be {@literal null}.
 * @param base the {@link PropertyPath} previously found.
 */
PropertyPath(String name, TypeInformation<?> owningType, List<PropertyPath> base) {

    Assert.hasText(name);
    Assert.notNull(owningType);

    String propertyName = name.matches(ALL_UPPERCASE) ? name : StringUtils.uncapitalize(name);
    boolean isSpecli = org.apache.commons.lang3.StringUtils.contains(propertyName, "*");

    String _propertyName = isSpecli
            ? propertyName.replaceAll("\\*(.*)", org.apache.commons.lang3.StringUtils.EMPTY)
            : propertyName;

    if (propertyName.startsWith("iF")) {
        _propertyName = "name";
    }
    TypeInformation<?> propertyType = owningType.getProperty(_propertyName);

    if (!isSpecli) {
        if (propertyType == null) {
            throw new PropertyReferenceException(propertyName, owningType, base);
        }
    } else {

        if (true) {
            //for debug
        }

    }

    this.owningType = owningType;
    this.isCollection = propertyType == null ? false : propertyType.isCollectionLike();

    if (propertyType == null) {
        throw new PropertyReferenceException(_propertyName, owningType, base);
    }
    this.type = propertyType.getActualType();
    this.name = propertyName;
}

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  a  va2  s. c om

    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);
}