You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by to...@apache.org on 2015/09/24 22:13:03 UTC
[07/39] usergrid git commit: remove location if its not a map
remove location if its not a map
Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/56801735
Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/56801735
Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/56801735
Branch: refs/heads/USERGRID-909
Commit: 56801735c9af438b090df700c17b8ca8c22f6be9
Parents: 58f7402
Author: Shawn Feldman <sf...@apache.org>
Authored: Fri Sep 11 17:44:57 2015 -0600
Committer: Shawn Feldman <sf...@apache.org>
Committed: Fri Sep 11 17:44:57 2015 -0600
----------------------------------------------------------------------
.../corepersistence/CpEntityMapUtilsTest.java | 5 ++-
stack/corepersistence/model/pom.xml | 6 ++-
.../model/entity/MapToEntityConverter.java | 39 +++++++++++++++-----
3 files changed, 37 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/usergrid/blob/56801735/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java b/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java
index 6e4e9a4..325e51f 100644
--- a/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java
+++ b/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java
@@ -38,6 +38,7 @@ import java.io.IOException;
import java.util.*;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -132,14 +133,14 @@ public class CpEntityMapUtilsTest {
cpEntity = CpEntityMapUtils.fromMap(properties, "loc", true);
- assertTrue(cpEntity.getFieldMap().get("location") instanceof EntityObjectField);
+ assertNull(cpEntity.getFieldMap().get("location"));
properties = new LinkedHashMap<String, Object>();
properties.put("location", "denver");
cpEntity = CpEntityMapUtils.fromMap(properties, "loc", true);
- assertTrue(cpEntity.getFieldMap().get("location") instanceof StringField);
+ assertNull(cpEntity.getFieldMap().get("location") );
}
http://git-wip-us.apache.org/repos/asf/usergrid/blob/56801735/stack/corepersistence/model/pom.xml
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/pom.xml b/stack/corepersistence/model/pom.xml
index 142bca1..591935f 100644
--- a/stack/corepersistence/model/pom.xml
+++ b/stack/corepersistence/model/pom.xml
@@ -40,7 +40,11 @@
<artifactId>commons-lang3</artifactId>
<version>${commons.lang.version}</version>
</dependency>
-
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>${slf4j.version}</version>
+ </dependency>
<!-- the core, which includes Streaming API, shared low-level abstractions (but NOT data-binding) -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
http://git-wip-us.apache.org/repos/asf/usergrid/blob/56801735/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java
index e6744c4..dbd1b2a 100644
--- a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java
+++ b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java
@@ -25,15 +25,22 @@ import com.fasterxml.jackson.datatype.guava.GuavaModule;
import org.apache.usergrid.persistence.model.collection.SchemaManager;
import org.apache.usergrid.persistence.model.field.*;
import org.apache.usergrid.persistence.model.field.value.Location;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.util.*;
public class MapToEntityConverter{
+ public static final Logger logger = LoggerFactory.getLogger(MapToEntityConverter.class);
private final JsonFactory jsonFactory = new JsonFactory();
private final ObjectMapper objectMapper = new ObjectMapper(jsonFactory).registerModule(new GuavaModule());
+ static final String locationKey = "location";
+ static final String lat = "latitude";
+ static final String lon = "longitude";
+
public Entity fromMap( Map<String, Object> map, boolean topLevel ) {
Entity entity = new Entity();
@@ -48,9 +55,15 @@ public class MapToEntityConverter{
}
Object value = map.get( fieldName );
boolean unique = schemaManager == null ? topLevel : topLevel && schemaManager.isPropertyUnique(entityType, fieldName);
+ //cannot store fields that aren't locations
+ if(fieldName.toLowerCase().equals(locationKey) && !(value instanceof Map)) {
+ logger.warn("can't map entity with location field");
+ continue;
+ }
if ( value instanceof String ) {
- entity.setField( new StringField( fieldName, (String)value, unique ));
+ String stringValue =(String)value;
+ entity.setField(new StringField(fieldName, stringValue, unique));
} else if ( value instanceof Boolean ) {
entity.setField( new BooleanField( fieldName, (Boolean)value, unique ));
@@ -190,26 +203,28 @@ public class MapToEntityConverter{
// Expect at least two fields in a Location object and must have lat lon
if (m.size() >= 2 && (
- (m.containsKey("latitude") && m.containsKey("longitude") )
+ (m.containsKey(lat) && m.containsKey(lon) )
|| (m.containsKey("lat") && m.containsKey("lon") )
)) {
- Double lat, lon;
+ Double latVal, lonVal;
// check the properties to make sure they are set and are doubles
- if (m.containsKey("latitude") && m.containsKey("longitude")) {
+ if (m.containsKey(lat) && m.containsKey(lon)) {
try {
- lat = Double.parseDouble(m.get("latitude").toString());
- lon = Double.parseDouble(m.get("longitude").toString());
+ latVal = Double.parseDouble(m.get(lat).toString());
+ lonVal = Double.parseDouble(m.get(lon).toString());
} catch (NumberFormatException ignored) {
throw new IllegalArgumentException(
"Latitude and longitude must be doubles (e.g. 32.1234).");
}
} else if (m.containsKey("lat") && m.containsKey("lon")) {
+ logger.warn("Entity contains latitude and longitude in old format location{lat,long}"
+ );
try {
- lat = Double.parseDouble(m.get("lat").toString());
- lon = Double.parseDouble(m.get("lon").toString());
+ latVal = Double.parseDouble(m.get("lat").toString());
+ lonVal = Double.parseDouble(m.get("lon").toString());
} catch (NumberFormatException ignored) {
throw new IllegalArgumentException(""
+ "Latitude and longitude must be doubles (e.g. 32.1234).");
@@ -219,9 +234,13 @@ public class MapToEntityConverter{
+ "latitude and longitude, or lat and lon");
}
- entity.setField(new LocationField(fieldName, new Location(lat, lon)));
+ entity.setField(new LocationField(fieldName, new Location(latVal, lonVal)));
} else {
- entity.setField(new EntityObjectField(fieldName,fromMap( value, false))); // recursion
+ //can't process non enties
+ logger.warn(
+ "entity cannot process location values that don't have valid location{latitude,longitude} values"
+ );
+ // entity.setField(new EntityObjectField(fieldName,fromMap( value, false))); // recursion
}
}
}