Geo+Company record refactoring
authorTomas Zeman <tzeman@volny.cz>
Fri, 20 Apr 2012 08:26:23 +0200
changeset 65 a35a0edf9ddd
parent 64 90a44ee46732
child 66 f8c666b75c31
Geo+Company record refactoring
db/db-schema.sql
src/main/scala/fis/crm/model/BankAccount.scala
src/main/scala/fis/crm/model/Company.scala
src/main/scala/fis/crm/model/CrmSchema.scala
src/main/scala/fis/geo/model/Address.scala
src/main/scala/fis/geo/model/AddressCrud.scala
src/main/scala/fis/geo/model/City.scala
src/main/scala/fis/geo/model/CityCrud.scala
src/main/scala/fis/geo/model/CityField.scala
src/main/scala/fis/geo/model/Country.scala
src/main/scala/fis/geo/model/CountryCrud.scala
src/main/scala/fis/geo/model/CountryField.scala
src/main/scala/fis/geo/model/GeoSchema.scala
--- a/db/db-schema.sql	Fri Apr 20 08:26:22 2012 +0200
+++ b/db/db-schema.sql	Fri Apr 20 08:26:23 2012 +0200
@@ -60,15 +60,21 @@
     "created_by" bigint,
     "updated_by" bigint
   );
-create sequence "s_city_id";
+create sequence "city_id_seq";
 create table "address" (
+    "city_id" bigint not null,
+    "name" varchar(100) not null,
+    "updated_at" timestamp not null,
     "id" bigint primary key not null,
-    "city_id" bigint not null,
     "zip_code" varchar(100) not null,
     "street_name" varchar(100) not null,
-    "street_num" varchar(100) not null
+    "note" varchar(10240),
+    "created_at" timestamp not null,
+    "created_by" bigint,
+    "street_num" varchar(100) not null,
+    "updated_by" bigint
   );
-create sequence "s_address_id";
+create sequence "address_id_seq";
 create table "country" (
     "iso3" varchar(3) not null,
     "name" varchar(100) not null,
@@ -80,7 +86,7 @@
     "created_by" bigint,
     "updated_by" bigint
   );
