Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing implicit Conversion for java.util.Calendar #72

Open
BowlingX opened this issue Dec 27, 2012 · 5 comments
Open

Missing implicit Conversion for java.util.Calendar #72

BowlingX opened this issue Dec 27, 2012 · 5 comments

Comments

@BowlingX
Copy link

Latest Build (beta21) Rogue.scala lacks a conversion from Calendar to a QueryField. It still exists in master branch (beta14).

@BowlingX
Copy link
Author

This will bring back Calendar Field support (Lift's DateTimeField)

object RogueAdditions {
    import com.foursquare.rogue.LiftRogue._
    import com.foursquare.field.{Field => RField}

    class CalendarQueryField[M](field: RField[Calendar, M]) extends AbstractQueryField[Calendar, DateTime, Date, M](field) {
      override def valueToDB(d: DateTime) = d.toDate

      def before(d: DateTime) = new LtQueryClause(field.name, d.toDate)
      def after(d: DateTime) = new GtQueryClause(field.name, d.toDate)
      def onOrBefore(d: DateTime) = new LtEqQueryClause(field.name, d.toDate)
      def onOrAfter(d: DateTime) = new GtEqQueryClause(field.name, d.toDate)
    }

    class CalendarModifyField[M](field: RField[Calendar, M])
      extends AbstractModifyField[Calendar, java.util.Date, M](field) {
      override def valueToDB(c: Calendar) = c.getTime

      def setTo(d: DateTime) = new ModifyClause(ModOps.Set, field.name -> d.toDate)
    }

    implicit def calendarFieldToDateQueryField[M <: BsonRecord[M]]  (f: Field[Calendar, M]): CalendarQueryField[M] = new CalendarQueryField(f)

    implicit def calendarFieldToCalendarModifyField[M <: BsonRecord[M]](f: Field[Calendar, M]): CalendarModifyField[M] =
      new CalendarModifyField(f)

    implicit def rcalendarFieldToCalendarQueryField[M](f: RField[Calendar, M]): CalendarQueryField[M] = new CalendarQueryField(f)

    implicit def rcalendarFieldToCalendarModifyField[M](f: RField[Calendar, M]): CalendarModifyField[M] =      new CalendarModifyField(f)
  }

@agilesteel
Copy link

I took the liberty of fixing the compilation errors, due to missing imports.

object RogueAdditions {
  import java.util.Calendar
  import com.foursquare.rogue._
  import com.foursquare.rogue.LiftRogue._
  import net.liftweb.mongodb.record.BsonRecord
  import com.foursquare.field.{Field => RField}
  import org.joda.time.DateTime

  class CalendarQueryField[M](field: RField[Calendar, M]) 
    extends AbstractQueryField[Calendar, DateTime, java.util.Date, M](field) {
    override def valueToDB(d: DateTime) = d.toDate

    def before(d: DateTime) = new LtQueryClause(field.name, d.toDate)
    def after(d: DateTime) = new GtQueryClause(field.name, d.toDate)
    def onOrBefore(d: DateTime) = new LtEqQueryClause(field.name, d.toDate)
    def onOrAfter(d: DateTime) = new GtEqQueryClause(field.name, d.toDate)
  }

  class CalendarModifyField[M](field: RField[Calendar, M])
    extends AbstractModifyField[Calendar, java.util.Date, M](field) {
    override def valueToDB(c: Calendar) = c.getTime

    def setTo(d: DateTime) = new ModifyClause(ModOps.Set, field.name -> d.toDate)
  }

  implicit def calendarFieldToDateQueryField[M <: BsonRecord[M]]
    (f: net.liftweb.record.Field[Calendar, M]): CalendarQueryField[M] = 
        new CalendarQueryField(f)

  implicit def calendarFieldToCalendarModifyField[M <: BsonRecord[M]]
    (f: net.liftweb.record.Field[Calendar, M]): CalendarModifyField[M] = 
        new CalendarModifyField(f)

  implicit def rcalendarFieldToCalendarQueryField[M]
    (f: RField[Calendar, M]): CalendarQueryField[M] = new CalendarQueryField(f)

  implicit def rcalendarFieldToCalendarModifyField[M]
    (f: RField[Calendar, M]): CalendarModifyField[M] = new CalendarModifyField(f)
}

@alebon
Copy link

alebon commented Apr 4, 2013

Hey guys, i'm not able to compile the following sample:

SampleRecord where (_.created between (d1, d2))

"created" is a lift DateTimeField and "d1" and "d2" are of type DateTime - between expects Caledar values as input. I tried it on 2.0.0-RC1 2.0.0-beta22 (which one is newer?).

Anyway, how can i apply the changes agilesteel posted?

Thanks a lot!

@agilesteel
Copy link

Hi I'm using 2.0.0-RC1 and had the same issue. You just take the code and import RogueAdditions._ where needed (in addition to standard rogue imports). Just to be clear, the code is not in rogue itself you need to copy paste it ;)

@alebon
Copy link

alebon commented Apr 4, 2013

THANKS! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants