81aa2ebe282b4664 Company model
authorTomas Zeman <tzeman@volny.cz>
Fri, 10 Feb 2012 09:53:06 +0100
changeset 10 fc7b6a99deb4
parent 9 fe20033afdf1
child 11 43fa65f5072e
81aa2ebe282b4664 Company model
src/main/scala/fis/base/model/CodeList.scala
src/main/scala/fis/base/model/Entity.scala
src/main/scala/fis/base/model/codeLists.scala
src/main/scala/fis/crm/model/BankAccount.scala
src/main/scala/fis/crm/model/Company.scala
src/main/scala/fis/crm/model/Contact.scala
src/main/scala/fis/crm/model/CrmSchema.scala
src/test/scala/fis/base/model/CodeListSpec.scala
--- a/src/main/scala/fis/base/model/CodeList.scala	Fri Feb 10 09:53:05 2012 +0100
+++ b/src/main/scala/fis/base/model/CodeList.scala	Fri Feb 10 09:53:06 2012 +0100
@@ -57,31 +57,38 @@
   def item(id: Long): Box[T]
 }
 
-class CodeListImpl[T](val id: Int, tr: CodeListItem => T) extends
-  CodeList[T] {
+class CodeListImpl[T](val id: CodeListRegister.CodeListRegister,
+  tr: CodeListItem => T) extends CodeList[T] {
 
-  def items = CodeListItem.items(id) map (tr(_))
-  def defaultItem = CodeListItem.defaultItem(id) map (tr(_))
+  def items = CodeListItem.items(id.id) map (tr(_))
+  def defaultItem = CodeListItem.defaultItem(id.id) map (tr(_))
   def item(id: Long) = CodeListItem.findByKey(id) map (tr(_))
 }
 
 import net.liftweb.http.SHtml
 import net.liftweb.util.Helpers._
+import CodeListRegister.CodeListRegister
 
 class CodeListItemField[T, OwnerType <: Record[OwnerType]](own: OwnerType,
-  codeListType: Int, tr: CodeListItem => T) extends LongField(own) with
+  codeListType: CodeListRegister, tr: CodeListItem => T)
+  extends AbstractCodeListItemField[T, OwnerType](own, codeListType) {
+
+  def cvt: Box[T] = item map { tr(_) }
+}
+
+abstract class AbstractCodeListItemField[T, OwnerType <: Record[OwnerType]](
+  own: OwnerType, codeListType: CodeListRegister) extends LongField(own) with
   Converter0[Box[T]] {
 
   def item: Box[CodeListItem] = CodeListItem.findByKey(get)
-  def cvt: Box[T] = item map { tr(_) }
 
   override def defaultValueBox: Box[Long] =
-    CodeListItem.defaultItem(codeListType).map { _.id }
+    CodeListItem.defaultItem(codeListType.id).map { _.id }
 
   override def toXHtml = item map { _.toXHtml } openOr NodeSeq.Empty
 
   protected def buildDisplayList: List[(Long, String)] =
-    CodeListItem.items(codeListType).toList.map { i =>
+    CodeListItem.items(codeListType.id).toList.map { i =>
       (i.id, i.linkName)
     }
 
--- a/src/main/scala/fis/base/model/Entity.scala	Fri Feb 10 09:53:05 2012 +0100
+++ b/src/main/scala/fis/base/model/Entity.scala	Fri Feb 10 09:53:06 2012 +0100
@@ -64,6 +64,9 @@
   MetaEntity[EntityUnion] {
 
   def getTable = BaseSchema.entities
+
+  def apply(e: Entity[_]): EntityUnion =
+    createRecord.idField(e.id).name(e.name.get).note(e.note.get)
 }
 
 /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/base/model/codeLists.scala	Fri Feb 10 09:53:06 2012 +0100
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2011 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.base.model
+
+object CodeListRegister extends Enumeration {
+  type CodeListRegister = Val
+  val TestCL = new Val(-1, "TestCL")
+
+}
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/crm/model/BankAccount.scala	Fri Feb 10 09:53:06 2012 +0100
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2011 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.crm.model
+
+import net.liftweb.record.{MetaRecord, Record}
+import net.liftweb.record.field._
+import net.liftweb.squerylrecord.KeyedRecord
+import org.squeryl.annotations.Column
+
+class BankAccount private() extends Record[BankAccount] with KeyedRecord[Long] {
+  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)
+}
+
+object BankAccount extends BankAccount with MetaRecord[BankAccount]
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/crm/model/Company.scala	Fri Feb 10 09:53:06 2012 +0100
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2011 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.crm.model
+
+import fis.base.model.{Entity, MetaEntity}
+import net.liftweb.record.{MetaRecord, Record}
+import net.liftweb.record.field._
+
+class Company private() extends Record[Company] with Entity[Company]
+  with ContactsAware {
+
+  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)
+}
+
+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")
+}
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/scala/fis/crm/model/Contact.scala	Fri Feb 10 09:53:05 2012 +0100
+++ b/src/main/scala/fis/crm/model/Contact.scala	Fri Feb 10 09:53:06 2012 +0100
@@ -15,7 +15,7 @@
  */
 package fis.crm.model
 