-create sequence "s_country_id";
+create sequence "country_id_seq";
 create table "contact" (
     "name" varchar(100) not null,
     "updated_at" timestamp not null,
@@ -105,32 +111,38 @@
 create sequence "contact_id_seq";
 create table "company" (
     "name" varchar(100) not null,
+    "dic" varchar(40) 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,
+    "address_id" bigint not null,
+    "post_adress_id" bigint,
     "updated_by" bigint,
-    "pin" integer not null,
-    "address_id" bigint not null
+    "pin" integer not null
   );
-create sequence "s_company_id";
+create sequence "company_id_seq";
 create table "bank_account" (
     "number" bigint not null,
+    "name" varchar(100) not null,
+    "updated_at" timestamp not null,
     "id" bigint primary key not null,
     "prefix" bigint not null,
     "bank_code" varchar(100) not null,
-    "company_id" bigint not null
+    "company_id" bigint not null,
+    "note" varchar(10240),
+    "created_at" timestamp not null,
+    "created_by" bigint,
+    "updated_by" bigint
   );
-create sequence "s_bank_account_id";
+create sequence "bank_account_id_seq";
 -- foreign key constraints :
 alter table "address" add constraint "addressFK1" foreign key ("city_id") references "city"("id");
 alter table "city" add constraint "cityFK2" foreign key ("country_id") references "country"("id");
 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");
+alter table "company" add constraint "companyFK4" foreign key ("post_adress_id") references "address"("id");
+alter table "bank_account" add constraint "bank_accountFK5" foreign key ("company_id") references "company"("id") on delete cascade;
 -- column group indexes :
 create index "user_deleted_active_idx" on "user" ("deleted","active");
--- a/src/main/scala/fis/crm/model/BankAccount.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/crm/model/BankAccount.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
+ * 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.
@@ -15,24 +15,23 @@
  */
 package fis.crm.model
 
+import fis.base.model.Entity
 import net.liftweb.record.{MetaRecord, Record}
 import net.liftweb.record.field._
 import net.liftweb.squerylrecord.KeyedRecord
+import net.tz.lift.model.{FieldLabel => FL}
 import org.squeryl.annotations.Column
 
-class BankAccount private() extends Record[BankAccount] with KeyedRecord[Long] {
+class BankAccount private() extends Record[BankAccount] with Entity[BankAccount]
+{
   def meta = BankAccount
 
-  @Column(name="id")
-  val idField = new LongField(this)
-
   val prefix = new LongField(this)
   val number = new LongField(this)
   val bankCode = new StringField(this, "")
 
-  val companyId = new LongField(this)
-
-  lazy val company = CrmSchema.companyToBankAccounts.rightStateful(this)
+  @Column(name="company_id")
+  val company = new LongField(this)
 }
 
 object BankAccount extends BankAccount with MetaRecord[BankAccount]
--- a/src/main/scala/fis/crm/model/Company.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/crm/model/Company.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -15,34 +15,28 @@
  */
 package fis.crm.model
 
-import fis.base.model.{Entity, MetaEntity}
+import fis.base.model.Entity
+import fis.geo.model.Address
+import net.liftweb.common._
 import net.liftweb.record.{MetaRecord, Record}
 import net.liftweb.record.field._
+import net.liftweb.util._
+import net.tz.lift.model.{FieldLabel => FL, FieldHelp => FH}
+import org.squeryl.annotations.Column
 
 class Company private() extends Record[Company] with Entity[Company] {
 
   def meta = Company
 
-  val ico = new StringField(this, 40)
-  val pin = new IntField(this)
-  val partner = new EnumField(this, ParterType)
-  val addressId = new LongField(this)
-  lazy val address = CrmSchema.companyToAddress.rightStateful(this)
-  val correspAddressId = new OptionalLongField(this)
-  lazy val correspAddress = CrmSchema.companyToCorrespAddress.rightStateful(this)
-  lazy val accounts = CrmSchema.companyToBankAccounts.leftStateful(this)
+  val ico = new StringField(this, 40) with FL
+  val dic = new StringField(this, 40) with FL
+  val pin = new IntField(this) with FL
+  @Column(name="address_id")
+  val address = new LongField(this) with FL
+  @Column(name="post_adress_id")
+  val postAddress = new OptionalLongField(this) with FL
 }
 
-object Company extends Company with MetaRecord[Company] with
-  MetaEntity[Company] {
-
-  def getTable = CrmSchema.companies
-}
-
-object ParterType extends Enumeration {
-  type PartnerType = Val
-  val Customer = new Val(1, "Customer")
-  val Supplier = new Val(2, "Supplier")
-}
+object Company extends Company with MetaRecord[Company]
 
 // vim: set ts=2 sw=2 et:
--- a/src/main/scala/fis/crm/model/CrmSchema.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/crm/model/CrmSchema.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -40,18 +40,19 @@
     entityContacts).via((cli, ec) => cli.id === ec.typeId)
   */
 
-  val companies = table[Company]("company")
+  val companyT = tableWithSeq[Company]
 
-  val companyToAddress = oneToManyRelation(addresses, companies).
-    via((a, c) => c.addressId === a.id)
+  val companyAddress = oneToManyRelation(addressT, companyT).
+    via((a, c) => c.address === a.id)
 
-  val companyToCorrespAddress = oneToManyRelation(addresses, companies).
-    via((a, c) => c.correspAddressId === a.id)
+  val companyPostAddress = oneToManyRelation(addressT, companyT).
+    via((a, c) => c.postAddress === a.id)
 
-  val bankAccounts = table[BankAccount]("bank_account")
+  val bankAccountT = tableWithSeq[BankAccount]
 
-  val companyToBankAccounts = oneToManyRelation(companies, bankAccounts).
-    via((c, a) => c.id === a.companyId)
+  val companyBankAccounts = oneToManyRelation(companyT, bankAccountT).
+    via((c, a) => c.id === a.company)
+  companyBankAccounts.foreignKeyDeclaration.constrainReference(onDelete cascade)
 }
 
 object CrmSchema extends CrmSchema
--- a/src/main/scala/fis/geo/model/Address.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/geo/model/Address.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
+ * 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.
@@ -15,23 +15,21 @@
  */
 package fis.geo.model
 
+import fis.base.model.Entity
 import net.liftweb.record.{MetaRecord, Record}
 import net.liftweb.record.field._
 import net.liftweb.squerylrecord.KeyedRecord
+import net.tz.lift.model.{FieldLabel => FL}
 import org.squeryl.annotations.Column
 
