Frage

I'm trying to achieve the following: basically just coping a mongodb document and add a timestamp field to it in order to reconstruct the order in which the document was altered and in case if needed to restore these entries.

My approach is the following:

@Salat
trait Version[A <: DBEntity] extends ModelCompanion[A, ObjectId] {

  def timeStamp: Long = System.currentTimeMillis()

  /**
   * this method overrides the default salat dao save method in order to make a     copy    for versioning of Objects
   */
  override def save(entity: A) =
    {
      if (entity.id != null) {
        // let salat deserialze the case class into a DBObject
        val dbo = dao._grater.asDBObject(entity)
        //convert the Object into a Map and append our timestamp
        val builder = BasicDBObjectBuilder.start(dbo.toMap()).add("timeStamp", timeStamp)
        val copyCollection = MongoDBLayer.mongoDB("history")
        //and save it in the historic collection
        copyCollection.insert(builder.get())
      }
      //delegate to the superclass to perform the actual save process
      val wr = dao.save(entity)
      wr

    }
}

Is there a more elegant /convienent way to do it?

Or how would your approach be?

Thanks in advance,

Stefan

War es hilfreich?

Lösung

See SalatDAO#decorateDBO - this method is called before every insert/save/update. This could be a logical place to park the code that adds a timestamp to your DBO and saves a copy in your history collection. Just override it and call super.decorateDBO at the beginning. Then proceed to add your timestamp and do whatever else you need to do.

   /** A central place to modify DBOs before inserting, saving, or updating.
    *  @param toPersist object to be serialized
    *  @return decorated DBO for persisting
    */
   def decorateDBO(toPersist: ObjectType) = {
     val dbo = _grater.asDBObject(toPersist)
     if (forceTypeHints) {
       // take advantage of the mutability of DBObject by cramming in a type hint
       dbo(ctx.typeHintStrategy.typeHint) = ctx.typeHintStrategy.encode(toPersist.getClass.getName).asInstanceOf[AnyRef]
     }
     dbo
   }

(Also DBOs are mutable so there is no need to make call toMap. You can just directly assign the timestamp to dbo("timestamp"))

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top