public static Optional<EntityMap> fromEntity(Optional<Entity> entity) { if(entity.isPresent()){ EntityMap map = fromEntity(entity.get()); return Optional.fromNullable(map); }else{ return Optional.absent(); } }
/** * hacky impl, for outbound implementations longitude needs to be \ "longitude" and not "lon" * @param entityObject * @return */ public EntityMap toMap( EntityObject entityObject ) { EntityMap map = new EntityMap(); return toMap(entityObject, map); }
/** * Return true if the value is a location field which contains valid values for latitude and longitude * @param fieldValue * @return */ public static boolean isLocationField(Map<String, ?> fieldValue) { //short circuit since valid location objects contain only 2 properties (latitude and longitude) if (fieldValue.size() != 2) { return false; } // we need to make sure that latitude and longitude are numbers or strings that can be parsed as a number if (fieldValue.containsKey(EntityToMapConverter.LAT) && fieldValue.containsKey(EntityToMapConverter.LON)){ for(Map.Entry<String,?> value : fieldValue.entrySet()){ if(!(value.getValue() instanceof Number) && !isDouble(String.valueOf(value.getValue()))){ if(logger.isDebugEnabled()){ logger.debug("Field [{}] with value [{}] is not a valid geo coordinate", value.getKey(), value.getValue() ); } return false; } } return true; } return false; }
if( field instanceof DistanceField){ if(!entityMap.containsKey("metadata"))entityMap.put("metadata",new HashMap<String,Object>()); DistanceField distanceField = (DistanceField) field; Map<String,Object> metaMap = (Map) entityMap.get("metadata"); metaMap.put(DistanceField.NAME, distanceField.getValue()); }else if ( field instanceof ListField || field instanceof ArrayField || field instanceof SetField) { Collection list = ( Collection ) field.getValue(); entityMap.put( field.getName(), processCollection( list ) ); entityMap.put( field.getName(), toMap( eo ) ); // recursion entityMap.put( field.getName(), locMap ); , e ); entityMap.put( bf.getName(), o ); }else if (field instanceof SerializedObjectField) { SerializedObjectField bf = (SerializedObjectField) field; throw new RuntimeException("Can't deserialize object " + serilizedObj, e); entityMap.put(bf.getName(), o); } else { entityMap.put(field.getName(), field.getValue());
/** * visit a value */ private void visitValue( final Object value ) { if ( value instanceof Map ) { //if it's a location, then create a location field. if ( EntityMap.isLocationField( (Map)value ) ) { Map<String,Object> map = ( Map ) value; Map<String,Object> location = new HashMap<>(2); //normalize location field to use lat/lon for es location.put("lat",map.get("latitude")); location.put("lon",map.get("longitude")); fields.add( EntityField.create( fieldStack.peek(), location) ); return; } iterate( ( Map<String, ?> ) value ); } //TODO figure out our nested array structure else if ( value instanceof Collection) { iterate( ( Collection ) value ); } else { visitPrimitive( value ); } }
@Override public MutationBatch write( final ApplicationScope applicationScope, final MvccEntity entity ) { Preconditions.checkNotNull( applicationScope, "applicationScope is required" ); Preconditions.checkNotNull( entity, "entity is required" ); final Id entityId = entity.getId(); final UUID version = entity.getVersion(); Optional<EntityMap> map = EntityMap.fromEntity(entity.getEntity()); ByteBuffer byteBuffer = entitySerializer.toByteBuffer( new EntityWrapper(entityId,entity.getVersion(), entity.getStatus(), map.isPresent() ? map.get() : null, 0 ) ); entity.setSize(byteBuffer.array().length); return doWrite( applicationScope, entityId, version, colMutation -> colMutation.putColumn( COL_VALUE, byteBuffer ) ); }
final EntityMap entityMap = EntityMap.fromEntity( entity );