Global entity removal from base schema
authorTomas Zeman <tzeman@volny.cz>
Mon, 02 Apr 2012 01:14:48 +0200
changeset 23 16066379860c
parent 22 94ed9cfdb123
child 24 149113e059bd
Global entity removal from base schema
src/main/scala/fis/base/model/BaseSchema.scala
src/main/scala/fis/base/model/Entity.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/main/scala/fis/geo/model/GeoSchema.scala
src/test/scala/fis/crm/model/ContactSpec.scala
src/test/scala/fis/crm/model/CrmSchemaSpec.scala
src/test/scala/fis/top/model/FisDbSchemaSpec.scala
--- a/src/main/scala/fis/base/model/BaseSchema.scala	Mon Apr 02 00:48:43 2012 +0200
+++ b/src/main/scala/fis/base/model/BaseSchema.scala	Mon Apr 02 01:14:48 2012 +0200
@@ -15,55 +15,16 @@
  */
 package fis.base.model
 
-import net.liftweb.squerylrecord.RecordTypeMode._
-import net.liftweb.util.Helpers.{snakify, tryo}
-import org.squeryl.{Schema, Session, Table}
-import org.squeryl.internals.StatementWriter
-import scala.collection.mutable.ListBuffer
+import net.liftweb.util.Helpers.snakify
+import org.squeryl.Schema
 
 trait BaseSchema extends Schema {
-  val entityTableName = "entity"
-  val entities = new Table[EntityUnion](entityTableName) {
-    val tables = new ListBuffer[String]
-  }
-
-  val codeListItems = entityTable[CodeListItem]("code_list_item")
+  val codeListItems = table[CodeListItem]
 
   override def tableNameFromClass(c: Class[_]): String =
     snakify(c.getSimpleName)
 
   override def columnNameFromPropertyName(n: String): String = snakify(n)
-
-  protected def entityTable[T <: Entity[_]](name: String)(implicit manifestT:
-    Manifest[T]): Table[T] = {
-
-    val tbl = table(name)(manifestT)
-    on(tbl) { t => declare(t.id.is(autoIncremented("entity_id_seq"))) }
-    entities.tables += name
-    tbl
-  }
-
-  override def create = {
-    table[EntityUnion](entityTableName) // register entity table as the last one
-    super.create
-  }
-
-  override def drop = {
-    val dba = Session.currentSession.databaseAdapter
-    val sw = new StatementWriter(dba)
-    sw.write("drop view " + entityTableName + " cascade")
-    ex(sw)
-    super.drop
-  }
-
-  private def ex(sw: StatementWriter) = {
-    val sql = sw.statement
-    val cs = Session.currentSession
-    cs.log(sql)
-    val s = cs.connection.createStatement
-    tryo { s.execute(sql) }
-    tryo { s.close }
-  }
 }
 
 object BaseSchema extends BaseSchema
--- a/src/main/scala/fis/base/model/Entity.scala	Mon Apr 02 00:48:43 2012 +0200
+++ b/src/main/scala/fis/base/model/Entity.scala	Mon Apr 02 01:14:48 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.
@@ -56,6 +56,7 @@
   def entity: T
 }
 
+/*
 class EntityUnion private() extends Record[EntityUnion] with
   Entity[EntityUnion] {
   def meta = EntityUnion
@@ -70,6 +71,7 @@
   def apply(e: Entity[_]): EntityUnion =
     createRecord.idField(e.id).name(e.name.get).note(e.note.get)
 }
+*/
 
 /**
  * Converter interface mixin.
--- a/src/main/scala/fis/crm/model/Company.scala	Mon Apr 02 00:48:43 2012 +0200
+++ b/src/main/scala/fis/crm/model/Company.scala	Mon Apr 02 01:14:48 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.
@@ -19,8 +19,7 @@
 import net.liftweb.record.{MetaRecord, Record}
 import net.liftweb.record.field._
 
-class Company private() extends Record[Company] with Entity[Company]
-  with ContactsAware {
+class Company private() extends Record[Company] with Entity[Company] {
 
   def meta = Company
 
--- a/src/main/scala/fis/crm/model/Contact.scala	Mon Apr 02 00:48:43 2012 +0200
+++ b/src/main/scala/fis/crm/model/Contact.scala	Mon Apr 02 01:14:48 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,7 +15,7 @@
  */
 package fis.crm.model
 
-import fis.base.model.{Entity, EntityUnion, MetaEntity}
+import fis.base.model.{Entity, MetaEntity}
 import net.liftweb.common._
 import net.liftweb.http.S
 import net.liftweb.record.{MetaRecord, Record}
@@ -56,8 +56,6 @@
 
   override def linkName = lastName.get + " " + firstName.get
 
-  lazy val entities = CrmSchema.entityContacts.right(this)
-
   def fieldsForView = List(lastName, firstName, position, workMail, workMobile,
     workPhone, privateMail, privateMobile, privatePhone, fax,
     otherMail, otherMobile, note)
@@ -79,10 +77,6 @@
   def delete(c: Contact) = getTable.delete(c.id)
 }
 
