2013-09-16 2 views
0

다음 작업을 수행하려고합니다. 기본적으로 mongodb 문서를 복사하고 문서에 변경된 순서를 재구성하기 위해 timestamp 필드를 추가하기 만하면됩니다. 이 항목들.Salat 버전 객체에 대한 피드백

@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 

    } 
} 

이 그것을 할 수있는 더 우아한/convienent 방법이 있나요 :

내 접근 방식은 다음과 같다?

또는 귀하의 접근 방식은 어떻습니까? 사전에

감사합니다,

스테판

답변

0

SalatDAO#decorateDBO 참조 -이 방법은/업데이트를 저장/모든 삽입하기 전에 호출된다. DBO에 타임 스탬프를 추가하고 기록 컬렉션에 복사본을 저장하는 코드를 저장하는 논리적 인 장소 일 수 있습니다. 그것을 무시하고 처음에는 super.decorateDBO를 호출하십시오. 그런 다음 타임 스탬프를 추가하고 다른 작업을 수행하십시오.

/** 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 
    } 

(또한 DBOs은 변경할 수 있습니다 그래서 전화 toMap을 할 필요가 없다. 그냥 직접 dbo("timestamp")에 타임 스탬프를 할당 할 수 있습니다)