-class Address private() extends Record[Address] with KeyedRecord[Long] {
+class Address private() extends Record[Address] with Entity[Address] {
   def meta = Address
 
-  @Column(name="id")
-  val idField = new LongField(this)
-
-  val streetName = new StringField(this, "")
-  val streetNum = new StringField(this, "")
-  val cityId = new LongField(this)
-  val zipCode = new StringField(this, "")
-
-  lazy val city = GeoSchema.cityToAddresses.rightStateful(this)
+  val streetName = new StringField(this, "") with FL
+  val streetNum = new StringField(this, "") with FL
+  @Column(name="city_id")
+  val city = new LongField(this) with CityField with FL
+  val zipCode = new StringField(this, "") with FL
 }
 
 object Address extends Address with MetaRecord[Address]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/geo/model/AddressCrud.scala	Fri Apr 20 08:26:23 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.geo.model
+
+import fis.base.model.RecordCrud
+
+trait AddressCrud extends RecordCrud[Address] {
+  val table = GeoSchema.addressT
+}
+
+object AddressCrud extends AddressCrud
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/scala/fis/geo/model/City.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/geo/model/City.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
+ * 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.
@@ -15,19 +15,22 @@
  */
 package fis.geo.model
 
-import fis.base.model.{Entity, MetaEntity}
+import fis.base.model.Entity
 import net.liftweb.record.{MetaRecord, Record}
 import net.liftweb.record.field._
+import net.liftweb.util._
+import net.tz.lift.model.{FieldLabel => FL}
+import org.squeryl.annotations.Column
 
 class City private() extends Record[City] with Entity[City] {
   def meta = City
 
-  val countryId = new LongField(this)
-  lazy val country = GeoSchema.countryToCities.rightStateful(this)
+  @Column(name="country_id")
+  val country = new LongField(this) with CountryField with FL
 }
 
-object City extends City with MetaRecord[City] with MetaEntity[City] {
-  def getTable = GeoSchema.cities
+object City extends City with MetaRecord[City] with SimpleInjector {
+  object cities extends Inject[Iterable[City]](() => Nil)
 }
 
 // vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/geo/model/CityCrud.scala	Fri Apr 20 08:26:23 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.geo.model
+
+import fis.base.model.RecordCrud
+
+trait CityCrud extends RecordCrud[City] {
+  val table = GeoSchema.cityT
+}
+
+object CityCrud extends CityCrud
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/geo/model/CityField.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -0,0 +1,38 @@
+/*
+ * 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.geo.model
+
+import fis.base.model._
+import fis.base.ui.EntityLink
+import net.liftweb.common._
+import net.liftweb.util._
+
+trait CityField extends SelectField[Long] with Vendor[Box[City]] {
+
+  override def asHtml = (for {
+    c <- vend
+    l <- EntityLink(c)
+  } yield l.asHtml) openOr super.asHtml
+
+  protected def buildDisplayList = 
+    City.cities() map { c => (Full(c.id), c.linkName) } toSeq
+
+  def vend = valueBox flatMap(CityCrud.get _)
+  def make = Full(vend)
+
+}
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/scala/fis/geo/model/Country.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/geo/model/Country.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
+ * 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.
@@ -15,21 +15,23 @@
  */
 package fis.geo.model
 
-import fis.base.model.{Entity, MetaEntity}
+import fis.base.model.Entity
+import net.liftweb.common._
 import net.liftweb.record.{MetaRecord, Record}
 import net.liftweb.record.field._
+import net.liftweb.util._
+import net.tz.lift.model.{FieldLabel => FL}
 
 class Country private() extends Record[Country] with Entity[Country] {
   def meta = Country
 
-  val iso2 = new StringField(this, 2)
-  val iso3 = new StringField(this, 3)
-  lazy val cities = GeoSchema.countryToCities.left(this)
+  val iso2 = new StringField(this, 2) with FL
+  val iso3 = new StringField(this, 3) with FL
 }
 
-object Country extends Country with MetaRecord[Country] with
-  MetaEntity[Country] {
-  def getTable = GeoSchema.countries
+object Country extends Country with MetaRecord[Country] with 
+  SimpleInjector {
+  object countries extends Inject[Iterable[Country]](() => Nil)
 }
 
 // vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/geo/model/CountryCrud.scala	Fri Apr 20 08:26:23 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.geo.model
+
+import fis.base.model.RecordCrud
+
+trait CountryCrud extends RecordCrud[Country] {
+  val table = GeoSchema.countryT
+}
+
+object CountryCrud extends CountryCrud
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/geo/model/CountryField.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -0,0 +1,38 @@
+/*
+ * 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.geo.model
+
+import fis.base.model._
+import fis.base.ui.EntityLink
+import net.liftweb.common._
+import net.liftweb.util._
+
+trait CountryField extends SelectField[Long] with Vendor[Box[Country]] {
+
+  override def asHtml = (for {
+    c <- vend
+    l <- EntityLink(c)
+  } yield l.asHtml) openOr super.asHtml
+
+  protected def buildDisplayList = 
+    Country.countries() map { c => (Full(c.id), c.linkName) } toSeq
+
+  def vend = valueBox flatMap(CountryCrud.get _)
+  def make = Full(vend)
+
+}
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/scala/fis/geo/model/GeoSchema.scala	Fri Apr 20 08:26:22 2012 +0200
+++ b/src/main/scala/fis/geo/model/GeoSchema.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -19,15 +19,21 @@
 import net.liftweb.squerylrecord.RecordTypeMode._
 
 trait GeoSchema extends BaseSchema {
-  val cities = table[City]("city")
-  val addresses = table[Address]("address")
-  val countries = table[Country]("country")
+  val cityT = tableWithSeq[City]
+  val addressT = tableWithSeq[Address]
+  val countryT = tableWithSeq[Country]
+
+  val cityAddresses = oneToManyRelation(cityT, addressT).
+    via((c, a) => c.id === a.city)
 
-  val cityToAddresses = oneToManyRelation(cities, addresses).
-    via((c, a) => c.id === a.cityId)
+  val countryCities = oneToManyRelation(countryT, cityT).
+    via((country, city) => country.id === city.country)
 
-  val countryToCities = oneToManyRelation(countries, cities).
-    via((country, city) => country.id === city.countryId)
+  City.cities.default.set { () => from(cityT)( c =>
+    select(c) orderBy(c.name asc)) }
+
+  Country.countries.default.set { () => from(countryT)( c =>
+    select(c) orderBy(c.name asc)) }
 }
 
 object GeoSchema extends GeoSchema