Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 371 lines (302 sloc) 11.419 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
/**
* Copyright (c) 2010 MongoDB, Inc. <http://mongodb.com>
* Copyright (c) 2009, 2010 Novus Partners, Inc. <http://novus.com>
*
* 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.
*
* For questions and comments about this product, please see the project page at:
*
* http://github.com/mongodb/casbah
*
*/

package com.mongodb.casbah.commons
package conversions
package scala

import com.mongodb.casbah.commons.Imports._
import com.mongodb.casbah.commons.Logging

import org.bson.{BSON, Transformer}

import com.github.nscala_time.time.Imports._

/**
* " Register" Object, calls the registration methods.
*
* By default does not include JodaDateTime as this may be undesired behavior.
* If you want JodaDateTime support, please use the RegisterJodaTimeConversionHelpers Object
*
* @since 1.0
* @see RegisterJodaTimeConversionHelpers
*/
object RegisterConversionHelpers extends Serializers
with Deserializers {
  def apply() {
    log.debug("Registering Scala Conversions.")
    super.register()
  }
}

/**
* "DeRegister" Object, calls the unregistration methods.
*
* @since 1.0
*/
@deprecated("Be VERY careful using this - it will remove ALL of Casbah's loaded BSON Encoding & Decoding hooks at " +
            "runtime. If you need to clear Joda Time use DeregisterJodaTimeConversionHelpers.", "2.0")
object DeregisterConversionHelpers extends Serializers
with Deserializers {
  def apply() {
    log.debug("Deregistering Scala Conversions.")
    // TODO - Figure out how to clear specific hooks as this clobbers everything.
    log.warning("Clobbering Casbah's Registered BSON Type Hooks (EXCEPT Joda Time). Reregister any specific ones you may need.")
    super.unregister()
  }
}

/**
* Converters for reading Scala types from MongoDB
*
* These should be setup in a way which requires
* an explicit invocation / registration of individual
* deserializers, else unexpected behavior will occur.
*
* Because it's likely to be controversial, JodaDateTime is NOT mixed in by default.
*
* @since 1.0
*/
trait Deserializers extends MongoConversionHelper {
  override def register() {
    log.debug("Deserializers for Scala Conversions registering")
    super.register()
  }

  override def unregister() {
    super.unregister()
  }
}

/**
* Converters for saving Scala types to MongoDB
*
* For the most part these are 'safe' to enable automatically,
* as they won't break existing code.
* Be very careful with the deserializers however as they can come with
* unexpected behavior.
*
* Because it's likely to be controversial, JodaDateTime is NOT mixed in by default.
*
* @since 1.0
*/
trait Serializers extends MongoConversionHelper
with ScalaRegexSerializer
with ScalaCollectionSerializer
with ScalaProductSerializer
with OptionSerializer {
  override def register() {
    log.debug("Serializers for Scala Conversions registering")
    super.register()
  }

  override def unregister() {
    super.unregister()
  }
}

trait OptionSerializer extends MongoConversionHelper {
  private val transformer = new Transformer {
    log.trace("Encoding a Scala Option[].")

    // scalastyle:off null
    def transform(o: AnyRef): AnyRef = o match {
      case Some(x) => x.asInstanceOf[AnyRef]
      case None => null
      case _ => o
    }
    // scalastyle:on null

  }

  override def register() {
    log.debug("Setting up OptionSerializer")

    BSON.addEncodingHook(classOf[_root_.scala.Option[_]], transformer)

    super.register()
  }
}

object RegisterJodaTimeConversionHelpers extends JodaDateTimeHelpers {
  def apply() {
    log.debug("Registering Joda Time Scala Conversions.")
    super.register()
  }
}

object DeregisterJodaTimeConversionHelpers extends JodaDateTimeHelpers {
  def apply() {
    log.debug("Unregistering Joda Time Scala Conversions.")
    super.unregister()
  }
}

trait JodaDateTimeHelpers extends JodaDateTimeSerializer with JodaDateTimeDeserializer

trait JodaDateTimeSerializer extends MongoConversionHelper {

  private val encodeTypeDateTime = classOf[DateTime]
  private val encodeTypeLocalDateTime = classOf[LocalDateTime]

  /** Encoding hook for MongoDB To be able to persist JodaDateTime DateTime to MongoDB */
  private val transformer = new Transformer {
    log.trace("Encoding a JodaDateTime DateTime.")

    def transform(o: AnyRef): AnyRef = o match {
      case d: DateTime => d.toDate // Return a JDK Date object which BSON can encode
      case l: LocalDateTime => l.toDateTime.toDate
      case _ => o
    }

  }

  override def register() {
    log.debug("Hooking up Joda DateTime serializer.")

    /** Encoding hook for MongoDB To be able to persist JodaDateTime DateTime to MongoDB */
    BSON.addEncodingHook(encodeTypeDateTime, transformer)
    BSON.addEncodingHook(encodeTypeLocalDateTime, transformer)
    super.register()
  }

  override def unregister() {
    log.debug("De-registering Joda DateTime serializer.")
    BSON.removeEncodingHooks(encodeTypeDateTime)
    BSON.removeEncodingHooks(encodeTypeLocalDateTime)
    super.unregister()
  }
}

trait JodaDateTimeDeserializer extends MongoConversionHelper {

  private val encodeType = classOf[java.util.Date]
  private val transformer = new Transformer {
    log.trace("Decoding JDK Dates .")

    def transform(o: AnyRef): AnyRef = o match {
      case jdkDate: java.util.Date => new DateTime(jdkDate)
      case d: DateTime => d
      case l: LocalDateTime => l.toDateTime
      case _ => o
    }
  }

