-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathAddressBook.java
More file actions
88 lines (71 loc) · 2.42 KB
/
AddressBook.java
File metadata and controls
88 lines (71 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.raizlabs.android.databasecomparison.dbflow;
import com.raizlabs.android.databasecomparison.MainActivity;
import com.raizlabs.android.databasecomparison.interfaces.IAddressBook;
import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.ModelContainer;
import com.raizlabs.android.dbflow.annotation.OneToMany;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.BaseModel;
import java.util.Collection;
/**
* Description:
*/
@Table(name = "AddressBook", database = DBFlowDatabase.class,
cacheSize = MainActivity.ADDRESS_BOOK_COUNT,
orderedCursorLookUp = true)
@ModelContainer
public class AddressBook extends BaseModel implements IAddressBook<AddressItem, Contact> {
@PrimaryKey(autoincrement = true)
@Column
long id;
@Column(name = "name")
String name;
@Column(name = "author")
String author;
Collection<AddressItem> addresses;
Collection<Contact> contacts;
@Override
public void setId(long id) {
// not needed because we have autoincrementing keys
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setAddresses(Collection<AddressItem> addresses) {
this.addresses = addresses;
}
@OneToMany(methods = OneToMany.Method.ALL)
public Collection<AddressItem> getAddresses() {
if (addresses == null) {
addresses = SQLite.select().from(AddressItem.class)
.where(AddressItem_Table.addressBook.is(id)).queryList();
}
return addresses;
}
@OneToMany(methods = OneToMany.Method.ALL)
public Collection<Contact> getContacts() {
if (contacts == null) {
contacts = SQLite.select().from(Contact.class)
.where(Contact_Table.addressBook.is(id)).queryList();
}
return contacts;
}
public void setContacts(Collection<Contact> contacts) {
this.contacts = contacts;
}
@Override
public void saveAll() {
super.insert();
for (AddressItem addressItem : addresses) {
addressItem.saveAll();
}
for (Contact contact : contacts) {
contact.saveAll();
}
}
}