| author | Tomas Zeman <tzeman@volny.cz> |
| Tue, 01 May 2012 23:54:09 +0200 | |
| changeset 91 | 494b3b9db463 |
| parent 84 | 43674362ff5e |
| permissions | -rw-r--r-- |
| 8 | 1 |
/* |
|
23
16066379860c
Global entity removal from base schema
Tomas Zeman <tzeman@volny.cz>
parents:
10
diff
changeset
|
2 |
* Copyright 2011-2012 Tomas Zeman <tzeman@volny.cz> |
| 8 | 3 |
* |
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package fis.crm.model |
|
17 |
||
| 74 | 18 |
import fis.base.model.{BaseSchema, Entity}
|
| 91 | 19 |
import fis.cl.model.CodeListSchema |
| 10 | 20 |
import fis.geo.model.GeoSchema |
| 8 | 21 |
import net.liftweb.squerylrecord.RecordTypeMode._ |
22 |
||
| 91 | 23 |
trait CrmSchema extends BaseSchema with CodeListSchema with GeoSchema {
|
| 8 | 24 |
|
| 35 | 25 |
val contactT = tableWithSeq[Contact] |
26 |
||
27 |
def allContacts: Iterable[Contact] = from(contactT) (c => |
|
28 |
select(c) orderBy(c.lastName asc, c.firstName asc) |
|
29 |
) |
|
| 8 | 30 |
|
|
23
16066379860c
Global entity removal from base schema
Tomas Zeman <tzeman@volny.cz>
parents:
10
diff
changeset
|
31 |
/* |
| 8 | 32 |
val entityContacts = manyToManyRelation(entities, contacts). |
33 |
via[EntityContact]((e, c, ec) => ( |
|
34 |
e.id === ec.entityId, |
|
35 |
c.id === ec.contactId |
|
36 |
)) |
|
37 |
||
38 |
entityContacts.leftForeignKeyDeclaration.unConstrainReference |
|
39 |
||
40 |
val contactTypeToEntityContacts = oneToManyRelation(codeListItems, |
|
41 |
entityContacts).via((cli, ec) => cli.id === ec.typeId) |
|
|
23
16066379860c
Global entity removal from base schema
Tomas Zeman <tzeman@volny.cz>
parents:
10
diff
changeset
|
42 |
*/ |
| 10 | 43 |
|
|
65
a35a0edf9ddd
Geo+Company record refactoring
Tomas Zeman <tzeman@volny.cz>
parents:
35
diff
changeset
|
44 |
val companyT = tableWithSeq[Company] |
| 10 | 45 |
|
| 91 | 46 |
val companyStatus = oneToManyRelation(cli, companyT). |
47 |
via((i, c) => i.id === c.status) |
|
48 |
||
|
65
a35a0edf9ddd
Geo+Company record refactoring
Tomas Zeman <tzeman@volny.cz>
parents:
35
diff
changeset
|
49 |
val companyAddress = oneToManyRelation(addressT, companyT). |
|
a35a0edf9ddd
Geo+Company record refactoring
Tomas Zeman <tzeman@volny.cz>
parents:
35
diff
changeset
|
50 |
via((a, c) => c.address === a.id) |
| 10 | 51 |
|
|
65
a35a0edf9ddd
Geo+Company record refactoring
Tomas Zeman <tzeman@volny.cz>
parents:
35
diff
changeset
|
52 |
val companyPostAddress = oneToManyRelation(addressT, companyT). |
|
a35a0edf9ddd
Geo+Company record refactoring
Tomas Zeman <tzeman@volny.cz>
parents:
35
diff
changeset
|
53 |
via((a, c) => c.postAddress === a.id) |
| 69 | 54 |
companyPostAddress.foreignKeyDeclaration.constrainReference(onDelete setNull) |
| 10 | 55 |
|
|
65
a35a0edf9ddd
Geo+Company record refactoring
Tomas Zeman <tzeman@volny.cz>
parents:
35
diff
changeset
|
56 |
val bankAccountT = tableWithSeq[BankAccount] |
| 10 | 57 |
|
|
65
a35a0edf9ddd
Geo+Company record refactoring
Tomas Zeman <tzeman@volny.cz>
parents:
35
diff
changeset
|
58 |
val companyBankAccounts = oneToManyRelation(companyT, bankAccountT). |
|
a35a0edf9ddd
Geo+Company record refactoring
Tomas Zeman <tzeman@volny.cz>
parents:
35
diff
changeset
|
59 |
via((c, a) => c.id === a.company) |
|
a35a0edf9ddd
Geo+Company record refactoring
Tomas Zeman <tzeman@volny.cz>
parents:
35
diff
changeset
|
60 |
companyBankAccounts.foreignKeyDeclaration.constrainReference(onDelete cascade) |
| 69 | 61 |
|
| 72 | 62 |
val companyContacts = manyToManyRelation(companyT, contactT). |
63 |
via[CompanyContact]((comp, cnt, cc) => ( |
|
64 |
comp.id === cc.entity, |
|
65 |
cnt.id === cc.contact |
|
66 |
)) |
|
67 |
companyContacts.leftForeignKeyDeclaration.constrainReference(onDelete cascade) |
|
68 |
companyContacts.rightForeignKeyDeclaration.constrainReference(onDelete cascade) |
|
| 74 | 69 |
ContactEntities.register(companyContacts) |
| 72 | 70 |
|
| 84 | 71 |
Company.companies.default.set { () => from(companyT)(c =>
|
72 |
select(c) orderBy(c.name asc)) } |
|
| 69 | 73 |
|
| 8 | 74 |
} |
75 |
||
76 |
object CrmSchema extends CrmSchema |
|
77 |
||
| 72 | 78 |
object CompanyContacts {
|
79 |
def apply(company: Company): Iterable[Contact] = |
|
80 |
from(CrmSchema.companyContacts.left(company)) (c => |
|
81 |
select(c) orderBy(c.lastName asc, c.firstName asc) |
|
82 |
) |
|
83 |
} |
|
84 |
||
| 35 | 85 |
import org.squeryl.KeyedEntity |
86 |
import org.squeryl.dsl._ |
|
87 |
||
| 74 | 88 |
object ContactEntities {
|
89 |
type T = ManyToManyRelation[Entity[_], Contact, _] |
|
90 |
||
91 |
import java.util.concurrent.{ConcurrentHashMap => CHash}
|
|
92 |
import scala.collection.JavaConversions._ |
|
93 |
private val rels = new CHash[String, T] |
|
94 |
||
95 |
def register[L <: Entity[_], A <: KeyedEntity[_]]( |
|
96 |
rel: ManyToManyRelation[L, Contact, A])(implicit man:Manifest[L]) {
|
|
97 |
rels.put(man.toString, rel.asInstanceOf[T]) |
|
98 |
} |
|
99 |
||
100 |
def apply(c: Contact): Iterable[Entity[_]] = rels.values flatMap { _.right(c) }
|
|
101 |
} |
|
102 |
||
| 72 | 103 |
trait EntityContact[T] extends KeyedEntity[CompositeKey2[Long, Long]] {
|
104 |
def entity: Long |
|
105 |
def contact: Long |
|
106 |
def id = CompositeKey2(entity, contact) |
|
107 |
} |
|
| 35 | 108 |
|
| 72 | 109 |
case class CompanyContact(entity: Long, contact: Long) extends |
110 |
EntityContact[Company] |
|
| 35 | 111 |
|
112 |
case class EntityContactWithType[T](val entityId: Long, val contactId: Long, |
|
113 |
val typeId: Long) extends KeyedEntity[CompositeKey3[Long, Long, Long]] {
|
|
114 |
||
115 |
def id = CompositeKey3(entityId, contactId, typeId) |
|
116 |
} |
|
117 |
||
| 8 | 118 |
// vim: set ts=2 sw=2 et: |