diff --git a/src/main/scala/poc/model/Cashier.scala b/src/main/scala/poc/model/Cashier.scala index 28fde76..24c5d45 100644 --- a/src/main/scala/poc/model/Cashier.scala +++ b/src/main/scala/poc/model/Cashier.scala @@ -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 +} diff --git a/src/main/scala/poc/model/Schedule.scala b/src/main/scala/poc/model/Schedule.scala deleted file mode 100644 index 5153ab4..0000000 --- a/src/main/scala/poc/model/Schedule.scala +++ /dev/null @@ -1,11 +0,0 @@ -package poc.model -import org.squeryl.KeyedEntity -import org.squeryl.dsl.CompositeKey2 -import org.squeryl.PrimitiveTypeMode._ - -class Schedule ( - val timetableId: Long, - val workingTimeRangeId: Long -) extends KeyedEntity[CompositeKey2[Long, Long]] { - def id = compositeKey(timetableId, workingTimeRangeId) -} \ No newline at end of file diff --git a/src/main/scala/poc/model/Timetable.scala b/src/main/scala/poc/model/Timetable.scala index 1b8974a..f78b84f 100644 --- a/src/main/scala/poc/model/Timetable.scala +++ b/src/main/scala/poc/model/Timetable.scala @@ -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 + } + } diff --git a/src/main/scala/poc/model/WorkingTimeRange.scala b/src/main/scala/poc/model/WorkingTimeRange.scala index 75c61f4..dcb4b74 100644 --- a/src/main/scala/poc/model/WorkingTimeRange.scala +++ b/src/main/scala/poc/model/WorkingTimeRange.scala @@ -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) -} \ No newline at end of file + lazy val timetables = TimetableDb.timetableToWorkingTimeRanges.right(this) +} diff --git a/src/main/scala/poc/schema/BootStrap.scala b/src/main/scala/poc/schema/BootStrap.scala index eded73e..6f6b555 100644 --- a/src/main/scala/poc/schema/BootStrap.scala +++ b/src/main/scala/poc/schema/BootStrap.scala @@ -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 @@ -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)) diff --git a/src/main/scala/poc/schema/TimetableDb.scala b/src/main/scala/poc/schema/TimetableDb.scala index e1068f5..3307681 100644 --- a/src/main/scala/poc/schema/TimetableDb.scala +++ b/src/main/scala/poc/schema/TimetableDb.scala @@ -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 { @@ -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) + } diff --git a/src/main/scala/poc/service/impl/TimetableServiceImpl.scala b/src/main/scala/poc/service/impl/TimetableServiceImpl.scala index 9306159..4cd32b0 100644 --- a/src/main/scala/poc/service/impl/TimetableServiceImpl.scala +++ b/src/main/scala/poc/service/impl/TimetableServiceImpl.scala @@ -1,14 +1,19 @@ 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 { @@ -16,22 +21,41 @@ class TimetableServiceImpl extends TimetableService { 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)) } } +