src/main/scala/fis/pm/model/PmSchema.scala
author Tomas Zeman <tzeman@volny.cz>
Wed, 30 May 2012 22:51:02 +0200
changeset 100 1fcbeae1f9da
parent 98 eac38214183d
child 103 6a2a19785cd8
permissions -rw-r--r--
8f7cff383e0a8601 Attachments

/*
 * Copyright 2012 Tomas Zeman <tzeman@volny.cz>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fis.pm.model

import fis.aaa.model.{AaaSchema, User}
import fis.base.model.{BaseSchema, Entity}
import fis.cl.model.CodeListSchema
import fis.fs.model.FsSchema
import net.liftweb.squerylrecord.RecordTypeMode._

trait PmSchema extends BaseSchema with AaaSchema with CodeListSchema with
  FsSchema {

  /* project */
  val projectT = tableWithSeq[Project]

  val projectResponsible = oneToManyRelation(userT, projectT).
    via((u, p) => (u.id === p.responsible))

  val projectProductLine = oneToManyRelation(cli, projectT).
    via((i, p) => (i.id === p.productLine))
  projectProductLine.foreignKeyDeclaration.constrainReference(onDelete setNull)

  val projectState = oneToManyRelation(cli, projectT).
    via((i, p) => (i.id === p.stateFld))

  Project.users.default.set(activeUsersF)

  def projects: Iterable[Project] = from(projectT)(p =>
    select(p) orderBy(p.name asc))

  /* task */
  val taskT = tableWithSeq[Task]

  val taskResponsible = oneToManyRelation(userT, taskT).
    via((u, t) => (u.id === t.responsible))

  val taskType = oneToManyRelation(cli, taskT).
    via((i, t) => (i.id === t.taskType))

  val taskState = oneToManyRelation(cli, taskT).
    via((i, t) => (i.id === t.stateFld))

  val taskProject = oneToManyRelation(projectT, taskT).
    via((p, t) => (p.id === t.project))

  Task.users.default.set(activeUsersF)

  /* comment */
  val commentT = tableWithSeq[Comment]

  val commentTask = oneToManyRelation(taskT, commentT).
    via((t, c) => (t.id === c.task))
  commentTask.foreignKeyDeclaration.constrainReference(onDelete cascade)

  /* project - company */
  val projectCompany = manyToManyRelation(projectT, companyT).
    via[ProjectCompany]((p, c, pc) => (
      p.id === pc.project,
      c.id === pc.company
    ))
  projectCompany.leftForeignKeyDeclaration.constrainReference(onDelete cascade)
  projectCompany.rightForeignKeyDeclaration.constrainReference(onDelete cascade)

  /* project - location */
  val projectLocation = manyToManyRelation(projectT, locationT).
    via[ProjectLocation]((p, l, pl) => (
      p.id === pl.project,
      l.id === pl.location
    ))
  projectLocation.leftForeignKeyDeclaration.constrainReference(onDelete cascade)
  projectLocation.rightForeignKeyDeclaration.constrainReference(onDelete cascade)

  /* project/task attachments */
  val projectAttachment = manyToManyRelation(projectT, attachmentT).
    via[ProjectAttachment]((p, a, pa) => (
      p.id === pa.project,
      a.id === pa.attachment
    ))
  projectAttachment.leftForeignKeyDeclaration.constrainReference(onDelete cascade)
  projectAttachment.rightForeignKeyDeclaration.constrainReference(onDelete cascade)

  val taskAttachment = manyToManyRelation(taskT, attachmentT).
    via[TaskAttachment]((t, a, ta) => (
      t.id === ta.task,
      a.id === ta.attachment
    ))
  taskAttachment.leftForeignKeyDeclaration.constrainReference(onDelete cascade)
  taskAttachment.rightForeignKeyDeclaration.constrainReference(onDelete cascade)

}

object PmSchema extends PmSchema

object ProjectTasks {
  def apply(p: Project): Iterable[Task] =
    from(PmSchema.taskProject.left(p))(t => select(t) orderBy(t.deadline asc))
}

object TaskComments {
  def apply(t: Task): Iterable[Comment] =
    from(PmSchema.commentTask.left(t))(c => select(c) orderBy(c.createdAt asc))
}

object CompanyProjects {
  import fis.crm.model.Company
  def apply(c: Company): Iterable[Project] =
    from(PmSchema.projectCompany.right(c))(p => select(p) orderBy(p.name asc))
}

object AssignedTasks {
  def apply(u: User): Iterable[Task] =
    from(PmSchema.taskResponsible.left(u))(t =>
      select(t) orderBy(t.deadline asc))
}

object AssignedProjects {
  def apply(u: User): Iterable[Project] =
    from(PmSchema.projectResponsible.left(u))(p =>
      select(p) orderBy(p.name asc))
}

object ProjectLocations {
  import fis.geo.model._
  def apply(p: Project): Iterable[Location] =
    from(PmSchema.projectLocation.left(p), GeoSchema.addressT,
    GeoSchema.cityT)((l, a, c) =>
      where(l.address === a.id and a.city === c.id)
      select(l) orderBy(c.name asc, l.name asc))
}

object LocationProjects {
  import fis.geo.model._
  def apply(l: Location): Iterable[Project] =
    from(PmSchema.projectLocation.right(l))(p => select(p) orderBy(p.name asc))
}

object ProjectAttachments {
  import fis.fs.model._
  def apply(p: Project): Iterable[Attachment] =
    from(PmSchema.projectAttachment.left(p))(a =>
      select(a) orderBy(a.name asc))
}

object TaskAttachments {
  import fis.fs.model._
  def apply(t: Task): Iterable[Attachment] =
    from(PmSchema.taskAttachment.left(t))(a =>
      select(a) orderBy(a.name asc))
}

/* Many-to-many relations */

import org.squeryl.KeyedEntity
import org.squeryl.dsl._

case class ProjectCompany(val project: Long, val company: Long)
  extends KeyedEntity[CompositeKey2[Long, Long]] {
  def id = CompositeKey2(project, company)
}

case class ProjectLocation(val project: Long, val location: Long)
  extends KeyedEntity[CompositeKey2[Long, Long]] {
  def id = CompositeKey2(project, location)
}

case class ProjectAttachment(val project: Long, val attachment: Long)
  extends KeyedEntity[CompositeKey2[Long, Long]] {
  def id = CompositeKey2(project, attachment)
}

case class TaskAttachment(val task: Long, val attachment: Long)
  extends KeyedEntity[CompositeKey2[Long, Long]] {
  def id = CompositeKey2(task, attachment)
}

// vim: set ts=2 sw=2 et: