|
15 | 15 | */
|
16 | 16 | package org.springframework.data.mongodb.core.convert;
|
17 | 17 |
|
| 18 | +import java.math.BigDecimal; |
| 19 | +import java.math.BigInteger; |
18 | 20 | import java.util.Arrays;
|
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.HashSet; |
19 | 23 | import java.util.Iterator;
|
| 24 | +import java.util.Map; |
20 | 25 | import java.util.Map.Entry;
|
| 26 | +import java.util.Set; |
21 | 27 |
|
22 | 28 | import org.springframework.core.convert.converter.Converter;
|
| 29 | +import org.springframework.dao.InvalidDataAccessApiUsageException; |
23 | 30 | import org.springframework.data.mapping.Association;
|
24 | 31 | import org.springframework.data.mapping.context.MappingContext;
|
25 | 32 | import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
|
43 | 50 | */
|
44 | 51 | public class UpdateMapper extends QueryMapper {
|
45 | 52 |
|
| 53 | + @SuppressWarnings({ "rawtypes", "serial" })// |
| 54 | + private static final Map<String, Set<Class>> RESTRICTED_TYPES_PER_KEYWORD = new HashMap<String, Set<Class>>() { |
| 55 | + { |
| 56 | + put("$min", new HashSet<Class>(Arrays.<Class> asList(BigDecimal.class, BigInteger.class))); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
46 | 60 | private final MongoConverter converter;
|
47 | 61 |
|
48 | 62 | /**
|
@@ -115,6 +129,34 @@ private Entry<String, Object> getMappedUpdateModifier(Field field, Object rawVal
|
115 | 129 | return createMapEntry(field, value);
|
116 | 130 | }
|
117 | 131 |
|
| 132 | + /* |
| 133 | + * (non-Javadoc) |
| 134 | + * @see org.springframework.data.mongodb.core.convert.QueryMapper#getMappedKeyword(org.springframework.data.mongodb.core.convert.QueryMapper.Keyword, org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) |
| 135 | + */ |
| 136 | + protected DBObject getMappedKeyword(Keyword keyword, MongoPersistentEntity<?> entity) { |
| 137 | + |
| 138 | + assertKeywordCanBeAppliedOnProperty(keyword, entity); |
| 139 | + return super.getMappedKeyword(keyword, entity); |
| 140 | + } |
| 141 | + |
| 142 | + private void assertKeywordCanBeAppliedOnProperty(Keyword keyword, MongoPersistentEntity<?> entity) { |
| 143 | + |
| 144 | + if (RESTRICTED_TYPES_PER_KEYWORD.containsKey(keyword.getKey()) && keyword.getValue() instanceof DBObject) { |
| 145 | + |
| 146 | + for (String key : ((DBObject) keyword.getValue()).keySet()) { |
| 147 | + |
| 148 | + Field field = createPropertyField(entity, key, converter.getMappingContext()); |
| 149 | + |
| 150 | + if (field != null && field.getProperty() != null) { |
| 151 | + if (RESTRICTED_TYPES_PER_KEYWORD.get(keyword.getKey()).contains(field.getProperty().getActualType())) { |
| 152 | + throw new InvalidDataAccessApiUsageException(String.format("%s is not supported for %s", field |
| 153 | + .getProperty().getActualType(), keyword.getKey())); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
118 | 160 | /*
|
119 | 161 | * (non-Javadoc)
|
120 | 162 | * @see org.springframework.data.mongodb.core.convert.QueryMapper#isAssociationConversionNecessary(org.springframework.data.mongodb.core.convert.QueryMapper.Field, java.lang.Object)
|
|
0 commit comments