From 8110ef112c73abccd16cb6157fae6bb4298bc780 Mon Sep 17 00:00:00 2001 From: Tim Nelson Date: Mon, 26 Apr 2010 13:34:50 -0500 Subject: [PATCH] Updated for use with mongo-java-driver 1.4 and lift 2.0-M4 --- README.textile | 24 ++++++++++++++++ project/build.properties | 4 +-- project/build/ScamongoProject.scala | 10 +++---- .../eltimn/scamongo/field/DBRefField.scala | 13 ++++++--- .../eltimn/scamongo/field/JObjectField.scala | 14 ++++++---- .../scamongo/field/MongoListField.scala | 12 +++++--- .../eltimn/scamongo/field/MongoMapField.scala | 19 ++++++++----- .../eltimn/scamongo/field/ObjectIdField.scala | 20 +++++++++---- .../com/eltimn/scamongo/DirectExamples.scala | 10 +++---- .../eltimn/scamongo/DocumentExamples.scala | 6 ++-- .../com/eltimn/scamongo/RecordExamples.scala | 28 +++++++++++++------ 11 files changed, 112 insertions(+), 48 deletions(-) diff --git a/README.textile b/README.textile index 0d2fd8c..d6c44db 100644 --- a/README.textile +++ b/README.textile @@ -1,3 +1,10 @@ +README: Scamongo is being moved to the Lift project. In doing so I'm comtemplating +what to do with the scamongo-document part of this library. I'm trying to decide if I +should move that with it, discontinue support for it, or keep it as a separate project. +Please contact me if you are using this at all and let me know what you think. + +Tim + See wiki for project info. To build you will need to use sbt @@ -7,9 +14,26 @@ Jar is available at: http://dl.dropbox.com/u/1154284/scamongo-0.1.jar http://dl.dropbox.com/u/1154284/scamongo-0.1.1.jar http://dl.dropbox.com/u/1154284/scamongo-0.1.2.jar +http://dl.dropbox.com/u/1154284/scamongo-0.1.3.jar h2. Release Notes +*version-0.1.3* +* Updated for use with mongo-java-driver 1.4 and lift 2.0-M4 +NOTE: There is a small breaking change in this version for scamongo-record users; +record companion objects must now include a createRecord method. Here's an example: + +class TestRecord extends MongoRecord[TestRecord] with MongoId[TestRecord] { + + def meta = TestRecord + + object stringfield extends StringField(this, 32) +} + +object TestRecord extends TestRecord with MongoMetaRecord[TestRecord] { + def createRecord = new TestRecord +} + *version-0.1.2* * Added harryh's changes to DBRef diff --git a/project/build.properties b/project/build.properties index d156a51..bb88d52 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1,8 +1,8 @@ #Project properties -#Tue Mar 30 11:20:07 CDT 2010 +#Mon Apr 26 13:31:31 CDT 2010 project.organization=com.eltimn project.name=scamongo sbt.version=0.5.6 -project.version=0.1.3 +project.version=0.1.4 scala.version=2.7.7 project.initialize=false diff --git a/project/build/ScamongoProject.scala b/project/build/ScamongoProject.scala index 08ed514..6ac7e3d 100644 --- a/project/build/ScamongoProject.scala +++ b/project/build/ScamongoProject.scala @@ -4,13 +4,13 @@ class ScamongoProject(info: ProjectInfo) extends DefaultProject(info) { //override def compileOptions = super.compileOptions ++ Seq(Unchecked) - val mongo = "org.mongodb" % "mongo-java-driver" % "1.3" % "compile->default" + val mongo = "org.mongodb" % "mongo-java-driver" % "1.4" % "compile->default" - val liftjson = "net.liftweb" % "lift-json" % "2.0-M3" % "compile->default" - val liftrecord = "net.liftweb" % "lift-record" % "2.0-M3" % "compile->default" + val liftjson = "net.liftweb" % "lift-json" % "2.0-M4" % "compile->default" + val liftrecord = "net.liftweb" % "lift-record" % "2.0-M4" % "compile->default" - val junit = "junit" % "junit" % "4.5" - val specs = "org.scala-tools.testing" % "specs" % "1.5.0" + val junit = "junit" % "junit" % "4.7" + val specs = "org.scala-tools.testing" % "specs" % "1.6.1" // other repositories //val scalaToolsSnapshots = ScalaToolsSnapshots diff --git a/src/main/scala/com/eltimn/scamongo/field/DBRefField.scala b/src/main/scala/com/eltimn/scamongo/field/DBRefField.scala index 99a7e1c..faf64b5 100644 --- a/src/main/scala/com/eltimn/scamongo/field/DBRefField.scala +++ b/src/main/scala/com/eltimn/scamongo/field/DBRefField.scala @@ -16,9 +16,10 @@ package com.eltimn.scamongo.field * and limitations under the License. */ -import net.liftweb.common.{Box, Empty, Failure, Full} -import net.liftweb.http.js.JE.Str -import net.liftweb.record.{Field, Record} +import _root_.net.liftweb.common.{Box, Empty, Failure, Full} +import _root_.net.liftweb.http.js.JE.Str +import _root_.net.liftweb.json.JsonAST.{JNothing, JObject, JValue} +import _root_.net.liftweb.record.{Field, Record} import com.mongodb.{BasicDBObject, BasicDBObjectBuilder, DBObject, DBRef, ObjectId} import com.mongodb.util.JSON @@ -28,7 +29,7 @@ import com.mongodb.util.JSON */ //abstract class MongoRefField[OwnerType <: MongoRecord[OwnerType]](rec: OwnerType) //abstract class MongoRefField[OwnerType <: MongoRecord[OwnerType], RefType <: MongoMetaRecord[RefType]](rec: OwnerType, ref: RefType) -abstract class DBRefField[OwnerType <: MongoRecord[OwnerType], RefType <: MongoRecord[RefType]](rec: OwnerType, ref: RefType) +class DBRefField[OwnerType <: MongoRecord[OwnerType], RefType <: MongoRecord[RefType]](rec: OwnerType, ref: RefType) extends Field[DBRef, OwnerType] { /* @@ -53,6 +54,10 @@ abstract class DBRefField[OwnerType <: MongoRecord[OwnerType], RefType <: MongoR private var _calcedObj = false def asJs = Str(toString) + + def asJValue = (JNothing: JValue) // not implemented + + def setFromJValue(jvalue: JValue) = Empty // not implemented def asXHtml =
diff --git a/src/main/scala/com/eltimn/scamongo/field/JObjectField.scala b/src/main/scala/com/eltimn/scamongo/field/JObjectField.scala index 489e405..9a04660 100644 --- a/src/main/scala/com/eltimn/scamongo/field/JObjectField.scala +++ b/src/main/scala/com/eltimn/scamongo/field/JObjectField.scala @@ -16,15 +16,19 @@ package com.eltimn.scamongo.field * and limitations under the License. */ -import net.liftweb.common.{Box, Empty, Failure, Full} -import net.liftweb.http.js.JE.Str -import net.liftweb.json.JsonAST.JObject -import net.liftweb.json.JsonParser -import net.liftweb.record.{Field, Record} +import _root_.net.liftweb.common.{Box, Empty, Failure, Full} +import _root_.net.liftweb.http.js.JE.Str +import _root_.net.liftweb.json.JsonAST.{JNothing, JObject, JValue} +import _root_.net.liftweb.json.JsonParser +import _root_.net.liftweb.record.{Field, Record} class JObjectField[OwnerType <: Record[OwnerType]](rec: OwnerType) extends Field[JObject, OwnerType] { def asJs = Str(toString) + + def asJValue = (JNothing: JValue) // not implemented + + def setFromJValue(jvalue: JValue) = Empty // not implemented def asXHtml =
diff --git a/src/main/scala/com/eltimn/scamongo/field/MongoListField.scala b/src/main/scala/com/eltimn/scamongo/field/MongoListField.scala index de72cd0..05b64fd 100644 --- a/src/main/scala/com/eltimn/scamongo/field/MongoListField.scala +++ b/src/main/scala/com/eltimn/scamongo/field/MongoListField.scala @@ -20,10 +20,10 @@ import java.util.Date import scala.collection.jcl.Conversions._ -import net.liftweb.common.{Box, Empty, Failure, Full} -import net.liftweb.json.JsonAST.JObject -import net.liftweb.http.js.JE.Str -import net.liftweb.record.{Field, Record} +import _root_.net.liftweb.common.{Box, Empty, Failure, Full} +import _root_.net.liftweb.json.JsonAST.{JNothing, JObject, JValue} +import _root_.net.liftweb.http.js.JE.Str +import _root_.net.liftweb.record.{Field, Record} import com.mongodb._ import com.mongodb.util.JSON @@ -37,6 +37,10 @@ class MongoListField[OwnerType <: MongoRecord[OwnerType], ListType](rec: OwnerTy import Meta.Reflection._ def asJs = Str(toString) + + def asJValue = (JNothing: JValue) // not implemented + + def setFromJValue(jvalue: JValue) = Empty // not implemented def asXHtml =
diff --git a/src/main/scala/com/eltimn/scamongo/field/MongoMapField.scala b/src/main/scala/com/eltimn/scamongo/field/MongoMapField.scala index 8e102a5..1f1c778 100644 --- a/src/main/scala/com/eltimn/scamongo/field/MongoMapField.scala +++ b/src/main/scala/com/eltimn/scamongo/field/MongoMapField.scala @@ -16,10 +16,11 @@ package com.eltimn.scamongo.field * and limitations under the License. */ -import net.liftweb.common.{Box, Empty, Failure, Full} -import net.liftweb.http.js.JE.Str -import net.liftweb.record.{Field, Record} -import net.liftweb.util.Log +import _root_.net.liftweb.common.{Box, Empty, Failure, Full} +import _root_.net.liftweb.http.js.JE.Str +import _root_.net.liftweb.json.JsonAST.{JNothing, JValue} +import _root_.net.liftweb.record.{Field, Record} +import _root_.net.liftweb.util.Log import com.mongodb._ @@ -27,9 +28,13 @@ class MongoMapField[OwnerType <: MongoRecord[OwnerType], MapValueType](rec: Owne extends Field[Map[String, MapValueType], OwnerType] with MongoFieldFlavor[Map[String, MapValueType]] { - def asJs = Str(toString) + def asJs = Str(toString) // not implemented + + def asJValue = (JNothing: JValue) // not implemented + + def setFromJValue(jvalue: JValue) = Empty // not implemented - def asXHtml =
+ def asXHtml =
// not implemented def defaultValue = Map[String, MapValueType]() @@ -50,7 +55,7 @@ class MongoMapField[OwnerType <: MongoRecord[OwnerType], MapValueType](rec: Owne } } - def toForm =
+ def toForm =
// not implemented def owner = rec diff --git a/src/main/scala/com/eltimn/scamongo/field/ObjectIdField.scala b/src/main/scala/com/eltimn/scamongo/field/ObjectIdField.scala index 88dd6cf..33ebd3a 100644 --- a/src/main/scala/com/eltimn/scamongo/field/ObjectIdField.scala +++ b/src/main/scala/com/eltimn/scamongo/field/ObjectIdField.scala @@ -16,9 +16,11 @@ package com.eltimn.scamongo.field * and limitations under the License. */ -import net.liftweb.common.{Box, Empty, Failure, Full} -import net.liftweb.http.js.JE.Str -import net.liftweb.record.{Field, Record} +import _root_.net.liftweb.common.{Box, Empty, Failure, Full} +import _root_.net.liftweb.http.js.JE.{JsNull, Str} +import _root_.net.liftweb.json.JsonAST.{JNothing, JNull, JString, JValue} +import _root_.net.liftweb.record.{Field, Record} +import _root_.net.liftweb.record.FieldHelpers import com.mongodb.{ObjectId, DBRef} @@ -27,8 +29,16 @@ import com.mongodb.{ObjectId, DBRef} */ class ObjectIdField[OwnerType <: MongoRecord[OwnerType]](rec: OwnerType) extends Field[ObjectId, OwnerType] { - - def asJs = Str(toString) + + def asJs = valueBox.map(v => Str(v.toString)) openOr JsNull + + def asJValue: JValue = valueBox.map(v => JString(v.toString)) openOr (JNothing: JValue) + + def setFromJValue(jvalue: JValue): Box[ObjectId] = jvalue match { + case JNothing|JNull if optional_? => setBox(Empty) + case JString(s) => setFromString(s) + case other => setBox(FieldHelpers.expectedA("JString", other)) + } def asXHtml =
diff --git a/src/test/scala/com/eltimn/scamongo/DirectExamples.scala b/src/test/scala/com/eltimn/scamongo/DirectExamples.scala index 39c71eb..b7c0f70 100644 --- a/src/test/scala/com/eltimn/scamongo/DirectExamples.scala +++ b/src/test/scala/com/eltimn/scamongo/DirectExamples.scala @@ -33,9 +33,9 @@ import com.mongodb.{BasicDBObject, BasicDBObjectBuilder, DBObject} object DirectExamples extends Specification { - doFirst { + doBeforeSpec { // define the db - MongoDB.defineDb(DefaultMongoIdentifier, MongoAddress(MongoHost("localhost", 27017), "test_direct")) + MongoDB.defineDb(DefaultMongoIdentifier, MongoAddress(MongoHost(), "test_direct")) } import com.mongodb.util.JSON // Mongo parser/serializer @@ -228,7 +228,7 @@ object DirectExamples extends Specification { doc2.put("type", "db") doc2.put("count", 1) - doc3.put("name", "MongoDB") + doc3.put("name", "MongoDB") doc3.put("type", "db") doc3.put("count", 1) @@ -236,7 +236,7 @@ object DirectExamples extends Specification { coll.save(doc) db.getLastError.get("err") must beNull coll.save(doc2) // this should return an error - db.getLastError.get("err").toString must startWith("E11000 duplicate key errorindex") + db.getLastError.get("err").toString must startWith("E11000 duplicate key error index") coll.save(doc3) db.getLastError.get("err") must beNull @@ -290,7 +290,7 @@ object DirectExamples extends Specification { }) } - doLast { + doAfterSpec { if (!debug) { /* drop the collections */ MongoDB.useCollection(DefaultMongoIdentifier, "testCollection") ( coll => { diff --git a/src/test/scala/com/eltimn/scamongo/DocumentExamples.scala b/src/test/scala/com/eltimn/scamongo/DocumentExamples.scala index e7f00b4..20bcd28 100644 --- a/src/test/scala/com/eltimn/scamongo/DocumentExamples.scala +++ b/src/test/scala/com/eltimn/scamongo/DocumentExamples.scala @@ -41,7 +41,7 @@ object TestDBb extends MongoIdentifier { //class DocumentExampleTest extends Runner(DocumentExamples) with JUnit object DocumentExamples extends Specification { - doFirst { + doBeforeSpec { // create a Mongo instance val mongoHost = MongoHost("localhost", 27017) // define the dbs @@ -325,7 +325,7 @@ object DocumentExamples extends Specification { TestCollection.save(tc, db) db.getLastError.get("err") must beNull TestCollection.save(tc2, db) // this should return an error - db.getLastError.get("err").toString must startWith("E11000 duplicate key errorindex") + db.getLastError.get("err").toString must startWith("E11000 duplicate key error index") TestCollection.save(tc3, db) db.getLastError.get("err") must beNull @@ -443,7 +443,7 @@ object DocumentExamples extends Specification { } - doLast { + doAfterSpec { if (!debug) { /** drop the collections */ SimplePerson.drop diff --git a/src/test/scala/com/eltimn/scamongo/RecordExamples.scala b/src/test/scala/com/eltimn/scamongo/RecordExamples.scala index 8f08ceb..31e1bef 100644 --- a/src/test/scala/com/eltimn/scamongo/RecordExamples.scala +++ b/src/test/scala/com/eltimn/scamongo/RecordExamples.scala @@ -40,7 +40,7 @@ object RecordExamples extends Specification { val debug = false - doFirst { + doBeforeSpec { // define the db MongoDB.defineDb(DefaultMongoIdentifier, MongoAddress(MongoHost("localhost", 27017), "test_record")) } @@ -312,7 +312,7 @@ object RecordExamples extends Specification { } } - doLast { + doAfterSpec { if (!debug) { /** drop the collections */ TestRecord.drop @@ -390,7 +390,9 @@ class TestRecord extends MongoRecord[TestRecord] { */ } -object TestRecord extends TestRecord with MongoMetaRecord[TestRecord] +object TestRecord extends TestRecord with MongoMetaRecord[TestRecord] { + def createRecord = new TestRecord +} case class RPerson(name: String, age: Int, address: Address, children: List[Child]) extends JsonObject[RPerson] { @@ -414,12 +416,16 @@ class MainDoc extends MongoRecord[MainDoc] with MongoId[MainDoc] { def obj = RefDoc.find(value) } } -object MainDoc extends MainDoc with MongoMetaRecord[MainDoc] +object MainDoc extends MainDoc with MongoMetaRecord[MainDoc] { + def createRecord = new MainDoc +} class RefDoc extends MongoRecord[RefDoc] with MongoId[RefDoc] { def meta = RefDoc } -object RefDoc extends RefDoc with MongoMetaRecord[RefDoc] +object RefDoc extends RefDoc with MongoMetaRecord[RefDoc] { + def createRecord = new RefDoc +} // string as id class RefStringDoc extends MongoRecord[RefStringDoc] { @@ -436,7 +442,9 @@ class RefStringDoc extends MongoRecord[RefStringDoc] { new DBRef(db, meta.collectionName, _id.value) ) } -object RefStringDoc extends RefStringDoc with MongoMetaRecord[RefStringDoc] +object RefStringDoc extends RefStringDoc with MongoMetaRecord[RefStringDoc] { + def createRecord = new RefStringDoc +} class ListDoc extends MongoRecord[ListDoc] with MongoId[ListDoc] { def meta = ListDoc @@ -505,7 +513,8 @@ class ListDoc extends MongoRecord[ListDoc] with MongoId[ListDoc] { } object ListDoc extends ListDoc with MongoMetaRecord[ListDoc] { - override def formats = DefaultFormats.lossless // adds .000 + override def formats = DefaultFormats.lossless // adds .000 to Dates + def createRecord = new ListDoc } case class JsonDoc(id: String, name: String) extends JsonObject[JsonDoc] { @@ -525,6 +534,7 @@ class MapDoc extends MongoRecord[MapDoc] with MongoId[MapDoc] { } object MapDoc extends MapDoc with MongoMetaRecord[MapDoc] { override def formats = DefaultFormats.lossless // adds .000 + def createRecord = new MapDoc } class OptionalDoc extends MongoRecord[OptionalDoc] with MongoId[OptionalDoc] { @@ -535,5 +545,7 @@ class OptionalDoc extends MongoRecord[OptionalDoc] with MongoId[OptionalDoc] { override def defaultValue = "nothin" } } -object OptionalDoc extends OptionalDoc with MongoMetaRecord[OptionalDoc] +object OptionalDoc extends OptionalDoc with MongoMetaRecord[OptionalDoc] { + def createRecord = new OptionalDoc +}