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

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

Introduction

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

Prototype

Class<S> getType();

Source Link

Document

Returns the type of the property.

Usage

From source file:com.frank.search.solr.core.convert.MappingSolrConverter.java

private Collection<SolrInputField> writeWildcardMapPropertyToTarget(Map<? super Object, ? super Object> target,
        SolrPersistentProperty persistentProperty, Map<?, ?> fieldValue) {

    TypeInformation<?> mapTypeInformation = persistentProperty.getTypeInformation().getMapValueType();
    Class<?> rawMapType = mapTypeInformation.getType();
    String fieldName = persistentProperty.getFieldName();

    Collection<SolrInputField> fields = new ArrayList<SolrInputField>();

    for (Map.Entry<?, ?> entry : fieldValue.entrySet()) {

        Object value = entry.getValue();
        String key = entry.getKey().toString();

        if (persistentProperty.isDynamicProperty()) {
            key = WildcardPosition.getAppropriate(key).createName(fieldName, key);
        }/*from   ww w. ja va  2s  .c om*/

        SolrInputField field = new SolrInputField(key);

        if (value instanceof Iterable) {

            for (Object o : (Iterable<?>) value) {
                field.addValue(convertToSolrType(rawMapType, o), 1f);
            }
        } else {

            if (rawMapType.isArray()) {
                for (Object o : (Object[]) value) {
                    field.addValue(convertToSolrType(rawMapType, o), 1f);
                }
            } else {
                field.addValue(convertToSolrType(rawMapType, value), 1f);
            }

        }

        target.put(key, field);
        fields.add(field);
    }

    return fields;
}

From source file:com.frank.search.solr.core.convert.MappingSolrConverter.java

@SuppressWarnings("unchecked")
protected <S extends Object> S read(TypeInformation<S> targetTypeInformation, Map<String, ?> source) {
    if (source == null) {
        return null;
    }//from w  ww  .  j av  a  2 s  . c  om
    Assert.notNull(targetTypeInformation);
    Class<S> rawType = targetTypeInformation.getType();

    // in case there's a custom conversion for the document
    if (hasCustomReadTarget(source.getClass(), rawType)) {
        return convert(source, rawType);
    }

    SolrPersistentEntity<S> entity = (SolrPersistentEntity<S>) mappingContext.getPersistentEntity(rawType);
    return read(entity, source, null);
}

From source file:com.joyveb.dbpimpl.cass.prepare.convert.MappingCassandraConverter.java

@SuppressWarnings("unchecked")
public <R> R readRow(Class<R> clazz, Row row) {
    Class<R> beanClassLoaderClass = transformClassToBeanClassLoaderClass(clazz);
    TypeInformation<? extends R> type = ClassTypeInformation.from(beanClassLoaderClass);
    // TypeInformation<? extends R> typeToUse = typeMapper.readType(row, type);
    TypeInformation<? extends R> typeToUse = type;
    Class<? extends R> rawType = typeToUse.getType();

    if (Row.class.isAssignableFrom(rawType)) {
        return (R) row;
    }/*from ww w .  j  av  a2s .  c o  m*/
    CassandraPersistentEntity<R> persistentEntity = (CassandraPersistentEntity<R>) mappingContext
            .getPersistentEntity(typeToUse);
    if (persistentEntity == null) {
        throw new MappingException("No mapping metadata found for " + rawType.getName());
    }

    return readRowInternal(persistentEntity, row);
}

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

private JsonSchemaProperty getSchemaProperty(BeanPropertyDefinition definition, TypeInformation<?> type,
        ResourceDescription description) {

    String name = definition.getName();
    String title = resolveMessageWithDefault(new ResolvableProperty(definition));
    String resolvedDescription = resolveMessage(description);
    boolean required = definition.isRequired();
    Class<?> rawType = type.getType();

    if (!rawType.isEnum()) {
        return new JsonSchemaProperty(name, title, resolvedDescription, required).with(type);
    }//ww w .j  ava 2 s  . c om

    String message = resolveMessage(new DefaultMessageSourceResolvable(description.getMessage()));

    return new EnumProperty(name, title, rawType,
            description.getDefaultMessage().equals(resolvedDescription) ? message : resolvedDescription,
            required);
}

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