  override def register() {
    log.debug("Hooking up Joda DateTime deserializer")

    /** Encoding hook for MongoDB To be able to read JodaDateTime DateTime from MongoDB's BSON Date */
    BSON.addDecodingHook(encodeType, transformer)
    super.register()
  }

  override def unregister() {
    log.debug("De-registering Joda DateTime deserializer.")
    BSON.removeDecodingHooks(encodeType)
    super.unregister()
  }
}


trait JodaLocalDateTimeHelpers extends JodaLocalDateTimeSerializer with JodaLocalDateTimeDeserializer

trait JodaLocalDateTimeSerializer extends MongoConversionHelper {

  private val encodeTypeDateTime = classOf[DateTime]
  private val encodeTypeLocalDateTime = classOf[LocalDateTime]

  /** Encoding hook for MongoDB To be able to persist JodaDateTime DateTime to MongoDB */
  private val transformer = new Transformer {
    log.trace("Encoding a JodaLocalDateTime DateTime.")

    def transform(o: AnyRef): AnyRef = o match {
      case d: DateTime => d.toLocalDateTime.toDate // Return a JDK Date object which BSON can encode
      case l: LocalDateTime => l.toDate
      case _ => o
    }

  }

  override def register() {
    log.debug("Hooking up Joda LocalDateTime serializer.")

    /** Encoding hook for MongoDB To be able to persist JodaLocalDateTime DateTime to MongoDB */
    BSON.addEncodingHook(encodeTypeDateTime, transformer)
    BSON.addEncodingHook(encodeTypeLocalDateTime, transformer)
    super.register()
  }

  override def unregister() {
    log.debug("De-registering Joda LocalDateTime serializer.")
    BSON.removeEncodingHooks(encodeTypeDateTime)
    BSON.removeEncodingHooks(encodeTypeLocalDateTime)
    super.unregister()
  }
}

trait JodaLocalDateTimeDeserializer extends MongoConversionHelper {

  private val encodeType = classOf[java.util.Date]
  private val transformer = new Transformer {
    log.trace("Decoding JDK Dates .")

    def transform(o: AnyRef): AnyRef = o match {
      case jdkDate: java.util.Date => new LocalDateTime(jdkDate)
      case d: DateTime => d.toLocalDateTime
      case l: LocalDateTime => l
      case _ => o
    }
  }

  override def register() {
    log.debug("Hooking up Joda DateTime deserializer")

    /** Encoding hook for MongoDB To be able to read JodaDateTime DateTime from MongoDB's BSON Date */
    BSON.addDecodingHook(encodeType, transformer)
    super.register()
  }

  override def unregister() {
    log.debug("De-registering Joda DateTime deserializer.")
    BSON.removeDecodingHooks(encodeType)
    super.unregister()
  }
}

trait ScalaRegexSerializer extends MongoConversionHelper {
  private val transformer = new Transformer {
    log.trace("Encoding a Scala RegEx.")

    def transform(o: AnyRef): AnyRef = o match {
      case sRE: _root_.scala.util.matching.Regex => sRE.pattern
      case _ => o
    }

  }

  override def register() {
    log.debug("Setting up ScalaRegexSerializers")

    log.debug("Hooking up scala.util.matching.Regex serializer")

    /** Encoding hook for MongoDB to translate a Scala Regex to a JAva Regex (which Mongo will understand) */
    BSON.addEncodingHook(classOf[_root_.scala.util.matching.Regex], transformer)

    super.register()
  }
}

object RegisterJodaLocalDateTimeConversionHelpers extends JodaLocalDateTimeHelpers {
  def apply() {
    log.debug("Registering Joda Time Scala Conversions.")
    super.register()
  }
}

object DeregisterJodaLocalDateTimeConversionHelpers extends JodaLocalDateTimeHelpers {
  def apply() {
    log.debug("Unregistering Joda Time Scala Conversions.")
    super.unregister()
  }
}

/**
* Implementation which is aware of the possible conversions in scalaj-collection and attempts to Leverage it...
* Not all of these may be serializable by Mongo However... this is a first pass attempt at moving them to Java types
*/
trait ScalaCollectionSerializer extends MongoConversionHelper {

  private val transformer = new Transformer {

    import _root_.scala.collection.JavaConverters._

    def transform(o: AnyRef): AnyRef = o match {
      case mdbo: MongoDBObject => mdbo.underlying // MongoDBObject is a custom Iterable
      case mdbl: MongoDBList => mdbl.underlying // MongoDBList is a custom Iterable
      case m: _root_.scala.collection.mutable.Map[_, _] => m.asJava // Maps are Iterable but we need to keep them Map like
      case m: _root_.scala.collection.Map[_, _] => m.asJava // Maps are Iterable but we need to keep them Map like
      case i: _root_.scala.collection.Iterable[_] => i.asJava
      case i: _root_.scala.collection.Iterator[_] => i.asJava
      case _ => o
    }
  }

  override def register() {
    log.debug("Setting up ScalaCollectionSerializer")
    BSON.addEncodingHook(classOf[_root_.scala.collection.Iterator[_]], transformer)
    BSON.addEncodingHook(classOf[_root_.scala.collection.Iterable[_]], transformer)
    super.register()
  }
}


trait ScalaProductSerializer extends MongoConversionHelper {

  import _root_.scala.collection.JavaConverters._

  private val transformer = new Transformer {
    log.trace("Encoding a Scala Product.")

    def transform(o: AnyRef): AnyRef = o match {
      case l: java.util.List[_] => l // Ignore converted Products that are wrapped java.util.Lists
      case p: Product => p.productIterator.toList.asJava
      case _ => o
    }
  }

  override def register() {
    log.debug("Setting up ScalaProductSerializer")
    BSON.addEncodingHook(classOf[Product], transformer)
    super.register()
  }
}
Something went wrong with that request. Please try again.