Java Code Examples for org.springframework.data.util.TypeInformation#isMap()

The following examples show how to use org.springframework.data.util.TypeInformation#isMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityColumnMapper.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * @param properties properties of array/collection types, must not be {@literal null}.
 * @return list of columns of crate type array
 * @throws {@link InvalidCrateApiUsageException}
 */
public List<Column> mapColumns(Set<CratePersistentProperty> properties) {
	
	List<Column> columns = new LinkedList<>();
	
	for(CratePersistentProperty property : properties) {
		
		TypeInformation<?> typeInformation = from(property.getComponentType());
		// safety check
		if(property.isCollectionLike() && typeInformation.isMap()) {
			
			// could be a list or an array
			TypeInformation<?> actualType = property.getTypeInformation().getActualType();					
			// get the map's key type
			Class<?> componentType = actualType.getTypeArguments().get(0).getType();					
			
			checkMapKey(componentType);
			
			columns.add(createColumn(property));
		}
	}
	
	return columns;
}
 
Example 2
Source File: CrateDocumentConverter.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @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());
		}
	}
}
 
Example 3
Source File: CrateDocumentConverter.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * 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.
 */
@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);
		}
	}
}
 
Example 4
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <S> S read(TypeInformation<S> type, Object source) {

	SecretDocument secretDocument = getSecretDocument(source);

	TypeInformation<? extends S> typeToUse = secretDocument != null
			? this.typeMapper.readType(secretDocument.getBody(), type)
			: (TypeInformation) ClassTypeInformation.OBJECT;
	Class<? extends S> rawType = typeToUse.getType();

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

	if (SecretDocument.class.isAssignableFrom(rawType)) {
		return (S) source;
	}

	if (Map.class.isAssignableFrom(rawType) && secretDocument != null) {
		return (S) secretDocument.getBody();
	}

	if (typeToUse.isMap() && secretDocument != null) {
		return (S) readMap(typeToUse, secretDocument.getBody());
	}

	if (typeToUse.equals(ClassTypeInformation.OBJECT)) {
		return (S) source;
	}

	return read((VaultPersistentEntity<S>) this.mappingContext.getRequiredPersistentEntity(typeToUse),
			secretDocument);
}
 
Example 5
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the given {@link Map} to the given {@link Map} considering the given
 * {@link TypeInformation}.
 * @param obj must not be {@literal null}.
 * @param bson must not be {@literal null}.
 * @param propertyType must not be {@literal null}.
 * @return the converted {@link Map}.
 */
protected Map<String, Object> writeMapInternal(Map<Object, Object> obj, Map<String, Object> bson,
		TypeInformation<?> propertyType) {

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

		Object key = entry.getKey();
		Object val = entry.getValue();

		if (this.conversions.isSimpleType(key.getClass())) {

			String simpleKey = key.toString();
			if (val == null || this.conversions.isSimpleType(val.getClass())) {
				bson.put(simpleKey, val);
			}
			else if (val instanceof Collection || val.getClass().isArray()) {

				bson.put(simpleKey, writeCollectionInternal(asCollection(val), propertyType.getMapValueType(),
						new ArrayList<>()));
			}
			else {
				SecretDocumentAccessor nested = new SecretDocumentAccessor(new SecretDocument());
				TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType()
						: ClassTypeInformation.OBJECT;
				writeInternal(val, nested, valueTypeInfo);
				bson.put(simpleKey, nested.getBody());
			}
		}
		else {
			throw new MappingException("Cannot use a complex object as a key value.");
		}
	}

	return bson;
}
 
Example 6
Source File: MappingCrateConverter.java    From spring-data-crate with Apache License 2.0 5 votes vote down vote up
/**
 * Read an incoming {@link CrateDocument} 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.
 * @return the converted entity.
 */
@SuppressWarnings("unchecked")
protected <R> R read(final TypeInformation<R> type, final CrateDocument source, final Object parent) {
	
    if(source == null) {
    	return null;
    }

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

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

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

    CratePersistentEntity<R> entity = (CratePersistentEntity<R>) mappingContext.getPersistentEntity(typeToUse);
    
    if(entity == null) {
      throw new MappingException("No mapping metadata found for " + rawType.getName());
    }
    
    return read(entity, source, parent);
}
 
Example 7
Source File: MappingCrateConverter.java    From spring-data-crate with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to write the map into the crate document.
 * 
 * @param source the source object.
 * @param sink the target document.
 * @param type the type information for the document.
 * @return the written crate document.
 */
private CrateDocument writeMapInternal(final Map<Object, Object> source, final CrateDocument sink, final TypeInformation<?> type) {

	for(Map.Entry<Object, Object> entry : source.entrySet()) {
		
		Object key = entry.getKey();
		Object val = entry.getValue();
		
		if(conversions.isSimpleType(key.getClass())) {
			
			String simpleKey = key.toString();
			
			if(val == null || (conversions.isSimpleType(val.getClass()) && !val.getClass().isArray())) {
				writeSimpleInternal(val, sink, simpleKey);
			}else if(val instanceof Collection || val.getClass().isArray()) {
				sink.put(simpleKey, writeCollectionInternal(asCollection(val), new CrateArray(), type.getMapValueType()));
			}else {
				CrateDocument document = new CrateDocument();
				TypeInformation<?> valueTypeInfo = type.isMap() ? type.getMapValueType() : OBJECT;
				writeInternal(val, document, valueTypeInfo);
				sink.put(simpleKey, document);
			}
		} else {
			throw new MappingException("Cannot use a complex object as a key value.");
		}
	}
	
	return sink;
}
 
Example 8
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
private Object readInternal(final TypeInformation<?> type, final VPackSlice source) {
	if (source == null) {
		return null;
	}

	if (VPackSlice.class.isAssignableFrom(type.getType())) {
		return source;
	}

	final TypeInformation<?> typeToUse = (source.isArray() || source.isObject()) ? typeMapper.readType(source, type)
			: type;
	final Class<?> rawTypeToUse = typeToUse.getType();

	if (conversions.hasCustomReadTarget(VPackSlice.class, typeToUse.getType())) {
		return conversionService.convert(source, rawTypeToUse);
	}

	if (conversions.hasCustomReadTarget(DBDocumentEntity.class, typeToUse.getType())) {
		return conversionService.convert(readSimple(DBDocumentEntity.class, source), rawTypeToUse);
	}

	if (!source.isArray() && !source.isObject()) {
		return convertIfNecessary(readSimple(rawTypeToUse, source), rawTypeToUse);
	}

	if (DBDocumentEntity.class.isAssignableFrom(rawTypeToUse)) {
		return readSimple(rawTypeToUse, source);
	}

	if (BaseDocument.class.isAssignableFrom(rawTypeToUse)) {
		return readBaseDocument(rawTypeToUse, source);
	}

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

	if (!source.isArray() && ClassTypeInformation.OBJECT.equals(typeToUse)) {
		return readMap(ClassTypeInformation.MAP, source);
	}

	if (typeToUse.getType().isArray()) {
		return readArray(typeToUse, source);
	}

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

	if (ClassTypeInformation.OBJECT.equals(typeToUse)) {
		return readCollection(ClassTypeInformation.COLLECTION, source);
	}

	final ArangoPersistentEntity<?> entity = context.getRequiredPersistentEntity(rawTypeToUse);
	return readEntity(typeToUse, source, entity);
}
 
Example 9
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void writeInternal(
	final String attribute,
	final Object source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	final Class<?> rawType = source.getClass();
	final TypeInformation<?> type = ClassTypeInformation.from(rawType);

	if (conversions.isSimpleType(rawType)) {
		final Optional<Class<?>> customWriteTarget = conversions.getCustomWriteTarget(rawType);
		final Class<?> targetType = customWriteTarget.orElse(rawType);
		writeSimple(attribute, conversionService.convert(source, targetType), sink);
	}

	else if (BaseDocument.class.equals(rawType)) {
		writeBaseDocument(attribute, (BaseDocument) source, sink, definedType);
	}

	else if (BaseEdgeDocument.class.equals(rawType)) {
		writeBaseEdgeDocument(attribute, (BaseEdgeDocument) source, sink, definedType);
	}

	else if (type.isMap()) {
		writeMap(attribute, (Map<Object, Object>) source, sink, definedType);
	}

	else if (type.getType().isArray()) {
		writeArray(attribute, source, sink, definedType);
	}

	else if (type.isCollectionLike()) {
		writeCollection(attribute, source, sink, definedType);
	}

	else {
		final ArangoPersistentEntity<?> entity = context.getRequiredPersistentEntity(source.getClass());
		writeEntity(attribute, source, sink, entity, definedType);
	}
}
 
Example 10
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked" })
protected void writePropertyInternal(@Nullable Object obj, SecretDocumentAccessor accessor,
		VaultPersistentProperty prop) {

	if (obj == null) {
		return;
	}

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

	if (valueType.isCollectionLike()) {
		List<Object> collectionInternal = createCollection(asCollection(obj), prop);
		accessor.put(prop, collectionInternal);
		return;
	}

	if (valueType.isMap()) {
		Map<String, Object> mapDbObj = createMap((Map<Object, Object>) obj, prop);
		accessor.put(prop, mapDbObj);
		return;
	}

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

	if (basicTargetType.isPresent()) {

		accessor.put(prop, this.conversionService.convert(obj, basicTargetType.get()));
		return;
	}

	VaultPersistentEntity<?> entity = isSubtype(prop.getType(), obj.getClass())
			? this.mappingContext.getRequiredPersistentEntity(obj.getClass())
			: this.mappingContext.getRequiredPersistentEntity(type);

	SecretDocumentAccessor nested = accessor.writeNested(prop);

	writeInternal(obj, nested, entity);
	addCustomTypeKeyIfNecessary(ClassTypeInformation.from(prop.getRawType()), obj, nested);
}