private PersistentEntity getOrCreateAssociatedEntity(MappingContext context, Class propType) {
   PersistentEntity associatedEntity = context.getPersistentEntity(propType.getName());
   if (associatedEntity == null) {
     associatedEntity = context.addPersistentEntity(propType);
   }
   return associatedEntity;
 }
 private PersistentEntity getPersistentEntity(
     Class javaClass, MappingContext context, ClassMapping classMapping) {
   PersistentEntity entity;
   if (classMapping != null) {
     entity = classMapping.getEntity();
   } else {
     entity = context.getPersistentEntity(javaClass.getName());
   }
   return entity;
 }
 private static Object convertPrimitiveToNative(Object item, MappingContext mappingContext) {
   Object nativeValue;
   if (item != null) {
     ConversionService conversionService = mappingContext.getConversionService();
     // go for toInteger or toString.
     if (conversionService.canConvert(item.getClass(), Integer.class)) {
       nativeValue = conversionService.convert(item, Integer.class);
     } else if (conversionService.canConvert(item.getClass(), String.class)) {
       nativeValue = conversionService.convert(item, String.class);
     } else {
       // fall back if no explicit converter is registered, good for URL, Locale, etc.
       nativeValue = item.toString();
     }
   } else {
     nativeValue = null;
   }
   return nativeValue;
 }
  /**
   * Convert a value into a type suitable for use in Mongo. Collections and maps are converted
   * recursively. The mapping context is used for the conversion if possible, otherwise toString()
   * is the eventual fallback.
   *
   * @param value The value to convert (or null)
   * @param mappingContext The mapping context.
   * @return The converted value (or null)
   */
  public static Object getSimpleNativePropertyValue(Object value, MappingContext mappingContext) {
    Object nativeValue;

    if (value == null || mappingContext.isPersistentEntity(value)) {
      nativeValue = null;
    } else if (MongoMappingContext.isMongoNativeType(value.getClass())) {
      // easy case, no conversion required.
      // Checked first in case any of these types (such as BasicDBObject) are instances of
      // collections
      // or arrays, etc.!
      nativeValue = value;
    } else if (value.getClass().isArray()) {
      Object[] array = (Object[]) value;
      List<Object> nativeColl = new ArrayList<Object>(array.length);
      for (Object item : array) {
        nativeColl.add(getSimpleNativePropertyValue(item, mappingContext));
      }
      nativeValue = nativeColl;
    } else if (value instanceof Collection) {
      Collection existingColl = (Collection) value;
      List<Object> nativeColl = new ArrayList<Object>(existingColl.size());
      for (Object item : existingColl) {
        nativeColl.add(getSimpleNativePropertyValue(item, mappingContext));
      }
      nativeValue = nativeColl;
    } else if (value instanceof Map) {
      Map<String, Object> existingMap = (Map) value;
      Map<String, Object> newMap = new LinkedHashMap<String, Object>();
      for (Map.Entry<String, Object> entry : existingMap.entrySet()) {
        newMap.put(entry.getKey(), getSimpleNativePropertyValue(entry.getValue(), mappingContext));
      }
      nativeValue = newMap;
    } else {
      nativeValue = convertPrimitiveToNative(value, mappingContext);
    }
    return nativeValue;
  }
 public DocumentCollectionMapping(PersistentEntity entity, MappingContext context) {
   super(entity, context);
   this.mappedForm =
       (Collection)
           context.getMappingFactory().createMappedForm(DocumentEmbeddedPersistentEntity.this);
 }