Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013-2014 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.data.mongodb.core.convert;
 
 
 import  org.springframework.core.convert.converter.Converter;
 import  org.springframework.util.Assert;
 
A subclass of QueryMapper that retains type information on the mongo types.

Author(s):
Thomas Darimont
Oliver Gierke
Christoph Strobl
 
 public class UpdateMapper extends QueryMapper {
 
 	private final MongoConverter converter;

Creates a new UpdateMapper using the given MongoConverter.

Parameters:
converter must not be null.
 
 	public UpdateMapper(MongoConverter converter) {
 
 		super(converter);
 		this. = converter;
 	}

Converts the given source object to a mongo type retaining the original type information of the source type on the mongo type.

See also:
org.springframework.data.mongodb.core.convert.QueryMapper.delegateConvertToMongoType(java.lang.Object, org.springframework.data.mongodb.core.mapping.MongoPersistentEntity)
 
 	protected Object delegateConvertToMongoType(Object sourceMongoPersistentEntity<?> entity) {
 		return entity == null ? super.delegateConvertToMongoType(sourcenull) : .convertToMongoType(source,
 				entity.getTypeInformation());
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * @see org.springframework.data.mongodb.core.convert.QueryMapper#getMappedObjectForField(org.springframework.data.mongodb.core.convert.QueryMapper.Field, java.lang.Object)
 	 */
 	protected Entry<StringObjectgetMappedObjectForField(Field fieldObject rawValue) {
 
 		if (!isUpdateModifier(rawValue)) {
 			return super.getMappedObjectForField(fieldgetMappedValue(fieldrawValue));
 		}
 
 		Object value = null;
 
 		if (rawValue instanceof Modifier) {
 
 			value = getMappedValue((ModifierrawValue);
 
 		} else if (rawValue instanceof Modifiers) {
 
 			DBObject modificationOperations = new BasicDBObject();
 
 			for (Modifier modifier : ((ModifiersrawValue).getModifiers()) {
 				modificationOperations.putAll(getMappedValue(modifier).toMap());
 			}
 
 			value = modificationOperations;
 		} else {
 
			throw new IllegalArgumentException(String.format("Unable to map value of type '%s'!"rawValue.getClass()));
		}
		return Collections.singletonMap(field.getMappedKey(), value).entrySet().iterator().next();
	}
	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.convert.QueryMapper#isAssociationConversionNecessary(org.springframework.data.mongodb.core.convert.QueryMapper.Field, java.lang.Object)
	 */
	protected boolean isAssociationConversionNecessary(Field documentFieldObject value) {
		return super.isAssociationConversionNecessary(documentFieldvalue) || documentField.containsAssociation();
	}
	private boolean isUpdateModifier(Object value) {
		return value instanceof Modifier || value instanceof Modifiers;
	}
	private DBObject getMappedValue(Modifier modifier) {
		return new BasicDBObject(modifier.getKey(), value);
	}
	/* 
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.convert.QueryMapper#createPropertyField(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity, java.lang.String, org.springframework.data.mapping.context.MappingContext)
	 */
			MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentPropertymappingContext) {
		return entity == null ? super.createPropertyField(entitykeymappingContext) : //
				new MetadataBackedUpdateField(entitykeymappingContext);
	}

MetadataBackedField that handles $ paths inside a field key. We clean up an update key containing a $ before handing it to the super class to make sure property lookups and transformations continue to work as expected. We provide a custom property converter to re-applied the cleaned up $s when constructing the mapped key.

Author(s):
Thomas Darimont
Oliver Gierke
	private static class MetadataBackedUpdateField extends MetadataBackedField {
		private final String key;

Creates a new MetadataBackedField with the given MongoPersistentEntity, key and MappingContext. We clean up the key before handing it up to the super class to make sure it continues to work as expected.

Parameters:
entity must not be null.
key must not be null or empty.
mappingContext must not be null.
				MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentPropertymappingContext) {
			super(key.replaceAll("\\.\\$"""), entitymappingContext);
			this. = key;
		}
		/* 
		 * (non-Javadoc)
		 * @see org.springframework.data.mongodb.core.convert.QueryMapper.MetadataBackedField#getPropertyConverter()
		 */
		}

Converter to skip all properties after an association property was rendered.

Author(s):
Oliver Gierke
		private static class AssociationConverter implements Converter<MongoPersistentPropertyString> {
			private boolean associationFound;

Creates a new AssociationConverter for the given Association.

Parameters:
association must not be null.
				Assert.notNull(association"Association must not be null!");
				this. = association.getInverse();
			}
			/* 
			 * (non-Javadoc)
			 * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
			 */
					return null;
				}
				if (.equals(source)) {
				}
				return source.getFieldName();
			}
		}

Special Converter for MongoPersistentProperty instances that will concatenate the $ contained in the source update key.

Author(s):
Oliver Gierke
		private static class UpdatePropertyConverter implements Converter<MongoPersistentPropertyString> {
			private final Iterator<Stringiterator;

Creates a new UpdatePropertyConverter with the given update key.

Parameters:
updateKey must not be null or empty.
			public UpdatePropertyConverter(String updateKey) {
				Assert.hasText(updateKey"Update key must not be null or empty!");
				this. = Arrays.asList(updateKey.split("\\.")).iterator();
				this..next();
			}
			/* 
			 * (non-Javadoc)
			 * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
			 */
			public String convert(MongoPersistentProperty property) {
				return .hasNext() && .next().equals("$") ? String.format("%s.$"mappedName) : mappedName;
			}
		}
	}
New to GrepCode? Check out our FAQ X