User record, mixins
authorTomas Zeman <tzeman@volny.cz>
Thu, 12 Apr 2012 17:07:35 +0200
changeset 56 9409e7ab3f9d
parent 55 2f089a5d97da
child 57 14557aef6287
User record, mixins
db/db-schema.sql
src/main/scala/fis/aaa/model/AaaSchema.scala
src/main/scala/fis/aaa/model/User.scala
src/main/scala/fis/aaa/model/UserCrud.scala
src/main/scala/fis/aaa/model/UserField.scala
src/main/scala/fis/top/model/FisDbSchema.scala
--- a/db/db-schema.sql	Thu Apr 12 14:39:51 2012 +0200
+++ b/db/db-schema.sql	Thu Apr 12 17:07:35 2012 +0200
@@ -30,6 +30,18 @@
 create sequence "code_list_item_id_seq";
 -- indexes on code_list_item
 create index "code_list_item_code_list_idx" on "code_list_item" ("code_list");
+create table "user" (
+    "name" varchar(100) not null,
+    "id" bigint primary key not null,
+    "note" varchar(10240),
+    "login" varchar(40) not null,
+    "deleted" boolean not null,
+    "active" boolean not null,
+    "password" varchar(128) not null
+  );
+create sequence "user_id_seq";
+-- indexes on user
+create index "user_login_idx" on "user" ("login");
 create table "city" (
     "name" varchar(100) not null,
     "id" bigint primary key not null,
@@ -96,3 +108,5 @@
 alter table "company" add constraint "companyFK3" foreign key ("address_id") references "address"("id");
 alter table "company" add constraint "companyFK4" foreign key ("corresp_address_id") references "address"("id");
 alter table "bank_account" add constraint "bank_accountFK5" foreign key ("company_id") references "company"("id");
+-- column group indexes :
+create index "user_deleted_active_idx" on "user" ("deleted","active");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/aaa/model/AaaSchema.scala	Thu Apr 12 17:07:35 2012 +0200
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2011-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.aaa.model
+
+import fis.base.model.BaseSchema
+import net.liftweb.squerylrecord.RecordTypeMode._
+
+/**
+ * Database schema for users, ...
+ */
+trait AaaSchema extends BaseSchema {
+  val userT = tableWithSeq[User]
+  on(userT)(t => declare(
+    t.login   defineAs(indexed("user_login_idx")),
+    columns(t.deleted, t.active) are(indexed("user_deleted_active_idx"))
+  ))
+
+  User.byId.default.set(UserCrud.get _)
+
+  /** All existing (undeleted) users. */
+  val usersF = () => from(userT)(u =>
+    where(u.deleted === false) select(u) orderBy(u.name asc))
+
+  /** Active users. */
+  val activeUsersF = () => from(userT)(u =>
+    where(u.deleted === false and u.active === true) select(u)
+    orderBy(u.name asc))
+}
+
+object AaaSchema extends AaaSchema
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/aaa/model/User.scala	Thu Apr 12 17:07:35 2012 +0200
@@ -0,0 +1,57 @@
+/*
+ * 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.aaa.model
+
+import fis.base.model.Entity
+import net.liftweb.common._
+import net.liftweb.http.Factory
+import net.liftweb.record.{MetaRecord, Record}
+import net.liftweb.record.field._
+import net.liftweb.squerylrecord.RecordTypeMode._
+import net.tz.lift.model.{FieldLabel => FL}
+
+/**
+ * User record.
+ */
+class User private() extends Record[User] with Entity[User] {
+  def meta = User
+
+  val login = new StringField(this, 40) with FL
+  val password = new PasswordField(this) with FL
+  val active = new BooleanField(this) with FL {
+    override def defaultValue = true
+  }
+  val deleted = new BooleanField(this) with FL
+}
+
+/**
+ * User metarecord.
+ */
+object User extends User with MetaRecord[User] with Factory {
+
+  object cur extends FactoryMaker[Box[User]](Empty)
+
+  object byId extends Inject[Long => Box[User]]({id: Long => Empty}) {}
+
+  /**
+   * Returns current logged in user.
+   */
+  def get: Box[User] = cur()
+
+}
+
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/aaa/model/UserCrud.scala	Thu Apr 12 17:07:35 2012 +0200
@@ -0,0 +1,26 @@
+/*
+ * 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.aaa.model
+
+import fis.base.model.RecordCrud
+
+trait UserCrud extends RecordCrud[User] {
+  val table = AaaSchema.userT
+}
+
+object UserCrud extends UserCrud
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/aaa/model/UserField.scala	Thu Apr 12 17:07:35 2012 +0200
@@ -0,0 +1,106 @@
+/*
+ * 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.aaa.model
+
+import net.liftweb.common._
+import net.liftweb.common.Box._
+import net.liftweb.http.SHtml
+import net.liftweb.record.{LifecycleCallbacks, Record}
+import net.liftweb.record.field.{OptionalLongField, LongField}
+import net.liftweb.util.Vendor
+import net.tz.lift.model._
+import net.tz.lift.model.{FieldLabel => FL}
+import scala.xml.NodeSeq
+
+/**
+ * User field.
+ */
+class UserField[T <: Record[T]](own: T, l: Vendor[Iterable[User]],
+  u: Vendor[Long => Box[User]]) extends LongField(own) {
+
+  override def asHtml = user.dmap(NodeSeq.Empty)(_.name.asHtml)
+  override def toForm = Full(SHtml.selectObj[Long](
+    l() map { u => (u.id, u.name.get) } toSeq, valueBox, set(_)))
+  def user: Box[User] = u()(get)
+}
+
+/**
+ * Optional user field.
+ */
+class OptionalUserField[T <: Record[T]](own: T, l: Vendor[Iterable[User]],
+  u: Vendor[Long => Box[User]]) extends OptionalLongField(own) {
+
+  override def asHtml = user.dmap(NodeSeq.Empty)(_.name.asHtml)
+  override def toForm = Full(SHtml.selectObj[Box[Long]](
+    (Empty, emptyLabel) +: (l() map { u => (Full(u.id), u.name.get) } toSeq),
+    Full(valueBox), setBox(_)))
+  def user: Box[User] = get flatMap { u()(_) }
+  def emptyLabel: String = "------"
+}
+
+/**
+ * CreatedBy field.
+ */
+class CreatedByField[T <: Record[T]](own: T, cur: Vendor[Box[User]],
+  u: Vendor[Long => Box[User]]) extends OptionalLongField(own) with
+  LifecycleCallbacks {
+
+  override def asHtml = user.dmap(NodeSeq.Empty)(_.name.asHtml)
+  override def defaultValue = cur().dmap(super.defaultValue)(_.id) 
+  override def displayName = l10n(name)
+  override def beforeCreate {
+    set(cur().map { _.id })
+  }
+  def user: Box[User] = get flatMap { u()(_) }
+}
+
+/**
+ * UpdatedBy field.
+ */
+class UpdatedByField[T <: Record[T]](own: T, cur: Vendor[Box[User]],
+  u: Vendor[Long => Box[User]]) extends OptionalLongField(own) with
+  LifecycleCallbacks {
+
+  override def asHtml = user.dmap(NodeSeq.Empty)(_.name.asHtml)
+  override def defaultValue = cur().dmap(super.defaultValue)(_.id)
+  override def beforeSave {
+    set(cur().map { _.id })
+  }
+  override def displayName = l10n(name)
+  def user: Box[User] = get flatMap { u()(_) }
+}
+
+/**
+ * Created/UpdatedBy mixin.
+ */
+trait CreatedUpdatedBy[T <: Record[T]] { self: T =>
+  val createdBy = new CreatedByField(this.asInstanceOf[T], curUserVendor,
+    getUserVendor)
+  val updatedBy = new UpdatedByField(this.asInstanceOf[T], curUserVendor,
+    getUserVendor)
+
+  protected def curUserVendor: Vendor[Box[User]] = User.cur
+  protected def getUserVendor: Vendor[Long => Box[User]] = User.byId
+}
+
+/**
+ * Mixin for User field to have current user as default.
+ */
+trait DefaultCurUser { self: UserField[_] => 
+  override def defaultValue: Long = User.get.map(_.id).getOrElse(0L)
+}
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/scala/fis/top/model/FisDbSchema.scala	Thu Apr 12 14:39:51 2012 +0200
+++ b/src/main/scala/fis/top/model/FisDbSchema.scala	Thu Apr 12 17:07:35 2012 +0200
@@ -16,12 +16,14 @@
 package fis.top.model
 
 import fis.base.model.BaseSchema
+import fis.aaa.model.AaaSchema
 import fis.cl.model.CodeListSchema
 import fis.crm.model.CrmSchema
 import fis.geo.model.GeoSchema
 
 trait FisDbSchema extends BaseSchema
   with CodeListSchema
+  with AaaSchema
   with CrmSchema
   with GeoSchema