-import fis.base.model.{Entity, MetaEntity}
+import fis.base.model.{Entity, EntityUnion, MetaEntity}
 import net.liftweb.record.{MetaRecord, Record}
 import net.liftweb.record.field._
 
@@ -47,6 +47,10 @@
   def getTable = CrmSchema.contacts
 }
 
+trait ContactsAware { self: Entity[_] =>
+  lazy val contacts = CrmSchema.entityContacts.left(EntityUnion(this))
+}
+
 import org.squeryl.KeyedEntity
 import org.squeryl.dsl.CompositeKey3
 
--- a/src/main/scala/fis/crm/model/CrmSchema.scala	Fri Feb 10 09:53:05 2012 +0100
+++ b/src/main/scala/fis/crm/model/CrmSchema.scala	Fri Feb 10 09:53:06 2012 +0100
@@ -16,9 +16,10 @@
 package fis.crm.model
 
 import fis.base.model.BaseSchema
+import fis.geo.model.GeoSchema
 import net.liftweb.squerylrecord.RecordTypeMode._
 
-trait CrmSchema extends BaseSchema {
+trait CrmSchema extends BaseSchema with GeoSchema {
 
   val contacts = entityTable[Contact]("contact")
 
@@ -32,6 +33,19 @@
 
   val contactTypeToEntityContacts = oneToManyRelation(codeListItems,
     entityContacts).via((cli, ec) => cli.id === ec.typeId)
+
+  val companies = entityTable[Company]("company")
+
+  val companyToAddress = oneToManyRelation(addresses, companies).
+    via((a, c) => c.addressId === a.id)
+
+  val companyToCorrespAddress = oneToManyRelation(addresses, companies).
+    via((a, c) => c.correspAddressId === a.id)
+
+  val bankAccounts = table[BankAccount]("bank_account")
+
+  val companyToBankAccounts = oneToManyRelation(companies, bankAccounts).
+    via((c, a) => c.id === a.companyId)
 }
 
 object CrmSchema extends CrmSchema
--- a/src/test/scala/fis/base/model/CodeListSpec.scala	Fri Feb 10 09:53:05 2012 +0100
+++ b/src/test/scala/fis/base/model/CodeListSpec.scala	Fri Feb 10 09:53:06 2012 +0100
@@ -36,7 +36,7 @@
       cli1.id should equal (1)
       val l1 = from(codeListItems)(cli => select(cli)).toList
       l1.size should equal (1)
-      val cl = new CodeListImpl(1, { i => i } )
+      val cl = new CodeListImpl(CodeListRegister.TestCL, { i => i } )
       val l2 = cl.items
       l2.size should equal (1)
       l2.head should equal (cli1)