private List<AbstractJsonSchemaProperty<?>> getPropertiesFor(Class<?> type, final ResourceMetadata metadata,
        final Definitions definitions) {

    final PersistentEntity<?, ?> entity = entities.getPersistentEntity(type);
    final JacksonMetadata jackson = new JacksonMetadata(objectMapper, type);

    if (entity == null) {
        return Collections.<AbstractJsonSchemaProperty<?>>emptyList();
    }//from   ww w. ja va  2s . c  o m

    JsonSchemaPropertyRegistrar registrar = new JsonSchemaPropertyRegistrar(jackson);

    for (BeanPropertyDefinition definition : jackson) {

        PersistentProperty<?> persistentProperty = entity.getPersistentProperty(definition.getInternalName());

        // First pass, early drops to avoid unnecessary calculation
        if (persistentProperty != null) {

            if (persistentProperty.isIdProperty() && !configuration.isIdExposedFor(type)) {
                continue;
            }

            if (persistentProperty.isVersionProperty()) {
                continue;
            }

            if (!definition.couldSerialize()) {
                continue;
            }
        }

        AnnotatedMember primaryMember = definition.getPrimaryMember();

        if (primaryMember == null) {
            continue;
        }

        TypeInformation<?> propertyType = persistentProperty == null
                ? ClassTypeInformation.from(primaryMember.getRawType())
                : persistentProperty.getTypeInformation();
        TypeInformation<?> actualPropertyType = propertyType.getActualType();
        Class<?> rawPropertyType = propertyType.getType();

        JsonSchemaFormat format = configuration.getMetadataConfiguration().getSchemaFormatFor(rawPropertyType);
        ResourceDescription description = persistentProperty == null
                ? jackson.getFallbackDescription(metadata, definition)
                : getDescriptionFor(persistentProperty, metadata);
        JsonSchemaProperty property = getSchemaProperty(definition, propertyType, description);

        boolean isSyntheticProperty = persistentProperty == null;
        boolean isNotWritable = !isSyntheticProperty && !persistentProperty.isWritable();
        boolean isJacksonReadOnly = !isSyntheticProperty && jackson.isReadOnly(persistentProperty);

        if (isSyntheticProperty || isNotWritable || isJacksonReadOnly) {
            property = property.withReadOnly();
        }

        if (format != null) {

            // Types with explicitly registered format -> value object with format
            registrar.register(property.withFormat(format), actualPropertyType);
            continue;
        }

        Pattern pattern = configuration.getMetadataConfiguration().getPatternFor(rawPropertyType);

        if (pattern != null) {
            registrar.register(property.withPattern(pattern), actualPropertyType);
            continue;
        }

        if (jackson.isValueType()) {
            registrar.register(property.with(STRING_TYPE_INFORMATION), actualPropertyType);
            continue;
        }

        if (persistentProperty == null) {
            registrar.register(property, actualPropertyType);
            continue;
        }

        if (configuration.isLookupType(persistentProperty.getActualType())) {
            registrar.register(property.with(propertyType), actualPropertyType);
        } else if (associations.isLinkableAssociation(persistentProperty)) {
            registrar.register(property.asAssociation(), null);
        } else {

            if (persistentProperty.isEntity()) {

                if (!definitions.hasDefinitionFor(propertyType)) {
                    definitions.addDefinition(propertyType,
                            new Item(propertyType, getNestedPropertiesFor(persistentProperty, definitions)));
                }

                registrar.register(property.with(propertyType, Definitions.getReference(propertyType)),
                        actualPropertyType);

            } else {

                registrar.register(property.with(propertyType), actualPropertyType);
            }
        }
    }

    return registrar.getProperties();
}

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 .j av  a 2 s  . 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.document.mongodb.convert.MappingMongoConverter.java

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

    if (null == dbo) {
        return null;
    }/*www  .j  a  v  a  2  s .c  om*/

    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);
            }/*w w  w  . j a v  a  2s . 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.document.mongodb.convert.MappingMongoConverter.java

/**
 * Inspects the a custom class definition stored inside the given {@link DBObject} and returns that in case it's a
 * subtype of the given basic one.//from   ww  w.  jav a2 s .c  o m
 * 
 * @param dbObject
 * @param basicType
 * @return
 */
@SuppressWarnings("unchecked")
private <S> TypeInformation<? extends S> getMoreConcreteTargetType(DBObject dbObject,
        TypeInformation<S> basicType) {
    Class<?> documentsTargetType = findTypeToBeUsed(dbObject);
    Class<S> rawType = basicType.getType();
    boolean isMoreConcreteCustomType = documentsTargetType != null
            && rawType.isAssignableFrom(documentsTargetType);
    return isMoreConcreteCustomType
            ? (TypeInformation<? extends S>) ClassTypeInformation.from(documentsTargetType)
            : basicType;
}