-trait ContactsAware { self: Entity[_] =>
-  lazy val contacts = CrmSchema.entityContacts.left(EntityUnion(this))
-}
-
 import org.squeryl.KeyedEntity
 import org.squeryl.dsl.CompositeKey3
 
@@ -90,9 +84,6 @@
   val typeId: Long) extends KeyedEntity[CompositeKey3[Long, Long, Long]] {
 
   def id = CompositeKey3(entityId, contactId, typeId)
-
-  lazy val contactType =
-    CrmSchema.contactTypeToEntityContacts.rightStateful(this)
 }
 
 // vim: set ts=2 sw=2 et:
--- a/src/main/scala/fis/crm/model/CrmSchema.scala	Mon Apr 02 00:48:43 2012 +0200
+++ b/src/main/scala/fis/crm/model/CrmSchema.scala	Mon Apr 02 01:14:48 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.
@@ -21,8 +21,9 @@
 
 trait CrmSchema extends BaseSchema with GeoSchema {
 
-  val contacts = entityTable[Contact]("contact")
+  val contacts = table[Contact]("contact")
 
+  /*
   val entityContacts = manyToManyRelation(entities, contacts).
     via[EntityContact]((e, c, ec) => (
       e.id === ec.entityId,
@@ -33,8 +34,9 @@
 
   val contactTypeToEntityContacts = oneToManyRelation(codeListItems,
     entityContacts).via((cli, ec) => cli.id === ec.typeId)
+  */
 
-  val companies = entityTable[Company]("company")
+  val companies = table[Company]("company")
 
   val companyToAddress = oneToManyRelation(addresses, companies).
     via((a, c) => c.addressId === a.id)
--- a/src/main/scala/fis/geo/model/GeoSchema.scala	Mon Apr 02 00:48:43 2012 +0200
+++ b/src/main/scala/fis/geo/model/GeoSchema.scala	Mon Apr 02 01:14:48 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.
@@ -19,9 +19,9 @@
 import net.liftweb.squerylrecord.RecordTypeMode._
 
 trait GeoSchema extends BaseSchema {
-  val cities = entityTable[City]("city")
+  val cities = table[City]("city")
   val addresses = table[Address]("address")
-  val countries = entityTable[Country]("country")
+  val countries = table[Country]("country")
 
   val cityToAddresses = oneToManyRelation(cities, addresses).
     via((c, a) => c.id === a.cityId)
--- a/src/test/scala/fis/crm/model/ContactSpec.scala	Mon Apr 02 00:48:43 2012 +0200
+++ b/src/test/scala/fis/crm/model/ContactSpec.scala	Mon Apr 02 01:14:48 2012 +0200
@@ -21,7 +21,7 @@
 import net.liftweb.util.Helpers._
 import org.scalatest._
 
-class ContactSpec extends AbstractTest {
+class ContactSpec extends AbstractTest with BeforeAndAfterAll {
   import CrmSchema._
 
   override def beforeAll() {
@@ -36,6 +36,7 @@
     codeListItems.insert(t1)
     val c2 = Contact.createRecord.name("c2") // acts here as entity
     contacts.insert(c2)
+    /*
     entityContacts.insert(EntityContact(c2.id, c1.id, t1.id))
 
     val el1 = c1.entities.toList
@@ -45,6 +46,7 @@
     val al1 = c1.entities.associations.toList
     al1.size should equal (1)
     al1.head.contactType.one should equal (Some(t1))
+    */
   }}
 
   object schema extends CrmSchema with DropAndCreate
--- a/src/test/scala/fis/crm/model/CrmSchemaSpec.scala	Mon Apr 02 00:48:43 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,35 +0,0 @@
-/*
- * 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._
-import net.liftweb.common._
-import net.liftweb.util._
-import net.liftweb.util.Helpers._
-
-class CrmSchemaSpec extends AbstractTest {
-
-  "CrmSchema" should "create" in { doInDB {
-    schema.dropAndCreate
-  }}
-
-  object schema extends CrmSchema with DropAndCreate
-
-}
-
-
-
-// vim: set ts=2 sw=2 et:
--- a/src/test/scala/fis/top/model/FisDbSchemaSpec.scala	Mon Apr 02 00:48:43 2012 +0200
+++ b/src/test/scala/fis/top/model/FisDbSchemaSpec.scala	Mon Apr 02 01:14:48 2012 +0200
@@ -17,20 +17,21 @@
 
 import fis.base.model._
 import java.io._
-
-class FisDbSchemaSpec extends AbstractTest {
+import org.scalatest._
 
-  "FisDbSchema" should "create" in { doInDB {
-    schema.dropAndCreate
-  }}
+class FisDbSchemaSpec extends AbstractTest with BeforeAndAfterAll {
 
-  it should "generate schema definition" in {
+  "FisDbSchema" should "generate schema definition" in { doInDB {
     val f = new File("target/fis-db-schema.sql")
     val pw = new PrintWriter(f)
     schema.printDdl(pw)
     pw.flush
     pw.close
-  }
+  }}
+
+  it should "create" in { doInDB {
+    schema.dropAndCreate
+  }}
 
   object schema extends FisDbSchema with DropAndCreate
 }