Skip to content

Commit

Permalink
Some stuff to persist data.
Browse files Browse the repository at this point in the history
  • Loading branch information
dboissin committed Jul 31, 2011
1 parent f0e535f commit fedfed9
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 31 deletions.
8 changes: 8 additions & 0 deletions src/main/scala/poc/model/Cashier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ class Cashier (
) extends KeyedEntity[Long] {
lazy val timetables: OneToMany[Timetable] = TimetableDb.cashierToTimetables.left(this)
}

object Cashier {
import poc.schema.TimetableDb._
import org.squeryl.PrimitiveTypeMode._

def findByName(name: String) =
cashiers.where(c => c.name === name).single
}
11 changes: 0 additions & 11 deletions src/main/scala/poc/model/Schedule.scala

This file was deleted.

15 changes: 14 additions & 1 deletion src/main/scala/poc/model/Timetable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,18 @@ class Timetable (
val id: Long = 0
) extends KeyedEntity[Long] {
lazy val cashier: ManyToOne[Cashier] = TimetableDb.cashierToTimetables.right(this)
lazy val workingTimeRanges = TimetableDb.schedule.left(this)
lazy val workingTimeRanges = TimetableDb.timetableToWorkingTimeRanges.left(this)
}

object Timetable {
import poc.schema.TimetableDb._
import org.squeryl.PrimitiveTypeMode._

def findByWeekAndCashier(cashierName: String, startWeek: Timestamp) = {
from(cashiers, timetables)((c, t) =>
where(c.name === cashierName and c.id === t.cashierId and t.startWeek === startWeek)
select(t)
).headOption
}

}
5 changes: 3 additions & 2 deletions src/main/scala/poc/model/WorkingTimeRange.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import poc.schema.TimetableDb
class WorkingTimeRange (
val begin: Timestamp,
val end: Timestamp,
val timetableId: Long,
val id:Long = 0
) extends KeyedEntity[Long] {
lazy val timetables = TimetableDb.schedule.right(this)
}
lazy val timetables = TimetableDb.timetableToWorkingTimeRanges.right(this)
}
11 changes: 5 additions & 6 deletions src/main/scala/poc/schema/BootStrap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object BootStrap {
inTransaction {
import poc.schema.TimetableDb._

drop // Bad idea in production application !!!
// drop // Bad idea in production application !!!
create
printDdl

Expand All @@ -40,15 +40,14 @@ object BootStrap {
println ("after insert => blopId : " + blop.id)


val wtr = new WorkingTimeRange(new Timestamp(System.currentTimeMillis), new Timestamp(System.currentTimeMillis + 60000))
workingTimeRanges.insert(wtr)

//timetables.insert(t)
val t3 = timetables.where(t => t.id === 2L).single

val wtr = new WorkingTimeRange(new Timestamp(System.currentTimeMillis),
new Timestamp(System.currentTimeMillis + 60000), t3.id)
t3.workingTimeRanges.associate(wtr)
t3.workingTimeRanges.dissociateAll
t3.workingTimeRanges.associate(wtr)
t3.workingTimeRanges.deleteAll
//t3.workingTimeRanges.associate(wtr)
// schedules.associate(t, wtr)

//timetables.insert(new Timetable(new Timestamp(System.currentTimeMillis), 1L))
Expand Down
9 changes: 4 additions & 5 deletions src/main/scala/poc/schema/TimetableDb.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.squeryl.PrimitiveTypeMode._
import poc.model.Cashier
import poc.model.Timetable
import poc.model.WorkingTimeRange
import poc.model.Schedule

object TimetableDb extends Schema {

Expand All @@ -25,12 +24,12 @@ object TimetableDb extends Schema {
columns(workingTimeRange.begin, workingTimeRange.end) are (unique)
))

val schedule =
manyToManyRelation(timetables, workingTimeRanges).
via[Schedule]((t, w, s) => (t.id === s.timetableId, w.id === s.workingTimeRangeId))

val cashierToTimetables =
oneToManyRelation(cashiers, timetables).
via((c, t) => c.id === t.cashierId)

val timetableToWorkingTimeRanges =
oneToManyRelation(timetables, workingTimeRanges).
via((t, wtr) => t.id === wtr.timetableId)

}
36 changes: 30 additions & 6 deletions src/main/scala/poc/service/impl/TimetableServiceImpl.scala
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
package poc.service.impl

import java.sql.Timestamp
import java.util.Calendar
import java.util.Date

import scala.collection.immutable.StringOps
import scala.reflect.BeanProperty

import org.squeryl.PrimitiveTypeMode._
import poc.model.Timetable
import poc.model.Cashier
import poc.resources.Snippets._
import poc.resources.TimetableView
import poc.schema.TimetableDb._
import poc.service.TimetableService
import poc.schema.BootStrap
import poc.model.WorkingTimeRange

class TimetableServiceImpl extends TimetableService {

val BEGIN_HOUR = 8
val BEGIN_MINUTE = 30

def cashierTimetable(year: String, week: String, name: String): TimetableView = {

implicit def string2Int(s: String): Int = new StringOps(s).toInt

val cal = Calendar.getInstance
cal.set(Calendar.YEAR, year)
cal.set(Calendar.WEEK_OF_YEAR, week)
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
cal.set(Calendar.HOUR_OF_DAY, BEGIN_HOUR)
cal.set(Calendar.MINUTE, BEGIN_MINUTE)

val workingTime = (new Date(), new Date())::Nil
TimetableView(name, (workingTime).toArray, cal.getTime, countTime(workingTime))
inTransaction {
Timetable.findByWeekAndCashier(name, new Timestamp(cal.getTime.getTime))
} match {
case Some(t) => TimetableView(name, workingTime.toArray, t.startWeek, countTime(workingTime))
case None => TimetableView(name, workingTime.toArray, cal.getTime, 0L)
}
}

def saveSelection(name: String, selection: String, startWeekIdx: Long): TimetableView = {
val workingTime = groupWorkingTimeList(string2Date(selection.split(',').toList))
TimetableView(name, (workingTime).toArray, new Date(startWeekIdx), countTime(workingTime))
val week = new Timestamp(startWeekIdx)
transaction {
val timetable = Timetable.findByWeekAndCashier(name, week).getOrElse(
timetables.insert(new Timetable(week, Cashier.findByName(name).id)))
timetable.workingTimeRanges.deleteAll
for (timeRange <- workingTime) {
val wtr = new WorkingTimeRange(new Timestamp(timeRange._1.getTime),
new Timestamp(timeRange._2.getTime), timetable.id)
timetable.workingTimeRanges.associate(wtr)
}
}

TimetableView(name, workingTime.toArray, new Date(startWeekIdx), countTime(workingTime))
}

}

0 comments on commit fedfed9

Please sign in to comment.