Trackable entity: created/updated fields
authorTomas Zeman <tzeman@volny.cz>
Fri, 20 Apr 2012 08:26:22 +0200
changeset 61 b65843860274
parent 60 e2a2e2045c0e
child 62 08c42c1684f3
Trackable entity: created/updated fields
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/UserField.scala
src/main/scala/fis/base/model/Entity.scala
--- a/db/db-schema.sql	Fri Apr 20 08:26:22 2012 +0200
+++ b/db/db-schema.sql	Fri Apr 20 08:26:22 2012 +0200
@@ -4,6 +4,7 @@
     "l1" bigint not null,
     "i18n" boolean not null,
     "name" varchar(100) not null,
+    "updated_at" timestamp not null,
     "ol2" bigint,
     "s3" varchar(200) not null,
     "id" bigint primary key not null,
@@ -20,10 +21,13 @@
     "s1" varchar(200) not null,
     "note" varchar(10240),
     "os2" varchar(200),
+    "created_at" timestamp not null,
+    "created_by" bigint,
     "i2" integer not null,
     "oi3" integer,
     "l2" bigint not null,
     "ol3" bigint,
+    "updated_by" bigint,
     "os3" varchar(200),
     "deleted" boolean not null
   );
@@ -32,9 +36,13 @@
 create index "code_list_item_code_list_idx" on "code_list_item" ("code_list");
 create table "user" (
     "name" varchar(100) not null,
+    "updated_at" timestamp not null,
     "id" bigint primary key not null,
     "note" varchar(10240),
+    "created_at" timestamp not null,
+    "created_by" bigint,
     "login" varchar(40) not null,
+    "updated_by" bigint,
     "deleted" boolean not null,
     "active" boolean not null,
     "password" varchar(128) not null
@@ -44,9 +52,13 @@
 create index "user_login_idx" on "user" ("login");
 create table "city" (
     "name" varchar(100) not null,
+    "updated_at" timestamp not null,
     "id" bigint primary key not null,
     "country_id" bigint not null,
-    "note" varchar(10240)
+    "note" varchar(10240),
+    "created_at" timestamp not null,
+    "created_by" bigint,
+    "updated_by" bigint
   );
 create sequence "s_city_id";
 create table "address" (
@@ -60,13 +72,18 @@
 create table "country" (
     "iso3" varchar(3) not null,
     "name" varchar(100) not null,
+    "updated_at" timestamp not null,
     "id" bigint primary key not null,
     "iso2" varchar(2) not null,
-    "note" varchar(10240)
+    "note" varchar(10240),
+    "created_at" timestamp not null,
+    "created_by" bigint,
+    "updated_by" bigint
   );
 create sequence "s_country_id";
 create table "contact" (
     "name" varchar(100) not null,
+    "updated_at" timestamp not null,
     "id" bigint primary key not null,
     "work_mobile" varchar(40) not null,
     "private_mail" varchar(256),
@@ -75,9 +92,12 @@
     "first_name" varchar(80) not null,
     "fax" varchar(40),
     "note" varchar(10240),
+    "created_at" timestamp not null,
+    "created_by" bigint,
     "other_mail" varchar(256),
     "position" varchar(40),
     "other_mobile" varchar(40),
+    "updated_by" bigint,
     "private_mobile" varchar(40),
     "private_phone" varchar(40),
     "work_phone" varchar(40)
@@ -85,11 +105,15 @@
 create sequence "contact_id_seq";
 create table "company" (
     "name" varchar(100) not null,
+    "updated_at" timestamp not null,
     "id" bigint primary key not null,
     "partner" integer not null,
     "ico" varchar(40) not null,
     "note" varchar(10240),
+    "created_at" timestamp not null,
+    "created_by" bigint,
     "corresp_address_id" bigint,
+    "updated_by" bigint,
     "pin" integer not null,
     "address_id" bigint not null
   );
--- a/src/main/scala/fis/aaa/model/AaaSchema.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/aaa/model/AaaSchema.scala	Fri Apr 20 08:26:22 2012 +0200
@@ -28,7 +28,7 @@
     columns(t.deleted, t.active) are(indexed("user_deleted_active_idx"))
   ))
 
-  User.byId.default.set(UserCrud.get _)
+  UserVendors.byId.default.set(UserCrud.get _)
 
   /** All existing (undeleted) users. */
   val usersF = () => from(userT)(u =>
--- a/src/main/scala/fis/aaa/model/User.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/aaa/model/User.scala	Fri Apr 20 08:26:22 2012 +0200
@@ -42,16 +42,20 @@
  */
 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()
-
+  def get: Box[User] = UserVendors.cur()
 }
 
+/**
+ * User vendors.
+ * Must be separate from <code>User</code> due to initialization errors
+ * in <code>Entity</code> (recursive defs).
+ */
+object UserVendors extends Factory {
+  object cur extends FactoryMaker[Box[User]](Empty)
+  object byId extends Inject[Long => Box[User]]({id: Long => Empty}) {}
+}
 
 // vim: set ts=2 sw=2 et:
--- a/src/main/scala/fis/aaa/model/UserField.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/aaa/model/UserField.scala	Fri Apr 20 08:26:22 2012 +0200
@@ -92,8 +92,8 @@
   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
+  protected def curUserVendor: Vendor[Box[User]] = UserVendors.cur
+  protected def getUserVendor: Vendor[Long => Box[User]] = UserVendors.byId
 }
 
 /**
--- a/src/main/scala/fis/base/model/Entity.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/base/model/Entity.scala	Fri Apr 20 08:26:22 2012 +0200
@@ -15,16 +15,21 @@
  */
 package fis.base.model
 
+import fis.aaa.model.{CreatedByField, UpdatedByField, User, UserVendors}
+import net.liftweb.common._
+import net.liftweb.http.Factory
 import net.liftweb.record.{MetaRecord, Record}
 import net.liftweb.record.field._
 import net.liftweb.squerylrecord.KeyedRecord
-import net.tz.lift.model.{FieldLabel, OptionalFieldDisplay}
+import net.liftweb.util._
+import net.tz.lift.model.{CreatedField, FieldLabel, OptionalFieldDisplay, UpdatedField}
 import org.squeryl.annotations.Column
 
 /**
  * Base data/model entity trait, common for all FIS entities.
  */
 trait Entity[OwnerType <: Record[OwnerType]] extends KeyedRecord[Long] {
+
   @Column(name="id")
   val idField = new LongField(this.asInstanceOf[OwnerType])
   val name = new StringField(this.asInstanceOf[OwnerType], "") with FieldLabel
@@ -33,6 +38,17 @@
 
   /** Display representation of this entity. */
   def linkName = name.get
+
+  /* Can't mixin traits directly - must be implemented here. */
+  val createdAt = new CreatedField(this.asInstanceOf[OwnerType])
+  val updatedAt = new UpdatedField(this.asInstanceOf[OwnerType])
+  val createdBy = new CreatedByField(this.asInstanceOf[OwnerType],
+    curUserVendor, getUserVendor)
+  val updatedBy = new UpdatedByField(this.asInstanceOf[OwnerType],
+    curUserVendor, getUserVendor)
+
+  protected def curUserVendor: Vendor[Box[User]] = UserVendors.cur
+  protected def getUserVendor: Vendor[Long => Box[User]] = UserVendors.byId
 }
 
 import net.liftweb.common.Box