src/main/scala/fis/base/model/CodeList.scala
author Tomas Zeman <tzeman@volny.cz>
Fri, 10 Feb 2012 09:53:06 +0100
changeset 10 fc7b6a99deb4
parent 4 bb4478e1cff7
permissions -rw-r--r--
81aa2ebe282b4664 Company model
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     1
/*
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     2
 * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     3
 *
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     4
 * Licensed under the Apache License, Version 2.0 (the "License");
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     5
 * you may not use this file except in compliance with the License.
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     6
 * You may obtain a copy of the License at
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     7
 *
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     8
 *     http://www.apache.org/licenses/LICENSE-2.0
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     9
 *
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    10
 * Unless required by applicable law or agreed to in writing, software
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    11
 * distributed under the License is distributed on an "AS IS" BASIS,
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    13
 * See the License for the specific language governing permissions and
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    14
 * limitations under the License.
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    15
 */
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    16
package fis.base.model
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    17
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    18
import net.liftweb.common._
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    19
import net.liftweb.record.{Field, MetaRecord, Record}
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    20
import net.liftweb.record.field._
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    21
import net.liftweb.squerylrecord.KeyedRecord
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    22
import net.liftweb.squerylrecord.RecordTypeMode._
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    23
import scala.xml.{NodeSeq, Text}
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    24
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    25
class CodeListItem private () extends Record[CodeListItem] with
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    26
  Entity[CodeListItem] {
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    27
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    28
  def meta = CodeListItem
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    29
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    30
  /** I18n flag for this item's name. */
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    31
  val i18n = new BooleanField(this)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    32
  /** Default item flag. */
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    33
  val dflt = new BooleanField(this)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    34
  /** Reference to code list id (enum id). */
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    35
  val codeListType = new IntField(this)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    36
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    37
  override def toXHtml = Text(linkName)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    38
}
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    39
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    40
object CodeListItem extends CodeListItem with MetaRecord[CodeListItem] with
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    41
  MetaEntity[CodeListItem] {
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    42
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    43
  def getTable = BaseSchema.codeListItems
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    44
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    45
  def items(codeListType: Int) = from(getTable) { cli =>
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    46
    where(cli.codeListType === codeListType)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    47
    select(cli)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    48
  }
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    49
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    50
  def defaultItem(codeListType: Int): Box[CodeListItem] =
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    51
    items(codeListType) find (_.dflt.get)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    52
}
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    53
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    54
trait CodeList[T] {
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    55
  def items: Iterable[T]
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    56
  def defaultItem: Box[T]
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    57
  def item(id: Long): Box[T]
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    58
}
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    59
10
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    60
class CodeListImpl[T](val id: CodeListRegister.CodeListRegister,
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    61
  tr: CodeListItem => T) extends CodeList[T] {
4
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    62
10
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    63
  def items = CodeListItem.items(id.id) map (tr(_))
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    64
  def defaultItem = CodeListItem.defaultItem(id.id) map (tr(_))
4
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    65
  def item(id: Long) = CodeListItem.findByKey(id) map (tr(_))
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    66
}
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    67
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    68
import net.liftweb.http.SHtml
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    69
import net.liftweb.util.Helpers._
10
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    70
import CodeListRegister.CodeListRegister
4
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    71
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    72
class CodeListItemField[T, OwnerType <: Record[OwnerType]](own: OwnerType,
10
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    73
  codeListType: CodeListRegister, tr: CodeListItem => T)
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    74
  extends AbstractCodeListItemField[T, OwnerType](own, codeListType) {
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    75
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    76
  def cvt: Box[T] = item map { tr(_) }
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    77
}
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    78
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    79
abstract class AbstractCodeListItemField[T, OwnerType <: Record[OwnerType]](
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    80
  own: OwnerType, codeListType: CodeListRegister) extends LongField(own) with
4
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    81
  Converter0[Box[T]] {
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    82
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    83
  def item: Box[CodeListItem] = CodeListItem.findByKey(get)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    84
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    85
  override def defaultValueBox: Box[Long] =
10
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    86
    CodeListItem.defaultItem(codeListType.id).map { _.id }
4
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    87
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    88
  override def toXHtml = item map { _.toXHtml } openOr NodeSeq.Empty
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    89
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    90
  protected def buildDisplayList: List[(Long, String)] =
10
fc7b6a99deb4 81aa2ebe282b4664 Company model
Tomas Zeman <tzeman@volny.cz>
parents: 4
diff changeset
    91
    CodeListItem.items(codeListType.id).toList.map { i =>
4
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    92
      (i.id, i.linkName)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    93
    }
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    94
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    95
  private def elem = SHtml.selectObj[Long](buildDisplayList, Full(get),
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    96
    set(_)) % ("tabindex" -> tabIndex.toString)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    97
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    98
  override def toForm: Box[NodeSeq] = uniqueFieldId match {
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    99
    case Full(id) => Full(elem % ("id" -> id))
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   100
    case _ => Full(elem)
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   101
  }
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   102
}
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   103
 
bb4478e1cff7 9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   104
// vim: set ts=2 sw=2 et: